Calculation of Merkle’s roots on Ethereumon: step by step
Introduction
——
Ethereum is a decentralized blockchain open source platform that allows developers to create and install intelligent contracts. One of the basic aspects of the Ethereum network is the consent mechanism that is based on the creation of blocks containing transactions. In this article, we examine how to calculate the root of the Merkle of the Genesis Block in an independent Pura application.
What are the blocks of Genesis?
———
The Genesis block is the first block in a blockchain and is used as a starting point for all the following blocks. The Genesis block contains metadata of the blockchain and its initial state. To create the Genesis block, we have to calculate the root of the Merkle of transactions in this block.
Merkle roots
——
Merkle Root is a digital imprint that represents a series of transactions in the block. This is calculated by performing the Sha-256 hash of each transaction and thus combining it using Betwise operations. The resulting hash acts as a root of the Block Merkle.
Calculation of the Genesis block in Merkle Root C
———–
Here is a detail C -Code that shows how to calculate the Merkle root a block of genesis:
`c
#include
#Include
// structure to represent a transaction
Typedef Stuct {
Uint256 value; // Transactions data (as a balance of the account, amount of activity)
} Transaction;
// You can calculate the Merkle root using SHA-256
Uint256 Get_merkle_root (Const Transaction* Transactions) {
Uint256 hash;
Sha256_ctx Sha256;
Sha256_init (& Sha256);
for (int i = 0; i
Const Char* Data = Transactions-> Value.data [i] .bytes;
Uint8* Bytes = New Uint8 [Data];
Sha256_update (& Sha256, data, I + 1);
Hash = Sha256_final (Byte, & Sha256);
Wipe [] bytes;
}
Return hash;
}
Int Main () {
// Example of Genesis lock transactions
Transaction Transactions [2] = {{{
{0x00000000, 0x12345678}, // account balance: 10 ether
{0x00000001, 0x90123456} // The amount of the device: 5 units
};
// Calculate the root of the Merkle of the Genesis Block
Uint256 Mercle_root = Get_merkle_root (transactions);
Printf (“Genesis Block Merkle Root: %S \ n”, Hex_string (Merkle_root, 64));
Return 0;
}
`
This detail of the code calculates the Merkle root for a simple block of genesis containing two transactions. TheGet_merkle_root` Gets a series of transactions such as input and uses Sha-256 to calculate Merkle root.
Note that this is only an example implementation and it may be necessary to modify it in a certain use.
Conclusion
—–
The calculation of the Merkle root for an independent clean C application for a Genesis block can be obtained by calculating the Sha-256 hash of each transaction and therefore combining the Betwise operations. This process includes the following steps:
- Start the SHA-256 context.
- Itera through each transaction, updates the context with the transactions.
- Convert each byte into an Uint8 indicator and update the context.
- Use the calculation by returning the resulting hash.
Following this approach, you can create a clean C application that independently calculates the Merkle root to the Genesis blocks without relying on the reference client bookcases or other external addictions.