Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Blockchain By Example

You're reading from   Blockchain By Example A developer's guide to creating decentralized applications using Bitcoin, Ethereum, and Hyperledger

Arrow left icon
Product type Paperback
Published in Nov 2018
Publisher Packt
ISBN-13 9781788475686
Length 528 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Authors (3):
Arrow left icon
Xun (Brian) Wu Xun (Brian) Wu
Author Profile Icon Xun (Brian) Wu
Xun (Brian) Wu
Bellaj Badr Bellaj Badr
Author Profile Icon Bellaj Badr
Bellaj Badr
Richard Horrocks Richard Horrocks
Author Profile Icon Richard Horrocks
Richard Horrocks
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Say Hello to Blockchain FREE CHAPTER 2. Building a Bitcoin Payment System 3. Building Your Own Cryptocurrency 4. Peer-to-Peer Auctions in Ethereum 5. Tontine Game with Truffle and Drizzle 6. Blockchain-Based Futures System 7. Blockchains in Business 8. Creating an ICO 9. Distributed Storage IPFS and Swarm 10. Supply Chain on Hyperledger 11. Letter of Credit (LC) Hyperledger 12. Other Books You May Enjoy

The emergence of blockchain and cryptocurrency

Many find it hard to understand the logic and the concepts behind blockchain, and why they would need it. This is primarily because we don't have a clear idea what problems it solves, or what advantages it promises.

Therefore, I believe it is necessary to clarify from the start which problems are solved by blockchain. We will start by learning about the concept and history of cryptocurrencies.

From virtual currencies to cryptocurrency

Blockchain didn't appear out of the blue. It was the product of the evolution of fintech and virtual currencies over the last few decades.

At the end of the last century, the widespread use of the internet favored the emergence of digital currencies as an extension of electronic cash systems. Many projects were developed to create new digital currencies: E-cash, E-gold, WebMoney, and Liberty Reserve, to name just a few

Despite huge success in the 1990s, these projects had ceased to exist by the beginning of the new century, either through bankruptcy or being halted by authorities. A currency which is capable of disappearing overnight is a real financial nightmare, but this situation was inevitable due to the centralized nature of such digital currency systems.

There was also always a need for a central authority to be involved, to fight fraud and manage trust within the system.

Because of this fatal weakness, the opposite, decentralized model was presented as a solution. However, it was hard to establish trust in such environments without any central authority. This contrast made creating a reliable digital currency a disentangled Gordian Knot.

