DFINITY Internet Computer, developer introduction

What’s the internet computer (ICP)?#

Dfinity internet computer is a blockchain-based cloud computing infrastructure that aims to be the basis for the next generation of the internet. It enables developers to build and deploy, secure, autonomous and tamper-proof applications! Similar to what we have today, but differing in that they’ll run directly on the network, bypassing the monopoly and control that the major tech companies have presently - Amazon, Google, Microsoft and such, control nearly all internet traffic since they own the insfrastructure that powers it!

In a nutshell, Dfinity provides a new decentralized protocol called ICP (Internet computer protocol), that combines the collective computing power of a large number of computer nodes, producing a single unified computer platform capable of supporting applications, smart contracts and data storage at any scale.

How does it work?#

Applications run on independent servers on a blockchain network, rather then on a dedicated server or cluster owned by the big tech coporations. The applications are neither owned or controlled by anyone - but a decentralized governance in which developers decide on regulation.

The independent data centers that provide Dfinity network computer power to run the applications are compensated in tokens - similar to the concept of mining, but paid for processing power. While the token’s primary utility is the medium of payment for the data centers and servers, in order to use ICP to install or run applications, is necessary to pay for gas fee in tokens.

Although the data centers held the data, blockchain encryption prevents it from ever being misused or forged, thus keeping full ownership of private data to the authors.

The Network Nervous System (NNS) is an autonomous software that governs the internet computer, the system, network structure, the economics, creates the subnets that are used to host the applications, continuosly monitoring the capacity of the network and increasing the number of nodes and subnets as required, scaling infinitely. The NNS is hosted by the network and is an integral part of the protocols used to create the ICP blockchain.

In short, the Network Nervous System (NNS) it’s the “main” blockchain that controls, configures and manages the entire network.

When the NNS creates a subnet (a unique blockchain configuration within the ICP, that integrates with other blockchains), the subnets can then hold Canisters .

Canisters are similar to smart contracts, represented as code units, that function as applications or single functions. The canisters can call any other canister, regardless of the subnet host. A canister and it’s users are unware of the subnets, they only have to interact with the internet computer and unaware of it, the internet computer protocol delegates computation and data accross the subnet nodes.

Canisters consume cycles (Cycle is a computational resource on the Internet computer, to persist data, bandwith requirements, and cpu cycles. The canisters maintain an accounting of resources used by their applications, as units of cycles. The cycles reflect cost to operate the application and the physical resources used. As a comparison, cycles are similar to AWS credits or Ethereum gas.

End users can interact with canisters through an entry point - similar to the internet of today.

How to develop applications in the ICP?#

We’re going to use the DFINITY Canister Software development kit, to help us create programs to run on the Internet computer network.

The following steps will illustrate how to create, build and deploy a very simple application called “DoubleIt” in a local development scenario that simulates the ICP network.

“Double it” specs:#

  • Accept a single argument “number” of numerical type
  • Return the computation of “number” multiplied by 2
  • When running in a terminal, we get the result in stdout
  • When running in a browser, we get the result in an alert pop-up
  • Use-case, we call pass the argument 5 and get 10

Requirements#

Assuming you have nodejs and yarn installed.

Install the DFINITY Canister SDK, in your terminal shell run:

sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"

Verify that the SDK is installed:

dfx --help

The output should be related to the SDK usage.

We’re are going to write Motoko , if you’re using VSCode there’s a “Motoko language client” plugin you should install, as it provides syntax highlighting, type details and auto-completion.

Create a new project#

The execution of the following command creates a new project in the directory doubleit, including template files and a git repository by default.

dfx new doubleit

To develop, we need to connect to the internet computer, for our case we’ll run it locally. So, start the dfx in a separate terminal window, and keep it open as you would with webpack-dev-server, for example.

dfx start

Once it completes the initialisation, you should see (the port number might be different):

Starting webserver on port 54231 for replica at "http://localhost:54231"
binding to: V4(127.0.0.1:8000)
replica(s): http://localhost:54231/

In the workdir terminal, install the dependencies (I’m using Yarn, but feel free to use your package manager):

yarn install

DoubleIt computation#

If you are familiar with modern frontend stacks, you should find the file structure quite familiar.

We’re going to edit the main entry point for the application in /src/doubleit/main.mo. I’m not going to introduce Mokoto formaly, as I believe that you should be able to understand the syntax as it’s quite close to most modern C like syntax languages. Find the language quick reference, here and primitive types .

actor {
    public func doubleIt(number: Int) : async Int {
        return number * 2;
    };
};

The Motoko programing model is actor-based , each application consists of an actor that communicates with other actors, in which there’s no shared state but instead asynchronous message passing. This permits human-readable message-passing patterns.

Try it out! Start by deploying it,

dfx deploy

Execute the doubleIt function:

dfx canister call doubleit doubleIt 2

You should see the result (4)

You can also interact with the Canister by accessing it through the browser.

But first, let’s create the UI first, as that’s what the browser will render.

Edit the UI entry point at /src/doubleit_assets/public/index.js:

import doubleit from 'ic:canisters/doubleit';

(async () => {
  const userInput = parseInt(window.prompt("Provide a number:"));
  const result = await doubleit.doubleIt(userInput);

  window.alert(result ? result : "Failed to compute!");
})();

After you save the file, deploy it!

dfx deploy

Then open the url:

http://127.0.0.1:8000/?canisterId=YOUR-CANISTER-ID

Find your Canister Id in the dfx deploy output.

❯ dfx deploy                                                                                     ─╯
Deploying all canisters.
All canisters have already been created.
Building canisters...
Building frontend...
Installing canisters...
Installing code for canister doubleit, with canister_id XXXXX-XXXXX-XXXXXX-XXXX-XXX
Installing code for canister doubleit_assets, with canister_id XXXXX-XXXXX-XXXXXX-XXXX-XXX
Authorizing our identity (default) to the asset canister...
Uploading assets to asset canister...
Deployed canisters.

You should get a pop-up requesting to provide a number and after, the result, great!

Interoperability#

Interopability between the canisters is possible because of a interface description language (IDL) that provides a description for the public interface of a service. As we know, the services are usually programs run as Canisters in the internet computer.

The IDL is called Candid , so far the candid .did files were generated by the compiler, as it provides binding for the languages we use this far, i.e. clear mapping between types, Motoko has this, while Rust currently does not. You can easily write a manual candid file for your canister no matter which language is used.

You can see the files that were generated for us under the build directory .dfx/local/canisters/..

Further read is required to understand how to use your favourite language with the internet computer, more details here .

References#

https://medium.com/dfinity/how-i-built-a-multiplayer-reversi-game-on-the-internet-computer-f67d2fed0fc3

https://github.com/enzoh/superheroes

https://sdk.dfinity.org/docs/quickstart/local-quickstart.html

https://forum.dfinity.org/t/how-to-write-a-minimal-cdk/1381/5

comments powered by Disqus