Klaytn Docs
Search…
Nested ServiceChain
This chapter explains how to build ServiceChain networks in a hierarchical structure by adding a new ServiceChain network to the ServiceChain network built in the previous chapter. The ServiceChain network to be added also consists of 4 SCNs in this example. The ServiceChain network constructed in the previous chapter is defined as L2, and the ServiceChain network to be newly constructed is defined as L3. We are going to connect a bridge between L2 and L3 to create a hierarchical structure. The overall structure of the ServiceChain network to be constructed in this chapter is shown in the figure below.

Prerequisites

  • Assume that you have progressed to the ServiceChain configuration and Baobab EN described in Nested ServiceChain. So we will briefly explain what was explained in the previous section.
  • Assumptions and Limitations
    • One EN can bridge one-to-one to one of the SCNs of the ServiceChain L2. Similarly, one SCN in L2 of the ServiceChain can bridge one-to-one to one of the SCNs in L3.
    • An SCN node can have a main bridge and a sub bridge at the same time. However, the port numbers of the main bridge and the sub bridge must be set differently. (eg main-bridge: 50505, sub-bridge:50506)
    • Not all SCNs in L2 need to be bridged to EN, and likewise SCNs in L3 need not all be bridged to L2. However, for high availability, it is recommended that there are two or more main-bridge and sub-bridge pairs between chains. In this chapter, only one pair will be connected between L2 and L3, and the high availability between L2 and L3 is same to the HA between Baobab and L2.

Step 1: Create and update Homi data for L3

As when configuring ServiceChain L2, execute the homi command to create scripts and configuration files for building L3. You can run homi on any Linux/Mac PC. Baobab's chainID is 1001, and L2's chainID was set to 1002 in the previous example, so for convenience, L3's chainID is set to 1003. When operating a blockchain for an actual service, you must register a new chainID value at https://chainlist.defillama.com/ to avoid the chainID conflict with other ServiceChains and EVM chains.
1
$ ./homi setup local --cn-num 4 --test-num 1 --servicechain --chainID 1003 --p2p-port 22323 -o homi-output
2
Created : homi-output/keys/passwd1
3
Created : homi-output/keys/passwd2
4
Created : homi-output/keys/passwd3
5
Created : homi-output/keys/passwd4
6
Created : homi-output/scripts/genesis.json
7
Created : homi-output/keys/nodekey1
8
Created : homi-output/keys/validator1
9
Created : homi-output/keys/nodekey2
10
Created : homi-output/keys/validator2
11
Created : homi-output/keys/nodekey3
12
Created : homi-output/keys/validator3
13
Created : homi-output/keys/nodekey4
14
Created : homi-output/keys/validator4
15
Created : homi-output/scripts/static-nodes.json
16
Created : homi-output/keys_test/testkey1
17
Created : homi-output/keys_test/keystore1/0xdC7218621513f71d609653d22C39d79d558d9CDC
18
Created : homi-output/Klaytn.json
19
Created : homi-output/Klaytn_txpool.json
Copied!
Update IP address and port information of ServiceChain L3 nodes in homi-output/scripts/static-nodes.json.
1
[
2
"kni://[email protected]:22323?discport=0&type=cn",
3
"kni://[email protected]:22323?discport=0&type=cn",
4
"kni://[email protected]:22323?discport=0&type=cn",
5
"kni://[email protected]:22323?discport=0&type=cn"
6
]
Copied!
Copy homi-output to all SCN nodes (SCN-L3-01, SCN-L3-02, SCN-L3-03, SCN-L3-04) of ServiceChain L3.
1
$ scp -r path/to/homi-output [email protected]:~/
2
$ scp -r path/to/homi-output [email protected]:~/
3
$ scp -r path/to/homi-output [email protected]:~/
4
$ scp -r path/to/homi-output [email protected]:~/
Copied!
Initialize all nodes.
1
$ kscn --datadir ~/data init ~/homi-output/scripts/genesis.json
2
$ ls ~/data
3
keystore klay kscn
Copied!
Connect to all SCNs (SCN-L3-01, SCN-L3-02, SCN-L3-03, and SCN-L3-04), copy static-nodes.json to the data folder ~/data, and copy nodekeys one by one.
1
$ cp ~/homi-output/scripts/static-nodes.json ~/data/
2
$ cp ~/homi-output/keys/nodekey{1..4} ~/data/klay/nodekey
Copied!

Step 2: SCN configuration in L3

Edit conf/kscnd.conf on all SCNs in ServiceChain L3 as follows: The PORT uses 22323, the default port of the ServiceChain. DATA_DIR is ~/data.
1
...
2
PORT=22323
3
...
4
DATA_DIR=~/data
5
...
Copied!
Run the ServiceChain on all SCN nodes in L3 and check if it works properly.
1
$ kscnd start
2
Starting kscnd: OK
3
$ kscn attach --datadir ~/data
4
> klay.blockNumber
5
10
Copied!

Step 3: Restart after setting L2 main bridge

Connect to the console of SCN-L2-03 node, (Note: this is not in L3 but in L2) that will act as the main bridge in the ServiceChain L2.
Edit the kscn configuration file conf/kscnd.conf of SCN-L2-03 as follows.
1
SC_MAIN_BRIDGE=1
Copied!
Restart kscnd on SCN-L2-03.
1
SCN-L2-03$ kscnd stop
2
SCN-L2-03$ kscnd start
Copied!

Step 4: Check KNI of Main Bridge Node

Check the KNI information of SCN-L2-03 node. This value will be used to create the main-bridges.json file of SCN-L2-03 node, which will be set up the subbridge in the ServiceChain L3.
1
SCN-L2-03$ kscn attach --datadir ~/data
2
> mainbridge.nodeInfo.kni
3
"kni://[email protected][::]:50505?discport=0"
Copied!

Step 5: Configure L3 sub-bridge

Connect to SCN-L3-01 node that will have a subbridge of the ServiceChain L3 (Note: this is not L2). Create main-bridges.json under ~/data folder. Replace [::] after @ with the IP address of the node you checked in step 4.
1
SCN-L3-01$ echo '["kni://[email protected]:50505?discport=0"]' > ~/data/main-bridges.json
Copied!
Edit the configuration file conf/kscnd.conf of the SCN-L3-01 node with subbridge as follows. set SC_SUB_BRIDGEto 1 for activating the bridge connection, and SC_PARENT_CHAIN_ID is 1002, chainID of L2, Set SC_ANCHORING to 1 to automatically anchor data upon restart. You can also access the SCN-L3-01 shell and turn on data anchoring with the subbridge.anchoring(true) command or turn it off with the subbridge.anchoring(false) command. SC_ANCHORING_PERIOD is a parameter that determines how often anchoring transactions are sent to the parent chain. Set the node to anchor every 10 blocks by specifying a value of 10. Default is 1.
1
SC_SUB_BRIDGE=1
2
3
SC_PARENT_CHAIN_ID=1002
4
5
SC_ANCHORING=1
6
SC_ANCHORING_PERIOD=10
Copied!
Restart kscnd on SCN-L3-01 after completing the setup.
1
SCN-L3-01$ kscnd stop
2
Shutting down kscnd: Killed
3
SCN-L3-01$ kscnd start
4
Starting kscnd: OK
Copied!
Check subbridge.peers.length to see if SCN-L3-01 is connected to SCN-L2-03, check subbridge.latestAnchoredBlockNumber to check the latest anchored block number to see if anchoring is in progress.
1
SCN-L3-01$ kscn attach --datadir ~/data
2
> subbridge.peers.length
3
1
4
> subbridge.latestAnchoredBlockNumber
5
5010
Copied!