Friday, May 25, 2018

Block Chain

Have you ever wondered how to send a payment by email? Did you ever question the commission charged by banks and other financial agencies for transfer of money? Do you want to vote electronically from your computer? If the answer to all these questions is yes, then you want to know about a block chain. A block chain is a network of computers –aka nodes-- each of which maintains records of transactions—called blocks—to enable transfer of digital currency and assets over the internet. Before we dive into the subject, let us first review the current banking or financial industry where moving money is routinely carried out. The financial institutions use a ledger to keep track of transactions and current balance of accounts. Let us say, Rob with an initial balance of $100 sends $50 to Pete with an initial balance of $200. The ledger after the transaction is processed will be updated to show Rob's current balance $50 and Pete's current balance $250. However, if Rob with an initial balance of $100 decides to send Pete $100 and Sally $50. This should be denied by processing the transactions serially, i.e. one at a time. This is called double pay.

The problem with financial institutions

By keeping the ledgers in a central server, the financial institutions are vulnerable to attacks by hackers. If the ledger is corrupted, there is no easy way to fix it. Also financial institutions charge commission on transactions to compensate for their enormous storage and processing needs. On a small transaction like $100, a 2% commission might look normal. But as such transactions happen over millions of times, one can imagine the extent of the cost incurred by the initiators of the transactions. Some estimate that to be about $6 billion a year. Besides, the transactions won't happen instantaneously and typically take several hours to process. Suppose one sends dollars from USA to India by a bank transfer. The bank in USA posts the transaction on the gateway in USA. The US gateway contacts the gateway in India which in turn contacts the bank in India. Then a currency conversion is done from dollars to rupees. If there is a way to pay the end user, the transaction will go through. Given the time zones and delays in the transmission, the simple money transfer takes several hours.

centralizedbanking.png

Block chain efficiency

A block chain transfers the money or any electronic asset almost in real-time. Also the commission or fee charged by a block chain is much smaller than that of financial institutions. One can add a fee to the transaction in a block chain to incentivize the miners which will be discussed later but that is only optional. Another important aspect of block chain is the ledger is distributed. Every node in the block chain has its own copy of the ledger. This does away with the centralized ledger and monopoly of the financial institutions. The advantage of distributed ledger is that even if a node or a group of nodes is made off-line, say by hacking, the rest of the network has the complete ledger. It is next to impossible for all the nodes in a block chain to go off-line or without the ledger. Thus a block chain ensures that the list of transactions or the ledger is available for all to see and update. The key is the ledger can only be updated but never truncated or edited to modify an older transaction.

distributed ledger.png

Different types of computing

Centralized: where a single server acts as the point of contact for information storage and retrieval. Examples are facebook, google, amazon, etc. While this is easier to implement, the problem is it won't scale. For example, the number of transactions a server can handle is limited and the availability of storage too is limited.

Distributed: A centralized system can be distributed. Any of the tech companies mentioned before have geographically spread out their centralized systems in a distributed manner. So the server accessed from India is different from the server accessed in USA. As long as the data on the two are kept in sync in real-time, there is consistency in the services.

Decentralized: the centralized and distributed models are pervious to denial of service attacks. Whereas a decentralized system, like bitcoin's block chain, is spread over hundreds and thousands of computers that are virtually impossible to hack. In a decentralized system every node acts as a peer which means every node has the same data as its peer node. So when a node is hacked, the data is not lost and can be retrieved from another healthy node.

Cryptocurrency

Block chain is based on the notion of cryptocurrency which is nothing but a made up currency just as dollars, rupees and pounds. Let's call it e-money. The participants in a block chain has initially an electronic wallet with certain e-money. They can purchase the e-money with physical currency for instance. As they transact with each other the balance in the wallet will be recalculated. The bitcoin block chain, that went online in 2009, has bitcoin as the cryptocurrency. There is an upper limit on how many bitcoins can be issued. It is 21 million at this time (the account holders on the bitcoin network can vote to increase this limit). It is called cryptocurrency because it can only be created by an activity called mining which is based on encryption technology. Miners are nodes in the block chain that compete to validate transactions by solving complex math puzzles based on cryptography. As an incentive to the validation effort, the first miner that has successfully validated a transaction will receive, say 12.5 bitcoins. Also the winning miner gets to add a block to the chain of blocks by creating an id called nonce – which is a unique number that is used only once in the block chain—and a hash – an alpha-numeric identifier that points to the last block in the block chain. Calculation of nonce and hash (especially if the first few digits of the hash have to be zeroes as some block chains need) is a time consuming process. Besides the miner may have to start from the beginning of the block chain, not necessarily always, to compute say Rob's current balance before his transaction to pay Pete can be validated. This too involves computations that can span over several minutes. Hence the miners need to be reimbursed for their efforts which is based on the cryptocurrency or e-money.

blockchainabc.png

Consensus

What happens when someone creates a block with fake transactions and adds it to the block chain? Since blocks are stored in nodes by a process called distributed computing each node gets to vote on the legitimacy of the block. If a majority votes that a block is fake, then it will be removed from the block chain. And all the nodes will be updated. So the block chain is self-correcting by using distributed computing. This way the central ledger of a financial institution which is vulnerable to hacking is replaced with a distributed digital ledger that is impervious to hacking.

Land titles

