How to setup your own private Ethereum network
In this article we’ll setup a private Ethereum network on Ubuntu, the general setup is pretty much the same on other operating systems.
We’ll create a private Ethereum network comprised of 1 boot node, 1 miner and 2 peers and test API calls to peer nodes.
Terminology
- Private Network -Isolated from the other Ethereum networks
- Node- Participant in a network
- Bootnode — Helps nodes find each other in the p2p network
- Miner- Aggregates transactions into blocks
- Account- holds keys for interacting with the network
- Genesis configuration- A configuration used to bootstrap the network
Steps to create and test the private network
- Install Ethereum
- Create directory structure
- Create accounts with keypairs (public/private)
- Create a genesis configuration with the account details
- Create genesis blocks for each node
- Running a Bootnode
- Running a miner
- Running peer nodes
- Attaching to a node
Step 1: Install ethereum
sudo add-apt-repository ppa:ethereum/ethereum
sudo apt update
sudo apt install ethereum
sudo apt install puppeth
Step 2: Create directory structure
Create the directory structure shown below, the data for each of the nodes will stored in the directories node1, node2, node2 and the log files written out to the directory “log”.
Step 3: Create the accounts
Create 3 new accounts for each of the nodes, the keystore is stored in a specific directory for each node. Make a note the value after “Public address of the key: ”, you’ll need it for the genesis configuration in Step 4.
geth — datadir data/node1/ account new
geth — datadir data/node2/ account new
geth — datadir data/node3/ account new
Step 4: Genesis configuration
We’ll create a genesis configuration using the account information from the previous step. The 3 accounts are to be pre-funded in the genesis configuration to enable you to test various functionality.
The options below are self explanatory after running the “puppeth” command.
Mention the values for “Public address of the key:” to answer the question “Which accounts should be prefunded”. The genesis configuration is created and store in privnet.json (it changes based on your network name)
Step 5: Create genesis blocks for each Node
geth — datadir data/node1/ init privnet.json
geth — datadir data/node2/ init privnet.json
geth — datadir data/node3/ init privnet.json
Step 6: Running a Bootnode
Generate a key and start the boot node on any random port
bootnode -genkey boot.key
bootnode -nodekey -addr :8009
Copy the enode URL, this will be used by each node to find its peers.
Step 7: Running a miner
Every node can be a miner node by passing parameters to the geth command. For our example we’ll use just one node as the miner node and the other nodes for testing RPC API calls.
We’ll also use the account associated with the miner node for mining and rewards.
For demo purposes the password is the same for each account and stored in a file called password.txt.
The options to the command line are fairly self explanatory, use the boot node, the network id (in genesis config), unlock the account using the password in the password.txt file and “mine”.
geth — datadir data/node3 — nodiscover — syncmode ‘full’ -verbosity 6 — ipcdisable — port 30303 — bootnodes ‘enode://75e8bb7681194be0c77273454a03c328921efe81b6523f8c1e5eb819e9ad8ba676ed099b279484a8884f29cc7d5f63b5ad9d8ca655a62b3c7fc74b8f56295a2e@127.0.0.1:0?discport=8009’ — networkid 9876 — gasprice ‘1’ -unlock ‘0x219789a1844eF774Da4810e0437A105989b76332’ — password password.txt — mine
Step 8: Running peer nodes
Peer nodes are started with the details of
- Data directory
- RPC ports to use
- Network id (in genesis file)
- Bootnode to use
- APIs that are available on the node
geth — datadir data/node1 — nodiscover — syncmode ‘full’ -verbosity 6 — ipcdisable — port 30301 — rpc — rpcaddr ‘localhost’ — rpcport 8101 — rpcapi admin,debug,eth,miner,net,personal,shh,txpool,web3 — bootnodes ‘enode://75e8bb7681194be0c77273454a03c328921efe81b6523f8c1e5eb819e9ad8ba676ed099b279484a8884f29cc7d5f63b5ad9d8ca655a62b3c7fc74b8f56295a2e@127.0.0.1:0?discport=8009’ — networkid 9876
geth — datadir data/node2 — nodiscover — syncmode ‘full’ -verbosity 6 — ipcdisable — port 30302 — rpc — rpcaddr ‘localhost’ — rpcport 8102 — rpcapi admin,debug,eth,miner,net,personal,shh,txpool,web3 — bootnodes ‘enode://75e8bb7681194be0c77273454a03c328921efe81b6523f8c1e5eb819e9ad8ba676ed099b279484a8884f29cc7d5f63b5ad9d8ca655a62b3c7fc74b8f56295a2e@127.0.0.1:0?discport=8009’ — networkid 9876
Step 9: Attaching to peer nodes
Connect to a node 1 and run commands if you want to test various APIs (admin,debug,eth,miner,net,personal,shh,txpool,web3)
geth attach http://localhost:8101
> admin.nodeInfo gives the details of Node 1
You can test the other APIs using similar calls. The entire setup is too chatty and I need to look at ways of optimizing it.
A static file can also be used to find peers (an article for later).
Check out the below links for Ethereum’s p2p networking under the hood.
https://medium.com/shyft-network-media/understanding-ethereums-p2p-network-86eeaa3345
