Structure of a Bitcoin Block

0
35

A Bitcoin block records the data related to a Bitcoin transaction. The blocks are mined one after the other with all the transactions in the network being recorded permanently. Being a very secure network, the Bitcoin blockchain makes it very difficult to modify or delete the data that has been registered on a block. More or less, a block acts like a book of the ledger with each page referring to one block and connected to both the previous and the next blocks by the addressed called “hashes”. A blockchain is created by linking these individual block ledgers one after the other. A block structure has several elements. Let’s have a look at some of these block components.

1. Block Identifiers

2. Block Header

3. Transactions

1. Block Identifiers: The block identifiers are the elements that identify a block’s address, its height, and its size. There are the main block identifiers:

a. Hash: A hash is a unique identifier that distinguishes one block from the other. A block hash is generated by passing the block header metadata through the SHA256 algorithm. (Secure Hash Algorithm used in cryptography). As a block header is hashed, a hash is also termed a block header hash. A hash of a block consists of a series of numbers and alphabets and is encrypted to make blocks safe from malicious attacks. An example of hash:

f7257cbe6d961f8fef0f93360245a90d1f6962c3c3fbc83213515ad809999bd3

How a hash is generated: Hashing in a blockchain is done through mathematical functions called hash functions. A hash function works by taking in the input of some length and always generating an output of a fixed length. No matter how long or short the input data is, the output generated via the hash function always has the same length. This makes a hash function “deterministic” (output remains the same of the input is same) and using a cryptographic hash function makes the data secure: an attacker cannot guess the length of the input by decrypting the output.

A simple change to an input value results in an entirely different output which renders the collision resistance to the hash function: Two different input data to the hash function would not result in the same output. Along with this, a hash function is also image-resistant: it is almost impossible to reverse engineer the output to get the input, thus making it difficult for the attackers to guess the input. Hash functions are inherent to a secure blockchain and do not require complex resources to work efficiently.

Subscribe to Coinmonks Youtube Channel to get daily crypto news.

Parent-Child Relation between Blocks: Each block on the blockchain is connected to the previous block via the previous block’s hashed address. As you read above, the blocks of the blockchain are like pages of the ledger, with each page having a link to the previous page and this link is the previous block’s hash. Every new block mined on the chain becomes the child of the previous block, with the previous block being called a parent. So a child block carries the hash of its parent block in its block header, in addition to its own hash value. This way all blocks are connected to form a long chain, with the first block ever mined on Bitcoin called the “Genesis Block”. Such connectivity makes the blockchain resistible to attacks: any new block being added to the chain has the information if the previous block has been tampered with. This means if the parent block has changed, the previous block hash field also changes, which further changes the child’s hash to change in the child’s child, i.e. grandchild. This way all the blocks in the blockchain are affected.

As Bitcoin blockchain is mined with a Proof of Work mechanism, a random input fed to a hash function leads to a hashed output that contains several leading zeroes, further reducing the possibility of attacks. Following is the hash of the genesis block (First block created in the Bitcoin network):

000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f

Note: A block can have only one parent but sometimes, the situation leads to 2 or more children with the same parent. This happens when different miners mine the same block at the same time and all carry the same parents’ hash field. This leads to forking, a situation when different blocks are mined at the same time by different miners. Out of the multiple children, any of the child blocks is appended to the parent block and the rest are forked to a shorter chain.

b. Block Height: The first-ever Bitcoin block was created in January 2009 and is termed “Genesis Block”. As it was the first block, it was assigned a zero height. The height of a block is the number of blocks that have been mined between the genesis block and the current block. A block height of 6234, for example, means that there are 6234 blocks stacked on the top of the genesis block and 6234 is the block height. More than one block can have the same height; i.e. more than one block contends to be the next in the blockchain and this results in the formation of a fork. As Block Height is metadata (gives information about a block), it is not stored in the block.

2. Block Header: A block in a blockchain is identified by its unique block header. In the Proof of Work mechanism, the block headers are hashed many times to generate a unique hash for each block header. This block header hash becomes the identifier of the block. The Bitcoin block header length is 80 bytes and it consists of the following metadata:

a. 4-byte Version

b. 4-byte Timestamp

c. 4-byte Difficulty Target

d. 4-byte Nonce

e. 32-byte Previous Block Hash

f. 32-byte Merkle Root

Miners hash the block header to get the right nonce and add the validated block to the blockchain. The block header contains all the information about the block.

a. Version: With version number, miners can track any changes or upgrades made in the protocol.

b. Timestamp: The time at which a particular block was hashed. The timestamp is recorded in Unix Time (which counts the number of seconds elapsed since a particular time. For Bitcoin blockchain, that particular time is January 1, 1970; this was the time around which Unix was invented). The timestamp is prone to manipulation by the miners.

