C 340: Making a Private Ethereum Blockchain (10 pts.)

What You Need

Purpose

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

Installing geth

On your Linux machine, in a Terminal window, execute these commands, one at a time. Enter your password when you are prompted to, and approve the installation when you are prompted to.
wget https://gethstore.blob.core.windows.net/builds/geth-linux-amd64-1.10.3-991384a7.tar.gz
tar xvf geth-linux-amd64-1.10.3-991384a7.tar.gz 
sudo mv geth-linux-amd64-1.10.3-991384a7/geth /usr/local/bin
geth -h
You see the geth help page, as shown below.

Creating an Account

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

Execute this command:

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 a Public address, as shown below. This is your account's public key.

Creating a Configuration File

Execute this command to make a file which will be used to create the initial block of your blockchain:
nano init.json
Paste in this code. Replace the address in the line beginning with "alloc" with your own Address.
{
  "config": {
    "chainId": 10,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "ethash": {}
  },
  "nonce": "0x0",
  "timestamp": "0x5e4a53b2",
  "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "gasLimit": "0x47b760",
  "difficulty": "0x80000",
  "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "alloc": {
    "0x8535fe94e84541626e1B3cB95cEdf4E2d1FAdCDC": {
      "balance": "0x2000000000000000000"
    }
  },
  "number": "0x0",
  "gasUsed": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}


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

Initializing your Blockchain

Execute this command:
geth init init.json
The program runs successfully, ending with "Successfully wrote genesis state", as shown below.

Starting Mining (THIS MAY TAKE HOURS)

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

To start mining, execute this command:

geth --mine --nodiscover --maxpeers 0 --networkid 10 -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 "Generating DAG". The DAG is a 1 GB file used for "proof of work", as explained here.

After a few minutes, it starts mining blocks, as shown below.

Leave this window open.

Viewing Accounts

Open a second Terminal window.

Execute these commands to see the accounts in your blockchain.

sudo apt update
sudo apt install curl -y
curl -H "Content-Type: application/json" -X POST --data \
'{"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":1}' \
http://localhost:8545
There's only one account: the one you created previously, as shown below.

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":["0x8535fe94e84541626e1b3cb95cedf4e2d1fadcdc", "latest"],"id":1}' \
http://localhost:8545
Your balance appears, as a large hexadecimal value.

After a few seconds, repeat the command again. Your balance is increasing, as shown below, as you earn rewards for mining blocks.

C 340.1: eth_mining (10 pts)

Execute this command:

curl  -H "Content-Type: application/json" -X POST \
--data '{"jsonrpc":"2.0","method":"eth_mining","params":[],"id":1}' \
http://localhost:8545
The flag is covered by a green rectangle in the image below.

Other JSON-RPC Commands

The complete list of JSON-RPC commands are here:

JSON RPC API

Sources

Ethereum: Setting Up A Private Blockchain
How to Build a Private Ethereum Blockchain


Posted 5-12-2021 by Sam Bowne