Back to all articles

How to set up Ethereum private network

Hello crypto folks!

It seems like the internet have tons of articles how to set up your own Ethereum private network but in order to do this I walked through several problems and decide to write down full articles which will include problems I faced and solutions.

Please keep in mind if you might rewrite or remove your "life" keys and you may lose your ether forever. Don't do any experiments on without backing up your real keys.

I assuming we already have get installed and working from the command line.

Fresh start:

In case if you did something before. The first step, start on a fresh note, remove or move all our past experiments, some "leftovers" can break everything.

Set up genesis file:

In order for our private network to work, we need to set up genesis file, which we will have to copy on all our nodes manually:

{"config": {
       "chainId": 873,
       "homesteadBlock": 0,
       "eip155Block": 0,
       "eip158Block": 0
   },
 "alloc"      : {},
 "coinbase"   : "0x0000000000000000000000000000000000000000",
 "difficulty" : "0xfe000",
 "extraData"  : "",
 "gasLimit"   : "0x2fefd8",
 "nonce"      : "0x0000000000000042",
 "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
 "timestamp"  : "0x00"
}

Problems and issues I faced:

  • chainId:

    Chain id identifies the current chain and is used for replay protection. Do not use 0 here, you won’t be able to deploy your contracts. Don't ask me why.
  • difficulty:

    This value is used to control the Block generation time of a Blockchain, keeping the Block generation frequency within a target range. On the test network, we keep this value low to avoid waiting during tests since the discovery of a valid Block is required to execute a transaction on the Blockchain.Don't make it too small or too big. Ability to generate 1-2 coin per minute is totally enough.
  • alloc:

    Allows defining a list of pre-filled wallets. That's an Ethereum specific functionality to handle the "Ether pre-sale" period.
  • timestamp:

    The timestamp also allows verifying the order of block within the chain (Yellow Paper, 4.3.4. (43)).
  • gasLimit:

    A scalar value equal to the current chain-wide limit of Gas expenditure per block. High in our case to avoid being limited by this threshold during tests.

Let's save our genesis file to the ~/private_network/genesis.json file.

Initial folder creation:

Second, important, the step is to create a basic Ethereum chain based on the genesis file.

I guess, its a part which is confusing. Your nodes will run anyway and you don't get any strict error if you will miss an initial step or use different/empty/wrong directory in the next step.

geth init ~/private_network/genesis.json --datadir ~/private_network/ethdata

Command will take a few seconds to run and in ~/private_network/ethdata folder, you should see two more folders: geth and keysore. Make sure you can see them.

If you can see that -- it's great. If not double check your paths. It's quite possible you'll recognize an error only on the next step.

Turn on a private server on the first node:

geth --verbosity 6 --datadir="~/private_network/ethdata" --networkid 928928928 --rpc 127.0.0.1:8545 --rpcapi personal,web3,miner,eth --nodiscover 
  • --datadir:

    Must be the same with a step two.
  • --nodiscover:

    Sometimes helps other nodes to connect to the first node. Again, don’t ask me why.
  • --verbosity:

    Controls output information, must have for debugging issues.

In geth output find out a text:

RLPx listener up                         self="enode://947afec11dd0b2b531e3806b43a7c3b12202a2f765d5a2da36394d89e49fe795b[email protected][]:30303?discport=0" 

and save enode address.

Set up node on the second machine:

  • Create initial eth data:
    ~/private_network/genesis.json file
  • Set up initial eth data:
    geth init ~/private_network/genesis.json --datadir ~/private_network/ethdata
  • Run:
    geth --datadir="~/private_network/ethdata" --networkid 928928928 --rpc 127.0.0.1:8545 --rpcapi personal,web3,miner,eth --nodiscover --bootnodes enode://947afec11dd0b2b531e3806b43a7c3b12202a2f765d5a2da36394d89e49fe795b[email protected]<first_node_ip>:30303 
    You have to use enode address from the first node.You have to set up <first_node_ip> and make sure the first node is accessible from that IP.

Testing and mining process:

After our set up and run geth on the second machine, let's try to connect to the geth.ipc and create new accounts for mining.

Let's do on the both nodes:

geth attach ~/private_network/ethdata/geth.ipc

Let's check we are really using new network:

> personal.listAccounts
[]

Should return empty array, let's create new address then:

> personal.newAccount(“mypassword”)
“0xXXXYYYZZZZ”

and do some mining:

miner.start()

Check your balance minute after:

eth.getBalance(“0x123123”)

Common problems with Ethereum private networks:

DAG generation takes too much time:

There is nothing really you can do, I can take from 3 minutes up to 30. You just need to wait.

Balance is always 0, node do not mine, miner does not work:

Check out our geth output, look for the warnings or error messages.

There few possible problems, I'll start with the most common ones:

  • No peers added to the second node:

    Open geth console, and type:

    geth attach ~/private_network/ethdata/geth.ipc
    admin.addPeer("enode://[email protected]_IP:PORT");
    miner.start()

    If that does not help:

  • Make sure you are using init folder from geth init genesis.json command:

    As I told before, it's confusing, you can run geth with bootnodes without using right genesis.json start dir and it will run.

  • Network issues:

    First node not accessible or network packages blocked by a firewall. Try to connect to your node with a telnet:

    telnet <ip> <port>

    and see if you can connect to the node. The node will terminate the connection after 5-10 sec automatically. That's normal.

If you want to use mist or anything else with your private network -- run:

mist --rpc ~/private_network/geth.ipc 

Let me know if you have any other problems in comments, and let's try to figure it out together.

PREVIOUS ARTICLENEXT ARTICLE