Book Image

Blockchain Developer's Guide

By : Brenn Hill, Samanyu Chopra, Paul Valencourt, Narayan Prusty
Book Image

Blockchain Developer's Guide

By: Brenn Hill, Samanyu Chopra, Paul Valencourt, Narayan Prusty

Overview of this book

Blockchain applications provide a single-shared ledger to eliminate trust issues involving multiple stakeholders. It is the main technical innovation of Bitcoin, where it serves as the public ledger for Bitcoin transactions. Blockchain Developer's Guide takes you through the electrifying world of blockchain technology. It begins with the basic design of a blockchain and elaborates concepts, such as Initial Coin Offerings (ICOs), tokens, smart contracts, and other related terminologies. You will then explore the components of Ethereum, such as Ether tokens, transactions, and smart contracts that you need to build simple DApps. Blockchain Developer's Guide also explains why you must specifically use Solidity for Ethereum-based projects and lets you explore different blockchains with easy-to-follow examples. You will learn a wide range of concepts - beginning with cryptography in cryptocurrencies and including ether security, mining, and smart contracts. You will learn how to use web sockets and various API services for Ethereum. By the end of this Learning Path, you will be able to build efficient decentralized applications. This Learning Path includes content from the following Packt products: • Blockchain Quick Reference by Brenn Hill, Samanyu Chopra, Paul Valencourt • Building Blockchain Projects by Narayan Prusty
Table of Contents (37 chapters)
Title Page
Copyright
About Packt
Contributors
Preface
Index

Introducing blockchain


The whitepaper released by Bitcoin's founder or a group of founders called Satoshi Nakamoto, in 2008, described Bitcoin as a purely peer-to-peer version of electronic cash. Blockchain was introduced along with Bitcoin. During the initial stages, blockchain was only used with Bitcoin for Bitcoin-based financial transactions.

Blockchain not only restricts financial transactions in Bitcoin, but in general any transaction between two parties that is maintained by the open, decentralized ledger. Most importantly, this underlying technology can be separated and can have other applications create a surge in the number of experiments and projects surrounding the same.

Numerous projects inspired by blockchain started, such as Ethereum, Hyperledger, and so on, along with currencies such as Litecoin, Namecoin, Swiftcoin, and so on.

Blockchain at its core is a distributed and decentralized open ledger that is cryptographically managed and updated various consensus protocols and agreements among its peers. People can exchange values using transactions without any third party being involved, and the power of maintaining the ledger is distributed among all the participants of the blockchain or the node of the blockchain, making it a truly distributed and decentralized system.

Some of the industry verticals using blockchain are as follows:

  • Cryptocurrency: Bitcoin is the prime example of this. Apart from this, there are various alternative cryptocurrencies, such as Ethereum, Litecoin, Peercoin, and so on.
  • Cybersecurity: There are various companies and projects harnessing the distributed nature of blockchain to create special authentication protocols for mass usage.
  • Healthcare: Scalability is one of the best catalysts of blockchain; a lot of projects are securely storing data and using analytics to come to the perfect solution. Providing decentralized patient records and authentication is already being used at various facilities.
  • Financial services: A lot of insurance and financial institutions are using blockchain to maintain the vast bank data, such as financial records, ledgers, guarantees, bonds, and so on.
  • Manufacturing: Supply chain, prototyping and proof-of-concept, along with the tracking and tracing of goods is utilizing blockchain at the best efficiency.
  • Governance: A lot of governments are racing toward becoming the world's first blockchain-powered state. Blockchains are being used across most government departments and not restricted only to public safety, transport, health, and shipping.
  • Retail: A lot of startups and projects are aiming to introduce open no-middle man-based ecosystems; some are working on loyalty systems and blockchain-derived gift card systems.
  • Media: Record labels can use blockchains to keep their ownership network and intellectual property rights secure.
  • Travel: Projects are being worked on to introduce and revolutionize vehicle leasing, ride sharing, and other travel-related queries.
  • Legal: Blockchain can bring transparency and solve the scaling issues in the ever-complex legal systems that are in place.

Furthermore, we will be discussing various other elements of blockchain and what other problems blockchain can solve.

General elements of blockchain

