Setting up Private Ethereum TestNet

None of the tutorials on the internet that involve setting up of a private Ethereum TestNet on the latest version 1.5.8 and above. so I decided to document my experiences on getting this done.

Most Ethereum clients allow you to connect to two separate public block chains. The main blockchain network, which would require you to make use of *real ether* for all transactions or you can make use of the TestNet. On the test network, you have the ability to mine ether which can then be used for all your transactions.

In order to develop distributed apps (Dapps) you will need access to an Ethereum client and an Ethereum wallet. Mist the most popular wallet used by most developers for creating Dapps. However, if you have limited resources and would still like to build Dapps, I would recommend using MetaMask. MetaMask is a Chrome plugin(a plugin for FireFox is also expected to be released soon). This plugin acts a thin client that allows the user to connect to both the live Ethereum network as well the test network.

The objective of this blog is to demonstrate the ability to set up your own Ethereum test network (within your own infrastructure of cloud) and then use the same to develop Dapps. For the purpose of this blog we shall be making use of Amazon Web Services running Ubuntu 16.04, Geth Ethereum client.

  • AWS setup: Launch two Ubuntu 16.04 instances on Amazon Web Services and make a note of their ip addresses. You will need these for later configuration.
  • Geth: Install Geth in each of the instances using the following commands:
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum
  • Setting up genesis block: The first block (block zero) of the blockchain is called as the genesis block. This is the only block on the network that doesn't point to the predecessor block. By default, the genesis block is hardcoded into Ethereum clients. However as we are setting up our own test network, this step is essential. All subsequent blocks will be referencing this genesis block. On Geth client, this is achieved by setting up a JSON file (say mygenesis.json) with the following values. Ensure this file is added to both the instances that will be running Geth (as Ethereum consensus algorithm ensures no other node will agree with your version of blockchain unless they have the same genesis block). To understand what each of these params means please refer to this answer on stack exchange.
{
  "alloc": {},
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x20000",
  "extraData"  : "",
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0x0000000000000042",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}
  • Running geth init: Initialize the geth terminal with our genesis blockchain file.
    • mygenesis.json: The JSON file to be used for configuring the Ethereum node.
$ geth --datadir="ethdata" init mygenesis.json
  • Launch a geth console: Run geth in console mode and create a new account to fill some ether into it. Please ensure to run the geth console under a new --networkid, ensure the networkid is a random number sufficiently large (to avoid clashes with others). Also run the new node with the option --nodiscover which will prevent any untoward handshake from other ethereum nodes. ethdata  is a directory in the current folder from where I am executing the geth command. This is the location where the blockchain data will be stored.  
  • Create a new account: A new account can be created from the console using the command admin.newAccount("<password>")
ubuntu@ip-172-31-28-11:~$ geth --datadir="ethdata" --networkid 65535 --nodiscover console
I0326 11:36:47.028672 node/config.go:445] Failed to start Ledger hub, disabling: libusb: unknown error [code -99]
I0326 11:36:47.028882 cmd/utils/flags.go:613] WARNING: No etherbase set and no accounts found as default
I0326 11:36:47.028970 ethdb/database.go:83] Allotted 128MB cache and 1024 file handles to /home/ubuntu/ethdata/geth/chaindata
I0326 11:36:47.047079 ethdb/database.go:176] closed db:/home/ubuntu/ethdata/geth/chaindata
I0326 11:36:47.047475 node/node.go:176] instance: Geth/v1.5.9-stable/linux/go1.8
I0326 11:36:47.047573 ethdb/database.go:83] Allotted 128MB cache and 1024 file handles to /home/ubuntu/ethdata/geth/chaindata
I0326 11:36:47.078045 eth/backend.go:187] Protocol Versions: [63 62], Network Id: 65535
I0326 11:36:47.078398 eth/backend.go:215] Chain config: {ChainID: 0 Homestead: <nil> DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: <nil> EIP158: <nil>}
I0326 11:36:47.079106 core/blockchain.go:219] Last header: #0 [62e3c23e…] TD=131072
I0326 11:36:47.079187 core/blockchain.go:220] Last block: #0 [62e3c23e…] TD=131072
I0326 11:36:47.079279 core/blockchain.go:221] Fast block: #0 [62e3c23e…] TD=131072
I0326 11:36:47.081285 p2p/server.go:340] Starting Server
I0326 11:36:47.106873 p2p/server.go:608] Listening on [::]:30303
I0326 11:36:47.107290 node/node.go:341] IPC endpoint opened: /home/ubuntu/ethdata/geth.ipc
Welcome to the Geth JavaScript console!

