Now that we have a fair understanding of blockchain, we're going to give an overview of blocks, which are responsible for building a blockchain.
Overview of blocks
Block attributes
If we consider a blockchain as a data structure, then the blocks are aggregated sets of data that are used for formation of the blockchain. Blockchain formation is similar to linked list formation, where each node has a reference to the next node in the sequence. In the case of blockchain, each block has a reference to the previous node, thus forming a link all the way to the initial block (known as the genesis block) of the chain. As we mentioned earlier, a blockchain can be stored in either flat file or database format. Bitcoin uses LevelDB to store metadata about all the blocks that are downloaded to the disk.
Just like a linked list node, each block has a pointer, which is the identifier of the block. These are just hashed values of the block's header data. More detail about hashing will be covered in Chapter 2, A Bit of Cryptography. We can consider the hash as a unique identifier of a fixed size that represents each block; no two blocks will have the same identifier. Since all the blocks are linked together by this hash value, each block will have the identifier of the previous block. The previous block is referred to as the parent block, and each block can have only one parent.
Each block could also be referenced by the height of the blockchain. This height is nothing but the distance of the block, or the block count, from the genesis block. Height is an important attribute of the blockchain, as it's easier to refer to a block with a plain number rather than a lengthy hash value. The block hash is not a hash value of the entire block, but rather the hash value of the block header, which consists only of metadata. In Bitcoin, a SHA256 hashing algorithm is used to hash the block header and to create a unique identifier for the block.
Structure of the block
Although all blockchains consist of linking blocks together to form an immutable ledger, different block structures can be adopted depending on the application. Permissioned and permissionless blockchains, for instance, have slight variations in their block structure. We'
We'll be using Bitcoin's permissionless block structure as a reference to try and identify its characteristics:
A block consists of the components mentioned in Figure 1.3. Block Header and Transactions are the most important parts of the block, as they are responsible for the hash value, which is the identity of the block. The Block Size is the size of the entire block. The Block Header contains all the metadata of the block, and the Transaction Counter has the count of transactions. Finally, all the Transactions are stored in the block.
As mentioned before, a blockchain starts with an initial block called the genesis block. If the chain is traversed backward from any given block, it will end up at the genesis block, proving that the entire chain is legitimate and valid. The genesis block is often statically coded in a public or permissionless blockchain, but it's created by the first participant in the case of the permissioned blockchain.
Block header
The block header, as stated before, consists of the metadata of the block. This holds the information that's needed to link the blocks in the blockchain:
Each block header will have the components outlined in Figure 1.4. These are the minimum fields required in a permissionless blockchain, such as Bitcoin, to efficiently create a block that can be appended to an immutable blockchain. The Previous Block Hash field is a reference to the last block created. The Merkle Root is the value of the Merkle hash tree; it summarizes all the transactions in the block. Timestamp, Difficulty Target, and Nonce are used by the PoW consensus algorithm to solve the hash puzzle. We'll be revisiting these concepts in more depth throughout the book.
Linking blocks
As we know, blocks are linked in a blockchain using references, just like in a linked list, but here the blocks are linked by referencing the hash value (identifier) of the previous block. Each full node in a blockchain network will maintain a complete blockchain and append a new block whenever it has one to append. Due to the decentralized nature of the blockchain, each node will verify the block before linking it to the local blockchain record.
The computed hash value of each block is the combination of the hash of the previous block and its own block data. This results in a dependency between neighboring blocks and nearly unbreakable links:
Satoshi explained how the concept of timestamping should be used. All the items are hashed and the block is timestamped, meaning that the subsequent block will include this timestamp, creating an ordered chain of blocks.
Each node in the blockchain network follows a simple process for appending new blocks to its existing local blockchain. Whenever a node receives a block from the network, it checks for the previous block hash. If the hash value matches with the hash value of the last block on the node's local blockchain, then the node accepts this block and appends it to the current blockchain. As long as this is the longest known blockchain, the blocks would be considered valid by all the peers in the network in a PoW-based blockchain.