How to install Testnet for the Ethereum blockchain client

Anurag Srivastava
Zeonlab & Blockchain Semantics Blog
8 min readDec 10, 2017

Blockchain Semantics has had a lot of people reach out and tell us they have trouble installing testnet for the Ethereum blockchain. Needless to say, this is the first step in being able to develop smart contracts on Ethereum.

So, here is a set-by-step ready reckoner for getting your testnet going!

Ethereum Client:

Ethereum client provides an interface to create transactions, mine blocks and various other blockchain interactions. Currently three client implementations are available for Ethereum.

  1. Eth — C++ client
  2. Geth — Go lang client
  3. PyEthApp — Python client for Ethereum

Of these three, this article covers Geth. First let’s have a look at the installation instructions for Geth.

Video tutorial for setting-up Testnet for Ethereum Blockchain

Installing Geth:

Geth is available for Linux, Windows and OS X. Let’s see how to install it in various operating systems.

1. Installation Instructions for Ubuntu

  • Installing from PPA
  • To install GETH on ubuntu, open Terminal using Ctrl + Alt + T and type:

sudo apt-get install software-properties-common

Now hit Enter. The program will ask you to type the password. Enter your password and press Enter. You should see a result similar to the figure shown. Next type the following command at the prompt and type Enter.

sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

When the program asks whether you’d like to allow the installation to take some hard drive space, type Y(yes) and press Enter. After the installation is finished, you can check geth by typing the following at the prompt:

geth version

2. Installation Instructions for Windows

The download page provides an installer as well as a zip file.

Download exe file from the page.

Once the file has finished downloading, open and install geth.exe

3. Installation Instructions for Mac

  • Installation from source
  • GETH installation for MAC requires GO compiler to be installed. If you don’t have GO compiler, install it first using:

brew install go

Then clone the geth repository from github by running the following command at the terminal:

git clone https://github.com/ethereum/go-ethereum

Once the repository has cloned, change the working directory.:

cd go-ethereum

Finally, build the Geth program using:

make geth

Using Go-Ethereum to Setup your Own Custom Ethereum Blockchain:

Go-Ethereum enables a user to set up a “testnet” Ethereum chain that is separate from the main Ethereum chain. This is useful for testing distributed apps without having to expose your trials to the real Ethereum network which costs Ether. On your custom Ethereum chain, you can either pre-generate or mine your own Ether; so it is a much more cost effective way of trying out Ethereum. Having discussed about the advantages of custom ethereum chain, let’s look into the details of how to create a testnet chain.

The first step in creating your own testnet chain is to setup a genesis file.

1. The Genesis File:

The Genesis file is used to define the initial parameters of the blockchain for eg., chain configuration, level of difficulty to mine blocks etc. The consensus algorithm of Ethereum ensures that a node gets connected to your testnet chain only if they have the same genesis file.

Let us create a mygenesis.json file with the parameters shown. It is recommended that you make a random nonce for your custom Ethereum network to avoid the chance of someone connecting to your blockchain.

{

“config”:{

},

“mixhash”: “0x00000000000000000000000000000000000000647572616c65787365646c6578”,

“nonce”: “0x00000000”,

“difficulty”: “0x2”,

“coinbase”: “0x0000000000000000000000000000000000000000”,

“timestamp”: “0x00”,

“parentHash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,

“extraData”: “0x”,

“gasLimit”: “9223372036854775807”,

“alloc”: {

}

}

Let us now discuss the meaning for parameters in genesis file:

  • config — Specifies the configuration parameters for the network
  • mixhash — It is a 256 bit hash.
  • nonce — It is a 64-bit hash, which upon combination with mixhash allows to verify that the mathematical condition specified in Proof-of-Work (PoW) algorithm is satisfied.
  • difficulty — the difficulty level applied during the nonce discovering of this block. The higher the difficulty, the more calculations a Miner must perform to discover a valid block. On the test network, we keep this value low so that blocks get mined quickly.
  • coinbase — It is a 160-bit address. Upon successful mining of each block, mining reward would be transferred to this address. Rewards transferred are a sum of the block mining reward and the transaction fee.
  • timestamp — specifies time at the inception of genesis file. By setting the value to 0, geth automatically sets timestamp to the current time.
  • parentHash — It is the keccak 256-bit hash of the entire parent block header. For a genesis block, it would be 0 since a Genesis block is the first block and so doesn’t have any parent block.
  • gaslimit — It defines the maximum amount of gas expenditure allowed per block. For example, let’s say we have 5 transactions where each transaction has a gas limit of 10, 20, 30, 40, 50 and 120. If the block gas limit is 100, then only the first four transactions can fit in the block. If you try to include the last transaction, it will be rejected by network and geth throws error “that transaction gas exceeds block gas limit”. It cannot be included at all unless the block gas limit is increased to 120 or more. Hence it is important to set the block gas limit value high.
  • alloc — alloc parameter allows to define a list of accounts pre-filled with ether.

Note: We’ve lowered the difficulty level on testnet, so that mining of block goes faster.

  • The next thing we need to know is how to setup a genesis file. Open terminal and type:

geth — datadir <path to an empty folder>(e.g., /home/vishy/testNet/) init mygenesis.json

  • Here datadir parameter specifies the path of the directory which will store your chain data.

