This is a second post of “How to develop Altcoin?” tutorial series. If you haven’t seen previous ones, this post might be incomprehensible.
You can go to the first post of the tutorial by clicking the link.
Genesis blocks and Merkle trees
In the previous post, we have cloned the Litecoin source code from Github, then we made some changes to the source code of our altcoin like setting new consensus rules and declaring the maximum supply of our altcoin. Now it’s time generate our the first block of our blockchain.
A genesis block is the first block of your blockchain. Since you haven’t given hash values to your genesis block, you still have the genesis block of Litecoin. Let’s look at Litecoin’s genesis block first, and then we will find the hash values of our genesis block of the blockchain.
The original Litecoin’s genesis block and merkle root hash values are “0x12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2”, “0x97ddfbbae6be97fd6cdf3e7ca13232a3afff2353e29badfab7f73011edd4ced9”, respectively.
A genesis block can be considered as a block 0, and all other further generated blocks will come after the genesis block. As seen the structure of a genesis block in Figure 1, it does not have any field that contains a previous block hash value.
You may ask yourself how to find the correct hash values to construct your genesis block because without any transaction you can not see the hash value of a merkle tree node easily. At this point, this python script can help you by generating the hash values on behalf of you.
Before executing the python script, you need to define your coinbase parameters that help the python script during the generation of hash values. The script can be executed with following commands.
python genesis.py -a scrypt -z "One thing, I don't know why"
“One thing, I don’t know why” is our phrase, and it will be used during finding the hash value of the genesis block. The timestamp, the nonce, and public key will be assigned randomly. If you don’t want to get the hash value of genesis randomly, you can give these values on your own. Finding the hash value of the genesis block might take at most a day, it highly depends on your computer’s power.
When you find the hash value of your genesis block, you need to make some changes on your the source code of your altcoin. Let’s say, your hash value of the genesis block is “0000000000z6iw7y1lc3ps5wu7e6wt9tijn9doaioxvc1pyfmziy1skhg9iqkm8f” .
So, now your block 1 will be chained to the genesis block by carrying the hash value of the genesis block inside of it. Figure 2 illustrates the aforementioned chaining process.
I hope that you quickly realized the difference between the genesis block and other blocks. The first block has “previous block hash value” field, right?
Since we chose to leave other parameters to the script, we are good to go further steps.
You should do some changes in the “src/chainparams.cpp” file.
“0x12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2” is the hash value of the original Litecoin’s genesis block, so you need to change with yours. Then, you need to change your merkle tree value (you should get from the output of the script) with the Litecoin merkle tree value (0x97ddfbbae6be97fd6cdf3e7ca13232a3afff2353e29badfab7f73011edd4ced9).
Moreover, you need to change your public key which you got from the script with Litecoin’s public key that is “040184710fa689ad5023690c80f3a49c8f13f8d45b8c857fbcbc8bc4a8e4d3eb4b10f4d4604fa08dce601aaf0f470216fe1b51850b4acf21b179c45070ac7b03a9”.
Congrats, you are done with the genesis block.
Now you can build your the wallet by following the Litecoin wallet tutorials. Then, you can open your altcoin’s wallet application.
In addition to giving information about a genesis block, I would like to explain merkle trees.
Let’s say you have a large dataset, and you need to store them and also be sure whether your data is corrupted or not. A merkle tree ( aka. hash tree) might help you to solve your problem. In a merkle tree, every non-leaf nodes carry the hash value of a previous leaf or non-leaf nodes. However, leaves carry only the hash value of your dataset elements.
As seen in Figure 3, leaves can only carry of a hash value of the element of a given dataset. You can use a hash tree in verification of any data that is transferred to or stored in any device. It usually is used in a peer-to-peer network to check shared blocks between peers for verifying them. If you get a merkle root from a trusted source, you can confirm other peers if they are sending legitimate blocks to you. Do not forget that the blockchain is a kind of distributed database whose integrity is protected by a proof-of-work algorithm, and its network is a peer-to-peer network.
I hope you have fun with this tutorial.
See you in another post!