A private ethereum blockchain with multiple geth instances can be run over a network or on the same computer. We will explain how to do both, and also how to solve some common issues.
Configuring genesis.json
We need to create a JSON file, which will represent the genesis block of our private blockchain. This file which we will name “genesis.json” will be used by all of our nodes, and is in fact what determines which chain that we are on.
{
"nonce": "0xdeadbeefdeadbeef",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x686f727365",
"gasLimit": "0x8000000",
"difficulty": "0x0400",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {
}
}
The gasLimit allows us to execute contracts, and by setting the mining difficulty extra low we can easily create our own ethers. If you have several computers on a network, copy this file so all of them have the same genesis.json.
Launching Geth
Each geth instance should be run with the following parameters:
geth –genesis <path to genesis.json> –networkid 321 –nodiscover –datadir <path to empty folder> –port 30304 console
What all the instances will have in common is the genesis path and the network identification. The nodes will only attempt to connect to other nodes with the same network identification, but we are also adding a –nodiscover flag to avoid accidentally connecting to nodes outside our network.
Each node will have a different data directory where the blocks data is stored and, if running on the same computer, also a different port. So if you plan on launching several instances on the same computer, you will need an empty folder to be used for each of the nodes.
Windows
If you are running multiple geth instances on a Windows computer, add the flag –ipcdisable, to avoid “Error String IPC: Access is denied”. If you do not want to disable IPC, pick a different IPC path for each node with the flag –ipcpath <path> . The default path is “\\.\pipe\geth.ipc”, so name the second node something else (e.g. “\\.\pipe\geth2.ipc”).
Connecting Geth Nodes to Each Other
In order to explicitly connect one geth node to another, we need its URL. To get it, type the following into a running geth console:
admin.nodeInfo.NodeUrl admin.nodeInfo.enode
It will return something like:
“enode://702de1918184581815e1a00e63d849524cb4126b9b5886090eabd4d4b5dc6f24c4198249775df30f835b1bfbc9a9733c014b27a99008043f3274c22e49db6fca@[::]:30304?discport=0”
The node URL includes a public key, an IP, and a port.
If you are on Windows, you may need to replace the IP part of the enode URL (after the @), with your IP. If both instances are running on the same computer, the IP would be 127.0.0.1, and the URL would be:
“enode://702de1918184581815e1a00e63d849524cb4126b9b5886090eabd4d4b5dc6f24c4198249775df30f835b1bfbc9a9733c014b27a99008043f3274c22e49db6fca@127.0.0.1:30304?discport=0”
Now, on a different geth instance console, type:
admin.addPeer(<enode of first geth>)
To check that indeed the nodes are connected to each other, type web3.net.peerCount in either of them. You
should get “1” as a result.
You can connect as many nodes as you want. Happy testing!
Extra Resources
Check out the the chapter Testing contracts and transactions, about creating accounts and mining in the book Ethereum Frontier Guide, and check Adrian Duke’s guide for pre-seeding accounts using allocation.
Permalink