Description: https://images.manning.com/360/480/resize/book/e/fb67982-e2fa-4e63-9e10-6bc89c407461/Ramamurthy-Blockchain-HI.png

From Blockchain in Action by Bina Ramamurthy

What’s all the fuss about the blockchain? Find out in this article.


Take 40% off Blockchain in Action by entering fccramamurthy into the discount code box at checkout at manning.com.


From Bitcoin to blockchain

The initial excitement about blockchain technology was about enabling peer-to-peer transfers of digital currency to anybody in the world, crossing human-created boundaries (like the borders of countries) without any intermediaries such as banks. This excitement was further heightened by the realization that this peer-to-peer capability could be applied to other non-cryptocurrency types of transactions. These transactions involve assets, such as titles, deeds, music and art, secret codes, contracts between businesses, autonomous driver decisions, and artifacts resulting from many every day human endeavors.

DEFINITION A transaction recorded on a blockchain contains a peer-to-peer message that specifies the operations executed, data parameters used for the execution of operations, the sender and receiver of the message, transaction fee, and timestamp of its recording.

A transaction record may contain other details based on the protocol and the application. Bitcoin has been continuously in operation from its launch. At the time of this writing, according to online charts, it’s delivering over 200,000 transactions per day. Following its initial success, people began to wonder, if you could transact digital currency, why not any other digital assets? This question was answered around 2013 by the addition of an environment for code execution on another popular blockchain, Ethereum. The innovation here was that the verification, validation, and recording could be extended to other digital assets, and related transactions and systems. Blockchain can, therefore, play a central role in implementing decentralized systems by providing software-based intermediation to other (non-currency) peer-to-peer transactions.

Exploring a real blockchain

