Creation of block
Blocks are created and added to blockchain in mining process. Miners are competing in finding new block using Proof-of-work algorithm. This involves repeatedly hashing the header of the block and a random number with the SHA-256 cryptographic algorithm until a solution matching a predetermined pattern emerges. The first miner who finds the solution, fills the block with transactions which are picked (mostly according to highest fees included in them) from mempool and publishes that block into the blockchain. Transactions that miner add to block are considered confirmed, which means that the new owners can now spend them in new transactions. When the block is published, a new, different competition starts for all miners which now involves also the added block. A special transaction is also included (coinbase transaction) - this transaction pays miner the block reward. Miner also gets the sum of transaction fees from all the transactions included in the block. Every new block contains a hash of the previous block. It is also impractical to modify it once it is in the chain for some time because every block after it would also have to be regenerated.
As bitcoin's blockchain is a public ledger, it is possible to check the block and transactions included in it on the blockchain explorer (eg., see transactions in block 556701 - the first transaction shows block reward together with transaction fees that were paid to its successful miner)
The main data recorded in blocks are transactions. Every block also contains block header, block size and containing metadata.
Block header contains three sets of metadata:
- a reference to a previous block hash - this hash connects this block to the previous block in the blockchain.
- difficulty, timestamp, and nonce
- merkle tree root - a data structure used to summarize all the transactions in the block efficiently
Previous block hash
Every block in the blockchain has its parent block to which it is connected. When a new block is mined by the miner, its validity is checked by all nodes, and it is linked to existing blockchain using previous block hash.
Merkle root is part of the block metadata. It can be used to check if a specific transaction is part of the block. Transactions are firstly divided to pairs, and if there is an odd number of transactions in the block, the last transaction is doubled. The pairs are hashed using SHA-256 two times (double-SHA-256). These hashes are subsequently paired (concetenated) with the double SHA-256 hash of neighbour transactions, and these paired hashes are hashed again. This is repeated which creates merkle tree (or binary hash tree) until one final hash - merkle root (or root hash) emerges. Merkle root always summarizes all transactions to 32 bytes hash no matter how many transactions are there in the block.
Merkle root is a very efficient way to prove that a transaction is part of the block. To prove this a node needs to compute only log2(N) 32-byte hashes. The number of computations increases slowly even when the count of transactions within the block increase rapidly.
Simplified Payment Verification (SPV)
Simplified Payment Verification (SPV) are nodes that do not download full blocks but only block headers. These nodes use the merkle path to verify if transactions are part of the block.
- Andreas Antonopoulos - Mastering Bitcoin