Bitcoin - 2

Useful Websites

blockchain.info - to view current blockchain
bitcoin.org

Distributed Consensus

Network nodes track transactions and record them to the ledger. The transactions are grouped together into a block and this is appended to a block chain. Every node tracks the block chain to ensure there is no tampering.

When a transaction occurs, it is broadcasted to all nodes. The nodes collect the transaction in a block. Nodes need to 'mine' the next block i.e. solve a puzzle and along with proposing the next block they need to provide their answer to the puzzle. Other (honest) nodes will accept the block if the transactions it contains are valid, the answer to the puzzle is correct. They will then include the hash of the block in the next block they propose.

The agreement on a valid transaction and the sequence of transaction requires that there be consensus amongst the nodes. This distributed consensus is achieved within certain constraints -
- no central authority to approve a node as a honest or malicious node
- nodes don't need to reveal their identities

Sybils - are malicious nodes, created as copies of an honest node with the intent to disrupt the distributed consensus process.

Transaction - is a data structure containing the payers signature, instruction to transfer to the payees public key and a hash to the previous transaction in which the payer received the money (which is now being spent).

A transaction is successful when it gets added to a block which is then accepted by all nodes as the next block on the blockchain. Further more blocks should get appended to this block to ensure that this blockchain is the longest chain.

There is a possibility for a group of nodes to append block A to the current chain and another group of blocks to append block B to the chain. This can happen as the nodes are on an unreliable network, there is latency and the sequence of arrival of transactions cannot be guaranteed to be the same for all nodes. When block C is created and appended the selection of one of these two chains is critical, which chain gets selected is assumed to be longer and is taken as the ledger.

A successful transaction is one that ends up on the longest chain, has blocks appended to its own block.

Block Reward - is the reward given to the node which adds a block to the longest chain. This incentivizes the nodes to behave honestly so that their added block is supported by others and ends up on the ledger. The incentive was 50 bitcoins (when bitcoin started), the node that creates the block can append a special create bitcoin transaction which allows paying 50BTC to anyone (usually to itself). This incentive halves every 210,000 blocks, the current incentive is 12.5BTC as 420,000 blocks have been appended. It is estimated that every 4 years, 210,000 blocks will be created. This can be extrapolated, and it's estimated that by the year 2140, the incentive would become zero and a total of 21 million BTC issued to nodes.

Transaction Fees - when a transaction is created, the output of the transaction can be less than the input, the difference being the transaction fee. This fee can be offered to the nodes which batch transactions into a block and propose the addition of the block to the ledger. The incentive is for the nodes to collect the sum of fees for all transactions in a block. The transaction creators will offer this incentive to get a better quality of service i.e. nodes will listen for transactions (with higher fees) and propose to add them to (the next) blocks at the earliest.

All nodes attempt to propose the next block to be appended to the ledger, so that they can get the Block Reward and Transaction Fee incentives. In addition to the next proposed block, the nodes also need to solve a puzzle i.e. find a number (nonce), which when concatenated to the previous hash and the list of transactions that comprise the proposed block, and a hash is taken of this, the output should fall in a small target space. The first node to solve the puzzle, gets to propose the next block in the ledger. Since the output space is small the puzzle is sufficiently difficult. This concept of nodes competing amongst themselves to solve a puzzle is known as proof-of-work or mining.

The target space is revised every 2016 blocks. The target space is calculated so that it takes atleast 10 minutes for a node to compute the nonce, this implies successive blocks are created every 10 minutes. This is to ensure that blocks aren't proposed very close to each other, as that would reverse the optimization benefits of batching large number of transactions in one block.

Since the number of miners is increasing as more nodes are deployed, the cost of mining increases - more hardware needs to be deployed by a miner to maintain its supremacy otherwise it will fall behind in computing power. This ensures that if the majority of computing power of the total mining computing power is honest then the probability that the next block is hones nodes is 50%.

Transaction

A transaction specifies zero or more inputs, one or more outputs and is signed by the user authorizing the transaction. Transactions which represent minting new currency have no input, all other transactions have atleast one input. Each input refers to another transaction i.e. the output of the linked transaction provides inputs for the current transaction. To verify that a transaction is valid, that the input it refers to have not already been used in any other transaction, all that needs to be done is to verify all transactions starting from the input up to the end of the ledger.

A transaction can refer to multiple inputs and create one output that pays the user back to themselves - this is useful to consolidate the payments received from many transactions. Similarly, multiple users can create a joint transaction where they refer to their own inputs and create one output to pay someone.

If the value of inputs is more than the value of outputs, there is difference which can be collected by the nodes as transaction fees. The value of outputs can never exceed the value of inputs.

