Ethereum Coding Part 2: Create your own local private Ethereum Network

In part 1, we installed the Geth client. For part 2, I am going to create a private local Ethereum network on my local machine.

Step 1: Environment Setup (optional)

1. Go to root
cd ~
2. create a ethereum directory
mkdir ethereum
3. Set up your environment variable (in your case choose the proper user name).  This will allow you to access ethereum folder directly by typing 'cd $ethereum_home'
echo 'export ethereum_home=/Users/alexanderlee/ethereum' >>~/.bash_profile

Step 2: Create the Genesis block file

If you don’t know what a genesis block is please refer to

cd into your ethereum_home file. I personally use atom but open up any IE in that directory and create a file called genesis.json.

Copy the following in your file. (this is straight from the documentation)

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

I won’t go every detail but what’s good to notice is that the parentHash value is zero indicating the first block. You can also add a difficulty level which dictates how hard it is to mine. If you like to deep dive into these configurations you can refer to this link below

Step 3: Initiate a new blockchain

We are going initiate a new blockchain based on the genesis.json file.

geth --datadir "$ethereum_home/reddit1" init "$ethereum_home/genesis.json"

Instead of using the production ethereum network, we are creating a private one. Inside of our ethereum node, we will create a folder called “reddit1”. Inside this folder, is where this particular node will store its copy of the blockchain. This blockchain currently only has one node at the moment! Everything related to this node will be dictated by genesis file. (difficulty, gasLimit, etc)

Step 4: Start Geth via console

geth --datadir "$ethereum_home/reddit1" console 2>console.log

If all goes well, it should show something like this.

Step 5: Admin API

At this point you have the freedom to explore around the API. I’ll go over the one’s I find very useful

admin.nodeInfo
{
enode: "enode://d9bd81a46389cc0dad8613848ec2ee45b0783e323d082328419180283cd62556a27b780a4e34ed25afbc603d2d4040a4a4659766c6076b1950054b7c571e07c6@67.244.27.80:30303",
id: "d9bd81a46389cc0dad8613848ec2ee45b0783e323d082328419180283cd62556a27b780a4e34ed25afbc603d2d4040a4a4659766c6076b1950054b7c571e07c6",
ip: "67.244.27.80",
listenAddr: "[::]:30303",
name: "Geth/v1.6.1-stable-021c3c28/darwin-amd64/go1.8.1",
ports: {
discovery: 30303,
listener: 30303
},
protocols: {
eth: {
difficulty: 151004988445749800,
genesis: "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
head: "0xee333618cc2720397e0b1e1bebfec65a940d0908c1a102397a2fdbd846ce4bfe",
network: 1
}
}
}

a. The default port is 30303 via discovery key.

b. enode will be used a lot throughout the tutorial. This defines the particular node

Step 6: Personal API

a. create a personal account

personal.newAccount()
Passphrase:
Repeat passphrase
"0xb0b15a62a2b8e2c548caa0b4d9736b7445484173"

A new account is needed to send and receive ether // mining. Remember, do not forget your passphrase. If you do you’re screwed! No password recovery option AT ALL.

b. list accounts

personal.listAccounts
["0xb0b15a62a2b8e2c548caa0b4d9736b7445484173"]

will list all accounts

Step 7: eth API

Deals with any operations related to the blockchain

eth.blockNumber
0

The only block on the chain is the genesis block. Since no mining has been done, the block height = 0. When you start mining the block height will increase.

Conclusion:

So we got a very basic setup done. We created one node, created a personal account, and experimented with different geth API’s.

For next steps, we’re going to start learning how to attach a terminal to same node, creating a second block, and start mining.

**Update**

Step 3:

https://medium.com/@kkomaz/ethereum-coding-part-3-time-to-mine-e4be78d4be39