Bitcoin, Ethereum, and more than 1000 other cryptocurrencies are changing the way we think about money. Even more importantly — from a programmer's perspective — they have also created an extraordinary opportunity for new businesses based not only on cryptocurrencies but also on the Blockchain itself. This technology may be considered as a new, highly effective and secure system for exchanging information between banks, healthcare services, or applications distributed around the world.
In this series of articles, I will present the basics of the Blockchain, Bitcoin, Ethereum, and contracts from a developer’s point of view. They will enable you to understand and start playing with the mechanisms behind the Blockchain and cryptocurrencies. Finally, you will be able to implement your own integration, system, or even a game.
This course is divided into several sections in an order which I would have liked when I first started my Blockchain journey. Each main section will be called a Milestone. Every Milestone will build on the knowledge from the previous one.
Albert Einstein said: "If you can't explain it simply, you don't understand it well enough." I will attempt to explain all the parts as simply as I can and refer to real life as often as I can because I remember how many problems I had with understanding all the mechanisms and algorithms at the beginning.
WHAT IS THE BLOCKCHAIN
Some say that the Blockchain is a chain of blocks. It sounds funny in the beginning, but after the first impression, this sentence brings many questions: "What is a chain?", "What is a block?", "What kind of information does the block contain?", etc.
Imagine this is 1998, and you have just read a paper published by an engineer called Wei Dai, which describes a new electronic cash system that is based on a protocol which does not require any real money or checks to transfer funds from one account to another. This was an era before the credit cards became so popular and before PayPal (PayPal was established in December 1998 as Confinity).
Wei Dai claims that a community should own the money, not one centralized organization, company, or individual, and the owners should be anonymous. All parties are identified only by a number or a public key.
The system described by Wei Dai and inspired by Tim May's crypto-anarchy should cover a couple of aspects in his opinion:
- No one owns the system (neither the government nor a single institution)
In a traditional banking system, one organization — the bank — manages the funds that others put into their account. At this point, the Bank as the owner permits to transfer money from one account to another and confirms this transfer. The bank calculates the current money balance available on the account.
In the result, the Bank can manage the funds in whichever way they choose to — it is possible to reverse or cancel the transfer, block your account, or add some limitations. The question is, if the Bank can do all of this stuff with your account, does it mean that you still own the money?
A decentralized financial system may be a solution for this problem. If nobody owns the system itself, nobody can change the rules or agreements alone.
- It should provide an efficient medium of exchange the money
The main purpose of having money is to exchange it for some goods. Transferring funds from one account into another is one of the fundamental features of any financial system.
Moreover, making a transfer should be as easy and secure as possible. Nobody likes to write a check with all the mandatory information and then confirm in the Bank that this specific check is valid, and the owner has enough funds on his account.
In the best case scenario, the transfer should be a short message that says: "The X-number of B-money, should be transferred from Account A to Account B. Transfer signed by A-Account Owner."
- It should provide a way to enforce contracts
In the real world, all rules or laws from a specific country apply between the transaction parties. In the Blockchain there is no room for interpretation or misinterpretation, it should provide a mechanism based on conditions and numbers. If two or more sides agree to the agreement conditions and all requirements are qualified then the payment or the transfer should be done automatically.
- The system exists because of cooperation of all participants
This rule is an implication from the first one — no one owns the system. That also means that anybody who wants to participate in the system needs to cooperate to keep this system running. All transfers, contracts, and actions should be computed and confirmed by the participants.
- It should be fully anonymous
Because the system is distributed and every participant has a copy of all information, that makes the system quite transparent. However, nobody wants to share all the information about their financial transactions with the world. That means the system should also be anonymous.
The proposed solution is to change the names into numbers and do not add any additional fields to transfers. As a result, the accounts or the transfers cannot be identified, because they are just numbers, i.e., some amount of B-money are transfers from account 1111 to account 2222 signed by 3333.
The protocol did not describe how it should be done, but the general idea, which is to create a decentralized system where all nodes have their own copy of the same database keeping information about all users, transactions and the current state of this system. The question is how to keep those nodes or servers continually working, how to encourage their owners to participate in the system and how to secure the information from all vulnerabilities. And the answer is always the same — money.
Where the money comes from
Usually, in a traditional financial system, the government is responsible for printing and distributing money. In the system where everything should stay decentralized and none should be able to make or generate the money without a clear agreement with other participants, the question where the money comes from becomes pretty solid.
The system cannot be opened with one address that owns and distributes all funds. Wei Dai proposed to reward the users for solving specific, complex calculations with some amount of virtual units — B-money.
The computations should be quite complicated to perform, but easy to confirm by others. Moreover, all the work done by the servers should be useless and worthless, so there may be only one reward for doing them — the B-money or any other virtual currency. I.e., let us imagine a problem to compute that takes 100 hours to solve on a typical machine. Those 100 hours may be rewarded with 1 B-money coin, the same amount of funds that were required to perform the computations.
BITCOIN — THE FIRST BLOCKCHAIN IMPLEMENTATION
In 2008, an individual or an organization called Satoshi Nakamoto published a manifesto that described a possible implementation of a new virtual currency. In January 2009, this cryptocurrency was presented to the world — the Bitcoin. The idea behind Bitcoin was to create a system that meets Dai Wei’s B-money description and provides a solution for all the problems pointed out 10 years prior.
Because Bitcoin was the very first fully-functional and widely accepted blockchain implementation, and because most of the new systems and applications that are working with the blockchain are based on the Bitcoin mechanism, all further information about the blockchain will apply to Bitcoin.
WHAT KIND OF BLOCKCHAIN PROBLEMS DOES BITCOIN SOLVE?
Decentralization and Distribution
Bitcoin is designed as a peer-to-peer network where every node (server or individual computer that has installed a piece of software able to communicate properly with the Bitcoin network) has its own copy of the database that stores information about accounts and transactions between those accounts.
The nodes communicate with each other to synchronize the current state of this information and determine if all pieces of information have been broadcasted properly or whether they should be removed or omitted from the database.
As a result, the Blockchain as a service is owned by the community, nor any organization, government, or company,
One of the reasons why the blockchain is decentralized is security. No single entity, organization, or company is in the possession of the blockchain, so nobody except for the owner should be able to manage the funds.
This approach has pros and cons. From one point of view, nobody can block the account, take some money from it (because they can change the database or the code) or cancel the transfer. On the other hand, if you make a mistake you will not be able to undo it. The money may be lost.
Bitcoin as the first blockchain implementation cryptographically secures the data in the ledger. We will explain exactly how in the next part.
The blockchain idea does not specify if the data inside should be public or not. As far as the people are concerned about the personal information that may be published, it should always provide the option to stay anonymous. Just imagine that somebody publishes your name with a full list of your transactions. That is how the blockchain would look like if it asked for your name.
In most (if not all) implementations that I have worked with, the blockchain keeps only numbers — addresses of wallets, blocks, or transactions. The wallet is not assigned to any user. Everybody who has the password to the selected wallet can access it by passing this password and the address. The result is that one user can have access to many addresses and only they would know to which ones.
All transactions included in the blocks preserve this anonymity too. A transaction can be explained with a simple sentence: "Some amount of funds is sent from address A to address B and signed by address A’s owner." This provides all necessary information and does not publish any sensitive data at the same time.
If the network is distributed around the world and nobody owns it, how to keep it working continually? How to persuade the servers’ owners that they should keep their machines up and running? Promise an income that may compensate the cost of those machines and electricity.
Every block has to be confirmed by one of the nodes that are available in the network. The node calculates the hash from the block and publishes this hash across the blockchain. If other nodes confirm that the block hash is correct, the node which made the calculations is rewarded with some constant amount of funds (these days it is 12,5 BTC per block) and with all fees that were added to transactions in this block (I would say approximately 1-3 BTC per block).
DIFFERENT BLOCKCHAIN USE CASES
Currently, the blockchain supports mostly the financial industry. Almost 1000 different cryptocurrencies are available to share (the full list can be found at https://coinmarketcap.com/all/views/all/. The two most important cryptocurrencies are Bitcoin and Ethereum. They have some differences, but both are working on the same principles.
However, the Blockchain as a technology can support other industries. Imagine services that need to share information with each other, but do not share a common protocol — like the banks and transfers between them. All banks use some third party software to transfer money from one to another, like SWIFT or Fedwire. This increases the fee. The Blockchain solves that problem by unifying the information that can be shared between banks; imagine that every bank server is a node connected to some Blockchain (not Bitcoin, but a dedicated Blockchain). If one bank shares a piece of information about a transfer, every other bank will know about that in a matter of few minutes, without any broker in the middle. Insurance, gift cards, and loyalty programs. Government and Public records, and other systems that are divided and communicating with each other, but do not have a common protocol.
There are 26, if not more, electronic medical records options just in Boston. On the other hand, in my country, the main part of the health record is still kept on paper. Maintaining your own health record may be a huge challenge and just think about all the people living in your district.
Now, imagine that if the doctor writes a new prescription, it is immediately published to other facilities.
The voting process can be supported by the Blockchain technology. Every citizen may be able to vote from their home with a blockchain client. The votes will be automatically published to the network and counted after every mined block.
This is what everybody puts on the resume when looking for a new job opportunity or when they want to continue their education. Build apps that issue and verify blockchain-based certificates for academic credentials, professional certifications, workforce development, and civic records.
Intellectual Property during the Internet era may be a tough topic. Spreading information about the copyrights to a movie, song, or piece of art between countries, or registering a worldwide patent is a long, expensive process because the information about intellectual rights or new inventions needs to be shared across many different databases and systems.
Putting that information in the Blockchain would solve the distribution problem and help with commonizing a knowledge source.
This is one of the most-mentioned features of the blockchain. The blockchain allows one to write simple (in Bitcoin) or more sophisticated (in Ethereum) contracts between parties (two or many). Those contracts are based only on true/false conditions and numbers. It means that if all conditions meet the numbers, the Blockchain automatically transfers the funds.
ICO and Tokens
Another method to use the smart contracts is to distribute tokens. ICO (Initial Coin Offering) projects are meant to raise funds for companies for future investments. The ICO presents white papers where the project is described, and investors can buy tokens (usually called by the ICO name). The tokens are distributed in the selected Blockchain. The most popular one for this usage is the Ethereum.
You can think about the tokens like a sub-currency in another cryptocurrency. If somebody publishes a new token, you can exchange the tokens between investors or sell them, but every transaction requires a fee paid in ETH.
Cat Contracts and Games
Another type of smart contracts is what I like to call Cat Contracts. Some time ago, a new token type was published on Ethereum, and it caused many problems in a particular Blockchain, namely CryptoKitties. CryptoKitties is a game that is based on a smart contract which allows users to buy virtual Kitties, keep them, sell them or reproduce them. The game became so popular that it generates more than 10% of transactions in the Ethereum network.
On the Internet, the biggest challenge will always be the security. From my own professional experience, I can say that there will always be somebody who wants to crack your code. The first thing that comes to mind when you think about a hacker that can change the state of the Blockchain is stealing the money. On the other hand, let us take another example — the gun-tracking system — and imagine what may happen if somebody unregisters or re-registers a pistol that was used to commit a crime.
One of the main concerns regarding the Blockchain is the double spending problem — some amount of money is sent from one account to another, and then, the same money is sent to a third account. Bitcoin solved this problem by cryptographically connecting the newest block with the previous one and with a mining mechanism which calculates a hash number from all transactions included in a block. This mechanism will be described in the chapter dedicated to Bitcoin.
One of the methods that allow one to omit the cryptographical bridge is to possess more than 51% of all nodes in the network which will confirm the blocks. This may be pretty hard to achieve if you consider that the current hash rate is 13,523,349.50 TH/s and a mining machine which handles approximately 14 TH/s costs around $8000.
The complexity of calculations in the mining mechanism also prevents the brute-force attack vector. If somebody tried to publish all the possible hashes that may be accepted by the Blockchain and expect a miner's reward, I would say that:
- Supposing you could generate a billion (2^30) per second, you need 2^130 seconds.
- Doing this in parallel using a billion machines requires only 2^100 seconds.
- Getting a billion of your richest friends to join you gets it down to only 2^70 seconds.
- There are about 2^25 seconds per year, so you need 2^45 years.
- The age of the Universe is about 2^34 years so far — better get cracking!
- Is it possible to brute-force bitcoin address creation in order to steal money?
On the other hand, there is a running project called a Large Bitcoin Collider, which tries to guess or brute force the private keys to selected wallets containing some Bitcoin, and they have already broken some of them!
In this part, you learned about the basic concept of the Blockchain and its general idea, and got a historical overview. In the next section, I will explain how Bitcoin works and describe all terms necessary to start working with it.