Introducing the Internet Computer Learning Experience

Written by nnsdao | Published 2021/11/25
Tech Story Tags: dfinity | icp | nnsdao | icdev | dao | good-company | blockchain-technology | blockchain-development

TLDRThe content covered in this article is based on the DFINITY Canister SDK, which differs from the NNS system in that they have a different account system. With Internet Computer, developers can focus on writing code using smart contracts without similar distractions associated with the environment, such as: Physical or virtual network configuration requirements. The code is called 'Canister' and 'Actor' in modern programming languages that handles messages in an isolated state, allowing them to be processed remotely and asynchronously.via the TL;DR App

The content covered in this article is based on the DFINITY Canister SDK, which differs from the NNS system in that they have a different account system.

The Internet Computer blockchain enables multiple computers to operate as one very powerful virtual machine. The computers that make up a virtual machine are organized into subnetworks, each of which is a blockchain consisting of a certain number of independent machines (peer-connected computers called nodes) that run the software components of the Internet Computer protocol.

The Internet computer software components running on each node are called replicas, and they replicate state and computation among all nodes in the subnetwork blockchain, and the core components of the replicas are organized into the following logical layers:

  • A P2P network layer, which collects and publishes messages from users or other nodes in the current subnet or other subnets. The messages received by the network layer are replicated to all nodes in the current subnet.
  • A consensus layer that creates input blocks by selecting and serializing messages received from users and different subnets, and notarizes and finalizes the blocks before they are passed to the message routing layer.
  • A message routing layer for routing user- and system-generated block messages between subnets, managing the application's input and output queues and scheduling messages for execution.
  • An execution environment for computing the deterministic calculations involved in executing the program and for processing messages received from the message routing layer.

With Internet Computer, developers can focus on writing code using smart contracts without similar distractions associated with the environment, such as:

  • Physical or virtual network configuration requirements
  • Load balancing services
  • Firewall, network topology or port management
  • Database configuration and maintenance
  • Storage volumes and devices

Concept

Actor: An actor is a special object in modern programming languages that handles messages in an isolated state, allowing them to be processed remotely and asynchronously.

Usually, each canister contains the compiled code of an actor object. Each canister also contains some additional information, such as an interface description or front-end assets. It is possible to create projects containing multiple canisters, but each canister can only contain one Actor.

A canister is an object with a common unique identifier similar to the concept of a smart contract that defines the owner of a specific application, service, or microsite.

The canister encapsulates all programming logic, public entry methods, interface descriptions that provide message types, and state information for the application or service, or microservice.

When calling functions by sending messages to the canister entry point, there are two types of calls: Query calls (non-committed query calls): allow calling functions that query the current state of the container or call functions that operate on the container state but do not change it. Has the following characteristics:

  • Calls are synchronized and respond immediately.
  • It can be made to any node that holds a canister and does not require consensus to verify the result. But there are security issues.
  • Changes to the state of the canister are not allowed.
  • The canister being called is not allowed to call functions of the inter-canister type of other canisters.
  • Update calls (submit update calls): Allow calls to functions that change the state of a canister. Has the following characteristics.
  • The call is asynchronous and responsive.
  • Consensus must be passed to return the result. Changing the state of a container may take time because it requires two-thirds of the replicas in the subnet to agree on the result.
  • The canister being called is allowed to call functions of the inter-canister type of other canisters.

Controller is an identity with special privileges that are used to manage the canisters it controls. only the controller identity can be used to install, upgrade, or remove the canisters it controls.

The controller identity can be specified using the principal identifier associated with the user or canister.

The ledger Internet Computer records all transactions involving ICP tokens in an administrative canister called the ledger canister. ledger canister is a simplified parallel blockchain that runs in a subnet of Internet computers along with other network administrative canisters.

The ledger canister implements a smart contract that holds accounts and balances and keeps a history of transactions that affect them. Recorded transactions are used to track specific events such as:

  • Minting of ICP tokens.
  • Transferring ICP tokens.
  • Burning of ICP tokens.

Principal: The first time you use the DFINITY Canister SDK, the dfx command line tool creates a default developer identity for you using the public/private key pair from the PEM file. The developer identity is represented by the derived principal data type and the text representing the principal, also known as the principal identifier.

The developer identity can also be used to derive an account id (similar to a bitcoin or ethereum address) for holding ICP tokens in the ledger canister.

Wallet: On Internet Computer, a wallet is an application dedicated to cycle. The wallet application is implemented as a canister and runs on an Internet computer.

A wallet enables you to manage cycle balances, convert ICP tokens to cycles, and distribute cycles to your own or other users' canisters as a way to access or provide Internet services.

When a canister needs to be created or manipulated, a wallet canister must be bound to the current subject identifier before the operation can take place.

Smart contract: A smart contract is software that allows the execution of trusted transactions and agreements on a distributed, decentralized blockchain network without the need for any central authority or the legal system.

With a smart contract, the terms of the transaction or agreement are written directly into lines of code that are executed on the blockchain network. The code controls execution and the transaction is tamper-proof, traceable, and irreversible. On Internet computers, smart contracts are deployed into canisters.

WebAssembly WebAssembly (Wasm) is an underlying computer instruction format. Because WebAssembly defines a portable, open-standard binary format that can be clearly abstracted on most modern computer hardware, it is widely supported by programs running on the Internet. Programs written in Motoko are compiled into WebAssembly code for execution on a copy of an Internet computer.

Replica: In the context of the Internet Computer Blockchain, replica refers to an Internet Computer process running on a physical computer node in the network.

