How To: Create Your Own Private Ethereum Blockchain

Why we use Private Ethereum Blockchain

Ethereum software enables a user to set up a “private” or “testnet” Ethereum chain that is separate from the main Ethereum chain. This is useful for testing distributed apps built on Ethereum without having to expose your apps or trials to the real Ethereum network using real Ether.

Prerequisites

Mac

You need to have Geth installed. The easiest way to do this is through homebrew.

  1. Open Terminal and install homebrew

2. Now install geth

Windows

  1. Download Geth and install

Step :

  1. create a folder
  2. create custom genesis file
  3. create custom data directory
  4. set custom networkID (ChainID)

Create Genesis File

The Genesis block is the start block of the Blockchain — the first block, block 0, and the only block that does not point to a predecessor block. the genesis block is hard coded into clients, but in Ethereum it can be whatever you like.

The Genesis file is a JSON file that defines the characteristics of that initial block and subsequently the rest of the blockchain.

Image for post
Image for post
  1. Create a directory to hold your network files

2. Create your genesis file

3. Open your genesis file and paste the following

config

  • chainId — this is your chain’s identifier, and is used in replay protection.
  • homesteadBlock, eip155Block, eip158Block, byzantiumBlock — these relate to chain forking and versioning, so in our case lets leave them 0 since we’re starting a new blockchain.

difficulty

This dictates how difficult it is to mine a block. Setting this value low (~10–10000) is helpful in a private blockchain as it lets you mine blocks quickly, which equals fast transactions, and plenty of ETH to test with.

gasLimit

This is the the total amount of gas that can be used in each block. With such a low mining difficulty, blocks will be moving pretty quick, but you should still set this value pretty high to avoid hitting the limit and slowing down your network.

alloc

Here you can allocate ETH to specific addresses. This won’t create the account for you, so make sure its an account you already have control of. You will need to add the account to your private chain in order to use it, and to do that you need access to the keystore/utc file.

Nonce

A scalar value equal to the number of transactions sent by the sender.

mixhash

mixhash is an intermediary calculation to finding the nonce that is not as costly to determine.

coinbase

The ether rewards gained from “mining” the genesis block go to the 160-bit coinbaseaddress.

timestamp

The output of the Unix time() function when the block was created

parentHash

The Keccak 256-bit hash of the previous block’s header. This is meaningless in the genesis block, since block 0 has no parent.

extraData

An optional free, but max. 32 byte long space to conserve smart things for ethernity on the Blockchain.

Initial the genesis block

  • Init our blockchain with the settings in the genesis file and define a folder for storing chain data.
  • datadir : Data directory for the databases and keystore
  • init: initialize a new genesis block

Data Directory

•Everything geth persists gets written inside its data directory (except for the PoW Ethash DAG, see note below). The default data directory locations are platform specific:

  • Mac: ~/Library/Ethereum
  • Linux: ~/.ethereum
  • Windows: %APPDATA%\Ethereum
Image for post
Image for post

Output should look like this:

Image for post
Image for post

Start your Ethereum peer node.

Networkid helps ensure the privacy of your network. You can use any number here (where we used “ 123456”), but other peers joining your network must use the same one.

windows

Mac

Output should look like this:

Image for post
Image for post

Open a new terminal

Command Line Options

Image for post
Image for post

Management APIs

Beside the official DApp APIs interface go-ethereum has support for additional management APIs. Similar to the DApp APIs, these are also provided using JSON-RPC and follow exactly the same conventions.

Geth provides the following extra management API namespaces:

  • admin: Geth node management
  • debug: Geth node debugging
  • miner: Miner and DAG management
  • personal: Account management
  • txpool: Transaction pool inspection

For more detail here

Open a new terminal ,type

Image for post
Image for post

Output should look like this:

Image for post
Image for post

Node Information

The nodeInfo administrative property can be queried for all the information known about the running Geth node at the networking granularity.

Image for post
Image for post

Creating a new account

Creates a new account and prints the address. On the console, use:

Enter your password and then it will display account address. In my case, I set password to “123456”

Image for post
Image for post

The account is saved in encrypted format. You must remember this passphrase to unlock your account in the future.

Second way to create a new account

Image for post
Image for post

Check account

Check accounts

Image for post
Image for post

Accounts is an array so you can search account by index.

Image for post
Image for post

Balance

Check balance of account

Image for post
Image for post

Check balance by using web3

Mining

1. Set Default Account

  • Check your default account, type
    > eth.coinbase
  • If this address is the same as the one from step 4, skip the rest of step 5.
  • To set your default account, type
    > miner.setEtherbase(web3.eth.accounts[0])

2. Start mining

  • Check your balance with
    > eth.getBalance(eth.coinbase)
  • Run
    > miner.start()
  • Look at your other terminal window, you should see some mining action in the logs. Check your balance again and it should be higher.
  • To end mining, type
    > miner.stop()
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Transfer

  1. Check your balance

Or

2. Transfer Ether, type:

Or

But you should get error like this.

Image for post
Image for post

Actually , User have to unlockaccount first before transfer.

3. Unlockaccount

Image for post
Image for post

https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_unlockaccount

4. Transfer Ether again, type:

Transaction Pool

  1. shows transaction pool
Image for post
Image for post

2. number of pending txs

3. print all pending txs

You will find that Account[1] did not receive Ether from Account[0]. This is because no miner create a new block and verify your transaction. To process your transaction, you have to execute “miner.start(1)” again.

The whole process is :

Image for post
Image for post

Add Other Peers

Add more nodes to your private Ethereum network.

1. Start another peer

  • On your same machine instantiate a new datadir:
  • Launch the 2nd peer on a different port:

folder structure

Image for post
Image for post

2. Join the 2nd peer

  • In the geth JavaScript console of your 2nd peer, type:

Output will look like this:

  • In the geth JavaScript console of your new 1st peer, type:
  • Make sure you replace “enode://…@” above with the output from admin.nodeInfo.enode which is specific to you. As shown above, the “[::]” is replaced with “127.0.0.1:30304” which is the IP:Port of the 1st peer.

3. Verify your nodes are now communicating

  • In the geth JavaScript console of your new 2nd peer, type:
  • Output should show that peer 1 is connected to127.0.0.1:30304
Image for post
Image for post

Next chapter : How to build Ethereum Dashboard and to monitor your Ethereum Network Status

I do this because I love it, but if you want me to buy me a cup of coffee I won’t say no :O )

donation :

XEM : NCWZSUF4FPXJY3L3Y7657QNVBIUZ5D54F4TNJ64S

Ether : 0xf2d15dEAf62b8c4AFC0343006579E8E662c120D9

Bitcoin : 332UiyAfSXyvhqCYgDgBkNLFSf25ccNV9i

  • *Do CLAP, COMMENT and SHARE! I also welcome any business opportunities that arises**

Connect:

LinkedIn

Telegram

Facebook

Written by

撰寫任何事情,O型水瓶混魔羯,咖啡愛好者,Full stack/blockchain developer,Founder of Blockchain&Dapps meetup and DeFi-Decentralized-Finance-SG meetup,Udemy teacher。

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store