Mastering Bitcoin: Unlocking Digital Cryptocurrencies

The summary of ‘7. The Blockchain’
from Mastering Bitcoin: Unlocking Digital Cryptocurrencies
by Andreas M. Antonopoulos.

51tdyaevhcl-_sx379_bo1204203200_

Introduction

The blockchain is back-linked list of transactions. Bitcoin uses Google’s LevelDB. The first block serves as the foundation of the stack

Each block is identified by a hash generated by SHA256 algorithm & has the parent’s hash. The chain goes back to its first, genesis block

A block can have multiple children (“fork”) when different blocks are discovered simultaneously by different miners. But it’s temporal

Each block has its previous block’s hash. The cascade effect makes it immutable because it’s impossible to recalculate all subsequent blocks

While the possibility of any block being reversed always exists, it decreases as time passes like layers in a geological formation

Structure of a Block

A block is a container data structure made of: 1) a header (80 bytes); 2) transactions (avg 250 bytes x 500)

Table 7-1. The structure of a block

Block Header

A header consists of: 1) reference to a previous block hash; 2) difficulty, timestamp & nonce; 3) merkle tree root (summary of transactions)

Table 7-2. The structure of the block header

Block Identifiers: Block Header Hash and Block Height

The identifier of a block: 1) The 32-byte “block hash” made by hashing the block header twice through SHA256 algorithm

The block hash is not included inside the block’s data structure & might be stored in a separate database as part of the block’s metadata

2) the block height: the first block’s height is 0. Each subsequent block is one position “higher” in the blockchain. (278,000 in Jan 2014)

Unlike the block hash, the block height does not always identify a single block. Two or more blocks might have the same block height (fork)

The Genesis Block

The genesis block: the first block in the blockchain created in 2009. It is the common ancestor of all the blocks in the blockchain

The genesis block is statically encoded within the bitcoin client software & cannot be altered

The following identifier hash belongs to the genesis block:

Using the Bitcoin Core reference client on the command line:

The bitcoin node then receives a new block from the network, which it parses as follows:

The hidden message in genesis block: “The Times 03/Jan/2009 Chancellor on brink of second bailout for banks.”

Linking Blocks in the Blockchain

As a node receives incoming blocks from the network, it will validate these blocks and then link them to the existing blockchain

A node in the local copy 1) receives a new block from the network 2) finds it’s a child of the last block 3) adds it to the end of the chain

Merkle Trees

A merkle tree, a binary hash tree, efficiently summarizes and verifies the integrity of all the transactions in the block


Figure 7-1. Blocks linked in a chain, by reference to the previous block header hash

A Merkle tree is constructed by recursively hashing pairs of nodes until there is only one hash, called the root, or merkle root

We start with four transactions, A, B, C & D (Fig 7-2) whose data is hashed & the resulting hash is stored in each node as HA, HB, HC, & HD:

To construct the parent node HAB, the two 32-byte hashes of the children are concatenated to create a 64-byte string which is double-hashed:

This process continues until there is one 32-byte node, the Merkle root, which is stored in the block header & summarizes all transactions


Figure 7-2. Calculating the nodes in a merkle tree

A binary tree needs an even number of nodes. If there is an odd number of them, the last hash will be duplicated: a balanced tree (Fig 7-3)


Figure 7-3. Duplicating one data element achieves an even number of data elements

Whether there is one transaction or a hundred thousand in a block, the merkle root always summarizes them into 32 bytes (Fig 7-4)

To prove that a transaction is in a block, a node makes log2(N) hashes including a merkle path connecting the transaction to the root


Figure 7-4. A merkle tree summarizing many data elements

A node proves K is included by merkle path consists of 4[blue]. With them any node proves HK is in the root by computing 4[dotted] (Fig 7-5)


Figure 7-5. A merkle path used to prove inclusion of a data element

7-1 shows the process of creating a merkle tree from the leaf-node hashes up to the root. 7-2 is the result of compiling the merkle code

Example 7-1. Building a merkle tree

Example 7-2. Compiling and running the merkle example code

The efficiency of merkle trees becomes obvious as the scale increases. 7-3 shows the amount of data needs to be exchanged as a merkle path

Table 7-3. Merkle tree efficiency

This SPV (simplified payment verification) nodes use merkle paths to verify transactions without downloading gigabytes of full blocks

Merkle Trees and Simplified Payment Verification (SPV)

In order to verify a transaction is included, without downloading all the transactions, SPV nodes use an authentication path, or merkle path

A SPV node receives less than 1 KB of data for the block header and merkle path, more than a thousand times less than a full block (1 MB)

ページトップへ