The inputs and outputs of a transaction are Scripts. These scripts are sequence of instructions which are executed by nodes to verify if the transaction is correct. If a script runs without errors the transaction is considered valid. The scripting language has 256 instructions with 90 instructions disabled or reserved.

Scripts allow creating Escrow transactions. The MULTISIG instruction allow specifying two or more signatures that must be present for the script to succeed. In this case a third party can be involved in a transaction between the payer and receiver, and the transaction script can specify that two out of the three signatures must be involved to redeem the transaction. Thus the third party (assuming trustworthy) allows redeeming the transaction at an appropriate point in time, till then the bitcoins are held securely in the transaction.

The concept of Green Addresses came out of the use of scripts, where organizations acting as banks for bitcoins published their public key allowing users to transfer funds to them, which were to be then sent to someone else as the final recipient. This is useful when the recipient is offline, the transfer can be made to the bank which will transfer when the recipient is available to accept and monitor the transfer to their public key. Companies like Mt Gox started out on the use of these Green Addresses, but when they closed down this concept became less popular.

There are ways to creat micro-payments and payments after 'time t has elapsed'. These variations of payments using scripts is also known as Smart Contracts.

The minimum size of a transaction is 250 bytes.

Block

Each block contains a hash pointer to the previous block (in the blockchain), a hash pointer to the root of a Merkle tree, a Merkle tree containing all the transactions of the block.

Each block has a special transaction known as the Coinbase Transaction. This is the transaction in which miners generate new coins for themselves. The coinbase transaction has a null hash pointer as input, the value of output is the number of Bitcoins the miner can create plus the transaction fees the miner is collecting from the transaction in the block, and a special coinbase parameter which can be arbitrarily set to anything.

The size of a block is limited to 1MB. At max a block can have 1MB/250 bytes = 4000 transactions. Since a block is mined every 10 minutes, there are at max 7 transactions per second.

Network Concepts

Gossip Protocol - a node on the bitcoin network publishes a transaction to all its peer nodes, each node validates the transaction and if the transaction passes all the checks, these nodes publish it to their peer nodes. Each transaction a node hears about, it puts in a pool and if the same transaction is published to the node again it checks this pool to verify if this has already been processed. An already processed transaction is not (re)published by a node to its peers.

Transaction Validation - each transaction is validated against three checks -
- nodes run output scripts of all the previous transactions being referred to, they must all return true.
- the outputs being redeemed should not have already been spent.
- only a small whitelist of scripts is executed, other scripts are processed as invalid.

Double Spend - if the network receives two transactions which attempt to redeem the same previous output, its possible that one set of nodes processes one of these transactions as valid and another set of nodes processes the second transaction as valid. This will lead to two different blocks being published, and the first set of nodes may add the block containing the first transaction and the second set of nodes adds the block containing the second transaction. At this point there are two different block chains in the network. When the subsequent block is added, the nodes will accept one of these two chains as the final ledger. the nodes which see its block/transaction on the ledger will add the transaction to the list of processed transactions and the set of nodes which don't see their block/transaction on the ledger will drop their transaction as it will point to a double spend.

Replacement By Fee - RBF transactions mark themselves as transactions which should be replaced by another transaction if there is a conflict (double spend) and the subsequent transaction offers a higher transaction fee to the nodes.

Block Validation - each new block published by a node is validated by other nodes by -
- validating the header
- validating the transaction in the block
- verifying that the node builds on top of the longest chain

Full Validating Nodes - nodes which validate all new blocks, maintain a repository of all unspent transaction outputs to validate all new transactions and store the entire block chain.

Lightweight Nodes or Simple Payment Verification Clients - nodes which validate only those transactions which are relevant to them, they don't store the entire block chain and all the transactions.

Bitcoin Software Changes

There are two types of software changes -

Hard Fork -
- when new features are introduced to validate blocks which would have been previously considered invalid.
- new software would validate blocks which old software would reject.
- nodes on new software would build a block chain which nodes on old software would reject.
- nodes on old software would build their own block chain which will not contain the 'new' block types.
- the two block chains would never merge.

Soft Fork -
- when new feature are introduced to make validation rule stricter.
- new software would reject blocks or transactions which old software would consider valid.
- nodes on new software would reject blocks due to stricter rules.
- nodes on old software would continue to validate blocks as they are not aware of the new rules.
- nodes on new software would reject blocks which are validated by nodes on old software.
- this will cause two or more block chains to emerge, but eventually nodes on old software will realize their blocks are being invalidate, signalling there is a software update for them.
- eventually block chains will merge.