Adding nodes to your private blockchain

In my previous post I showed how to create your private blockchain, but a blockchain of one node is not very useful 🙂 Let’s learn how to connect other nodes.

If you followed the steps of the previous post you should already have Node 1 installed. In Node 1 launch geth console and run admin.nodeInfo.enode. You should get something like this:

“enode://639d05c6307c25db77frwkllljssqoe98b2sdasdasdsadasaef07a6a108d133348646b27334cf4ff86a154sadasdsadsdas4d92f322a80e057a9f67e08eeb8f@[::]:30303?discport=0”

This will be the address that you have to give Node 2 so it can connect to Node 1

restart geth on Node 1 setting –maxpeers

geth –mine –networkid 1337 –nodiscover –maxpeers 3 console

Now install geth in Node2 and initialise it using the same genesis block you used to set up Node 1.

geth init genesisBlock.json

In order to connect Node 2 with Node 1 you need to create file static-nodes.json adding the IP and port (which need to be accessible) of Node 1’s machine.

cat > $HOME/.ethereum/static-nodes.json

[
enode://639d05c6307c25db77frwkllljssqoe98b2sdasdasdsadasaef07a6a108d133348646b27334cf4ff86a154sadasdsadsdas4d92f322a80e057a9f67e08eeb8f@192.168.1.10:30303
]

run geth in Node 2, use the same network id as you did in Node 1

geth –networkid 1337 –nodiscover –verbosity 5 console

if everything has worked you will see in Node2 a message showing successful connection

I0829 22:16:38.318582 eth/handler.go:258] Peer 639d05c6307c25db [eth/63]: peer connected [Geth/v1.4.11-stable-fed692f6/linux/go1.6.2]

let’s transfer some ETH from the wallet in Node 1 to the wallet in Node 2. First create a wallet in Node 2 and save the address

> personal.newAccount()
Passphrase:
Repeat passphrase:
0x1329560ae07a9fc8a3161c66dfef13da609cfda5

in our account in Node 1 we can now send money to the newly created wallet in Node 2

>personal.unlockAccount(“0x55a0040fd8ac1177d598e8880cee248c7c9ac1b9″)
>eth.sendTransaction({from:”0x55a0040fd8ac1177d598e8880cee248c7c9ac1b9″, to:”0x1329560ae07a9fc8a3161c66dfef13da609cfda5″, value:web3.toWei(5,”ether”)})

if either Node 1 or Node 2 are mining blocks you will soon see the transaction happening. You can check your new balance at your account in Node 2

>web3.fromWei(eth.getBalance(eth.accounts[0]))
5

Creating a private ethereum blockchain from scratch

Following this webpage  and this one (beware, some steps are deprecated), I’ve been able to create my own ethereum private blockchain. I document it here for those who would like to do the same.

Create a new user, group and home directory, for example privateeth1 and install geth for this user

sudo su
useradd privateeth1
groupadd privateeth
mkdir /home/privateeth1
chown privateeth1:privateeth /home/privateeth1
apt-get install -y build-essential libgmp3-dev golang
sudo su privateeth1
mkdir /home/privateeth1/ethereum
cd /home/privateeth1/ethereum
git clone https://github.com/ethereum/go-ethereum
cd go-ethereum/
make geth

create the genesis block and initialise the blockchain

cd /home/privateeth1/ethereum/
cat > genesisBlock.json
{
“nonce”: “0x0000000000000042”,
“mixhash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“difficulty”: “0x4000”,
“alloc”: {},
“coinbase”: “0x0000000000000000000000000000000000000000”,
“timestamp”: “0x00”,
“parentHash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“extraData”: “Custom Ethereum Genesis Block”,
“gasLimit”: “0xffffffff”
}
^C
go-ethereum/build/bin/geth init genesisBlock.json

start up geth in a customised port and network Id

go-ethereum/build/bin/geth –networkid 9999

open a new terminal and attach a geth console

go-ethereum/build/bin/geth attach

from within the geth console create an account and write down the address. We will initialise the amount of Ether to this account.

> personal.newAccount()

Edit the genesis block to add your account’s initial balance, afterwards initialise the blockchain again

cd /home/privateeth1/ethereum/
cat > genesisBlock.json
{
“nonce”: “0x0000000000000042”,
“mixhash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“difficulty”: “0x4000”,
“alloc”: {

“write here the account address that you received from personal.newAccount(), for example 0x55a0040fd8ac1177d598e8880cee248c7c9ac1b9”:

{ “balance”:”10000000000000000000000″ }

},
“coinbase”: “0x0000000000000000000000000000000000000000”,
“timestamp”: “0x00”,
“parentHash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“extraData”: “Custom Ethereum Genesis Block”,
“gasLimit”: “0xffffffff”
}
^C
go-ethereum/build/bin/geth init genesisBlock.json

Now you can run geth again and you will see the balance in your account to have been updated.

go-ethereum/build/bin/geth –networkid 1234 console
> web3.fromWei(eth.getBalance(eth.coinbase),”ether”)

next step is to install geth on another computer and have them communicate. I leave that for the next post 🙂