A typical use case for block chain is land titles. Let us say the last known date of a land title is 1810 and the land has changed several owners. Until now, the records would be kept in physical repositories that are vulnerable to theft, fire and flood. If a land title transfer record in 1900 is lost, then there is no way to replace it and it is not possible to validate the seamless transfer of land ownership. Also someone could introduce a fake title in 2018 and gain possession of the land. Of course, by scanning the physical documents into digital format some of these issues can be mitigated. Still, a centralized storage of the digital information is vulnerable to hacking and misuse. A block chain will store the title repository in a distributed computing model and prevent all fraud.

Smart Contracts

One of the block chain implementations called Ethereum uses what are called smart contracts. A smart contract is a dynamic computer code. Suppose you run a company and want to use a block chain to pay the salaries of your employees. You can write a smart contract to implement the following pseudo code

If employee is a manager, then pay x% of the earnings plus $5000 
else if employee is a worker, then pay minimum wage plus y% of the earnings 
and so on.

Once a smart contract is created it exists for ever on the block chain and is immutable. Suppose you want to change the commission of manager from x% to z%, then a new smart contract has to be written. The old contract that is replaced would not be deleted but made inactive. This way we have a record of the history of salary payments made by your company. A tax auditor, for example, might be interested in your company's salary structure over the years and a block chain serves as the proof.

smart contract.png

Why can't we send money by email?

If you send some e-money by email to Pete, you can easily copy and paste the email to multiple recipients. The question is do you have enough e-money to pay for them all? Who is validating it?And how are they doing it? If you can generate e-money as you wish, then there will be inflation, due to increased money supply, and decrease in purchasing power with your e-money. With this now you understand why a block chain with mining is needed. In a block chain the generation of e-money is controlled by the mining activity which serves as incentive for proof of work. This is like in real world you get paid by your employer in return for work done, except that the proof of work in a block chain is done by employing algorithms and computer hardware.

Public and Private Keys

Each electronic wallet is made of public and private keys. They are based on a type of math called cryptography. A public key is what you share with others when you want to receive e-money. It is an alpha-numeric string like 0x23yu7Ia. This is like your name and address that is unique. Associated with each public key is a private key that is like your password. When you want to send money you use private key to encrypt and broadcast the encrypted transaction to the block chain. This way only you can send money from your electronic wallet and no one else can.

Different types of block chains

A block chain essentially is distributed computer storage and computing spanning over the internet. Just as web has revolutionalized the way we store electronic files, the block chain would do the same for electronic payments, land titles, voting for an election, etc. One can envision multiple block chains for different purposes. Some call it the Web 3.0 where different computers come together to participate in the digital economy based on block chains. Web2.0 is the conglomeration of web services provided by Amazon EC, Microsoft Azure, etc.

One can also envision public, private and hybrid block chains where the participants can vary based on the type of block chain. In a public block chain anyone can join and begin transacting. Such is the case with Ethereum. A private block chain can be set up by a financial institution for its members only. A hybrid block chain will have a mix of public and private participants whose main interest is to transact digital assets electronically at the speed of the internet.

The memory crunch

If a new node has to be created in a block chain, then the node has to download all of the blocks that were ever created on the block chain. This is a time consuming process. Also the blocks are usually stored in physical memory, not an off-line database, to make them available for transactions. This can be very expensive both money and computation wise. If you are a miner, who is not interested in initiating transactions but earning e-money by validating transactions, then it is worthwhile provided you have an efficient algorithm and powerful computer hardware to search for hashes in the blocks. So it is not unusual to find groups of miners pooling their resources to make an investment in expensive computer hardware and other infrastructure needed for mining.

Smart contracts based on code have to face the same scrutiny as programs that are not NP-complete, i.e. that won't terminate or take enormous resources to carry on their computation. In many cases, it is easier to run them on a subset of nodes than on each node in the entire network. For instance, a smart contract to handle voting for a candidate is better run on a single node where the results of votes cast for each candidate can be kept in memory. Each voter can be assigned a unique hash and allowed to vote only once. What makes the voting process tamper proof, ultimately, is that the code in the smart contact is shared within the block chain and can be verified by all the interested parties. Contrast this with electronic voting machines (EVM's) that are used in India whose software is not open for auditing.

The Haves and Have Nots

The proof of work is the central piece of block chain that is going to decide the winners and losers. But it is so hard and takes so much of computing power that people are already suggesting other kinds of proof to substitute proof of work. Some of them are proof of stake, proof of burn, etc. In proof of stake a wealthy miner or a consortium of miners can add a new block based on their reputation. For instance, in a real world scenario if two banks are willing to finance a project at the same interest rate and you have to pick just one bank, which one do you pick? That's where the balance sheet comes into play. The bank with the best balance sheet would win assuming a balance sheet is based on a bank's reputation. In proof of burn, the miners are actually going to spend their cryptocurrency. In real world, this is like engineers coming up with models of their designs by spending some of their money to attract investors. All of these methods that are alternatives to proof of work are going to make the block chain a highly contested topic in the future.

Online References

https://bitsonblocks.net/2016/02/01/a-gentle-introduction-to-smart-contracts/

https://www.youtube.com/watch?v=5Tr13l0O1Ws is a video presentation of proof of work coding and alternatives to proof of work

No comments:

Post a Comment