Blockchain & Smart Contracts Demystified

Srini Karlekar
18 min readFeb 14, 2018

Overview of Blockchain

In it’s simplest form, the block in blockchain is a registry of transactions. The transaction could be as simple as storing a piece of data such as the balance in your account, or Party A sending money to Party B, to something as complex as an insurance payoff. All this while the transactions are encrypted and digitally signed using cryptographic keys.

If it helps, think of it as a page in a file folder and each transaction written down on a line in the paper. Certain critical aspects of your transactions such as account numbers are not written in plain text, rather you would use a codec to translate these critical pieces of information into a cryptic text that only you with the knowledge of the codec key can understand.

When a block is complete (the page is used up), it is verified by multiple parties (auditors — or miners in the blockchain parlance) to ensure the pre-agreed method was used to record the transaction. Once a majority of parties have verified, a consensus is formed and the ‘block’ is added to the ‘chain’ (in our example, the page is added to a file folder) creating a Blockchain. As the number of transactions grows, so does the blockchain.

It should be noted that once a block is validated and added to the blockchain, the data (or transactions) in the blockchain cannot be modified. In an open blockchain network, anyone can write to a blockchain provided that they pay a small toll for using the infrastructure. Additionally, anyone can validate these transactions by contributing to the infrastructure. Such validitors also called miners are compensated for their efforts. This very nature of blockchain where data is immutable and untrusted parties collaborate and transact with confidence along with a non-retractable proof of action is what makes blockchain an amenable medium to serve as a System-of-Record for public records.

As a result, the Blockchain technology can be used when untrusted parties who are geographically-dispersed want to share data or exchange value. In the world of finance, this trust or lack thereof is called counterparty risk — the risk to each party of a transaction that the counterparty will not live up to his/her contractual obligation.

To summarize, Blockchain technology helps in creating permanent, non-repudiable & read-only records of transactions across blockchain nodes. Each node is a database of blocks linked together in a way that new blocks are added to existing blocks only after the new block is validated and cryptographically signed. This creates a Chain of Trust.

Blockchain — It Is Different From Your Bank’s Database

We established that a blockchain is a shared database consisting of a ledger of transactions. So is your bank’s database. But this is where the similarities end. Unlike a bank’s database which is centrally controlled and owned, a public blockchain is open to the entire world where anyone can verify the transactions in the ledger or operate the account provided they have the right credentials.

This decentralization of responsibility from a single entity (such as the bank in our example) also eliminates the custodial risk — the risk that the institution that is controlling the asset will flip over and thereby take down your assets (or records thereof) with it.

While on this topic, let us understand what a blockchain node and the concept of mining are. Each copy of the blockchain is called a node. A node usually runs on one computer — physical or virtual. To achieve decentralization, the blockchain network consists of multiple nodes where each node has a copy of the confirmed blocks as rest of the nodes in the network. While each node’s block may be a copy, the transactions in each of these blocks are independently verified by each and every node in the block by a process called mining (more on mining later).

This duplication of data across all the nodes in the blockchain has a positive side effect in that it makes the data in the blockchain extremely fault tolerant. Even if multiple nodes in the network fail, taking the systems down with it, the data is still intact in other nodes. Due to the global nature of blockchain network, the possibility of the entire blockchain network failing is very small. Needless to say, this built-in redundancy reduces not only reduces the likelihood of failure but also make the blockchain network resistant to hacking.

Blockchain — Reducing the Cost of Conducting Transactions

In contrast to today’s systems and processes, blockchain allows you to metaphorically digitize your assets and make it liquid. This in turns eliminates the need for authorities to certify and clear transactions. Additionally, transactions which used to take days due to search, verification, and attestation by third-parties can be reduced to minutes due to the elimination of such actors. This disintermediation not only reduces time but also the cost involved in such transaction due to the elimination of third-parties.

Blockchain — The Inner Sanctum

Now that we have a high-level overview of what a blockchain is, let us dig deeper to understand how the blockchain works.

The blockchain technology is built upon the following fundamental concepts:

Cryptography — Public & Private Key Cryptography

