jackofcrypto

Posted on Dec 19, 2021Read on Mirror.xyz

Building a Blockchain

Build a real Ethereum-based PoW blockchain using the puppeth tool bundled with geth, the official Ethereum node software.

Refresher:

  • What does the "chain" in blockchain refer to?

    Answer The chain of hashes that link each block to the previous.

  • What is a digital signature?

    Answer A message that you can validate the integrity and authenticity of cryptographically.

  • What is a node?

    Answer A node is a participant in the network that maintains a full copy of the blockchain.

Creating a Genesis Block

Open a terminal window (GitBash in Windows) navigate to Ethereum tools folder to program the chain.

Generation of a genesis block using the puppeth tool bundled with the Go Ethereum (geth) tool.

  • The Go Ethereum tool is one of the three original implementations of the Ethereum protocol.
  • It is written in the Go programming language, fully open source, and licensed under the GNU LGPL v3.
  • We will use the Go Ethereum tool via the geth command-line tool.
  • geth is the official Ethereum node software used to initialize, run and manage Ethereum nodes.
  • By default, running geth will create a standard Ethereum node that will sync to the main network.
  • However, since geth comes with a handy tool called puppeth, we will create the networks!

The Genesis block is the very first block in the chain. It contains the initial rules for the network, like the consensus algorithm and pre-funded accounts.

The consensus algorithm used is Proof of Work.

  • Note that the network difficulty will be low enough that our computers can mine blocks easily.

Pre-fund accounts by pasting an address from any Ethereum wallet that you control, without the 0x prefix.

Continue with the default option for the prompt that asks, Should the precompile-addresses (0x1 .. 0xff) be pre-funded with 1 wei? by hitting enter again until you reach the Chain ID prompt

Once you enter the chain ID, hit enter, and you should show a success message and redirect to the original prompt.

Creating nodes with accounts

The first step is to export our genesis configuration into a json file.

Now, create at least two nodes to build the chain from the genesis block onward.

Be sure you are under your Ethereum tools directory and run the following command from the terminal window:

./geth account new --datadir node1
  • This is where all data belonging to the first node will reside, and the address that the mining rewards will go.

Next, create another account using a different datadir by running the following command in the terminal window:

./geth account new --datadir node2

Your directory structure should look something like this:

Node 1 and Node 2 directories

Starting the Blockchain

The chain is ready to be started. Now it's time to initialize the nodes.

Open the terminal window (Git Bash in Windows), navigate to your Ethereum tools folder and launch the first node into mining mode with the following command:

./geth --datadir node1 --mine --miner.threads 1
  • The --mine flag tells the node to mine new blocks.
  • The --minerthreads flag tells geth how many CPU threads, or "workers" to use during mining.
  • Since the difficulty is low, it can be set to 1.

Launch the second node and configure it to talk to the chain!

Scroll up in the terminal window and copy the entire enode:// address (including the last @address:port segment) of the first node located in the Started P2P Networking line.

  • We will need this address to tell the second node where to find the first node.

Launch the second node, enabling RPC, changing the default port, and passing the enodeid of the first node you copied in quotes, the command will vary from OS X to Windows.

The output of the second node should show information about Importing block segments and synchronization.

Transacting on the chain

Connect a wallet (MyCrypto, MetaMask, etc.) with the blockchain you created.

  • "Change Network" in the wallet.
  • Click on "Add Custom Node", then add the custom network information that was set in the genesis (i.e. the name of the network and the network ID).
  • The chain ID must match what you came up with earlier.
  • The URL is pointing to the default RPC port on your local machine.
  • As a reminder:
    • node1 is mining.
    • node2 is RPC enabled.

Send a transaction on the chain. You should see the transaction go from Pending to Successful

Congrats!

Twitter thread summary of this post:

https://twitter.com/jackofcrypto/status/1472323509355356161?s=20

Recommended Reading