Let’s take a look at a blockchain to give you an idea of what transactions, blocks, and chains of blocks look like to visualize blockchain context and problem space discussed in the next sections. We explore the transactions and blocks for the Ethereum public blockchain. Figure 1 shows transactions (Tx and transaction#) which represent messages between two accounts (From and To) representing peer participants. These Txs enable the recording of information on the blockchain.

Each block is (Bk) made up of a set of transactions and it’s identified by a block number. For example, in figure 1, block #10163275 has 142 Txs, and block #10163274 has 60 Txs. You may see a different set of blocks when you visit the site, but you can always search for a particular block number (#10163275 in this case) and verify the number of Txs-it has the same number of Txs shown here, exemplifying the immutable nature of blockchain technology. The blocks are linked together to form a chain of blocks or blockchain.


Figure 1. A snapshot of the Ethereum public blockchain


What is a blockchain?

A blockchain is a technology for enabling trust in a decentralized system of transacting peer participants. The purpose of a blockchain is to verify and validate (or reject if invalid) a transaction initiated by a participant, then execute the transaction and record the proof of these actions with the consensus of the peer participants. As shown in figure 2, the blockchain-based trust infrastructure exists within a larger system. Blockchain infrastructure contains software for a specific purpose, that of trust intermediation among a large number of (typically unknown) peer-to-peer participants. On the left side of figure 2 is a distributed (client/server) system performing routine operations. This system may send messages which contain data to be verified, validated, and recorded on the blockchain (on the right) to establish trust in that larger system. In blockchain programming, you don’t replace an existing system but enhance it with code for trust intermediation through validation and verification.


Figure 2. Blockchain programming context


To help you further understand blockchain programming, let’s examine the blockchain stacks for Bitcoin and Ethereum, as shown in figure 3. Though mining equipment, which can be found on sites like https://coinminingdirect.se/product/goldshell-lt5-pro/ have helped cryptocurrencies like Dogecoin and Litecoin grow in popularity in recent years, it is no secret that Bitcoin and Ethereum are the two long-standing models of blockchain in its short history. Bitcoin has only the wallet application, whereas Ethereum features programmable code called smart contracts.


Figure 3. Blockchain stacks and types of programming


Figure 3. also shows the three levels of programming:

  • Protocol-level programming-this level involves software which is needed for the deployment and operation of the blockchain itself. This situation is similar to your operating system software or networking software. If you’re a systems programmer and administrator, you’ll program at this level. This article doesn’t cover protocol-level programming.
  • Smart contract–level programming-one level above is smart contract or rules engine programming. It’s at this level that you design and program the rules for verification and validation and specify the data and messages which are to be recorded on the underlying blockchain. The smart contract is the engine that drives the blockchain on behalf of the user application.
  • Application-level programming-this level involves programming using web (or enterprise or mobile) application frameworks and user interface design concepts that are outside the blockchain protocol.

Definition Dapps are web or enterprise applications that include application logic to invoke blockchain functions to implement trust intermediation.

Dapps embed a significant code element-that of smart contracts. For any given smart contract, an exact copy of the smart contract’s code is transmitted through a special transaction and deployed in the participant nodes of a blockchain network.

DEFINITION A smart contract is an immutable executable code representing the logic of a Dapp. The data variables and functions defined in a smart contract collectively represent the state and operations for enforcing an application’s (Dapp’s) rules for verification, validation, and recording on the blockchain.

Blockchain programming

In the evolution from sequential programming to structured programming, functional programming, object-oriented programming (OOP), web and database programming, and big data programming, programmers experienced shifts in approaches, artifacts, and architectures (for example, OOP with its classes and objects; Hadoop and Map Reduce for big data processing). Similarly, blockchain programming is yet another paradigm shift. Four fundamental concepts play a significant role in making blockchain programming different. You need to understand these concepts before you start programming, as you need to learn about class and object concepts before undertaking OOP. Given this context, the four key roles fulfilled by a blockchain are:

  • Decentralized infrastructure-Special computing hardware and software stack support the blockchain protocol, smart contracts, and applications (Dapps). The main components of this infrastructure are the computing nodes and networks connecting the nodes.
  • Distributed ledger technology-On top of the infrastructure is the ledger. Transactions and data are recorded simultaneously in all stakeholders’ ledgers. It’s a distributed ledger because all the stakeholders record the same facts. It’s immutable because each block is linked to the signature of the previous block, making it tamper-proof.
  • Disintermediation protocol-Participants in a decentralized system follow the same blockchain protocol to connect and to be able to communicate and transact with each other. The protocol is a set of rules for everyone to follow. For example, the Ethereum and Hyperledger protocols are two different blockchain protocols.
  • Trust enabler-In a decentralized system of participants, there are no central authorities or intermediaries such as banks. You, therefore, need an infrastructure that implements the rules for governance, provenance, compliance, and such automatically, without any intermediaries. Blockchain software assumes the role of a trust intermediator.

Decentralized infrastructure

Blockchain infrastructure is inherently decentralized, like the railway tracks or roadways connecting cities. You can think of the Dapps that you’ll deploy as like the trains or vehicles that travel on the tracks and roads. With this picture in your mind, let’s explore that infrastructure. Your aim in this article is to comprehend the crucial role played by blockchain infrastructure in supporting decentralized systems. What is a decentralized system?

A decentralized system is a type of distributed system where:

  • Participants communicate peer to peer
  • Participants are in control of their assets, digital or otherwise (for example, an audio file, a digital health record, or a piece of land)
  • Participants can join and leave the system as they wish
  • Participants operate beyond the typical boundaries of trust (such as within a university or a country)
  • Decisions are made by the distributed participants and not by any central authority
  • Intermediation is achieved using automated software such as a blockchain

Let’s now explore the architectural elements of blockchain that address these unique needs of a decentralized system.

Blockchain nodes, networks, and applications

Consider air traffic as an example. Flights have an origin, destination, and stopover airports and waypoints forming the airline network. Similarly, blockchain nodes host the computational environment that serves as endpoints of transactions and also performs other functions, such as relaying and broadcasting of transactions.

DEFINITIOn Node is a collective name for blockchain software and the machine or hardware on which it’s installed for the participant of a decentralized system.

Figure 4 shows the logical architecture of a single blockchain node. A node can support many accounts to represent the identities of peer participants in the decentralized network. A 256-bit number represents an account. Compare this size in bits to your traditional computer’s address size of 64 bits!

DEFINITION An account represents a unique identity for a transacting entity. An account is needed to initiate a transaction.


Figure 4. Blockchain node and application stack


A blockchain node hosts the elements represented by the stack in figure 4. It serves as a foundation for your blockchain application development. Let’s start from the bottom and move up. The lower two levels are the standard hardware and software of most computing systems. The next level up is the blockchain protocol level: it houses the components of the blockchain, but you won’t program at this level. The next layer hosts the application logic. This layer is where you solve problems like access control to data and code functions for validation, verification, and recording. The top layer is the user-facing interface where web (or enterprise) programming is done, for example, with HTML, JavaScript, and associated frameworks. These elements form the Dapp and its user interface (UI) layer.

A blockchain application isn’t a single-user application, unlike a handheld game or an income tax calculator. It typically connects a large number of participants through its network of nodes. Each node can host multiple accounts to identify the different customers it services. A node can also host more than one Dapp: for example, one for a decentralized supply chain management system and another for a decentralized payment system. Figure 5 shows a network of three nodes connected by a network. The network facilitates broadcast of the

  • transactions initiated by users
  • blocks formed out of the transactions

These transactions and blocks constitute the payload of the network and, eventually, after verification and validation, are recorded on the distributed ledger.


Figure 5. A blockchain network of nodes broadcasting transactions and blocks


A network identifier identifies a blockchain network of nodes. For example, network ID #1 is the main Ethereum public network; network ID #4 is another public network called Rinkeby. You’ll have to indicate the network using its identifier as you deploy your smart contract on the network. The participants on given a network share a shared unified distributed ledger for recording their transaction details.

The smart contracts are deployed in a sandbox environment (such as a virtual machine) hosted by a blockchain node. The syntax of a smart contract is similar to a class in an OO language. It contains data, functions, and rules for the execution of functions. Calling or invoking a smart contract function generates the transactions which are recorded on the blockchain, as shown in figure 6. If any of the verification and validation rules fail, the function invocation is reverted, but if the execution is successful, the generated transactions (Txs) are broadcast to the network for recording, as shown in figure 6. The figure illustrates how a function call is transformed into actions which are recorded on the blockchain.


Figure 6. From application messages to Txs on the blockchain


If you want to learn more, check out the book on Manning’s browser-based liveBook platform here.