Lesson 14

The Blockchain

VIDEO: Bitcoin Transaction Details - Part 2

Centralized ledgers

1. Asset and Transaction Ledgers

In many situations one needs to keep track of "who owns what". This is usually done in a ledger. A ledger is a database, that keeps record of "who owns what". This can be done for every kind of object or asset, be it apples, money, land or whatever you can imagine.

A ledger is required that you will know whether an asset (e.g. an apple, a piece of land or 100 Euros) are considered to be mine. This concept is of course not limited to physical goods, but would also apply for digital "goods" like world of warcraft gold. Or Bitcoins. 

Centralized Ledgers
This concept of a ledger is familar to most people, as it is well known from daily life. If you think about how a bank works, you are used to a centralized ledger. Centralized ledgers usually have a central and trusted record-keeper that is responsible for keeping track of "who owns what". In most cases this has to be a centralized authority or institution, as it is not feasible to allow everybody to just edit this ledger in their own responsibility. Just imagine you could edit your own bank account balance at will. Or you could edit yourself the land registry and assign ownership of land or delete the mortage on your house.

This is why in practice it used to be the only practical way to give this authority to a trusted party that is in charge of ledgers.

Centralized ledgers, however, also have certain disadvantages. Since one central institution or person is in charge for the record-keeping, it is always the most vulnerable point.

Record-keepers might in practice not be as trustworthy as they are supposed to be. It is for example possible for the record-keeper to change records at its own initiative.
Also, central record keepers are the single point of failure. This means that they can potentially lose important information, like the transaction records. This can happen not only intentionally, but also simply by accident (natural disasters, fire or similar incidents).
Lastly, the central record keepers can define arbitrary rules in order to exclude certain people, for example banks can deny bank accounts to people below a certain credit rating.

In the previous lection we discussed how do transactions work and now it is a time to see how transactions are stored. Afterwards, we will see what is going on during mining and how (and more importantly why) proof-of-work is constructed.

What is the blockchain?

Transactions are recorded into a distributed public database known as the block chain, with consensus achieved by a proof-of-work system called "mining".

The network timestamps transactions by including them in blocks that form an ongoing chain called the block chain. Such blocks cannot be changed without redoing the work that was required to create each block since the modified block. The longest chain serves not only as proof of the sequence of events but also records that this sequence of events was verified by a majority of the Bitcoin network's computing power.  To modify a past block, an attacker would have to redo the proof-of-work of that block and all blocks after it and then surpass the work of the honest nodes. The probability of a slower attacker catching up diminishes exponentially as subsequent blocks are added. As long as a majority of computing power is controlled by nodes that are not cooperating to attack the network, they will generate the longest chain of records and outpace attackers.

Each block includes the previous block hash as input for its own hash. This dependency of one hash on another is what forms a chain, with each additional block providing evidence that each of the previous hashes existed.



Image 1. Structure of the blockchain


Each block contains

  • A timestamp
  • The hash of the previous block as a reference (except the Genesis Block)
  • At least one transaction: The coinbase transaction and any other that were validated
  • The Merkle Root
  • The block's own hash
  • Difficulty statement


Below we will review in detail what information is included in the block. For more details see http://codesuppository.blogspot.com/2014/01/how-to-parse-bitcoin-blockchain.html


Structure of the block



Image 2. Content of a single Bitcoin block.

Block Header : Section B1 : Magic ID

The first 4 bytes of a block in the blockchain file is a 32 bit header referred to as a 'magic id'.  This is simply an identifier to let us know that we are at the beginning of a block.  This magic ID will always be the number 0xD9B4BEF9.

Block Header : Section B2 : Block Length

The next 4 bytes of the blockheader contain the length of this block.  Currently the maximum size of a single bitcoin block is 1mb; which is only big enough to support approximately 7 transactions per second.

Block Header : Section B3 : Version Number

The version number in the blockchain is always set to 1 in all cases.  

Block Header : Section B4 : Previous Block Hash

This entry contains a 32 byte hash of the 'previous' block.  Think of this like a linked list.  Each block in the blockchain points to the 'previous' block.

Block Header : Section B5 : MerkleRoot

The next part of the block is a 32 byte hash called the 'merkle root'.  It is an optimization feature used to rapidly access transaction data without needing to have full access to the entire blockchain transaction history in memory.

Block Header : Section B6 : TimeStamp

This is the value indicating when this block was created.  

Block Header : Section B7 : Target Difficulty

This value represents the target difficulty for this block. More details will be in the Mining lecture.

Block Header : Section B8 : nonce

The 'nonce' value is a number that was found by the miner and is below the target difficulty. More details will be in the Mining lecture.

Input Section : I2 : Transaction Index

If the transaction index is non-zero, then it refers to a particular output in some previous transaction. It is important to remember that inputs always completely spend all of a previous output – we discussed that fact into “How do transactions work?” section.

If the transaction index is 0xFFFFFFFF then it means that this input refers to no previous output – this happens when miner mines a block and gets a reward.  

Output Section : O1 : Value

This 64 bit unsigned integer represents the output value measured in 'satoshis'. One satoshi is equal to one hundred millionths of a bitcoin.

Output Section : O3 : Output Script

A script is a list of instructions stored with each transaction that describe how the recipient will gain access to bitcoins. To spend bitcoins from a typical transaction the spender must provide

  1. a public key that, when hashed, yields destination address embedded in the script, and
  2. a signature to show evidence of the private key corresponding to the public key just provided.

Scripting provides the flexibility to change the parameters of what's needed to spend transferred bitcoins. For example, the scripting system could be used to require two private keys, or a combination of several, or even no keys at all.

The public key in the output script can be stored in one of two forms. Either as the full 65 byte public key or as the 25 byte hash of the public key (20 byte hash of the key, plus 1 byte prefix, plus 4 bytes checksum – thats is the reason why it is almost impossible to make a typo and transfer money to a random address). Majority of all transactions store the public key in the 25 byte form both to save memory and for added security – public key itself is not exposed which makes impossible to guess the private key even having quantum computer :)

Transaction Section : Transaction Lock Time

The final data item at the end of each transaction is called the 'transaction lock time'. It represents the time until transaction is considered invalid. Transactions with lock time less than current cannot be included into the block, and this parameter is extensively used into smart contracts, for example backup transactions for multi-signature wallets.


Orphan blocks

An orphan block naturally occurs when two miners solve the same block at roughly the same time. They are both technically 'valid' but only one of the two blocks will be accepted into the main chain. We will see how this situation is solved when we discuss mining process specifically.


Image 3. Blocks in the main chain (black) are the longest series of blocks that go from the genesis block (green) to the current block. Gray blocks are blocks that are not in the longest chain and therefore not used (orphan blocks).



comments powered by Disqus

You will get an awesome place to trade your
Bitcoin and more. Click for further information!

Choose your topic

and start the journey