Book Image

Learn Bitcoin and Blockchain

By : Kirankalyan Kulkarni
Book Image

Learn Bitcoin and Blockchain

By: Kirankalyan Kulkarni

Overview of this book

Blockchain is a distributed database that enables permanent, transparent, and secure storage of data. Blockchain technology uses cryptography to keep data secure. Learn Bitcoin and Blockchain is the perfect entry point to the world of decentralized databases. This book will take you on a journey through the blockchain database, followed by advanced implementations of the blockchain concept. You will learn about Bitcoin basics and their technical operations. As you make your way through the book, you will gain insight into this leading technology and its implementation in the real world. You will also cover the technical foundation of blockchain and understand the fundamentals of cryptography and how they keep data secure. In the concluding chapters, you’ll get to grips with the mechanisms behind cryptocurrencies. By the end of this book, you will have learned about decentralized digital money, advanced blockchain concepts, and Bitcoin and blockchain security.
Table of Contents (6 chapters)

Structure of blockchain

In this section, we will learn about the following aspects of blockchain: its structure; its building blocks; and its core parts that make it disruptive, robust, strong, and tamperproof.

Let's have a look at the structure of the blockchain. The blockchain structure is very similar to that of linked lists or binary trees. Linked lists or binary trees are linked to each other using pointers, which point to the previous or next list elements on the nodes in the linked list. The structure of blockchain is not really different from that of binary trees, but the major difference is that blockchain is tamperproof and it is also very easy to find out if any tampering has taken place.

In the following diagram, we will look at a representation of how blockchain is constructed and how it is a linked list:

Let's now discuss the structure and elements of the blockchain.

The blockchain is a linked list that is built with hash pointers instead of pointers. This is the exact reason why blockchain, though it resembles a linked list, is different because, in the linked list we have been using the pointers to point to previous nodes for the elements in the lists, but in the case of the blockchain, the pointers are hash pointers and not just simple pointers.

So, typically, any block in the blockchain consists of three parts, or feet such as a Header, Merkle, and Transaction's Id list. This is a newly created block.

We can see the structure of blockchain in the following diagram:

We will now look into the following blocks and their elements:

  • Header: This block contains the version information of the block, the nonce, the previous block ID, and the timestamp that is being hashed again at the time the block is created.
  • Merkle: This block is a hash built from the block's transaction identifiers.
  • Transaction's Id list: This block represents the transactions themselves. It's a list of records, identification hashes, that are included in the block's Merkle tree.

The block is then created with all of the preceding details. This newly created block gets added to the blockchain.

Now, let's look at what the Merkle tree is, as follows:

  • In the blockchain structure, it's also known as a binary hash tree. It's a data structure used for summarizing and verifying the integrity of large sets of data.
  • It not only summarizes the data that is being captured on a particular block, but it also verifies the integrity of the data, which makes sure that the data represented in the block is integrated and not tampered with. It contains cryptographic hashes, which are used to make sure integrity is maintained across the block.
  • It's an upside-down tree where the root is at the top and the leaves are at the bottom.

Now, let's look at a representation of the Merkle tree in the following diagram:

Here is a brief overview of what the top hash, data blocks, and Merkle root stand for, as follows:

  • In the preceding diagram, we can see Top Hash, which is the root of the Merkle tree, and there are leaf nodes or leaves that make up the entire tree.
  • Data Blocks are transactions that have been captured and hashed. They are paired and hashed multiple times, and that's how you reach the top of the Merkle tree.
  • A block of one or more new records is collected, and such records are then paired and hashed together multiple times until a single hash remains. This single hash is called the Merkle root of that Merkle tree.

If any change or tampering occurs in any part of the transaction data, we can see this compromise in a bold and clear manner. Hence, the Merkle tree is important in the implementation of blockchain and is a major contributor to making sure that blockchain data has not been tampered with.

Structuring the Bitcoin block header

Now, we will look into the structure of the Bitcoin block header, in which the header consists of a block and each block holds the following three sets of block metadata:

  • A reference to a previous block hash
  • The difficulty, timestamp, and nonce parts of the header
  • The Merkle root

Let's look at the various terms represented in the following diagram:

The following is an elaboration of the terms mentioned in the preceding diagram:

  • Version: The first part of the header is the 4 bytes of version. A version number tracks software protocol upgrades. It is really important to capture that data as part of the block itself because, if there are any changes made in subsequent versions of the program, it becomes necessary to capture which program or node is running each program to send the transactions and blocks across the internet.
  • Previous Hash Block: The second part of the header is the 32 bytes previous hash block or previous hash block code. It's a reference to the hash of the previous block in the chain. It's not just a link to the previous block, it's a hashed pointer to the previous block, which makes sure that the new block added maintains the sequence of the blocks or the chain.
  • Merkle Root: The third part of the header is the 32 bytes of the Merkle root. We just looked at the Merkle root, which is a hash root of the Merkle tree. It is a final hash code of all the transactions that were captured as part of the block.
  • Timestamp: The fourth part of the header is the 4 bytes of timestamp. It is the approximate time taken for the creation of this block. We use the term approximate because once the block is created, it takes a few milliseconds to get added to the actual blockchain as the transactions are verified by different nodes.
  • Difficulty target: The fifth part of the header is the 4 bytes that capture the difficulty target. It's the proof of work algorithm of the blocks. It is given for a particular node that is running the Bitcoin blockchain program.
  • Nonce: The sixth part of the header is 4 bytes of the nonce. It is the counter used for the Proof of Work (PoW) algorithm. Whichever node is first in calculating and solving the mathematical problem actually yields the result of the problem-solving solution and the result contains this nonce, which is used to capture the transaction and verifies that the PoW being carried by the node is correct.

Thus, we can see that this is the typical structure of a Bitcoin block header, which is roughly 80 bytes of information that is captured in each of the blocks.

Representing the blockchain structure

Now, we will look at the overall structure of the Bitcoin blockchain header in the following diagram:

In this example, Block 16 represents the previous block's hash and consists of the timestamp of creation, the transaction root is also known as the Merkle root, and the nonce is the algorithm-cum-counter that has to be verified. All of the hidden information on the transactions is hashed again, and that hash is captured in Block 17. It consists of the previous block's hash, the timestamp, the Merkle root, and the nonce.

In the sections for Data 1, Data 2, Data 3, and Data 4 in the preceding diagram, all the data is paired and hashed multiple times. Thus, the data keeps going in the upward direction until it gets to one final hash. These blocks keep moving and the entire Merkle tree is formed by the transactions that are captured in the blocks. This is what makes it so strong, robust, tamperproof, integrated, and immutable. This is the beauty of blockchain.