Proj 11: Making a Private Ethereum Blockchain (15 pts.)

What You Need

Purpose

To learn how Ethereum works and set up your own local Ethereum testnet.

I am following this guide:

How to Build a Private Ethereum Blockchain

Ethereum Concepts (Simplified)

For much greater detail, see the Ethereum White Paper.

Exploring the Public Ethereum Blockchain

In a Web browser, go to

https://etherscan.io/

Live data about Ethereum appears, as shown below. When I did it (6-12-17), Ethers were worth $390 each, with a total market capitalization around $36 B. This makes Ethereum the second largest cryptocurrency--only Bitcoin is larger, with a market cap of about $47 B.

In the etherscan.io page, in the top center, hover the mouse over BLOCKCHAIN and click "View Pending Txns".

A list of pending transactions appears, as shown below. When I did it in July, 2016, there were typically 9 pending transactions, and none more than 30 seconds old.

As you can see, Ethereum is far more active now, with hundreds of pending transactions

In the etherscan.io page, in the top right, click CHARTS.

On the next page, scroll down and click "Block Count and Rewards Chart".

A chart appears, as shown below. As you can see, Ethereum has approximately 6000 blocks per day -- approximately one every 30 seconds.

Installing geth from Source

On your Linux machine, in a Terminal window, execute this command. Enter your password when you are prompted to.
sudo apt-get update
Execute this command to install C and Go compilers.
sudo apt-get install -y build-essential
wget https://storage.googleapis.com/golang/go1.12.1.linux-amd64.tar.gz
tar -xvf go1.12.1.linux-amd64.tar.gz
sudo mv go /usr/local
Execute this command to automatically set some environment variables on each login.
nano ~/.profile
At the end of this file, add these three lines, as shown below.
export GOROOT=/usr/local/go
export GOPATH=$HOME
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

Save the file with Ctrl+X, Y, Enter.

Execute this command to actually set the variables:

source ~/.profile
Execute these commands to clone the Ethereum source code and compile it.
cd 
git clone https://github.com/ethereum/go-ethereum
cd go-ethereum
make geth

Test geth Installation

Execute this command:
~/go-ethereum/build/bin/geth version
You should see information about geth, as shown below.

Create an Account

The mining rewards need to go into an account, and this account is also needed to pay gas fees.

Execute this command:

~/go-ethereum/build/bin/geth account new
You need to enter a passphrase twice. For this project, I recommend using P@ssw0rd

Obviously, if you get real Ether worth real money, you need to use a more secure passphrase.

You see an Address, as shown below. This is your account's public key.

Creating JSON File for a Genesis Block

Execute these commands to make a file which will be used to create your genesis block:
cd
mkdir eth-test
cd eth-test
nano genesis.json
Paste in this code. Replace the address in the line beginning with "alloc" with your own Address.
{
    "config": {
        "chainId": 13,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "difficulty": "200000",
    "gasLimit": "2100000",
    "alloc": {
        "554a6f4c0aac072ab5a5b90d1a815dc0cc877742": { "balance": "100000000" }
    }
}

Save the file with Ctrl+X, Y, Enter.

Initialize your Blockchain

Execute this command to create the blockchain:
~/go-ethereum/build/bin/geth init genesis.json   
You see messages, ending with "Successfully wrote genesis state", as shown below.

Exploring the Blockchain

Execute these commands to move to your home directory and list all files and folders, including hidden ones.
cd
ls -al  
You see a hidden folder named .ethereum, as shown below.

Execute these commands to move into the .ethereum directory and see what's there.

cd .ethereum
ls -al  
Two folders are here: geth and keystore.

Execute these commands to move into the keystore directory and see what's there.

cd keystore
ls -al  
There's only one file here, as shown below.

cat that file to see its contents. This is where your account is stored. The "address" value matches the value you saw earlier when you created this account. The private key is here, encrypted with your passphrase.

Execute these commands to move into the geth directory and see what's there.

cd ../geth
ls -al  
These folders contain blockchain data. These folders will grow when blocks are mined and the blockchain gets longer.

Starting Mining (THIS MAY TAKE HOURS)

Your blockchain isn't operating yet because it has no miners.

To start mining, execute this command:

~/go-ethereum/build/bin/geth --mine --nodiscover --maxpeers 0 --networkid 13 -rpc -rpccorsdomain "*" --miner.threads 4 
Here's a description of those command-line arguments, from How to Build a Private Ethereum Blockchain .

Geth starts, as shown below. It begins creating a "DAG". The DAG is a 1 GB file used for "proof of work", as explained here.

THIS CAN TAKE HOURS TO COMPLETE

First it counts up from 0% to 100% fairly quickly, as shown below.

Wait for the DAG to be built. When it's done, your server will start mining blocks, as shown below.

This is now a functioning blockchain, mining and listening for RPC commands.

Leave this window open.

Saving a Screen Image

Make sure you can see a "mined potential block" message, as shown above.

Capture a full-screen image.

YOU MUST SUBMIT A FULL-SCREEN IMAGE FOR FULL CREDIT!

Save the image with the filename "YOUR NAME Proj 11a", replacing "YOUR NAME" with your real name.

Sending an RPC Command

Open another Terminal window.

In the new window, execute this command:

curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67}' http://localhost:8545
This shows you the version of geth you are using, as shown below.

