Brad Lucas

Programming, Clojure and other interests
August 6, 2017

Creating a private Ethereum testnet

Working against a local private testnet is very useful when you are developing your own smart contracts for Ethereum. It is faster and you don't have to worry about ether and having enough for gas.


One of the simplest methods to run a local testnet is to install testrpc. When done just run the command testrpcs and you'll have a local node with accounts running for you to excercise your code against.

Your own network

At some point you'll want to move up a level and create your own local network. You might simply be curious as to how to do it or you may want to customize things a bit or you need to start a network with multiple machines for testing more real life scenarios.

To do this isn't all that difficult. A good starting point is the Homestead documentation. Here is a link to the section for details using Geth.

Note: The example genesis file as of today has an issue with the latest Geth (1.6) so feel free to use this example file which I've used successfully.

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

The differences are in the extraData where it needs a value with another 0 and the addition of a config section.


First off you'll want your network data to have it's own directory otherwise you'll end up with data in your main Ethereum directories. Let's create one called $HOME/work/ethereum-private for our example here.

In that directory put the example genesis file above and for our example call it homestead.json.


With our directory and genesis block in place we can initialize our chain. This is the done with the init command for geth.

geth --datadir $HOME/work/ethereum-private --nodiscover --maxpeers 0 --rpc --rpcapi "db,eth,net,web3" --rpcport "8545" --rpccorsdomain  "http://localhost:3000" --port "30303" --identity "Brad-Ethereum-TestNet" --networkid 9999 init $HOME/work/ethereum-private/homestead.json

Some points to note about this long command. The identity can be what every you want. It is a label. The networkid is important later when you want to have multiple nodes on your network. It can be any number but stay away from the low numbers starting at 1 because they are used by others.


With the chain initialized you can start your node. Here is the command to do that.

geth --datadir $HOME/work/ethereum-private --nodiscover --maxpeers 0 --rpc --rpcapi "db,eth,net,web3" --rpcport "8545" --rpccorsdomain  "http://localhost:3000" --port "30303" --identity "Brad-Ethereum-TestNet" --networkid 9999 console

The most important part to point out here is the datadir value. It needs to point to your private chain data directory as it did for the init command.


With your node running you'll need to create your account and get it some ether. To create the account run the following command in your console and note your new account number.


To obtain ether you can do two things. Either mine for it or reset your chain and use the alloc section in the genesis block to pre-allocate eth to your account.

To mine you mimply continue in your console with the following commands

miner.setEtherbase(<your account number>)

To pre-allocate stop your node from exiting your console. Edit your genesis file from above and update the alloc section as follows.

  "alloc": {  
    "<your account number": {
      "balance": "10000000000000000000"

Then go int your private chain's datadirectory and delete the geth directory and the history file. Leave the keystore diretory.

Now repeat the init command exactly as above and then start your node.

You'll find that your account balance is 10 eth.

To verify, here is the comand:

web3.fromWei(eth.getBalance(eth.accounts[0]), "ether");
Tags: ethereum