TP BlockChain.docx
TP BlockChain.docx
Introduction
Before we describe the steps to start the miners, it is important to understand the requirements
for each node to join the same private blockchain:
● Each node will use a distinct data directory to store the database and the wallet.
● Each node must initialize a blockchain based on the same genesis file.
● Each node must join the same network id different from the one reserved by
Ethereum (0 to 3 are already reserved).
● The port numbers must be different if different nodes are installed on the same
computer.
All the operations must be performed from your computer.
Step 1 – Create the datadir folder
When running a private blockchain, it is highly recommended to use a specific folder to store
the data (database and wallet) of the private blockchain without impacting the folders used to
store the data coming from the public blockchain.
From your computer, create the folder that will host your first miner:
computer$ mkdir -p ~/ChainSkills/miner1
Repeat the operation for the second miner :
computer$ mkdir -p ~/ChainSkills/miner2
Step 2 – Create the Genesis file
Each blockchain starts with a genesis block that is used to initialize the blockchain and
defines the terms and conditions to join the network.
Our genesis block is called “genesis.json” and is stored under “~/ChainSkills” folder. Create
a text file under ~/ChainSkills, called genesis.json, with the following content:
{
"nonce": "0x0000000000000042",
"mixhash":
"0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x400",
"alloc": {},
"coinbase": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash":
"0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x436861696e536b696c6c732047656e6573697320426c6f636b",
"gasLimit": "0xffffffff",
"config": {
"chainId": 42,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
}
}
Among the parameters, we have the following ones:
● difficulty: if the value is low, the transactions will be quickly processed within
our private blockchain.
● gasLimit: define the limit of Gas expenditure per block. The gasLimit is set to the
maximum to avoid being limited to our tests.
Step 3 – Initialize the private blockchain
It’s time to initialize the private blockchain with the genesis block.
This operation will create the initial database stored under the data directory dedicated to
each miner.
Step 3.1 – Initialize miner #1
Type the following command to create the blockchain for the first miner: computer$
cd ~/ChainSkills
computer$ geth --datadir ~/ChainSkills/miner1 init genesis.json
I1226 00:44:16.572007 cmd/utils/flags.go:615] WARNING: No etherbase set and no
accounts found as default
...
I1226 00:44:16.604262 cmd/geth/chaincmd.go:131] successfully wrote genesis block
and/or
chain rule set:
6e92f8b23bcdfdf34dc813cfaf1d84b71beac80530506b5d63a2df10fe23a660
The logs provide the following information:
● keystore: location of your wallet used to store the accounts that you will create on
this node.
● rpc and rpcport: enabling HTTP-RPC server and giving its listening port
number
● port: network listening port number, on which nodes connect to one another to
spread new transactions and blocks
● nodiscover: disable the discovery mechanism (we will pair our nodes later)
● password: path to the file containing the password of the default account
● ipcpath: path where to store the filename for IPC socket/pipe You can find more
information about Geth parameters right here.
We recommend that you store the Geth command into a runnable script. In our example, this
script is called “startminer1.sh” and is located here: ~/ChainSkills/miner1
#!/bin/bash
First, create the “password.sec” file under ~/ChainSkills/miner2 that will contain
the password of the default account of your miner #2: Create the script
“startminer2.sh“that will be stored at the following location: ~/ChainSkills/miner2
#!/bin/bash
geth --identity "miner2" --networkid 42 --datadir "~/ChainSkills/miner2" --nodiscover
--mine
--rpc --rpcport "8043" --port "30304" --unlock 0
--password
~/ChainSkills/miner2/password.sec --allow-insecure-unlock
--ipcpath
"~/ChainSkills/miner2/geth.ipc
"
The difference from the first miner is about the following parameters:
● password: path of the file containing the password of the default account on the
miner #2
You will notice that we have the same networkid (42). This will be useful later.
We are not providing the “ipcpath” parameter. This means that the “geth.ipc” will be created
in the data directory of the miner.
First, you can see that the “miner” module is not specified in the list of modules: eth:1.0
net:1.0 rpc:1.0 web3:1.0
This is the reason why we have an error when we try to stop the mining process.
The following command is more useful:
computer $ geth attach ipc:./miner2/geth.ipc Welcome
to the Geth JavaScript console!
instance: Geth/miner2/v1.5.5-stable-ff07d548/darwin/go1.7.4
coinbase: 0x22b2f81b46146aa3e1036fd3dc5cbcf12551b2db
at block: 1417 (Sat, 31 Dec 2016 16:12:25 CET) datadir:
/Users/eloudsa/ChainSkills/miner2
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0
shh:1.0
txpool:1.0
web3:1.0
> miner.stop() true
Here the miner can be managed through the console because the miner module is available
from the IPC interface.
Keep this in mind if you have any trouble running a command from the Geth console.
Of course, you can define the modules available for the Geth console by mentioning them
from the command line (ipcapi and rpcapi parameters).
For example, let’s change the Geth command by mentioning the “miner” module:
#!/bin/bash
geth --identity "miner2" --dev --networkid 42 --datadir "~/ChainSkills/miner2" --nodiscover
-
-mine --rpc --rpcport "8043" --rpcapi "db,eth,net,web3,miner" --port "30304" --unlock 0
--
password
~/ChainSkills/miner2/password.sec
Start the miner and open the console with the following command:
computer $ geth attach http://127.0.0.1:8043 Welcome
to the Geth JavaScript console!
instance: Geth/miner2/v1.5.5-stable-ff07d548/darwin/go1.7.4
coinbase: 0x22b2f81b46146aa3e1036fd3dc5cbcf12551b2db
at block: 1473 (Sat, 31 Dec 2016 16:21:22 CET) modules:
eth:1.0 miner:1.0 net:1.0 rpc:1.0 web3:1.0 Now the miner
module is available from the Geth console!
You can manage the mining process:
> miner.stop() true
This shows that by changing the Geth command, you can allow or restrict the usage of some
APIs available from the HTTP-RPC or IPC-RPC interfaces.
Step 5.7 – Miner #2: stop
The principle is the same as described for the miner #1.
Step 6 – Send ethers within miner #1
To ensure that our mining node is properly installed, let’s try to send some ethers between
accounts created on each miner.
Step 6.1 – Start the miner and its console
> eth.getBalance(eth.coinbase)
2.48859375e+21
> eth.getBalance(eth.accounts[1])
0
The default account has plenty of ethers obtained during the mining process. We will use
them to test our solutions.
Use the following command to get the balance in ether:
> web3.fromWei(eth.getBalance(eth.coinbase))
3008.125
By default, the balance is expressed in Wei that is the base unit of ether.
Step 6.3 – Stop mining
We stop the mining process to let us review the content of a transaction:
> miner.stop() true
transactionIndex: null,
v: "0x1c",
value: 10000000000000000000
}]
We can easily identify the following information:
● The cost of the transaction (gas x gasPrice) in Wei Let the miner
Test your miner #2 by following the steps described for miner #1.
Keep in mind that the Geth console will have to be started using one of the following
commands (change the port id according to your settings): computer$ geth attach
ipc:/Users/eloudsa/ChainSkills/miner2/geth.ipc
OR
computer$ geth attach http://127.0.0.1:8043
Step 8 – Send ethers between nodes of the private blockchain
We will send 5 ethers from the default account of the miner #1 to the account #1 of the miner
#2.
> web3.fromWei(eth.getBalance(eth.accounts[1]))
10
Step 8.3 – Send Ethers from miner #1
From the console of the first miner, send 5 ethers from the default account (eth.coinbase) to
the address of the account 1 in miner #2:
> eth.sendTransaction({from: eth.coinbase, to: '[REPLACE WITH ADDRESS OF
ACCOUNT 1 IN MINER #2]', value: web3.toWei(5, "ether")})
"0x866db90bade948dbec0679b0a673807a74d6b3d94b3a7616dd86d72eb8a72e9b"
Step 8.4 – Check balance from miner #1
Check the balance from the Geth console attached to the miner #1:
> web3.fromWei( eth.getBalance("[REPLACE WITH ADDRESS OF ACCOUNT 1 IN
MINER
#2]"))
5
We have 5 ethers while we should expect 15 ethers.
Why?
Each miner is running its own version of the private blockchain. The transactions are not
distributed within the same private blockchain.
To synchronise the blockchain, we have to pair the nodes with each other.