Viewing the Current Block Number

Execute this command:
curl  -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83}' http://localhost:8545
The "result" shows the current block number on the blockchain.

Wait 15-30 seconds and repeat the command.

You should see the block number increasing, as shown below. In my case, the block number increased from 0x8d to 0x8f.

Geth will automatically adjust the mining difficulty to make the mining rate stay near 15 seconds per block.

Viewing a Recent Block

Execute this command, replacing the block number 0x8f with the largest block number you found with the previous command:
curl  -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x8f", true],"id":1}' http://localhost:8545
The reply shows many details about that block, including the "difficulty", as shown below. (Your difficulty will probably be different--it is automatically adjusted.)

Viewing Block 1

Execute this command:
curl  -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x1", true],"id":1}' http://localhost:8545
The "difficulty" of this block was probably different from the more recent block, as shown below.

Other JSON-RPC Commands

The complete list of JSON-RPC commands are here:

JSON RPC API Wiki

Checking your Balance

Execute this command to how much Ether you have. Change the Address to your own Address, preceded by "0x", as highlighted in the image below.
curl  -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x554a6f4c0aac072ab5a5b90d1a815dc0cc877742", "latest"],"id":1}' http://localhost:8545
Your balance appears, as a large hexadecimal value, as outlined in green in the image below.

Saving a Screen Image

Make sure you can see a non-zero balance, as shown above.

Capture a full-screen image.

YOU MUST SUBMIT A FULL-SCREEN IMAGE FOR FULL CREDIT!

Save the image with the filename "YOUR NAME Proj 11b", replacing "YOUR NAME" with your real name.

Wei and Ether

How much is that? It's in wei, the smallest unit of ether.

1 ether = 10^18 wei.

In the unused Terminal window, execute these commands, inserting your balance in the second line:

python
0x456391824ae9e100 / 10**18
exit()
Your balance appears, as shown below. Mine was 365 Ether. Yours may be different--you earn 5 Ether for each block you mine.

Stopping Mining

Click in the Terminal window that is mining and press Ctrl+C to stop the process. Mining the private blockchain while it is not in use just fills the hard disk for no purpose.

Turning in your Project

Email the images to cnit.141@gmail.com with the subject line: Proj 11 from YOUR NAME.

Sources

How to setup a local test Ethereum Blockchain
ethereum/go-ethereum
A 101 Noob Intro to Programming Smart Contracts on Ethereum
Ethereum White Paper
Create a Hello World Contract in ethereum
Contract Tutorial
Okay, So I can run "geth --mine" and it appears that I am mining, how do I load up the wallet to check?
Why is my Greeter contract not mined on --testnet?
"Transaction w/ invalid nonce" error from eth_call RPC on develop branch - Issue #2771 - ethereum/go-ethereum - GitHub
Ethereum Frontier Guide
How to Build a Private Ethereum Blockchain


Posted 7-9-16 by Sam Bowne
Revised 7-12-16 12:26 PM
Revised extensively 6-12-17
Mining difficulty changed 6-13-17
Name of genesis JSON file corrected 10-16-17
Warning about hours added back in 10-24-17
Go version, curl content-type, and --miner.threads added 3-19-19