It is time to discuss the general elements of blockchain, starting from its basic structure to its formation and further details on the same.

Peer-to-peer network

This is a type of network whereby all peers can communicate with one another and are equally entitled, without the need for central coordination by servers or hosts. In conventional networks, the systems are connected to a central server, and this server acts as a central point for communication among the systems. On the other hand, in a peer-to-peer network, all the systems are connected to one another evenly, with no system having central authority. Look at this diagram:

The pictorial difference between a client-to-server network and a peer-to-peer network

Block

A block is the smallest element of a blockchain; the first block is called the genesis block. Each block contains batches of hashed and encoded transactions. The blocks are stored in a Merkle tree formation. Every block includes the hash of the previous block in the chain that links all blocks to one another. In Bitcoin, a block contains more than 500 transactions on average. The average size of a block is around 1 MB. A block is comprised of a header and a list of transactions.

Block header

The block header of a block in Bitcoin comprises of metadata about the block. Consider the following:

  • Bitcoin version: This field contains the Bitcoin version number.
  • Previous block hash: The previous block's hash is required to create the new block's hash.
  • Merkle root: This is the hash of the root of the Merkle tree of the current block's transactions.
  • Timestamp: This is the timestamp of the block in UNIX.
  • Mining difficulty: Mining is a crucial part of the blockchain ecosystem in Bitcoin. There is a difficulty target for mining, which is mentioned in the header.
  • Nonce: Blockchain adds deviations in each block; these are known as nonce. Take a look at this diagram:

The formation of block headers and what comprises the Merkle root and the Merkle tree

Addresses

Addresses are unique identifiers that are used in a transaction on the blockchain to send data to another address; in the case of Bitcoins, addresses are identifiers that are used to send or receive Bitcoins. Bitcoin blockchain addresses have evolved from time to time. Originally, IP addresses were used as the Bitcoin address, but this method was prone to serious security flaws; hence, it was decided to use P2PKH as a standard format. A P2PKH address consists of 34 characters, and its first character is integer 1. In literal terms, P2PKH means Pay to Public Key Has. This is an example of a Bitcoin address based on P2PKH: 1PNjry6F8p7eaKjjUEJiLuCzabRyGeJXxg.

Now, there is another advanced Bitcoin protocol to create a P2SH address, which means Pay to Script Hash. One major difference with a P2SH address is that it always starts with integer 3 instead of 1.

Wallets

A wallet is a digital wallet used to store public or private keys along with addresses for a transaction. There are various types of wallets available, each one offering a certain level of security and privacy.

Here is a list of the various types of wallets, based on their functions:

  • Software: This wallet is installed on the actual computer; the private key access is with the owner of the machine on which the wallet's software is installed. 
  • Web Wallets: These wallets are based on the cloud and can be accessed anywhere. The private keys are shared with the wallet service.
  • Paper Wallets: The private keys of this wallet are printed on paper.
  • Hardware: These are physical wallets and are small and portable in nature. The private keys are with the hardware users of each wallet.

It is important to understand the functioning and the need for various wallets along with the requirement for each.

Transaction

A transaction is the process of transferring data from one address in blockchain to another address. In Bitcoin, it is about transferring Bitcoins from one address to another address. All the transactions happening in blockchain are registered from the start of the chain till the current time; this information is shared across the network and all the P2P nodes. The transaction is confirmed by miners, who are economically compensated for their work.

Each transaction in a blockchain goes through a number of confirmations, as they are the consensus of a transaction. Without confirmation, no transaction can be validated.

Nodes

Nodes are part of a blockchain network and perform functions as assigned to them. Any device connected to the Bitcoin network can be called a node. Nodes that are integral components of the network and validate all the rules of the blockchain are called full nodes. Another type of Node is called a super node, which acts as a highly connected redistribution point, as well as a relay station.

What does blockchain solve?

A blockchain performs various functions. We will discuss each of them briefly here and in detail later:

  • Security: Due to its consensus structure and multiple failback points, there is minimum chance of failure. Its distributed nature gives better security and reliability.
  • Faster settlements: Traditional banking protocols are very time-consuming and incur fairly large forex charges; on the other hand, Bitcoins based on blockchain offer near-instant speed, saving time and costs for the entire financial industry.
  • Transparency: Being decentralized in nature, there is no need for a third party, as blockchain is shared with everyone with a wallet, making it a transparent system with trust.
  • Economical: Having no third party and the ledger being shared by everyone means no overhead costs or auditing expenses.