One of the key facets of the blockchain technology is creating trust between third-parties. To achieve this kind of trust between third parties a secure digital identity for reference between the parties is needed. The Public-Private-Key Cryptography (PPK) provides this digital reference. In PPK cryptography, every party is provided with a private and public key pair. These key pairs are one of kind such that, you need the public key to encrypt the data and private key to decrypt it. Therefore, the private key is kept confidential and the public key is shared by the party.

When a sender wants to send something of value to a recipient, he/she uses the recipient’s public key to encrypt the data and passes the encrypted data to the recipient. Once the recipient receives the data, he/she uses their private key to decrypt and consume the data. As the data is encrypted, only the recipient can decipher it. If the data is corrupted during transit, no one will be able to read it including the recipient.

Decentralized Network — The Key to Non-Repudiation

While PPK provides a strong case for establishing identity, this will all be naught if the transactions cannot be validated. This is where the blockchain network and the size of the network plays an important role. To draw an analogy, when a meteor burns through the earth’s atmosphere, thousands of telescopes around the earth capture the moment and is witnessed by astronomers, weather agencies, and hobbyists alike. Their recordings or the very act of witnessing the event provides the visual evidence that the action has taken place. With thousands of computers on the blockchain network validating every transaction, blockchain uses mathematical formulas instead of telescopes to validate the transaction. While the number of nodes in the blockchain network may go up or down slightly they are increasing steadily. For example, there were 11,660 reachable nodes on average in the Bitcoin network over the last year.

By combining the cryptographic keys discussed above with the massive blockchain network an unrefutable method of assurance is provided. This process, when one party uses their private key to sign a transaction and attach the counterparty’s public key, a block containing a digital signature, timestamp, and other relevant information is created. This block is then broadcast to all the nodes in the blockchain network. At this point, each node validates the transaction using a mathematical process called mining. Once a majority of the nodes have validated the transaction, a consensus is reached and the block is added to the network.

Mining — Incentive for Verifying Transactions

There is no such thing as free lunch. This is also true in the world of the blockchain. The blockchain protocol compensates the miners for the opportunity cost of validating the transactions in the form of the blockchain’s native currency (for instance, it is Bitcoins in the Bitcoin blockchain network and Ether in the Ethereum blockchain network). This process is called mining. Akin to mining for gold where there is only a finite amount of gold in the earth’s resources, so is a blockchain’s currency artificially restricted by the blockchain protocol to top-off at a certain point. In the Bitcoin network, this top-off is at 21 million bitcoins and the current reward for mining a block is 12.5 bitcoins which are equivalent to approximately $110,000 as of this writing. As a result, there is a massive interest in mining and any node in the blockchain network can establish itself as a mining node and start validating transactions to earn the proverbial gold.

Mining — Process Explained

The process of mining consists of solving a mathematical puzzle. While the puzzle’s difficulty and method may vary for different types of blockchains, the consistent theme behind the puzzle is to combine the data in the block with a random number and pass it through a hash function to produce a result that is within a certain range. This result is known as a nonce. In case of the Bitcoin network, the nonce can be in a range from 0 & 4,294,967,296. To arrive at the nonce, the mining node has to guess a random number that when combined with the data to be validated produces the valid nonce.

As all the mining nodes will be working independently on this mathematical puzzle, a race ensues. The first miner to arrive at the nonce announces the result to rest of the network and pockets the winning leaving the rest of the miners to verify the victor’s work and pursue their luck with the next block. While the rewards sound attractive, the cost of mining should factor in the huge computing power and the massive cost of electricity needed for mining.

Types of Blockchain

The most popular blockchain network in use today is the Bitcoin network which is a public network consisting of a cluster of blockchain nodes brought together to exchange value. As a result, anyone can use the blockchain network to send/receive value (money). Due to the resiliency, immutability, and provenance of assets that the blockchain behind the public Bitcoin brought to fore, ideas soon emerged to exploit the blockchain technology for use as a distributed ledgers in place of ledgers financial institutions have to maintain. By using a distributed digital ledger, these financial institutions surmised that they can reduce the cost of having to prove their customer’s provenance of assets from one institution to other. This gave rise to the concept of consortium network which are blockchain networks owned and operated by a handful of permissioned entities. By contrast, private networks are closed networks used within an organization to solve auditing, security and prevent fraud without the need for crypto-currencies.

