Friday, September 22, 2017

Created 3 node private Ethereum Blockchain network - Lou Person

Created 3 node private Ethereum Blockchain network

So far this weekend, I created a 3 node private Ethereum Blockchain network using AWS Ubuntu servers. Each server was independent of the other and distributed in regions around the world. There are still many aspects of Blockchain that remain a mystery, but the deeper I go, the more I learn and the more fascinating it becomes. The first thing I did was create a security group in the AWS console for the requisite ports:



Then, I launched the 3 servers, calling them server1, server2 and server3. Server1 will be used as the main node. Then, I logged into each server and installed Ethereum:  Instructions for using putty to connect to AWS launched server: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/putty.html
sudo apt-get install software-properties-common 
sudo add-apt-repository -y ppa:ethereum/ethereum 
sudo apt-get update
sudo apt-get install ethereum

On each server, before launching the Ethereum Blockchain, I created an account and noted the GUID for the account. This account ID will need to be entered in the genesis.json file when the Blockchain is initialized. This is one of those rudimentary items because each time a node is entered, it will need the same genesis.json file and each node will need to be reset with the new genesis file. I'll figure out how to make the private Blockchain more scaleable and work around this at a later time.  It actually turns out that these accounts are only needed to allocate ether to them when the Blockchain is initialized.  If the alloc statement isn't in the genesis file, you can still mine directly to those accounts.

I ran this command on each of the servers: 
geth account new
I had to enter a password for the account and then geth returned an address which I noted down for each of the three servers. This is what the genesis.json file looks like, note the alloc section where each account is listed and the initial allocation of Ethers (the crypto currency associated with Ethereum):



I created this file on each of the three nodes.
Next, the Ethereum Blockchain was initialized on each node by executing the following:
geth init Genesis.json
On server1, the main node, I launched the Blockchain through this command:
geth --networkid 23 --nodiscover --maxpeers 2 --port 30333 console

Since this is the main node, the nodeinfo is needed so it can be referenced in the other nodes, this command provided it:
> admin.nodeInfo.enode
The following was returned:
"enode://442f61b352151b4dfeb78ab01a2318v1255c9fb49ac9aedc22485f7c1aca8cc638540833e4577016f9a6180d1e911d907280af9b3892c53120e1e30619594eba@[::]:30333?discport=0"

I replaced the [::] with the IP of the server and saved it in the ./ethereum directory in the file:static-nodes.json. This file tells the node where to find the main node.

Next, I started the other two nodes on server2 and server3:
geth --networkid 23 --nodiscover --maxpeers 1 --port 30333 console
I checked that the balance matched what was set in the genesis.json file as follows:
balance = web3.fromWei(eth.getBalance(eth.accounts[0]), "ether");
and then mined 5 ethers:
miner.start();admin.sleepBlocks(1);miner.stop();
It was really exciting when I saw that I had 2 peers connecting to the main node by running:
> net.peerCount
Finally, I sent a transaction from one node to another.
I unlocked the sending node (server1):
> personal.unlockAccount(aba11d-----5e48fb33632a9b9--------b9508e')
Then sent the actual transaction across the Blockchain:
> eth.sendTransaction({from: 'aba11d-----5e48fb33632a9b9--------b9508e', to: '3--------11058759329634fb8053--------bab', value: web3.toWei(23, "ether")})

This command showed the pending transaction:
> eth.pendingTransactions
On the receiving node, the miner was started to post the transaction:
> miner.start(1);admin.sleepBlocks(1);miner.stop();
Checking the balance on the second node showed that it increased by the amount of the transaction and checking the balance of the first node, it decremented by the same.
Next, I'm going to setup a boot node in order to make this much more scaleable and automated.



No comments:

Post a Comment