instance: Geth/v1.5.9-stable/linux/go1.8
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

> personal.newAccount("testing")
"0xe94c3a6c176af3f091d3cc40ba35efa96a8df444"
> I0326 11:37:21.218308 cmd/geth/main.go:286] New wallet appeared: keystore:///home/ubuntu/ethdata/keystore/UTC--2017-03-26T11-37-20.112555465Z--e94c3a6c176af3f091d3cc40ba35efa96a8df444, Locked

  • Start mining ether to that account: Any operation on Ethereum is possible only if you have ether (the currency used in Ethereum blockchain). When you set up a test network, you will need to mine some money into the accounts, otherwise you won't be able to perform any operations. Mining is done by the command miner.start(<number of threads>). This command will take some time to execute and accumulate ether into the previously created account.
> miner.start(1)
true
> I0326 11:37:59.391891 eth/backend.go:473] Automatic pregeneration of ethash DAG ON (ethash dir: /home/ubuntu/.ethash)
I0326 11:37:59.391922 miner/miner.go:136] Starting mining operation (CPU=1 TOT=2)
I0326 11:37:59.392156 miner/worker.go:517] commit new work on block 1 with 0 txs & 0 uncles. Took 213.203µs
I0326 11:37:59.392194 vendor/github.com/ethereum/ethash/ethash.go:259] Generating DAG for epoch 0 (size 1073739904) (0000000000000000000000000000000000000000000000000000000000000000)
I0326 11:37:59.400043 eth/backend.go:480] checking DAG (ethash dir: /home/ubuntu/.ethash)
I0326 11:38:00.244427 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 0%
I0326 11:38:02.155513 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 1%
I0326 11:38:04.038246 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 2%
I0326 11:38:05.923593 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 3%
I0326 11:38:07.840755 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 4%
I0326 11:38:09.712698 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 5%
I0326 11:38:11.778678 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 6%
I0326 11:38:13.606591 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 7%
I0326 11:38:15.448152 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 8%
I0326 11:38:17.244839 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 9%
I0326 11:38:19.035317 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 10%
I0326 11:38:20.831274 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 11%
I0326 11:38:22.653658 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 12%
I0326 11:38:24.450757 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 13%
I0326 11:38:26.245772 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 14%
I0326 11:38:28.067064 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 15%
I0326 11:38:29.884266 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 16%
I0326 11:38:31.688547 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 17%
I0326 11:38:33.477853 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 18%
I0326 11:38:35.270448 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 19%
I0326 11:38:37.083875 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 20%
I0326 11:38:38.894926 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 21%
I0326 11:38:40.698617 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 22%
I0326 11:38:42.500703 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 23%
I0326 11:38:44.287999 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 24%
I0326 11:38:46.081991 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 25%
I0326 11:38:47.867427 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 26%
I0326 11:38:49.659364 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 27%
I0326 11:38:51.429142 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 28%
I0326 11:38:53.215271 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 29%
I0326 11:38:55.014460 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 30%
I0326 11:38:56.820705 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 31%
I0326 11:38:58.590597 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 32%
I0326 11:39:00.395519 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 33%
I0326 11:39:02.363880 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 34%
I0326 11:39:04.186879 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 35%
I0326 11:39:06.002551 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 36%
I0326 11:39:07.955736 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 37%
I0326 11:39:09.743026 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 38%
I0326 11:39:11.552874 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 39%
I0326 11:39:13.408890 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 40%
I0326 11:39:15.228422 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 41%
I0326 11:39:17.036883 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 42%
I0326 11:39:18.842579 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 43%
I0326 11:39:20.666090 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 44%
I0326 11:39:22.480225 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 45%
I0326 11:39:24.290427 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 46%
I0326 11:39:26.092289 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 47%
I0326 11:39:27.887400 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 48%
I0326 11:39:29.646587 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 49%
I0326 11:39:31.405099 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 50%
I0326 11:39:33.150191 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 51%
I0326 11:39:34.883658 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 52%
I0326 11:39:36.626548 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 53%
I0326 11:39:38.379738 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 54%
I0326 11:39:40.121179 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 55%
I0326 11:39:41.869099 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 56%
I0326 11:39:43.614320 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 57%
I0326 11:39:45.347021 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 58%
I0326 11:39:47.102058 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 59%
I0326 11:39:48.836464 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 60%
I0326 11:39:50.685036 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 61%
I0326 11:39:52.488388 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 62%
I0326 11:39:54.272054 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 63%
I0326 11:39:56.052606 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 64%
I0326 11:39:57.840091 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 65%
I0326 11:39:59.618988 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 66%
I0326 11:40:01.528229 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 67%
I0326 11:40:03.637357 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 68%
I0326 11:40:05.615453 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 69%
I0326 11:40:07.544560 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 70%
I0326 11:40:09.535733 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 71%
I0326 11:40:11.535300 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 72%
I0326 11:40:13.349811 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 73%
I0326 11:40:15.192857 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 74%
I0326 11:40:17.096354 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 75%
I0326 11:40:18.938943 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 76%
I0326 11:40:20.745968 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 77%
I0326 11:40:22.535773 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 78%
I0326 11:40:24.342986 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 79%
I0326 11:40:26.145004 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 80%
I0326 11:40:27.939168 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 81%
I0326 11:40:29.720689 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 82%
I0326 11:40:31.501600 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 83%
I0326 11:40:33.294428 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 84%
I0326 11:40:35.065629 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 85%
I0326 11:40:36.873239 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 86%
I0326 11:40:38.651257 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 87%
I0326 11:40:40.441565 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 88%
I0326 11:40:42.233516 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 89%
I0326 11:40:44.009383 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 90%
I0326 11:40:45.797414 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 91%
I0326 11:40:47.598367 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 92%
I0326 11:40:49.398880 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 93%
I0326 11:40:51.189433 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 94%
I0326 11:40:52.980044 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 95%
I0326 11:40:54.772565 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 96%
I0326 11:40:56.601140 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 97%
I0326 11:40:58.381893 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 98%
I0326 11:41:00.157136 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 99%
I0326 11:41:01.946248 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 100%
I0326 11:41:01.947647 vendor/github.com/ethereum/ethash/ethash.go:276] Done generating DAG for epoch 0, it took 3m2.555453735s
  • Determine the enode value of the present Ethereum node: enode is a URL that's used by each node within an Ethereum network to perform peer-peer handshake. The enode of an Ethereum node can be determined by the command admin.nodeInfo.enode. The [::] from the returned string must be replaced by your external ip address (If you running within LAN, please substitute this with your internal IP).
  • > admin.nodeInfo.enode
    "enode://dce52ff84d904d3a304c0977fbc01efd59c2f62ceb3ebcae16044768d15282a5b411f5774e7410da26648c55a1e9d5326ecc635fb2ad73ad5569cf9df41f1e1a@[::]:30303?discport=0"
    > 
  • Launch the second AWS instance and setup the genesis block again on the instance using geth init.
  • Launch geth console on the second AWS instance. Ensure to provide the same networkid that you had provided in the first terminal.
  • Once the geth console has started, you will need to execute the addPeer command in order to establish a handshake between the two nodes. 
> > admin.addPeer("enode://90b7cbbaee94ab6e5bc7c5e8080bf8e2dfed5047b7c19ac61ee82511bef40faf9be2066258228ce7a71ab97b508dbef3c8f50fcf31dedfd43f2f0abd7f618db9@172.129.23.46:30303?discport=0")
true
> 
  • You will now notice that a handshake has been established between both the nodes. This can be cross verified using the command admin.peers, which would list all the nodes within the network.
  • Commands such as net.peerCount will list the total number of peers within the ethereum network.
Now we have our own private blockchain network setup. We shall use this network for all our future development work. Note, you can always use the Ropsten TestNet for Dapp development, howeve,r setting up your own private blockchain provides you with more flexibility. In the next blog we shall look at a use-case for Dapp application.

Geth recently have started to provide an utility called bootnode which allows automatic discovery of all nodes within a private blockchain. However, I was unsuccessful in getting the thing working. I shall write a new blog once I have manged to get it working.

PREV                                                                                                                                         NEXT 


Popular posts from this blog

Ethereum and Blockchain - 2

Truffle for Dapp development