Setting up Private Ethereum Network
What is Ethereum Private Network?
An Ethereum Private Network is an isolated distributed system where each node is running an ethereum client(geth). Private Networks are mainly hosted by enterprises to restrict entry permissions to the network. Only the nodes with the right permissions will be able to access this Blockchain. The nodes in this network are not connected to the Ethereum mainnet, so ethers have no value here.
Installing Ethereum on Ubuntu
To install Ethereum, run the following commands in a terminal:
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository -y ppa:ethereum/ethereum
$ sudo apt-get update
$ sudo apt-get install ethereum
Now you have ethereum geth client in your system.
Create a new directory for private network
$ mkdir private-ethereum
$ cd private-ethereum
Now, create two EOA’s with the help of below command
geth --datadir ./datadir account new
Creating Genesis File
It’s basically the config file for your Ethereum network.
create a file named genesis.json
{
"config": {
"chainId": 15,
"homesteadBlock": 0
},
"difficulty": "1",
"gasLimit": "9999999",
"alloc": {
"0xd5874ff2e27b5aa6ce3943803b5799a41c1b4f67": {
"balance": "30000"
},
"0x104dc7d158c99c9a98a297ff82600eac99db7d02": {
"balance": "40000"
}
}
}
The genesis file is a JSON file that contains config parameters that create a good base network.
config: “config” block contains all the config parameters and thresholds that control the network's basic operations.
chainId: Unique Id of the network.
difficulty: This number decides how difficult the blocks will be to mine. For Private networks, it’s good to set a lower number as it lets you mine blocks quickly, which results in fast transactions.
gasLimit: This defines the limit of gas cost per block. Set this value high to avoid being limited when testing.
alloc: This part is used to allocate ethers to already created accounts.
Initialize the Node with genesis file
geth --datadir ./myDataDir init ./genesis.json
On successful instantiation, you should see the following output:
Start Node
With the Data Directory instantiated, we can now start the Blockchain. Now start the node, using the command below.
geth --datadir ./myDataDir --nodiscover --networkid 1114 console 2>> Eth.log
Done! Your private Ethereum Blockchain is up and running.
You can check the logs in a separate terminal
tail -f Eth.log
Importing accounts to Private Network
You need to copy the accounts keystore files from
path: ./datadir/keystore
path: ./myDataDir/keystore
Set up the second node
Now let’s set up a second node in the blockchain network. The process will be similar to setting up Node1.
Open a new terminal window and navigate to the project folder that contains the genesis.json file.
Initialize the new node with the following command:
geth --datadir ./yourDataDir init ./genesis.json
Note: Since we want this node to be part of the same blockchain, we use the same genesis block.
To get your node up and running with a Geth console, type in the following:
geth --datadir ./yourDataDir --nodiscover --networkid 1114 console 2>> Eth2.log
Your node is up and running.
admin.nodeInfo
Now we will connect both the nodes and form a blockchain network.
We will do this by adding one of the nodes to the other as a “peer”.
Run the following command on the Geth Consoles of both Node1 and Node2:
> admin.peers
> admin.nodeInfo
You can now add peers using the below command
admin.addPeer(“//enode id”)
All set, now you can use the console to use all sorts of geth commands like:
eth.accounts
admin.nodeInfo
personal.newAccount()
eth.accounts[0]
eth.getBalance(eth.accounts[0])
eth.coinbaseweb3.fromWei(eth.getBalance(eth.accounts[0]),"ether")personal.unlockAccount(eth.accounts[0], "<password>")web3.fromWei(eth.getBalance("0xd5874ff2e27b5aa6ce3943803b5799a41c1b4f67"), "wei")eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value: web3.toWei(1000, "ether")})eth.sendTransaction({from:”0xd5874ff2e27b5aa6ce3943803b5799a41c1b4f67”, to:”0x104dc7d158c99c9a98a297ff82600eac99db7d02”, value: web3.toWei(20, "wei")})
remember you need to mine the transaction to see the state change.
miner.start()
miner.stop()
Transaction Pool
- shows transaction pool
txpool.status