c. Difficulty Target: Difficulty measures the computational power to mine a block. As Bitcoin uses the Proof of Work mechanism, miners continually hash to find a block that can be added to the blockchain. The difficulty measures the time taken to mine a new block. A higher difficulty target indicates that more computational power is required to mine a new block. This means miners need to use a higher hashing power (more efficient machines). The difficulty level is stored as “Bits” which is stored as a 1-byte exponent followed by a 3-byte coefficient. The difficulty of the genesis block is 1. As difficulty is proportional to the computational or hashing power, it keeps on changing. When hashing power is more (more miners are there), the difficulty level increases and vice versa. In Bitcoin, the difficulty is readjusted after every 2016 blocks.

d. Nonce (Numbers Only Used Once): The maximum value of this 32-bit number can have the max value of 232. The nonce string is appended to the block hash and rehashed. Post rehashing, the nonce is compared to the target difficulty number. If the nonce is less than the target value, the block is added to the blockchain. If it is not, the nonce is changed and the process repeats until a nonce is found whose value is less than the target value.

A nonce is a hexadecimal number that the miners compete to find for each block. Unless a nonce is found that is less than the difficulty target set in the block’s header, miners continue to hash. The miner who finds the nonce first is rewarded and the block is added to the blockchain. This process of repeatedly guessing the right nonce is called Proof of Work. Higher the difficulty target, more time would be taken by the miners to guess the right nonce. The repeated actions of hashing by miners result in what is called a “Golden Nonce”, i.e. a hash value that is less than the target difficulty. A hash, to be lesser than the target hash, must be preceded with a string of zeroes.

e. Merkle Root: A Merkle Root is the hash of all the transaction hashes in a block. Every block transaction has a unique hash associated with it and these transaction hashes are saved in the form of an upside-down Merkle Tree. The Merkle Root is at its top. Transactions are structured in a Merkle Tree in such a way that the data is organized efficiently. All the transactions are hashed first and then paired with one another. For example, a transaction hash “TxA” is paired with another transaction hash “TxB” and hashed to produce another hash called “TxAB”. This hashed “TxAB” is then paired with another hash, say “TxCD” (formed from the hash of “TxC” and “TxD”). This produces a hash called “TxABCD”. Going this way, from the bottom to the top (The top value being called Merkle Root), a Merkle Tree is formed. If any of the hashed transactions is altered by someone, the resultant Merkle Root comes out to be a different value.

In a Merkle Tree, the transactions are termed leaves. It is the leaf level where the hashing starts and continues till the Merkle Root. A Merkle Root has information of all the block transactions since it is the hashed version of all the hashes of all the transactions that took place in a block. Due to the tree-like structure of a Merkle Tree, verification of the transactions can be done quickly. The Merkle Root hash is added to the block header.

A Merkle Tree

f. Previous Block Hash: Each block carries the hash of the block that was mined before it. This renders immutability to the blocks.

3. Transactions: When a block is added to a blockchain, it is called a confirmed transaction or a confirmation. After a transaction, when the other transactions get confirmed, that initial transaction gets further confirmed. A Bitcoin block is mined every 10 minutes.

The first transaction of a block is termed “Coinbase Transaction” or “Generation Transaction”. After a transaction is verified, it is added to the block and called to be confirmed. For security purposes, a transaction needs to be reconfirmed several times. A transaction is considered confirmed only after a certain number of blocks have been added to the blockchain. An unconfirmed transaction is prone to reversal and therefore at least one confirmation must be there for a transaction. A high amount of transaction value requires more confirmations than the lower amounts.

Steps to Create a Block Header

The mining node needs to add all the six fields of the block header, starting from the Version Number.

a. The node adds the version number of the software.

b. Hash of the previous block is added.

c. Transactions are added to the Merkle Tree and Merkle Root is created. The Coinbase transaction is added followed by the remaining transactions. The transaction hashes are paired and hashed until the Merkle Root is reached at the top of the tree.

d. A timestamp at which a block is created.

e. Difficulty Target is defined and stored in the form of “Difficulty Bits”.

f. A nonce with an initial value of zero is defined.

Once all the fields are created in the block header, the mining is initiated. Different miners compete with one another to find a nonce value that is lower than the difficulty target value set in the block header. The miners hash the block header repeatedly unless they find the right nonce value. Miners use algorithms like SHA256 for this process. They keep on trying with different input values until they find the right value. The output of all the different inputs is always 256 bits. After the right hash is found (whose value is less than the Target Difficulty hash), it is added to the block header. A new block is created and verified by all the nodes on the network.

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also, Read


Structure of a Bitcoin Block was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.