Genesis block
Now we will deal with the crucial part: the genesis block.
A genesis block is the first block in a blockchain. When a node boots, it initializes its copy of the blockchain alongside the genesis block and then begins the synchronization process. To start a new chain for our currency, we need to forge a new genesis block and override the original one hard coded in the Bitcoin code as it was set for an older date (January 2009).
Here's the source code of the function that generates the genesis blocks defined in chainparams.cpp:
We can easily spot some predefined values, such as the key for the genesis coinbase transaction and a timestamp message. Editing the content of this block implies calculating a new genesis hash required for other parameters within the chainparams.cpp code.
For creating the new genesis block, we'll use a dedicated Python script called GenesisH0.
For that, in a new terminal, clone the genesis generator script (GenesisH0) from its GitHub repository:
git clone https://github.com/lhartikk/GenesisH0.git && cd GenesisH0
And then install the required package:
sudo pip install scrypt construct==2.5.2
To reproduce the original genesis block, run GenesisH0 with the following arguments:
python genesis.py -z "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks" -n 2083236893 -t 1231006505 -v 5000000000
You need to understand what each argument means to replace it with an appropriate value:
- The -z option indicates an optional timestamp represented by an arbitrary paraphrase with a date– usually a headline of a news article can be chosen. The Bitcoin's genesis block famously contains the dated title of an article in the Financial Times: The Financial Times 03/Jan/2009 Chancellor on brink of second bailout for banks
This is probably intended as proof that no premining has taken place before 2009. You can use a recent news headline or any time-related information. For example, I'll use the book's name with the publication year: Blockchain By Example 2018.
- For the nonce (-n), you can set any value to be used as a start nonce.
- For the epoch (-t), you can use the current time epoch. You can get the current epoch time online from www.epochconverter.com/, or you can generate it from the command line of most *nix systems with this code: date +%s.
- For -v, you need to determine the coin reward value and multiply it by * 100000000. For example, if you have a block reward of 50, it would be -v 5000000000. We will generate only 10 Readercoins for the genesis block.
- -b represents the target in compact representation, associated with a difficulty of 1. To get a block in 2.5 minutes, we will use 0x1e0ffff0.
We end up with the following command with different arguments to generate the new genesis block:
python genesis.py -z "Blockchain by example 2018" -n 1 -t 1529321830 -v 1000000000 -b 0x1e0ffff0
After a short while, you should see an output similar to the following:
Bingo! You now have the genesis block information that you need to use in your code base.
Except the first line, which is the Scriptsig of the genesis transaction, the remainder of the output results are identified by an expressive keyword.
Let's edit the /src/chainparams.cpp file accordingly to integrate the newly generated genesis block. Specifically, our target will be the function CreateGenesisBlock: