How to Set Up an Ethereum Private Chain

·

In Ethereum development, setting up a private chain is often essential for testing and experimentation. This guide walks you through the process on a Mac.

Prerequisites

Before proceeding, ensure you have a basic understanding of Ethereum. If you're new, we recommend reading What Is Ethereum?.


Installing the Go-Ethereum Client (Geth)

Go-Ethereum (Geth) is a command-line interface that implements a full Ethereum node in Go. It supports multiple platforms, including Mac, Windows, and Linux. Geth enables core functionalities like account management, mining, transactions, and smart contract deployment.

Installation on Mac:

brew tap ethereum/ethereum
brew install ethereum

Verify Installation:

geth --help

If the command outputs help instructions, the installation succeeded.

👉 For other platforms, refer to Geth installation guide.


Setting Up the Private Chain

Step 1: Create a Genesis Block

Ethereum allows custom genesis blocks. Save the following JSON configuration as genesis.json:

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

Step 2: Initialize the Blockchain

Create a directory (e.g., data0) to store chain data, then run:

geth --datadir data0 init genesis.json

Success is confirmed by the log message: Successfully wrote genesis state.


Launching the Private Chain Node

Start the node with:

geth --datadir data0 --networkid 1108 console

This opens the Javascript Console, where you can interact with the blockchain using built-in objects like:


Key Operations in Javascript Console

1. Creating Accounts

> personal.newAccount() // Follow prompts to set a passphrase.
> eth.accounts // Lists all accounts.

2. Checking Balances

> eth.getBalance(eth.accounts[0])
> web3.fromWei(eth.getBalance(eth.accounts[0]), 'ether') // Converts to ETH.

3. Mining

> miner.start(1) // Starts mining with 1 thread.
> miner.stop() // Stops mining.

Rewards are credited to eth.coinbase (default: first account).

4. Sending Transactions

> personal.unlockAccount(eth.accounts[0]) // Unlock sender account.
> eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(10, 'ether')})

Mine a block to process the transaction:

> miner.start(1); admin.sleepBlocks(1); miner.stop();

5. Inspecting Blocks and Transactions

> eth.blockNumber // Latest block.
> eth.getBlock(66) // Block details.
> eth.getTransaction("0x...") // Transaction details.

FAQs

1. Why is my transaction not processed?

Transactions require mining. Use miner.start() to confirm pending transactions.

2. How do I change the coinbase address?

> miner.setEtherbase(eth.accounts[1])

3. What’s the difference between wei and ether?

1 ETH = 10¹⁸ wei. Use web3.fromWei() to convert.

👉 Explore advanced Ethereum development tools.


Conclusion

This guide covered setting up an Ethereum private chain, account management, mining, and transactions. For deeper dives into Solidity or DApp development, check out these resources:

For more blockchain insights, follow In-Depth Blockchain Blog.