Following are the properties of these various types of blockchain networks:

What is Blockchain good for?

Global System of Record

From its advent, blockchain has quickly established itself as a medium for information registry, data distribution, source-of-truth (or system-of-record). Following are few of the main use cases for the blockchain technology.

Information Registry

Its capability to act as an immutable system-of-record can be leveraged to store:

  • Unencrypted data to promote transparency and trust across business networks.
  • Encrypted data to exchange value between counterparties so that participants can access data that they are authorized to view.

Digital Identity

With its use of public and private key pairs, blockchain technology can be used to establish digital identity and consent to digital interactions. The public key can be used to identify entities and the private keys can be used by the entities to express consent for transactions. Combined with the ability to constrain network participation in a permissioned blockchain, organization, and public-sector agencies can use blockchain to exchange sensitive data while complying with data regulations, PCI and HIPAA.

Distributed Ledgers

The blockchain is an ideal platform for Distributed Ledgers because of its use of consensus. Simply put, a consensus is a set of rules with which a majority of participants working for a common cause can agree on an outcome. The outcome could be a value of an entity or an outcome of a transaction. This simple mechanism is leveraged in the blockchain domain to create Distributed Ledgers so that a majority of the nodes can agree on the value of a transaction or data item. Due to blockchain’s tolerance for failure, consensus mechanism used in the blockchain allows the distributed ledgers to flourish even if some of the nodes fail.

There are various consensus mechanisms that can be used in a blockchain network. For instance:

Proof-of-Work — To validate transactions, miners have to solve complex mathematical puzzles which consume considerable computing power and uses vast amounts of electricity. To bend the system to their needs, malevolent miners have to take over a majority (51%) of the nodes which may cost them more than the transactions they are validating are worth. As a result of this, proof-of-work is well-suited on a public network where trust is in short supply.

Proof-of-Stake — To validate transactions, validators (miners are called validators in the PoS model) have to pitch in a certain amount of digital assets in an escrow to participate. If the validators do not follow a strict protocol for validation they lose their assets in the escrow thereby reducing the incentive for malfeasance.

Multi-signature — In a multi-sig scenario, a majority of stakeholders have to digitally sign-off for a transaction to occur or to validate a transaction.

Practical Byzantine Fault Tolerance — Practical Byzantine Fault Tolerance of PBFT is a way to validate data and achieve consensus using a protocol that allows for a certain number of validators to go rogue and still arrive at a consensus beneficial for all the parties involved.

Smart Contracts

In a vanilla transaction, there are atmost two parties. One at the sending end and another at the receiving end. What if you want multiple parties to be involved in a transaction? What if you want the transactions to be executed based on certain logic? This is where Smart Contracts come in.

A Smart Contract is a method of digitizing the defining elements of a contract and linking it securely to other stakeholders to assure issuance of value. The stakeholders could be internal systems, external systems or other entities. It is stored on a blockchain and is executed as part of a transaction. Rules could be codified to be self-executed or self-enforced based on the passage of time or a change of state. The purpose of a smart contract is to reduce friction, cost, and delays associated with executing traditional contracts. While a standard contract outlines the terms of a transaction and is usually only enforceable by law, a smart contract enforces the term encoded in the contract when a certain event occurs. As a matter of fact, the enforcement happens at the protocol level by all the nodes participating the blockchain network.

Smart Contracts are usually written using a language called Solidity. Solidity is statically typed, supports inheritance, libraries, and complex user-defined types. The language syntax is similar to Javascript.

Following is a sample of a Smart Contract that stores a piece of data. It only allows the owner of the contract to change a item of value whereas anyone else can see how much value is stored in the contract. Additionally, only the owner can tear-down the contract.

pragma solidity ^0.4.0;contract Register {
address creator;
uint256 myNumber;
bytes32 name;
modifier onlyowner() {
if (msg.sender == creator) {
_;
}
}
event ValueChanged(address indexed user, uint256 indexed oldNumber, uint256 indexed newNumber); function Register(bytes32 _name, uint256 _seed) public {
creator = msg.sender;
name = _name;
myNumber = _seed;
}
function getCreator() public constant returns (address) {
return creator;
}
function getName () public constant returns(bytes32) {
return name;
}
function getMyNumber() public constant returns(uint256) {
return myNumber;
}
function setMyNumber(uint256 myNewNumber) public onlyowner {
myNumber = myNewNumber;
ValueChanged(msg.sender, myNumber, myNewNumber);
}
function kill() public {
if (msg.sender == creator) {
selfdestruct(creator);
}
}
}

