The financial transactions on the internet have exclusively been done through third party services, mainly Visa, Mastercard, Paypal and other banking institutes. The core basis of such a system is trust. We trust the institutes to store, verify and process our electronic payments.
These institutes take various measures to mediate the expense of maintaining the trust-based system, which in turn increases the cost of transaction in the financial network. These measures have hindered fair participation in the economic activity of individuals and businesses either by refusing participation, demanding identification or via other unfair practices. This system of money is different than what we had for centuries - economic activities via cash or coins. But today that has been replaced by a third-party trust.
Electronic payment based on mathematics:
Satoshi Nakamoto's proposal of Bitcoin in 2009 is considered to be the first digital, decentralized currency based on mathematics instead of trust. Bitcoin is a peer-to-peer network, which means that transactions take place directly between users and without a trusted third party. It is decentralized, which means that no one is in control of the "system" and everyone can openly participate in. It is digitally scarce, which means that no one can copy and spend the same coin twice. These technologies were unprecedented in the field of computer science at the time, and a combination of revolutionary inventions made the possibility of creating a decentralized peer-to-peer digital currency - Bitcoin.
What is a transaction in a banking system (centralized network)?
A transaction is a request to move $X from A to B. The system completes the process by reducing A's balance by $X and increasing B's balance by $X.
In a banking system, this is easy to execute and maintain. Every time a user tries to spend their money, the network checks if the user has the required balance. If so, it deducts the amount from their account. If not, the network does not allow the transaction because of insufficient balance.
What is a transaction in a bitcoin network (decentralized network)?
Transaction is not straightforward in a decentralized network because of the nature of the network. In a decentralized network, no one "machine" holds the absolute truth. Every machine in the network has their own version of truth, and they all find consensus on one version among them.
Different types of network: Centralized, Distributed & Decentralized.
The bitcoin network uses a distributed network of nodes* which verifies transactions via the use of cryptography and public distributed ledger called the blockchain. A blockchain is a public balance sheet that records all the bitcoin transactions that ever happened. It means that all the participating nodes in the network maintains copy of the blockchain & together agree on a set of updates to the blockchain.
*= computers and/or machines participating in the network.
Blocks are replicated by all the nodes in the network, it looks something like this:
Technically, transactions happen between bitcoin addresses, not users. A bitcoin address is a long string of alphanumeric characters with an associated public/private key pair. Think of a private key as the actual password that you only know. A digital signature can be generated only by using this key and is used to prove the ownership of a transaction. More on that later.
How updates happen in the blockchain?
When a transaction occurs between users, it is broadcasted to the entire network. The nodes validate and add them to their copy of the blockchain and it is then broadcasted again to the other nodes to notify them about the update. To maintain sanity in this network, every ten minutes, a set of transactions (called - a block) are "accepted" or "confirmed" and added to the blockchain. The nodes publish accepted block to the network, and the cycle continues.
Chain of blocks - blockchain
What is double spending?
In a physical cheque, a signature verifies the ownership and validity of the transaction. It proves that the owner wrote the cheque and is willing to transfer money from his account to the recipient. But in a bitcoin network, an owner transfers the coin by digitally signing it and attaching it to the transaction.
A digital signature looks something like this:
The payee can verify the signature to confirm the ownership by using cryptographic magic. But the problem is when the payee wants to validate if the payer has already spent the coin promised to him, i.e., if the coins are being double spent. The payee needs proof that at time of the transaction, his transaction was the first & other transactions should be processed in the correct order. If the payer tries to spend the "same" coins more than once, the system should deny them.
How does bitcoin network prevent double spending?
The bitcoin network solves this problem by using the blockchain and a consensus process called the proof-of-work.
We know that nodes in the network are aware of all the transactions and they all have a single history. The proof-of-work mechanism expects nodes to expend energy to update the blockchain. Use of electrical power is a way of proving that they have done the work to get the result. They solve a hard (but feasible) computational problem that is quickly verifiable but is extremely time-consuming to produce.
This means that a node will have to expend energy if they want to make a malicious update in the blockchain. Their computing resource must be higher than all the honest nodes in the network. It is mathematically almost impossible.
What is this hard computational problem?
A newly formed block contains a nonce value as a proof of work. A nonce is nothing but a number that when hashed with the block's body produces a "specific output" expected by the network. The system requires the constant hash to have a certain number of leading zeros to be accepted.
proof-of-work: finding the nonce value
Because SHA256 Hashing is completely pseudorandom function, the only way to create a block is by trial and error method with random numbers. The process to verify with a nonce is easily verifiable, but it is almost impossible to predict a nonce value. The average work needed is exponentially difficult with more required zeros.
Inside a block
What happens if two nodes find a nonce value?
The first node that finds the nonce value broadcasts it to the network and starts working on the next block on the chain. There will be times when more than one node finds the correct hash or the new block doesn't reach all the nodes in the network. This leads to several branches being created at the same time. It looks something like this:
Several branches created during mining
The proof-of-work represents that the longest chain has the most work done regards to CPU energy. Hence, as soon as a node receives a block with the longest branch, it ignores all the shorter branches and starts working on the new block.
Note: A node will always choose the first block it receives from the other nodes and keeps other blocks in its cache.
Mining is the process of repeatedly collecting, validating and updating broadcasted transactions in the ever-growing blockchain to ensure that everyone agrees to the order of the transactions. The miners, which are the nodes in the network, are the ones keeping the blockchain consistent and secure. Since there is no central authority issuing the coins, the system incentivizes the miners with new bitcoins every time they succeed in mining a new block, i.e., solve the hard computation problem and add a block to the blockchain. This is the only way new bitcoins are created in the network.
Note that miners are different to users (like you and me) who just use the bitcoin network to transfer bitcoins.
Nice to know concepts:
A Merkel tree is a type of binary tree with data at the leaf nodes. The intermediary nodes are made up of a hash of the two children. The hash propagates upward to the top of the tree which helps verify fraudulent transactions, as a small change in data will change the hashes of all the nodes above. Merkel tree provides a very efficient method to identify if a block is different from the other blocks in the chain.
Another protocol called the “Simplified Payment Network” (SPV) downloads only the block headers and the branches of the transactions associated with it. It helps secure the blockchain & provides back the balance by downloading a small portion of the blockchain.
Visualizing merkel tree inside a block
The Bitcoin whitepaper proposes a unique and revolutionary system for transferring electronic payments without a trusted third party. A network of nodes with their own copy of the transaction history - the blockchain, maintains consistency in the system; digital signatures authorize transactions & ownership; proof-of-work secures and makes it computationally impossible for attackers to tamper the history of transactions.