Note: We’re using a custom datadir flag to separate the data for the testnet blockchain from the real one.

Once you have setup your mygenesis.json file, type the following command at the terminal to start your testnet chain.

geth — datadir <folder specified in earlier command> — networkid 12345 — rpc — rpcaddr “localhost” — rpcport 8545 — rpccorsdomain “*” — rpcapi “eth,net,web3,personal,admin,mine” — nodiscover — maxpeers 0 console

This command opens a geth console session, which can be used for creating accounts, carrying transactions and any other blockchain interactions.

In the above command:

  • datadir specifies path of the directory which will store your chain data. This should be the same folder as specified in the earlier command.
  • networkid — Can be any ID other than 1; Id 1 is reserved for frontier(Main) network.
  • rpc — enables RPC interface on your node.
  • rpcapi — dictates what APIs that are allowed to be accessed over RPC. This will give everyone access to the API who can access this interface. By default, Geth enables the web3 interface over RPC.
  • rpccorsdomain — dictates what URLs can connect to your node in order to perform RPC client tasks. Specifying ‘*’ will allow any url to connect to your interface.
  • nodiscover — This is to make sure that your node is not discoverable by people who do not manually add you. Otherwise, there is a chance that your node may be inadvertently added to a stranger’s node if they have the same genesis file and network id.
  • maxpeers 0 prevents anyone else connecting to your test chain. Alternatively, you can adjust this number if you know exactly how many peers you want connecting to your custom Ethereum chain.

Pre seeding Accounts with local ether:

In order to carry out transactions on testnet chain, ether is required. Therefore you need to pre-allocate Ether to your account.

  1. First create a new Ethereum account in your testnet chain. To do this, open geth console and type:
  2. personal.newAccount()

This will request for a passphrase. Enter the passphrase and keep it safe. This passphrase would be required each time you want to carry out transaction.

  1. Once you’ve generated your account, quit geth console with ctrl+c. Now remove every folder except keystore from your datadir. Open terminal and type:
  2. cd <datadir specified earlier>
    rm -rf `ls | grep -v keystore
  3. Copy your new account address
  4. Add the following command to your mygenesis.json file:

“alloc”:
{
“<your account address e.g. 0x1fb891f92eb557f4d688463d0d7c560552263b5a>”:
{ “balance”: “20000000000000000000” }
}

5. Save your genesis file and re-run your testnet chain command. Let’s check the balance of the address once geth is fully loaded. Type the following in your geth console.

var primary = “0x2…021516”;

balance = web3.fromWei(eth.getBalance(primary), “ether”);

This should return you 20 Ether in your account. The reason we had to put such a large number in the alloc section of your genesis file is because the “balance” field takes a number in wei which is the smallest sub-unit of Ether. Recall that 1 Ether = ¹⁰¹⁸ wei.

Using the testnet chain:

For all the transactions to get executed and included in the Blockchain, mining session needs to be enabled. Start a miner instance for your custom testnet chain. Open terminal and type the following command:

geth — data dir <path to your testnet directory> — networkid 12345 — rpc — rpcaddr “localhost” — rpcport 8545 — rpccorsdomain “*” — rpcapi “eth,net,web3,personal,admin,mine” — mine — minerthreads 1 — nodiscover — maxpeers 0 miner

  • datadir specifies path of the directory which will store your chain data. This should be the same folder as specified in the earlier command.
  • networkid — should be the same network id as specified earlier.
  • rpcapi “mine” — Enables running miner commands in Javascript using ethereum webapi like web3.js or ethereum JS
  • mine — Enables mining
  • minerthreads — Number of CPU threads to be used for mining. “Successfully sealed new block”.

Troubleshooting tip: If your console session gets stuck saying “Commit new mining work”, clear all the files from /home/usr/.ethash directory and restart the miner session.

You should see the mining process going on with the logs displaying the message “Commit new mining work”. In order to carry out transactions, you need to attach another geth session.

Open another console/terminal and type the ‘geth attach’ command:

cd <your datadir>

geth — datadir <path to test network directory> — networkid 12345 attach ipc:geth.ipc

  • datadir — path of the directory which will store your chain data. This should be the same folder as specified in the earlier command.
  • networkid — the same network id as specified earlier.
  • geth attach — will connect your second console to the geth instance on your first console.

Now you can carry all your transactions and run commands from this session.

Each time you want to carry out a transaction, make sure your account is unlocked. If not, type the following in geth console.

personal.unlockAccount(‘<Your account address>’)

Or You may unlock your account for the entire geth session using:

geth — data dir <path to your testnet directory> — networkid 12345 — rpc — rpcaddr “localhost” — rpcport 8545 — rpccorsdomain “*” — rpcapi “eth,net,web3,personal,admin,mine” — mine — minerthreads 1 — unlock 0 — nodiscover — maxpeers 0 miner

  • datadir specifies path of the directory which will store your chain data. This should be the same folder as specified in the earlier command.
  • networkid — should be the same network id as specified earlier

This should be it for setting-up the testnet correctly. If you are still facing troubles, just watch the videos and repeat the steps more carefully. And if you are still stuck, write to us at hello@blockchainsemantics.com .

Useful Links:

--

--