Thankfully, the progress of cryptography and the emergence of some clever solutions such as proof of work (for example, the hashcash Project—see http://hashcash.org) brought hope of breaking the deadlock.

The invention of bitcoin

In 2008, Satoshi Nakamoto rose to the challenge and unveiled a digital currency called bitcoin. This new currency effectively harnessed cryptography techniques to manage ownership and to secure the system—hence the name cryptocurrency.

Satoshi solved the aforementioned problems by introducing what he called initially a chain of blocks. In his published whitepaper (see https://bitcoin.org/bitcoin.pdf), he presented his vision for a new peer-to-peer electronic cash system—bitcoin—and described in detail its underlying machinery—blockchain.

Bitcoin was the first reliable and distributed electronic cash system that's fully peer-to-peer, underpinned by the following basic concepts:

  • Encryption to ensure ownership and identity
  • A proof-of-work consensus mechanism for validating transactions and securing the network against double transactions
  • A transparent and shared ledger (a blockchain)
  • Pseudonymity

With the assumption that the network majority (>51%) is honest, the bitcoin system operates autonomously following the rules defined by the protocol (consensus rules) to validate a given transaction. By using a shared blockchain, each player has the ability to check the transaction's log history and the sender's solvency, then vote on whether the proceeded transaction is valid or not.

The voting depends on the overall hash-power the player puts into service to secure the network (initially, one CPU is one vote).

To use a cryptocurrency, users need to install a specific client which creates a wallet, generates cryptographic key pairs (private/public keys), and syncs the blockchain with the network. The public key is used by the client (software) to generate valid addresses, and the funds sent to a given address are controlled by the private key from which the address was calculated. In this way, we rely on secure cryptographic principles to manage ownership.

The following diagram depicts how transactions are processed in bitcoin's peer-to-peer network and added into a blockchain:

In a bitcoin network where users don't know one another, the blockchain is considered the single source of truth to which they refer to learn about previous consensus outcomes. The blockchain with the consensus protocol allows the network to manage transactions without a single point of failure.

What is blockchain?

Often confused with bitcoin, blockchain is the underlying technology used by bitcoin to operate. Concretely, it's an append-only and chronologically (timestamped) growing database, which harnesses basic cryptographic measures to protect stored transactions from being tampered with (in other words, data can't be deleted or altered).

This database, or ledger, collects and records monetary transactions validated by the network in elementary units called blocks. Once validated by the network consensus mechanism, these blocks are added to an existing sequential chain of cryptographic hash-linked blocks, to ensure the integrity of the data—hence the name blockchain.

If a single bit changes in one of the linked blocks, the hash-link collapses, the chain is broken, and it will be rejected by the network.

The following diagram shows how the blockchain is replicated and processed by the members of the network to ensure that everyone has a consistent view of the transaction log. When a new block is validated, all nodes synchronize the same copy:

The diagram also shows us that blockchain implements a special data structure, consisting of linked blocks storing transactions and smart contracts. Let us take a closer look at these key elements in detail.

Blocks

If we consider blockchain to be a ledger or a book, a block can be compared to a page or a table in which we record a collection of confirmed transactions. Each block stored in the blockchain is uniquely identified by a hash, and composed of a header and a body.

The header encloses information about its creation (timestamp, Merkle root, Nonce, difficulty target, and version), and a reference to a previous block, whereas the body is a collection of the accepted transactions.

When a block is successfully validated (mined), it becomes part of the official blockchain. New bitcoins are generated in the block (a coinbase transaction) and paid to the validators (or miners).

Transactions

Transactions are the most fundamental building blocks of the blockchain system. They represent the transfer of value (cryptocurrency) within the blockchain network between two addresses.

More tangibly, they are represented by small data structures, defined by the blockchain protocol (such as bitcoin or Ethereum), which specifies their attributes (metadata, inputs, outputs, and so on), and model.

Before broadcasting the transaction, the user sending the funds signs it using their private key (managed by their wallet), and specifies the destination address. Digital signatures and public keys are used to enable network users to validate the transaction, and to check whether the sender has the right to spend the bitcoins held by a specific address.

Smart contracts

Smart contracts are one of the most exciting concepts in blockchain, representing self-executing scripts stored on the blockchain itself. The smart contract takes the blockchain concept to the next stage, enabling it to translate business logic into inviolable contract terms, which will be autonomously executed without relying on a broker, lawyer, or other intermediary.

The earliest form of a smart contract was defined in bitcoin using basic locking and unlocking scripts, but the concept evolved with the emergence of other blockchains.

Smart contracts are one of the more powerful, disruptive forces within blockchain, and are garnering more and more business attention, as described in the Gartner report Why Blockchain’s Smart Contracts Aren’t Ready for the Business World (see https://www.gartner.com/smarterwithgartner/why-blockchains-smart-contracts-arent-ready-for-the-business-world/). Gartner estimates that by 2022, smart contracts will be used by more than 25% of global organizations.

Owing to their importance, we will return later in this book to introduce you to smart contracts in leading blockchain platforms—bitcoin, Ethereum, and Hyperledger.

We have now finished describing the concepts—let's practice a little bit to understand what has been presented so far.

You have been reading a chapter from
Blockchain By Example
Published in: Nov 2018
Publisher: Packt
ISBN-13: 9781788475686
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime