The Blockchain Internal Structure

In this article, we will cover the internal data structure of a blockchain (w.r.t Bitcoin). We delve into the blockchain organization and look at the contents of each block, forming the basis of the blockchain. As a novice to the blockchain, understanding blockchain internals assists a lot if one has the thirst to build the blockchain(cryptocurrency). If you are into blockchain apps such as smart contracts, game apps, etc., knowing the internals of the most widely used cryptocurrency, i.e., Bitcoin does not hurt, as this forms the base on which the apps run. We will see how the chains of blocks interact, creating the most secure data structure. Cited to an extent feasible differences with Ethereum blockchain. 

The Blockchain

A blockchain is a distributed ledger system and consists of blocks of information such as transactions. The name blockchain derives from the fact that the blocks are chronologically linked to form a chain. Each blockchain starts with a root block or the first block called the genesis block. Each block contains many transactions and records and is public among multiple computers (nodes or peers), without one controlling entity and an agreed policy. Also is the reason it is called distributed ledger or p2p network. A hash value identifies each block of the blockchain. The hash value is derived using the SHA256 cryptographic hash algorithm (Ethereum uses a different hash algorithm called KECCAK-256). Apart from the genesis block, each block will have a parent block through a field called the previous block hash, part of the header.

Every block and every transaction has a timestamp, allowing one to derive the correct order of the blockchain since its inception. Also is the reason why blockchain is considered a very secure database with no monopoly.

Hash function + Timestamp keeps the records in blocks unchanged.

Blockchain Operation

Fig: Blockchain operation

The blockchain operation would involve a set of stages as shown in the figure. I am a bit bored of using the regular Alice and Bob instead, prefer Tom and Jerry (By the way, Tom would never send any money to his no one enemy Jerry ! πŸ™‚ ).

The block becomes part of the blockchain, transparent to every node, all the nodes of the blockchain will have every transaction information since the beginning, making the blockchain very reliable.

miner is nothing but a node and is part of the blockchain network to mine (produce a valid hash) a new block added to the blockchain. A transaction in the blockchain involves an activity, like, transfer of an asset (in this case, Bitcoin) from one party to another.

The Block

In this section, let us see the contents of an individual block of the chain.

Fig: Chain of Blocks

The chain starts with the Genesis block, and then every new block added would be linked to its previous block using a hash value. An individual block contains the below structure.

Fig: An individual block

Below are the contents of an individual block.

Transaction count: Holds the total number of transactions

Block Header: Header comprises of

  • Version: Β Is a 4-byte long Bitcoin version number
  • Previous Hash: This is the hash value 32 bytes of the previous block (parent) header calculated using SHA256
  • Nonce: In cryptography, it means a number used only once. In Bitcoin, it is used to produce a 4-byte hash that is less than or equal to a target hash
  • TimeStamp: This is the time when the miner mines this block or the time this block was created
  • Merkle root: This is a type of binary tree that holds all the hashed pairs of the trees. It is 32 bytes in length
  • Difficulty: Is 4-byte information measuring how difficult it was to mine this block

Block Content: It contains mostly the transactions recorded in this block

Merkle Root

Merkle tree finds use in blockchains and cryptocurrencies and the Merkle root is the hash of all hashes of the transactions in a single block. A single block has multiple transactions, and each transaction has a hash. All the transaction hashes are hashed to form the Merkle root (Ethereum uses a slightly modified version of the Merkle tree called Merkle Patricia tree ). The advantage of using the Merkle tree and root is that it enables quick verification of the blockchain data confirming its validity and integrity. It is also memory efficient. Let’s see how a Merkle tree looks like.

Suppose we have four transactions A, B, C, and D, correspondingly there are four hash values hashA, hashB, hashC, and hashD. The Merkle tree is calculated as follows

Fig: Merkle Tree

Tools for Bitcoin block

To view the complete block information there are tools available using both command-line and graphical interfaces. For command-line, we use bitcoin-cli and for graphical we use bitcoin-qt. The prerequisite for both the tools is the bitcoin daemon (bitcoind) that must run before using these tools. In the below figure, we choose a block hash of the year 2009.

Hash = 00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09

Using bitcoin-cli it is possible to get the block details

bitcoin-cli getblock 00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09
{
    ######################## BLOCK DATA ############################# 
 
"hash": "00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09",
  "confirmations": 194359,
  "strippedsize": 216,
  "size": 216,
  "weight": 864,
  "height": 1000,
  "version": 1,
  "versionHex": "00000001",
  "merkleroot": "fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33",
  "tx": [
    "fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33"
  ],

   ##################### BLOCK HEADER ####################################
  "time": 1232346882,
  "mediantime": 1232344831,
  "nonce": 2595206198,
  "bits": "1d00ffff",
  "difficulty": 1,
  "chainwork": "000000000000000000000000000000000000000000000000000003e903e903e9",
  "nTx": 1,
  "previousblockhash": "0000000008e647742775a230787d66fdf92c46a48c896bfbc85cdc8acc67e87d",
  "nextblockhash": "00000000a2887344f8db859e372e7e4bc26b23b9de340f725afbf2edb265b4c6"
}

See the block content header and the block content for this hash.

Summary

In this post, we had a look into the internals of the bitcoin blockchain. We covered how the blockchain is created, the blockchain operation, contents of an individual block. We also peeked into how the blocks are connected with a previous hash value and why the Merkle tree is used as part of the block. The combination of Merkle root and the previous block hash makes blockchain a very reliable data structure. Also as there is no one entity controlling the blockchain, no client-server architecture, and having a distributed p2p network it is apparent that blockchain is the future.