Social media


How to create your own Ethereum testnet?

Author: LB 2021-11-21 209

I had been having hard times with setting up my own geth client on localhost. I faced several problems and it took at least 2-3 hours to create even two ethereum nodes and launch a mining thread. I thought it expedient to document my experience for the future generations. Let's see the steps I went through to create my own local ethereum network.


So first of all, I downloaded the most popular ethereum protocol implementation from https://geth.ethereum.org/downloads/ for Ubuntu and updated my system:


$ sudo apt-get update && sudo apt-get install build-essential -y

Now you can create an installation directory for geth and untar the binary into this location. The very first step of setting up your ethereum nodes is to create the genesis block of the blockchain. This first block will be loaded into every node and they'll reach consensus in the subsequent steps provided that they're aware of this genesis block. I use vim as a text editor, but feel free to use your favourite one to create your own genesis.json.


{
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x40000",
"alloc": {},
"coinbase": "0x3333333333333333333333333333333333333333",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x8000000",
"config": {}
}


So every time you want to create a new ethereum node you have to use this genesis.json as an input. Let's start with two nodes!


$ ./geth --datadir node1 init genesis.json

And initialize the second one using the following command:


$ ./geth --datadir node2 init genesis.json


As soon as the initialization ends you can try running the nodes with the following commands:


$ ./geth --datadir node1 --networkid 1234 --ipcpath node1/geth.ipc --rpc.allow-unprotected-txs console


You're free to choose your nodeId, it doesn't really matter this time so I chose 1234. Next, you can set the location to be used for IPC communication and I had to use the "allow-unprotected-txs" flag to enable transactions without specifying the chainId.



Now we're in a position to launch the second node on our computer:


$ ./geth --datadir node2 --port 30304 --nodiscover --ipcpath node2/geth.ipc --networkid 1234 --nat=extip:192.168.0.125 console


So this part was the most troublesome for me. First of all, you need to set a different port. 30303 is already in use by default by node 1, so I chose 30304. Obviously, you need to set the same networkId and the "--nat=extip:192.168.0.125" is a must if you want to run the ethereum node on localhost. You have to check your own local IP address and specify it in the --nat=extip: flag.


If no errors occurred, you have two ethereum nodes running and waiting for your commands. In order to connect them, you have to use admin.nodeInfo in the console of node 2 to find its enode address. This is what I got when I set up my network:


enode: "enode://48aeef026e3111463c8236a0777d6827b9330009937fd7bc2276c61c6d265b018f279f59a37a49f1c5ee6d3cdfebd9ad590485bcef87944d3e3e15bac49ccbff@10.138.189.226:30304?discport=0"


In the next step you need to use this address to connect node 2 to node 1. Use the console at node 1 and connect the two nodes with the following command using the proper enode address:


admin.addPeer("enode://48aeef026e3111463c8236a0777d6827b9330009937fd7bc2276c61c6d265b018f279f59a37a49f1c5ee6d3cdfebd9ad590485bcef87944d3e3e15bac49ccbff@10.138.189.226:30304?discport=0")


The enode address is the one displayed in the console of node 2. You can create a new ethereum wallet address with the following command:


personal.newAccount()


You need to unlock your wallet prior to sending funds:


personal.unlockAccount('')


You have to specify your ethereum wallet's public address in the function argument. If you wish to transfer funds in the test network, use the following command with the arguments corresponding to your use case:


eth.sendTransaction({from:'', to:'', value: web3.toWei(0.05, "ether"), gas:21000});


In order to evolve the blockchain you need to launch a miner thread. Use the following command to do that:


miner.start()



If you feel like you had enough block mined, use the following command to terminate mining:


miner.stop()


Interesting entries


23.01.2022

Profiling the RTX 3060 Ti and its memory transfer rate

22.01.2022

Core clock settings for RTX 3060 Ti when mining Ethereum

20.01.2022

Core clock settings for RTX 3070 Ti Founders Edition when mining ethereum

19.01.2022

A few thoughts on ethereum mining and overclocking

17.01.2022

Ethereum mining hashrates and the core clock frequency of an RTX 3080 Ti Founders Edition