Ethereum Block

In this article, we learn what makes up an Ethereum block and dive deep into how Ethereum maintains the integrity of blocks in the blockchain.

Table of Contents.

  1. Introduction.
  2. Components of an Ethereum Block.
  3. Maintaining the Integrity of a Block.
  4. Summary.
  5. References.

Introduction.

A blockchain is simply an immutable chain of blocks. Blocks are a collection of Ethereum transactions with the hash of the previous block joining the current to the previous thereby forming a chain. Given three blocks A, B, and C, A being the genesis block. B stores the hash of A and C stores the hash of B. This means that we cannot change C without first changing B and we cannot change A without first changing B. This is what makes it difficult to hack.

The following image summarizes how blocks are linked on the Ethereum protocol;
bit150

As we can see from the above image, blocks link to each other using the hash of the previous block. Unlike the Bitcoin protocol where linking ends there, Ethereum also links the current block state root with the previous block state root.

Mutating a block itself is very difficult because we would have to change the state root, the code hash, and the storage root for blocks to agree with each other. As we know, a block stores transactions and has a maximum limit. On the Ethereum blockchain, a block is also responsible for storing smart contract code. These transactions inside the block are stored in a tree referred to as the Merkle tree. This tree is made up of leaves which are hashes for all transactions on this block, two transaction hashes come together to form a root of a subtree, this continues up the tree and results in a root hash.

As we know hashing produces a unique fixed-length of a string, this means that if a transaction even changes a comma or a digit, the hash changes, if the hash changes, to root hash of the entire tree changes, and if the root hash changes, the block hash changes and we are back to the previous concept.

In this article, we learn what makes up an Ethereum block and dive deep into how Ethereum maintains the integrity of blocks in the blockchain.

Components of an Ethereum Block.

A single Ethereum block comprises of the following major components;

  • block header - this acts as a unique identifier used to identify a block on the entire Ethereum blockchain. It is repeatedly hashed to create proof of work for mining rewards.

  • Transactions - a block holds all transactions executed on the blockchain. These transactions have a transaction hash which is just the transaction after undergoing hashing and the transaction root which holds the cumulative hash of all transactions in the block(also serves as the root hash of the Merkle tree).

  • State Variable - This comprises of the state hash which is a cumulative hash of the current state of the entire system and the state root which is the entire state of the system including accounts, account balances, smart contract code, contract storage, account nonces among other information.

bit134

The block header comprises of;

  • Previous hash - this is the block hash of the predecessor block.

  • Beneficiary - this is a 160-bit address of the recipient of the mining reward after a block is mined.

  • Difficulty - this represents the effort it took to mine the block.

  • Gas Limit - this is the maximum amount of gas that transactions in this block can use. That is, the total amount of gas expended by all transactions in this block should be less than the block's gas limit.

  • Ommers Hash - ommer blocks are as a result of two miners finding the nonce that solves the PoW puzzle at similar times. This is the hash of this block.

  • Logs Bloom - this is a 256-byte string that acts as the bloom filter for logs of the block. It is used to filter the hash of each element inside the block.

  • Block Number - this number represents the height the block is at in the blockchain.

  • Gas Used - gas is the cost of executing transactions and smart contract code. In this case, gas used is the cumulative total gas used by all transactions and smart contracts in the block.

  • Timestamp - this is the exact time the block was mined.

  • Nonce - a number when combined with the mixHash proves that the block has undergone the necessary proof of work.

  • Mix Hash - this is a unique identifier for the particular block.

  • Extra data - this is an optional field that can be used to store arbitrary data.

  • State root - this is the root hash of a special Merkle tree that stores the entire state of the system at the time the block was mined.

  • Transaction root - this is the cumulative hash of all transactions that make up the merkle tree(merkle tree root hash).

  • Receipt root - this is a keccack 256-bit hash of the root node of the transaction receipt trie composed of all transaction receipts in the block.

The account state
The account states stores information about an Ethereum user account. That is;
Nonce - the number of transactions that have been set from the user address or the number of smart contracts created with the account.
Balance - this is the total ether owned by the account.
Storage Root - this is the hash of the root node of the account storage trie.
Code hash - this is the hash of the EVM code of the account.

All of the above except the code hash can be mutated. For example, for each transaction, the nonce will increase, and the balance might increase or decrease. The storage root hash will also change.
If the code hash is immutable it means that if we deploy a buggy smart contract, we cannot later change it rather we have to deploy a new one.

Maintaining the Integrity of a Block.

Maintaining the integrity of a block on Ethereum comprises of three checks;

  • First, we make sure that the contents of the block header are not tampered with.
  • Second, we make sure transactions inside the block are not mutated, and if at all they are we would know.
  • Finally, we make sure that the state transitions are computed correctly, hashed, and verified.

Having the above three checked in a block makes sure the block is immutable and tamper-proof.

On Ethereum, a block hash is the block of all elements in the block header. The block hash is computed by applying a variant SHA-3 hashing algorithm referred to as keccak to all block header components.

The following image shows an Ethereum block header;
bit135

A block can hold up to 2000 bitcoin transactions, and 100 Ethereum transactions. We have discussed how tampering is prevented on a block. Merkle tree hashing is used to compute the state root hash this is because we only have to recompute the hash of the chain states from block to block.
Merkle tree hashing is also used to compute the receipt root hash.
For a transaction to be verified, a single path in the tree is checked since all transactions contribute to the hash of the root of the Merkle tree. In the Bitcoin article, we referred to this path as the authentication/Merkle path

State transitions are a result of smart contract code execution, and every such transition requires a state root hash recomputation. Only the affected path in the Merkel tree is recomputed, instead of computing hash for the entire set of states.

Consider the following images demonstrating state changes;
bit136-2

We change the state from 19 to 20;
bit137-1

The change changes up the tree;
bit138-1

When the state of 19 changes to 20, it initiates other recomputations for 30 -> 31 and 40 -> 41 including the root hash 63 -> 64.

Etherium computes its block hash by computing state root hash, transaction root hash, and receipt root hash.

bit139

The above roots and the rest of the header items are hashed together with the nonce variable to solve the proof of work puzzle.

A block hash has two purposes;

  • Verification of the block and transactions' integrity
  • Formation of the chain link by embedding the previous block hash in the current block header.

Any participant cannot make changes to a node since the hash value will change. This result in mismatching hash values that leads to invalidation of the local chain. Future blocks that are initiated by the invalid node are rejected by other miners because of a hash mismatch. This makes sure the blockchain remains immutable.

Summary

Hashing and encryption are used to secure the blockchain. Encryption consists of public-key encryption and hashing algorithms used are SHA-3, SHA-256, and keccak. Public-key cryptography is what decentralized blockchains such as bitcoin and Ethereum are built upon.
They enable participants to trust that all is correct and valid and that the records cannot be tampered with.
In Ethereum, the block hash is the hash of all elements in the block header.
The Merkel tree hashing algorithm is used to compute the roots of the block header, these include, the state, transaction, and receipt roots.
The block hash allows for the formation of chains, this is done by embedding previous blockchain hashes in the current block header.
A participant cannot change a clock since it will result in a different hash, mismatches, and invalid states. This ensures the immutability of the blockchain.

References

Multi-Tenant Blockchain Systems
Ethereum Architecture