Ethereum Private Network Construction
I wrote this document for sharing my experience for constructing the private ethereum blockchain.
Installation VirtualBox
https://www.virtualbox.org/wiki/Downloads
Creating VM on VirtualBox. I made use of the image from below link.
https://ubuntu.com/download/desktop
I deployed "Ubuntu 20.04.3 LTS"
Installation of "git "
$ sudo apt install git
Create directory for ethereum installation & cloning the go-ethereum from git
$ mkdir ethereum
$git clone https://github.com/ethereum/go-ethereum.git
Enter into the go-ethereum directory and install the go language and compilers
$cd go-ethereum
$sudo apt-get install -y build-essential golang
In the go-ethereum folder build the geth using "make all" command
$sudo make all
During above step, there could be error like "hash/maphash": missing dot in first path element". I looked up with the error message and got some solution.
I just upgrade the go language version and re-install the build-essential go lang.
$ wget https://go.dev/dl/go1.17.5.linux-amd64.tar.gz
If above command is not working, we could download the file from the website of the go language.
$ sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xvf go1.1.7.3.linux-amd64.tar.gz
I added below shell variable update for this installation. However you could add it to .profile of the user home directory.
$ export PATH=$PATH:/usr/local/go/bin
Please check the "go" can be executed in your directory.
$go version
Another things I tried is shown as below.
$sudo add-apt-repository ppa:longsleep/golang-backports
$sudo apt update
$sudo apt install golang-go
$sudo make all
It takes more than 30 minutes in my envornment to make all the geth (I don't know why it takes so long, I thought it is caused by the network delay and virtual box)
Run the geth and see the version of the geth
$cd build/bin
$geth version
Update the path for the geth
$vim ~/.bash_profile
add below two lines for the geth's executions.
GETH=/home/john/ethereum/go-ethereum
PATH="$PATH:$GETH/build/bin"
$source ~/.bash_profile
Return to the ethereum directory and create data directory
$cd ../../../
$mkdir data
You'd better create password file for your convinenience. I just put the 9 decimals.
$vim data/password
123456789
Using the data directory and the password file, create new accounts as many as you want to create
$geth --datadir data account new --password data/password
You can find the account information from the keystore directory.
$ls -al data/keystore
create the genesis.json for the blockchain. I tried a few example but some of them are not working. Here is the working genesis.json from https://github.com/ethereum/go-ethereum/issues/20676. You'd better create your own genesis.json using "puppeth"
{
"config": {
"chainId": 10,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"ethash": {}
},
"nonce": "0x0",
"timestamp": "0x5e4a53b2",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x47b760",
"difficulty": "0x80000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"0000000000000000000000000000000000000088": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000"
}
},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
Initialize the blockchain using the genesis.json
$geth --datadir data init data/genesis.json
If you face a message like "Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory", you can type in below commands for fixing.
$sudo apt-get install pcscd
$sudo service pcscd start
Now execute the geth
$ cd ~/ethereum
$ geth --networkid 7402 --nodiscover --datadir data --http.addr 0.0.0.0 --http --http.port 8545 --http.corsdomain "*" --http.api="db,eth,net,web3,personal,web3,miner,admin" --miner.threads 1 console 2>> data/geth.log
The documents that I referred presented the --rpc flag but currently it is not working and replaced with http.
Now wre are in the console for the ethereum geth, we can try mining command with our coin.base.
> eth.coinbase
> miner.start()
The miner.start may return 'null' however you don't need to worry and please check the mining result using below command.
> eth.blockNumber
will return the number of mined blocks
> web3.eth.mining
will return "true", if the mining is proceeding.
Lastly, we can transfer ether to other accounts using below commands
> eth.sendTransaction({from : eth.coinbase, to : eth.accounts[1], value : web3.toWei(1,'ether')});
How ever to make it work, we need to unlock the account using a commands.
> personal.unlockAccount(eth.coinbase,'',0);
In some enviroment, above command is not working, in that case re-start the geth using below command.
>exit
$ geth --networkid 7402 --nodiscover --datadir data --http.addr 0.0.0.0 --http --http.port 8545 --http.corsdomain "*" --http.api="db,eth,net,web3,personal,web3,miner,admin" -allow-insecure-unlock --miner.threads 1 console 2>> data/geth.log
Connect the ethereum network using new terminal
$geth attach http://localhost:8545
If the geth path does not working, execute below command again.
$source ~/.bash_profile
If your private ethereum network is newly started and does not have any accounts, run below commands sequentially.
> personal.newAccount()
>miner.setEtherbase(eth.accounts[0])
>eth.coinbase
>miner.start()
If you want to use a public IP address for the private network, you can set the IP address for the geth node
$ geth --networkid 7402 --nodiscover --datadir data --http.addr 192.168.0.100 --http --http.port 8545 --http.corsdomain "*" --http.api="db,eth,net,web3,personal,web3,miner,admin" -allow-insecure-unlock --miner.threads 1 console 2>> data/geth.log
On the ubuntu linux, we'd better enable the geth service and make it started when the server boots. For that purpose, make the execution command as the script file and use the script in the systemd service. Please keep it mind that the systemd srvices are located in "/etc/systemd/system"
create a <geth.sh> file
geth --networkid 7402 --nodiscover --datadir data --http.addr "0.0.0.0" --http --http.port 8545 --http.corsdomain "*" --http.api="db,eth,net,web3,personal,web3,miner,admin" -allow-insecure-unlock --miner.threads 1 2>> data/geth.log
creat a <geth.service> file
[Unit]
Description=Ethereum go client
[Service]
Type=simple
ExecStart=/bin/bash geth.sh
[Install]
WantedBy=default.target
$ systemctl enable geth.service