Oracles

While a smart contract can be fully autonomous in a world where all the assets it manages such as currency, payment, obligations, property titles, assets and licenses are digitally represented, we are not at the age where such data and its provenance is fully digitized. In the absence of such digital representation, smart contracts uses special nodes called Oracles that act as an interface to the external world and react to it.

Ethereum

Ethereum is a version of blockchain that allows Smart Contracts to be run in a trusted environment — this is unlike the Bitcoin blockchain which only allows cryptocurrency transactions. To accommodate Smart Contracts to be executed, the Ethereum blockchain hosts a virtual machine called Ethereum Virtual Machine (EVM) on each of its nodes. The EVM allows code to be verified and executed on the blockchain, providing guarantees it will be run the same way on every node.

The main Ethereum public blockchain is called MainNet. Ethereum can also be downloaded and run locally for development. While the local network will be sufficient for development purposes, for testing Smart Contracts, the following public test networks are available:

Ropsten — Ropsten is the official test network from the Ethereum foundation and its a clone of the MainNet in that it uses the same consensus protocol as the MainNet.

Rinkeby — Rinkeby is a test network that uses a consensus method called, Proof of Authority. With this consensus mechanism, transactions are validated by set of predetermined nodes to control the supply of ether and provide a consistent response time.

Kovan — Similar to Rinkeby, Kovan also uses a Proof-of-Authority consensus mechanism.

Decentralized Applications

Decentralized Applications (also called DApps) are those applications that use Smart Contracts at its core to hold the logic and control the processing. DApps usually uses Javascript, HTML & CSS in the presentation tier with the Smart Contracts deployed on Ethereum blockchain.

The Ethereum White Paper splits DApps into three types: financial applications that manage money by using Ether such as money exchange applications where users sends ether to each other, semi-financial applications where money is involved in addition to other data such as insurance payout applications which pay out certain amount of ether as the policy matures, and applications in the other category, which includes voting and governance systems.

Lifecycle of a Transaction in a Decentralized Application

Let us take a closer look at a lifecycle of a transaction in a DApp.

  1. A transaction is digitally signed and the process begins.
  2. The block containing the transaction is sent through miners before it is stored in a blockchain. The mining process ensures that the transaction is legitimate.
  3. Once the transaction is verified by the mining nodes, the transactions in the block are broadcast to other nodes in the network.
  4. When the majority of the nodes verify that the transaction is legitimate (arrive at a consensus), the block containing the transaction is stored in the blockchain.
  5. As soon as the block is stored in the blockchain an event is triggered and the recipient receives the result of the transaction.

Advantages of Decentralized Applications

The key to the growth and adoption of blockchain is the decentralized nature of its network and the assurance it provides in minimal trust relationships. More specifically:

  1. Decentralization reduces the likelihood of failures due to the redundancy built-in due to duplication of data. As a result, the downtime on networks like the MainNet is literally zero.
  2. Again due to the resiliency and duplication of data among the nodes, it is not as prone to hacking as other traditional databases.
  3. Because transactions using Smart Contracts are signed by asymmetric cryptography, it is impossible for parties involved in the transaction to repudiate it.

Sidechains

With the proliferation of various blockchain networks, there is sometimes a need to send value from one blockchain network to another. For instance, send data from one private blockchain to another for faster processing or to make use of a certain service or contract that is only present in the other blockchain. This is where sidechains come into the picture.

When sidechains are enabled in a primary blockchain network, a range of addresses are dedicated for the sidechain. When a certain value has to be sent to the secondary blockchain (through sidechain), it is sent to a preconfigured address in the blockchain which is within this sidechain address range. Once the value is sent to this address, it is out of the control of the primary blockchain. This is called a immobilization transaction. Once the immobilized transaction is confirmed, a message is sent to the secondary blockchain with a proof that the value was transferred to the secondary blockchain. The secondary blockchain creates an equivalent value in its denomination and provides the address. This equivalent value can then be used in the secondary blockchain to conduct transactions.