For the DFINITY Canister SDK, use the dfx start and dfx stop commands to start and stop replica processes locally, providing a local network for development.

There are three accounts in IC as follows:

  • identity: the identity account is the subject identifier, which can be mapped to the ICP account (account id) and the identity is the key to control the canister.
  • ledger: The ledger account is the ICP account (account id), and all operations related to ICP are performed in the ledger.
  • wallet: The wallet account is the canister where the cycle wallet program is deployed. identity must have the wallet set up to deploy the canister.

Some of the account operations in the whole IC are as follows:

  • DFX canister SDK is associated with a subject identity (principal identity) the first time it is used. The current principal identity can be obtained by using the command: dfx identity get-principal.
  • Each principal identity derives an account id that is uniquely identified in the leger canister, similar to an address. use the command: dfx ledger account-id to obtain the current ICP address.
  • The first canister is created using the leger canister and specifies the controller of the canister, into which the specified number of cycles will be deposited. use the command: dfx ledger --network ic create-canister tsqwz-udeik-5migd- ehrev-pvoqv-szx2g-akh5s-fkyqc-zy6q7-snav6-uqe --amount .25 creates a canister and specifies the controller body and the initialized cycles.
  • The first created canister is empty and needs to be deployed to create a wallet canister. canister created. This canister is called the wallet canister.
  • By default, after deploying a wallet, the wallet canister will be used as the current subject's cycle wallet. You can also set the current subject's cycle wallet with the following command: dfx identity --network ic set-wallet --force gastn-uqaaa-aaae-aaafq-cai After successful setting, the current identity uses the cycle wallet for canister operations.
  • Once the current subject has a cycle wallet, it can use canister-related operations. For example, the command: dfx deploy --network ic deploys all canisters in the current project, and after the deployment is completed, the controller of these canisters is the current subject.

Quick Start

Local development

  1. Install nodejs and npm.
  2. Install dfx canister sdk. sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
  3. Create a new project. dfx new hello; cd hello;
  4. Start local network. dfx start
  5. Compile the project. npm install
  6. Deploy the canister. dfx deploy

Network deployment

  1. Install nodejs and npm.
  2. Install dfx canister sdk. sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
  3. Create a new project. dfx new hello; cd hello;
  4. Start local network. dfx start
  5. Get the current subject identity. dfx identity get-principal
  6. Get the address of the current ICP account and transfer money to it. dfx ledger account-id
  7. Create a canister for the cycle wallet. dfx ledger --network ic create-canister <principal-identifier> --amount <icp-tokens>
  8. Deploy the cycle wallet application to the newly created canister. dfx identity --network ic deploy-wallet <canister-identifer>
  9. Compile the project. npm install
  10. Deploy the canister. dfx deploy

Project structure#

When the command dfx new hello is used, a directory with the following structure is created:

hello/
├── README.md # default project documentation
├── dfx.json # project configuration file
├── node_modules # libraries for front-end development
├── package-lock.json
├── package.json
├── src # source files directory
│ ├── hello
│ │ └── main.mo
│ └── hello_assets
│ ├── assets
│ │ ├── logo.png
│ │ ├── main.css
│ │ └── sample-asset.txt
│ └── src
│ ├── index.html
│ └── index.js
└── webpack.config.js

Among them:

  • dfx.json is the configuration information for the whole project.
  • package.json is the dependency and packaging information of the front-end project.
  • The src directory holds the source files for the entire project.
  • src/xxx/ holds the source files of the back-end project.
  • The src/xxx_assets/ directory holds the source files of the front-end project.
  • The appointment src/xxx_assets/assets/ holds the resource files of the front-end project.
  • The appointment src/xxx_assets/src/index.html is the entry html file of the front-end project.
  • The appointment src/xxx_assets/src/index.js is the JavaScript script that will be loaded automatically and uniquely for the front-end project.

Dfx configuration file: When using the command dfx new hello, the dfx.json project configuration file with the following content will be created:


{
    "canisters": {
        "hello": {
            "main": "src/hello/main.mo",
            "type": "motoko"
        },
        "hello_assets": {
            "dependencies": [
                "hello"
            ],
            "frontend": {
                "entrypoint": "src/hello_assets/src/index.html"
            },
            "source": [
                "src/hello_assets/assets",
                "dist/hello_assets/"
            ],
            "type": "assets"
        }
    },
    "defaults": {
        "build": {
            "packtool": ""
        }
    },
    "dfx": "0.7.2",
    "networks": {
        "local": {
            "bind": "127.0.0.1:8000",
            "type": "ephemeral"
        }
    },
    "version": 1
}

Where:

  • canisters specify the canisters that the project needs to deploy. assets are the canister for the front-end type, and Motoko is the canister for the back-end type.
  • For backend types, the main specifies its compilation entry file.
  • For front-end types, frontend.entrypoint specifies the compilation entry file. This file and its js file with the same name will be in the directory dist/<canister_name>/ after compilation.
  • For frontend types, the source specifies all the files that need to be deployed after compilation.
  • For front-end types, dependencies are specified if there are back-end dependencies.
  • networks specify the network information for the connection. The default setting binds the local Internet computer network to the localhost address 127.0.0.1 and port 8000.
  • defaults specify some settings when programming in non-Motoko languages.

Author: Leo

Figure from: Developer Experience (DX) and the Internet Computer

Also published here.


Written by nnsdao | The boundaryless autonomous organization.
Published by HackerNoon on 2021/11/25