Sunday, February 15, 2015

Bitcoins Simplified - II The Blockchain

This is the second part of the Bitcoin Series. If you haven't already, please refer to Part I.

In the previous part we looked at how ownership and transfer of ownership is implemented in Bitcoin. We left off with the question, how do you ensure that Mark only spends Bitcoin X only once? What is to stop him from sending the same Bitcoin X to multiple people?

When you own an account at a bank, the bank ensures that you can only spend money that you own. The bank in this case acts as the central entity that enforces the correct set of rules on your bank account.

The Blockchain
In Bitcoin, the entire process is decentralized. The bitcoin network is a peer-to-peer network of multiple nodes running the Bitcoin client. All these nodes, in conjunction, maintain and validate a distributed ledger, called the blockchain, to enforce the rules. This is a central part of the innovation behind bitcoins.

  • The blockchain is a chain of blocks.
  • Each block contains a list of transactions.
  • As new bitcoin transactions are generated, they are captured together into a new block and the new block is added to the blockchain.

As can be seen above, each block contains a link to the previous block's hash, thus linking the blocks in the chain. The blockchain, thus, records all the Bitcoin transactions, right from the birth of Bitcoin. This way all the nodes in the bitcoin network know when Mark has spent the bitcoins that he owns, thus invalidating any subsequent double-spend transactions by Mark.

Proof of Work
Now the question is, since any node in the network can add blocks to the chain, how do you prove that someone malicious did not add an incorrect block to the chain? Or worse, fork the blockchain and create their own set of blocks thus misguiding everyone.

This is ensured by enforcing the following simple rules:
  1. adding any block to the blockchain takes a significant amount of processing (CPU) power (proof-of-work). While at the same time, validating any block in the blockchain is extremely trivial.
  2. if the blockchain is forked, all the nodes in the Bitcoin network pick the longer branch of the blockchain, ignoring the smaller branch.

Assuming that majority of the processing power in the Bitcoin network is controlled by 'good' nodes, this makes it extremely hard for malicious nodes to fork the blockchain. This is because the total processing power of the good nodes, will keep extending the blockchain further, while malicious nodes fail in their race to fork the blockchain since the forked branch will always be shorter. [1]

The process of adding new blocks to the blockchain is called bitcoin mining.

Bitcoin Mining
So all bitcoin miners are always in a race to find the appropriate proof-of-work with other miners in the network. This involves significant computer power. What is the incentive for these miners to expend this computing power?

The miner who added a new valid block to the blockchain gets a reward of new bitcoins. This not only acts as an incentive for miners, but also ensures a steady and predictable supply of new bitcoins in the system. [2]

Isn't all of this fascinating?

Additional Notes:
1. The proof-of-work algorithm makes the bitcoin miners choose a value for a set of fields in the new blocks header, such that the hash of the generated block satisfies a difficulty target. This criteria is that the hash of the block should be less than a defined target value. This makes the bitcoin miners try all the possible values in the proof-of-work fields and check the hash of the block to match with the current difficulty target, thus expending processing power. The verification process is trivial because all it does is computes the hash of the newly added block with the difficulty target.

The difficulty target of the bitcoin network keeps incrementing to keep up with the advances in hardware of the miners. The difficulty target is so adjusted that it takes an average of 10 minutes to mine a new block.

2. The current reward is of 25 bitcoins. The reward halves after every 210,000 blocks that are mined.

Acknowledgements: Thanks to Danny Hamilton for his corrections.

Saturday, February 14, 2015

Be the customer

I have learned this time and time again. 

Yes, you have brain stormed on the new features for a long time, and yes it is an elaborately well thought-out plan. But please,

Be your own customer.

Start by writing the User Manual for the feature that you envision before the development begins. If you are going to release the feature over multiple iterations, write the manual for all the customer-visible iterations. This helps you shed the developer mindset, and forces you to really be in the customer's shoes every step after step. Obviously some activities are exploratory in nature, and don't quite fit in this format. But most of them usually do.

Secondly, start using the generated product as early as you can. And use it like a customer would to solve her problem. This not only influences your scheduling, to have workable versions earlier in the development cycle, but also helps you identify / fix the minor annoyances, that make your product truly stand out. Depending on the nature of your product, you may have to go to great lengths to really enable this.

Easier said than done... :)

Bitcoins Simplified - I

For the past few weeks I have been reading up and studying the technology behind Bitcoin. I have found this exploratory activity to be truly exciting, and I have often admired the application of technology to solve this problem.

Here is my take at simplifying the technology behind the working of bitcoin:

Bitcoin is
  1. electronic currency: it does not rely on electronic tokens rather than physical tokens to represent money
  2. decentralized (peer-to-peer): there is no central authority (like a central bank etc.) responsible for creating the currency, or responsible for managing transactions of this currency

Let's begin by assuming that there is such a currency in an electronic form, then look at the various problems that such a currency would pose.

Since the currency is electronic, how do I prove ownership? Anybody could make a copy of my currency and claim that it is theirs.
If your mind is thinking about PKI, you are in the right direction. Digital signatures have been used for a long time now, to ensure the authenticity of the source of a message. The same principle is used for signing transactions. Since I am the only one who has the private key, I am the only one who can sign the transaction. That proves ownership.

In the example above, Mark owns bitcoin X. When he performs a transaction, his transaction will be treated as valid, since he can generated a valid signature. Larry cannot spend Bitcoin X, since he cannot generate a valid signature for bitcoin X.
You may be wondering how exactly would you know what public key should be used to validate the signature. Hold on to that thought, we will come to it in a minute.

Transfer of Ownership
Ok, now how do I transfer that ownership to someone else? What signifies ownership in this case?
I know the answer is forming in your head :).

Since we are using digital signatures, my signed transaction should include the 'public key' of the recipient. Thus the public key of the current owner will be encoded in the bitcoin transaction.

In the example above, Mark transfers Bitcoin X to Larry. He does so by including Larry's public key in the transaction.

So what does this mean for Larry, when he is spending Bitcoin X? He should sign the transaction with a private key that corresponds to the Public key that was encoded in the previous transaction. Only that will allow him to unlock/transfer the Bitcoin to the next recipient. This is shown below.

In this transaction, Larry transfers Bitcoin X to Elon. Larry's signature can be verified by anyone, since it is already encoded in Bitcoin X's previous transaction. After this transaction, Elon now owns the Bitcoin X.

So far so good, but the biggest question is, how do you ensure that Mark only spends Bitcoin X only once? What is to stop him from sending the same Bitcoin X to multiple people? This is where the distributed ledger concept of Bitcoin kicks in. It is a central part of the bitcoin innovation. More on that in a follow-on blog post.

Sunday, February 01, 2015

The Release Rush

I am an avid fan of Masterchef Australia!

I love watching the excellent participants, working on their culinary recipes, race with each other towards the deadline.

I wondered why they all feel so rushed up towards the finishing line often finishing only seconds, before the deadline.

It happens no matter how long the time slot is, or how simple the cuisine is. And it happens to every participant, even the most expert cooks, with a spectacular plan and meticulous execution. Why so? Bad planning?

And then I wonder, how I would feel if a participant finishes 10 minutes before their deadline. They finished alright, but could they have delivered more? Did they take it too easy? Not just for the sake of competition, but for realizing their own potential.

The goal is not just to deliver, in the given time. That is not success.
The goal is to deliver your best, in the given time. That has the potential to be a success.