This logic is symmetric in the sense, when the transaction in the secondary blockchain is complete it can transfer the value to the original blockchain (now its sidechain) and the process continues in the primary blockchain from where it left-off.

Barriers to Adoption

While blockchain offers improved data quality, accuracy, and better services through enhanced efficiencies, its adoption is not widespread outside of the cryptocurrency domain and a few tech-savvy financial institutions. Following are some of the major challenges to the widespread adoption of the blockchain technology.

Lack of Knowledge & Skill — Though many have heard of Bitcoin and may have used it, they may not understand how it works or what the underlying blockchain technology is capable of. Much less, it’s development eco-system.

Cost of Change — Even as more organizations learn about the potential benefits of blockchain and its capabilities, the internal resistance to change and the costs associated with the change is a major barrier to adoption of the blockchain. Additionally, the blockchain projects currently underway are mostly IT-driven rather than business-driven. As business becomes aware of the benefits of adopting blockchain and is able to justify the cost of the shift in technology, the adoption should improve.

Regulatory Environment — One of the main challenges of wide adoption of the blockchain technology is regulations. In many cases, the current regulatory environment is slowing down the adoption of the technology as it will disrupt existing services and institutions. As the current regulatory environment catches up with the innovation, more products and services will start using the blockchain technology.

As some of these benefits promised by blockchain are highly disruptive, a detailed cost-benefit analysis must be performed to determine if the potential benefits of blockchain outweigh the cost of the change.

Additionally, given the early stages of the blockchain and smart contract eco-system, a thorough feasibility analysis must be performed with a proof-of-concept to determine the viability of the technology for a particular use case.

Emerging Services Disrupting the Status Quo by using Blockchain

The following are a few services that use the blockchain technology as its core enabler.

Storj — Storj.io is a blockchain based, end-to-end encrypted, decentralized cloud storage platform. Unlike Dropbox and similar cloud-based storage platform which manages the servers on which your data is stored, Storj takes your files, shreds it into smaller pieces, encrypts each piece and stores them in a consortium blockchain network. You can subsidize your cost of storage by joining the consortium and leasing your computer and hard-disk space to be a part of the consortium network.

ShapeShift — Most of the cryptocurrency trading platforms requires the user to store their cryptocurrency metadata such as keys and addresses with them. ShapeShift has created a cryptocurrency trading platform that allows you to trade one blockchain asset for another without the associated custodial risk of storing the account related information.

Akasha — Akasha promotes freedom of expression, access to information and other basic fundamental human rights by encoding them into smart contracts and providing a social-networking platform to use it.

Balanc3 — Balanc3 has built a blockchain-based accounting platform that bridges the traditional accounting world to the blockchain technology. It provides utilities to monitor blockchain transactions and transform the data and events to traditional accounting formats for reporting and compliance.

Ujo — Ujo is looking to transform the music industry supply chain by connecting the artists to listeners without the need for the middleman. It uses blockchain to store music and uses smart contracts to allow singers and songwriters to trade produced songs and albums directly with their consumers.

Conclusion

Over the ages, businesses have to overcome different types of friction to differentiate itself, create value and forge ahead. Innovations starting from the invention of the wheel, steam engines, assembly lines and the internet have helped overcome geographical divides and other inefficiencies. In spite of this, today’s organizations have to deal with imperfect information that are tied up in silos, retrieving them while at the same time having to secure the channels through which such information is exchanged. This adds to the costs of processing information, delays in getting to it while increasing the touchpoints in the information supply chain. Add to this the bureaucracy and red-tape businesses have to cross to innovate, regulations they have to satisfy and the friction multiplies manifold.

The invention of blockchain comes at the right time by reducing these frictions among untrusted third-parties, increasing collaboration and promoting consensus among them. All this while removing third-parties and reducing the overall cost and delays in business processes. By promoting visibility, blockchain has the potential to transform the business landscape.

Originally published at The Pragmatic Architect.

--

--

Srini Karlekar

Servant Leader & Cloud Architect passionate about #Opensource, #Serverless, #MachineLearning, #AIaaS, Computer Vision and #IoT.