In today’s post, we will look into the architectural details of the blockchain technology. In doing so, we will touch upon the concepts of Smart Contracts and later on, about the internal details of a block that comprises the Bitcoin blockchain.
Interesting project but no money ☹️
Let us say your friend Alice has an interesting project idea and she wants to start it as soon as possible but she has no (or little) funding to start it. She is very upset and then as a good friend you come to her rescue (A friend in need is a friend indeed 😊). You suggest her that she should crowdfund the project. She is now intrigued and inquires about it.
What is crowdfunding and how it works?
Put simply, crowdfunding is the process of getting a large crowd of people to pay a small amount of money to invest in an idea or product, with the promise that when the said idea comes to fruition, they will get first access to it. The idea can be anything, from an innovative piece of tech to a video game to a novelty gift.
- Both the product team and the supporters need to trust the crowdfunding platform (or a Kickstarter).
- The product team expects the money to be paid as the project progresses.
- The supporters expect the money to go to the project.
As it is clear that this is a centralized system and the major drawback is that some monetary benefit goes to the Kickstarter for their efforts in managing the entire process. The solution to this problem leads us to the concept of “Smart Contracts” which is a decentralized version of the same platform.
In 1994, Nick Szabo realized that a decentralized system could be used for smart contracts or self-executing contracts. Here, contracts are converted into computer code, stored and replicated on the system and supervised by the network of computers that run the blockchain.
Thus, smart contracts help us exchange money, property, shares, or anything of value in a transparent, conflict-free way while avoiding the services of a middleman.
The best way to describe smart contracts is to compare the technology to a vending machine. Ordinarily, you would go to a lawyer or a notary, pay them, and wait while you get the document. With smart contracts, you simply drop a bitcoin into the vending machine (i.e. ledger), and your escrow, driver’s license, or whatever drops into your account. More so, smart contracts not only define the rules and penalties around an agreement in the same way that a traditional contract does, but also automatically enforce those obligations.
if we apply Smart Contracts to our example of crowdfunding, then the following things will happen –
- The contract is written in a “code” which is available to the stakeholders – product team (Alice) and the supporters (Note – a typical example of a blockchain)
- The code automatically transfers money as certain goals (contracts) of the project are accomplished.
- If the project goals (or contracts) fail, then the code will transfer money back to the supporters.
Advantages of Smart Contracts
- Immutable – No party will be able to change the contract once it is written to the public ledger.
- Distributed – Every stakeholder can validate the contract
Let us look into these components one by one –
The header contains metadata about a block. There are three different sets of metadata:
- The previous block hash – in a blockchain, every block inherits from the previous block as it uses the previous block’s hash to create the new block’s hash. For every block N, the hash of the block N-1 is used.
- Mining competition – each block in a blockchain has the timestamp, the nonce and the difficulty. It should be complicated enough to make the block tamper-proof. The nonce is a random variable that makes it possible for the hash of the Nth block with predefined complexity. For e.g., for bitcoin, this complexity is that there should be 20 zeros at the prefix.
- Merkle tree root – data structure that summarizes the transactions in the block.The root is the verification of all the transactions. I discussed Merkle algorithm in detail in my first post here.
If you look at the above figure closely and notice the word “over”, you will find that the hash function changes drastically. This is called Avalanche Effect. This is very important from the security aspect as if the message is encoded, it makes it very hard for someone to guess the message.
Transactions are organized as a Merkle Tree whose root is used to construct the block hash. A Merkle tree is constructed by recursively hashing pairs of nodes ( in this case, transactions ), until there is only one hash, called the root or Merkle root. If we stay in the Bitcoin world, the cryptographic hash algorithm used is SHA256. This is applied twice each time.
This link discusses the Merkle tree in more detail.
This provides us the capability to determine almost accurately if someone tampers a block. If some malicious attacker tries to tamper one transaction, then the hash of that transaction will be changed and thus there will be changes in the hashes of the subsequent blocks. Thus complexity of determing the hash of a block is so tough that it would take a lot of computing power and time to corrupt the subsequent blocks before a new block is generated. Thus, it is almost impossible for a attacker to reach the end of the blocks and making all of them corrupt.
- Every peer in a blockchain network maintains a local copy of the blockchain.
- All the replicas need to be updated with the latest mined block.
- All the replicas need to be consistent i.e. all the copies of the blockchain with peers must be exactly similar.
Distributed consensus, also known as Nakamoto consensus, is a method of establishing canonical state in a system. Think of it this way: There are an infinite amount of possible wrong answers, but only one right answer. In typical systems, this is done by a central authority that determines validity and provides data, also known as a trusted third party (TTP).
The problem of infinite wrong answers means there must be a way to find validity in blocks. Instead of using a central authority, distributed nodes receive new blocks, and either choose to withhold or propagate them. In such a system, there is assumed to be an “uncoordinated, honest majority” that prevents a few malicious nodes from propagating incorrect blocks. This is also called Byzantine Fault Tolerance (BFT).
Using the consensus rules of Bitcoin, for example, transactions must have valid signatures, originating from spends that are available (there must be enough balance to spend), be put into a Merkle tree with SHA-256d, and have a Hashcash-style proof of work attached that is under the widely agreed upon network difficulty, and other rules. If there are multiple blocks that are valid but branch off at some point, pick the longest series of blocks. Ultimately, because there is such an overwhelming majority that follows Bitcoin consensus rules exactly as they are written, consensus can be established through the Internet without any real problems.
Few key points related to distributed consensus are –
- Ensure that all the nodes in the network see the same data at nearly the same point of time.
- All nodes need to agree on a regualr basis, that the data stored by them are same.
- No single point of failure – as the data is decentralized
- Challenge-Response Protocol – In this, the network poses a challenge to the peers. The node thay solves the challenge first declares that it has solved the challenge and the transaction is veirfied.
- Good challenge is the one in which different nodes win in different runs of the challenge. This ensures that no single node controls the network.
- Proof of Work – this ensures consensus over a permissions-less setting based on challenge-response.
Economics of Blockchain consensus
The challenge-response requires that every node spends large amount of computation power to solve a mathematical challenge in each iteration of the consensus. But why should they do it? What is the incentive for them? The answer to these questions is that when a node expends computational power to solve the problem, they are paid by the cryptocurrency generated and managed by the network.
With this, we have reached the end of this post. In this post, we discussed the architecture of a blockchain on a high level along with a good application – Smart Contracts.
In the next post, we will further explore the concepts of blockchain. Stay tuned 😊