Blockchain for Enterprise
上QQ阅读APP看书,第一时间看更新

Generating enodes

In Raft, before setting up the network you have to decide the total number of nodes that will be in the network, and then generate and enode ID for each. Then, you create a static-nodes.json file listing the enode URL of all the nodes, and feed this file to every node in the network. Adding nodes to the network once the network is set up involves a different process.

Before going further, you need to know what an enode in Ethereum is. An enode is a way to describe an Ethereum node in the form of a URI. Every node in the network has a different enode. The enode conatins a  512-bit public key called a node ID, which is used to verify communication from a particular node on the network. The encode also contains the IP address and port number along with the node ID. The private key associated with the node ID is called a node key.

We will set up a network of three nodes, and then add the fourth node dynamically. Use the following three commands to generate the node keys of all four nodes:

./bootnode -genkey enode_id_1
./bootnode -genkey enode_id_2
./bootnode -genkey enode_id_3
./bootnode -genkey enode_id_4

The preceding command will create the private keys. Now, to find the node ID, you need to run the following commands:

./bootnode -nodekey enode_id_1
./bootnode -nodekey enode_id_2
./bootnode -nodekey enode_id_3
./bootnode -nodekey enode_id_4

The preceding commands will not create any new file; instead they will simply print a sample node URL with the actual node ID associated with the corresponding private key. For example: enode://[nodeID]@[IP]:[port].

Now, create a static-nodes.json file and add the following code. Make sure you replace the node IDs with your generated ones:

[
"enode://480cd6ab5c7910af0e413e17135d494d9a6b74c9d67692b0611e4eefea1cd082adbdaa4c22467c583fb881e30fda415f0f84cfea7ddd7df45e1e7499ad3c680c@127.0.0.1:23000?raftport=21000",
"enode://60998b26d4a1ecbb29eff66c428c73f02e2b8a2936c4bbb46581ef59b2678b7023d300a31b899a7d82cae3cbb6f394de80d07820e0689b505c99920803d5029a@127.0.0.1:23001?raftport=21001",
"enode://e03f30b25c1739d203dd85e2dcc0ad79d53fa776034074134ec2bf128e609a0521f35ed341edd12e43e436f08620ea68d39c05f63281772b4cce15b21d27941e@127.0.0.1:23002?raftport=21002"
]

Here, 2300x ports are for Ethereum protocol communication, and 2100x ports are for Raft protocol communication.

In Ethereum, static-nodes.json is used to list enodes of some nodes that you always want to connect to. And, using these nodes, your node can discover other nodes in the network. But, in the case of Quorum's Raft, this file has to include the enode of all the nodes in the network, as in Raft this file is used for achieving consensus, unlike in Ethereum, where this file is used for nodes discovery.