[NKN] Week 2 NKN Name Service Development Update

A
portalnetworkofficial
5 min readNov 2, 2018

--

Portal Network has been dedicated in building the NKN Name Service on top of NKN. Today, we would like to release some of our development progress to the community. We have successfully started a node and connected to their testnet, as well as sending and receiving data between two clients.

In this article, we will take an overview on NKN and discuss how we successfully send and receive data between the two clients. As most of the features of NKN are under development, we will discuss how we will implement NKN Name Service in our future updates.

This development update is to demonstrate our understanding of the NKN environment, which is crucial for future implementation of NKN-NS (NKN Name Service) standard.

What is NKN

NKN is the next generation of peer to peer network infrastructure built upon blockchain technology backed by Cellular Automata theory aiming at revolutionizing the Internet with true decentralization and native token incentive mechanism. NKN introduced the concept of Decentralized Data Transmission Network (DDTN), which combines multiple independent and self-organized relay nodes to provide clients with connectivity and data transmission capability. NKN introduces Proof of Relay as consensus mechanism and incentive model which encourages people to join the network to share and enhance network connectivity and data transmission. In short, the reward is depending on the data amount of a node relays. It makes whole network retain high efficiency as well. The “mining” is redefined as contributing to the data transmission layer, and the only way to get more rewards is providing more transmission power.

There are two main types of devices in NKN as nodes and clients:

  • Node: a device that sends, receives, and most importantly relays data.
  • Client: a device that only sends and receives but does not relay data.

Starting a Node

Prerequisites

  • Clone NKN project.
git clone https://github.com/nknorg/nkn.git
  • Docker installed.

Build using docker

1. Change current directory to nkn project.

cd nkn

2. Build dokcer image.

docker build -t nkn .

3. After docker image is built successfully, we need to prepare a configuration file to start a node. There are two default configuration files in nkn project:

  • config.testnet.json: used to join the testnet.
  • config.local.json: create and join a private chain on your localhost. In this tutorial, we want to make our own node become part of the testnet, so we will choose config.testnet.json as configuration file.
cp config.testnet.json config.json
  • And open the config.json file:
  • Replace Hostname with your IP address.

Note: NKN needs a public static IP or set up port forwarding on your router properly to join the testnet.

4. Before starting the node, we need to create a new wallet first:

docker run -it -v $PWD:/nkn nkn nknc wallet -c

5. After wallet created, we can start to deploy local node:

docker run -p 30000-30003:30000-30003 -v $PWD:/nkn --name nkn --rm -it nkn nknd

Starting a Client

Here is an example of using NKN infrastructure which allows you send packets within NKN. We’ll use the official NKN library (nkn-client-js) in the following example. It allows you to send and receive data between any NKN clients without setting up a server.

1. First of all, install the package nkn-client-js:

npm install nkn-client

2. Next, create a js file and paste the following code snippets:

//userA.js
const nkn = require('nkn-client');
const clientTest = () => {
const client = nkn();
console.log("ADDR: ", client.addr);
client.on('connect', () => {
console.log('Connection opened.');
});
client.on('message', (src, payload, payloadType) => {
if (payloadType === nkn.PayloadType.TEXT) {
console.log('Receive text message:', src, payload);
} else if (payloadType === nkn.PayloadType.BINARY) {
console.log('Receive binary message:', src, payload);
}
client.send(
src,
'Nice to meet you too!'
);
});

};
clientTest();

3. Execute the program with node:

node userA.js

4. Copy the address and create another file and paste the following code snippets:

//userB.js
const nkn = require('nkn-client');
// Replace destAddr with just copied address here
const destAddr = '03cbfbc19c1272a82408b0982ec6fa9bd91096f2508ea0108d55dae5021b3d25a6';
const clientTest = () => {
const client = nkn();
client.on('connect', () => {
console.log('Connection opened.');
console.log("ADDR: ", client.addr);
client.send(
destAddr,
'Nice to meet you!'
);
});
client.on('message', (src, payload, payloadType) => {
if (payloadType === nkn.PayloadType.TEXT) {
console.log('Receive text message:', src, payload);
} else if (payloadType === nkn.PayloadType.BINARY) {
console.log('Receive binary message:', src, payload);
}
});
};
clientTest();

Note: Remember to replace destAddr with just copied address.

5. Execute the program with node as well:

6. Check out the userA console and it should output the userB's message:

7. Back to userB console, you should see userA's response:

The above is a simple example of interchange messages between two different clients in NKN. Each NKN client has a designated NKN node that should establish a websocket connection depending on the client NKN address. Trying to connect to other nodes will get an error. During initialization, client should make a JSON-RPC call to rpc server to get the node it should establish websocket connection with.

Thank you for reading.

Stay tuned for future update.

Social Media: Telegram | Reddit | Twitter | Facebook | Github |Websites & Products: Official Website | FORUM | MUMEI | KAIZEN |

--

--