The following diagram depicts the difference between centralized, decentralized, and distributed networks:

Being distributed in nature, blockchain offers lots of out-of-the-box features, such as high stability, security, scalability, and other features discussed previously. 

Types of blockchains

Considering the way blockchain has evolved, we can classify blockchain into multiple types; these types define the course of blockchain and make it go beyond the use of P2P money. The following diagram displays the different types of blockchain networks currently available or proposed.

We will now discuss each type of blockchain network in detail.

Public blockchain

A public blockchain is a blockchain where anyone in the world can become a node in the transaction process. Economic incentives for cryptographic verification may or may not be present. It is a completely open public ledger system. Public blockchains can also be called permissionless ledgers.

These blockchains are secured by crypto economics, that is, economic incentives and cryptographic verification using mechanisms such as PoW or PoS or any other consensus mechanism. Some popular examples of this type of blockchain are Bitcoin, Ethereum, Litecoin, and so on.

Semi-private blockchain

A semi-private blockchain is usually run by a single organization or a group of individuals who grant access to any user, who can either be a direct consumer or for internal organizational purposes. This type of blockchain has a public part exposed to the general audience, which is open for participation by anyone.

Private blockchain

In private blockchains, the write permissions are with one organization or with a certain group of individuals. Read permissions are public or restricted to a large set of users. Transactions in this type of blockchain are to be verified by very few nodes in the system.

Some prime examples of private blockchain include Gem Health network, Corda, and so on.

Consortium blockchain

In this type of blockchain, as the name suggests, the consensus power is restricted to a set of people or nodes. It can also be known as a permission private blockchain. Transaction approval time is fast, due to fewer nodes. Economic rewards for mining are not available in these types of blockchains.

A few examples of consortium-based blockchains are Deutsche Boerse and R3 (financial institutions).

Byzantine generals problem

This is one of the classic problems faced by various computer networks, which until recently had no concrete solution. This problem is called Byzantine Generals' Problem (BGP). The problem at its root is about consensus, due to mistrust in the nodes of a network.

Let's imagine that various generals are leading the Byzantine army and are planning to attack a city, with each general having his own battalion. They have to attack at the same time to win. The problem is that one or more of generals can be disloyal and communicate a duping message. Hence, there has to be a way of finding an efficient solution that helps to have seamless communication, even with deceptive generals.

This problem was solved by Castro and Liskov, who presented the Practical Byzantine Fault Tolerance (PBFT) algorithm. Later, in 2009, the first practical implementation was made with the invention of Bitcoin by the development of PoW as a system to achieve consensus.

We will be discussing in detail the BGP in later chapters.

Consensus

Consensus is the process of reaching a general agreement among nodes within a blockchain. There are various algorithms available for this especially when it is a distributed network and an agreement on a single value is required.

Mechanisms of consensus: Every blockchain has to have one mechanism that can handle various nodes present in the network. Some of the prime mechanisms for consensus by blockchain are the following:

  • Proof of Work (PoW): This is the most commonly used consensus mechanism, also used by the first ever cryptocurrency, Bitcoin. This algorithm has proven most successful against Sybil attacks.
  • Proof of Stake (PoS) this makes the mining of new blocks easier for those who have the highest amount of cryptocurrency.
  • Delegated Proof of Stake (DPOS) one small change it has over PoS is that each node that has a stake can delegate the validation of a transaction to other nodes by means of voting.
  • Proof of Importance (POI) this is designed to be energy efficient and can also run on relatively less powerful machines. It relies on stake as well as the usage and movement of tokens to establish trust and importance.
  • Proof of Elapsed Time (PoET) this is a blockchain algorithm created by Intel, using Trusted Execution Environment (TEE) to have randomness and security in the voting process using a guaranteed wait time.
  • Proof of burn (PoB) this is mostly used for bootstrapping one cryptocurrency to another. The basic concept is that miners should prove that they have burned coins, that is, they have sent them to a verifiable unspendable address.
  • Proof of activity (PoA): A random peer is selected in this from the entire network to sign a new block that has to be tamper-proof.

