Create you PRIVATE Ethereum blockchain.

Alain | Web3hackingLabs
4 min readSep 29, 2022

--

When you use Remix, Ganache or Hardhat to test your smart contracts, you already use a private blockchain.

But in this article we will go, of course, deeper and see how you can create your own Ethereum blockchain and interacting with it.

There are already articles on medium or in the web which shows you the procedure but most of them are outdated and/or doesn’t work.

image source: https://www.insiderintelligence.com

1. Why should you create you own private blockchain ?

There are several reasons to that:

  • You’ll have a better understanding of the Ethereum ecosystem.
  • It may be useful to have you own infrastructure and to NOT depend on others.
  • To make private tests visible only by you (and not by others on testnet)

2. So, how can i create my own blockcahain ?

The first step is to download and install an Ethereum client.

This is a software that can interact with the Ethereum blockchain and all the Ethereum nodes by following the Ethereum protocol, in most of the clients you can even run your own node. (but we won’t do it here)

There are several programs which can do the job, but we will stick with Geth here because this is the most known.

Geth stands for “go ethereum”, this is written in “go”.

You can download and install geth here: https://geth.ethereum.org/downloads/ (in this tutorial I will use the 0.10 version)

3. Create a genesis block

Once it’s done, you need to create the “genesis” block, this is the first block of a blockchain (block number 0) and specify his properties.

{
"nonce": "0x0000000000000042",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x00",
"gasLimit": "0x8000000",
"difficulty": "0x400",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {
"0xaaC5322e456d45E7b6c452038836C5631C2AeBc0": {
"balance": "1000000000000000000000000000"
}
},
"config": {"chainId": 987, "homesteadBlock": 0, "eip150Block": 0,"eip155Block": 0, "eip158Block": 0}
}

You can name it “genesis.json”, there are several fields you need to know:

  • gasLimit, this is the block gas limit. (On Ethereum this is set to 30 Millions gas). This means that in each block the sum of all consumed gas mustn't exceed 30 Millions.
  • Difficulty , For validating a block each node need to calculate a hash, this hash need to fit certain “requirements”. A difficulty of 0x400 (1024 in decimal) means that, each time a node calculates a hash, he has 1 in 1024 chances to fit the requirements and to mine the block. The higher the difficulty is, the harder is to mine a new block. (on Ethereum it was 5–10 TRILLION before the merge)
  • coinbase , this is the address where all the mined rewards will be sent.
  • alloc , you can choose the initial state by allocating ETH to certain addresses or by per-deploying smart contracts. Here I choose to give 10.000.000.000 ETH to 0xaa… at the beginning.
  • config , you can configure other aspects of the blockchain. (like the chainId, the block when the blockchain will hard fork following the EIP150, 155 or 158 standard and others)
  • externalData , you can put anything here (no longer than 32 bytes)

So, once you’re understand this file you need to type the following command.

geth --datadir /path/to/directory/ init /path/to/genesis.json
If all is OK, you will get this message.

This will write the initial state of the blockchain in the directory of your choice.

4. Accessing the Blockchain on the network

It’s nice, the blockchain was “initializated” but now it want to add this network to metamask and stark making transactions, how to do it ?

geth --networkid 987 --datadir ./node1 --port 30303 --ipcdisable --syncmode full --http --http.addr 0.0.0.0 --http.api admin,eth,miner,net,txpool,personal,web3 --allow-insecure-unlock --http.corsdomain "*" --http.vhosts "*" --mine console --miner.etherbase=0x796a435f0de3f698fe041ead0f8f6f5a50e46871

There are a lot of options you need to specify here:

  • networkid the network id you choose.
  • datadir the directory containing the initial state initialisation.
  • http.addr allows the network to be accessed by JSON-RPC (essential for using it with metamask)
  • miner.etherbase is the address which will receive the rewards for the mining.
  • http.cors enable access for a particular domain * mean all.
  • console open the geth console after the blockchain is launched.

Once you’ve executed the command, you should see the console.

5. Adding your blockchain to metamask

To use the blockchain on metamask, you follow the standard way for adding a network (like any other).

Go to settings > network > Add network (to add a network)

New RPC URL: http://127.0.0.1:8545

ChainID: 987 (in my case)

You can chose a name for the network and a “currency symbol”

6. Interact with the blockchain on the console

At this point, you can interact with the blockchain you’ve created by using the console in JavaScript.

Here is some commands you can try:

miner.start() // mine new blocksminer.stop() // stop mining blocksweb3.fromWei(web3.eth.getBalance("0x...")) // Get the balance of an address
admin.nodeInfo() //list some informations about the node
eth.blockNumber() // print the current block number

You can send transactions from your metamask network too, but don’t forget to activate the miner for validating the transaction.

You can find all the “commands” here: https://ethereum.stackexchange.com/questions/28703/full-list-of-geth-terminal-commands

--

--

Alain | Web3hackingLabs

Smart contract Auditor & Cybersecurity engineer, follow me on Twitter to get more value: https://rebrand.ly/twitter_medium