All the preceding algorithms and a host of already available or currently under research make sure that the perfect consensus state is achieved and no possible security threats are present on the network.

Blockchain in a nutshell

It is time to discuss the benefits as well as the challenges or limitations faced by blockchain technology, and what steps are being taken by the community as a whole.

Benefits

If it's all about trust and security, do we really need a trusted system, even after everything is already highly secure and private? Let's go through the limitations of each of the existing ecosystems where blockchain is a perfect fit.

Banking records

Record keeping and ledger maintenance in the banking sector is a time and resource-consuming process and is still prone to errors. In the current system, it is easy to move funds within a state, but when we have to move funds across borders, the main problems faced are time and high costs.

Even though most money is just an entry in the database, it still incurs high forex costs and is incredibly slow.

Medical records

There are lot of problems in record keeping, authentication and transferring of records at a global scale, even after having electronic records, are difficult when implemented practically. Due to no common third party, a lot of records are maintained physically and are prone to damage or loss.

During a case of epidemiology, it becomes essential to access and mine medical records of patients pertaining to a specific geography. Blockchain comes as a boon in such situation, since medical records can be easily accessible if stored in the blockchain, and they are also secure and private for the required users.

Government records

Any government agency has to deal with a lot of records for all of its departments; new filings can be done on blockchain, making sure that the data remains forever secure and safe in a distributed system.

This transparency and distributed nature of data storage leads to a corruption-free system, since the consensus makes sure the participants in the blockchain are using the required criteria when needed. 

Creative and copyright records

Copyright and creative records can be secured and authenticated, keeping a tab on copyright misuse and licensing.

One premier example of this is KodakCoin, which is a photographer-oriented cryptocurrency based on blockchain, launched to be used for payments of licensing photographs.

University degree records

Verification, authentication, and inspection is hard. It is highly prone to theft and misuse. Blockchain can offer a great semi-private access to the records, making sure signing of degrees is done digitally using blockchain.

Gradual record keeping of degrees and scores will benefit efficient utilization of resources as well as proper distribution and ease in inspection process.

The preceding are just some of the varied use cases of blockchain, apart from Bitcoins and alternative cryptocurrencies. In the coming chapters, we will be discussing these points in much more detail.

Challenges

As with any technology, there are various challenges and limitations of blockchain technology. It is important to address these challenges and come up with a more robust, reliable, and resourceful solution for all. Let's briefly discuss each of these challenges and their solutions.

Complexity

Blockchain is complex to understand and easy to implement.

However, with widespread awareness and discussions, this might be made easier in the future.

Network scalability

If a blockchain does not have a robust network with a good grid of nodes, it will be difficult to maintain the blockchain and provide a definite consensus to the ongoing transactions.

Speed and cost

Although blockchain-based transactions are very high in speed and also cheaper when compared to any other conventional methods, from time to time, this is becoming difficult, and the speed reduces as the number of transactions per block reduces.

In terms of cost, a lot of hardware is required, which in turn leads to huge network costs and the need for an intermittent network among the nodes.

Various scaling solutions have been presented by the community. The best is increasing the block size to achieve a greater number of transactions per block, or a system of dynamic block size. Apart from this, there are various other solutions also presented to keep the speed reduced and the costs in check.

Double spending 

This is a type of attack on the blockchain network whereby a given set of coins is spent in more than one transaction; one issue that was noted here by the founder/founders of Bitcoin at the time of launch is 51 attacks. In this case, if a certain miner or group of miners takes control of more than half of the computing power of blockchain, being open in nature, anyone can be a part of the node; this triggers a 51 attack, in which, due to majority control of the network, the person can confirm a wrong transaction, leading to the same coin being spent twice.

Another way to achieve this is by having two conflicting transactions in rapid succession in the blockchain network, but if a lot of confirmations are achieved, then this can be avoided.

There are various other features that will be discussed in the coming chapters, it should be noted that all of these features exist in the present systems but considering active community support, all these limitations are being mitigated at a high rate.