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
Security Tokens and Stablecoins Quick Start Guide

You're reading from   Security Tokens and Stablecoins Quick Start Guide Learn how to build STO and stablecoin decentralized applications

Arrow left icon
Product type Paperback
Published in Apr 2019
Publisher Packt
ISBN-13 9781838551063
Length 234 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Authors (3):
Arrow left icon
Weimin Sun Weimin Sun
Author Profile Icon Weimin Sun
Weimin Sun
Xun (Brian) Wu Xun (Brian) Wu
Author Profile Icon Xun (Brian) Wu
Xun (Brian) Wu
Angela Kwok Angela Kwok
Author Profile Icon Angela Kwok
Angela Kwok
Arrow right icon
View More author details
Toc

Bitcoin basics

The bitcoin platform is a peer-to-peer network. It connects computers around the world. Each computer is a node with equal status, except for a subset of nodes called miners. Mining nodes play the role of collecting/validating transactions, creating a new block, and adding a validated block to the blockchain. A transaction refers to an action such as issuing bitcoin or transferring BTC from one address to another, and so on.

A peer-to-peer network can connect worldwide nodes and allow participants to trade with one another. However, the physical connection is not enough to make two untrusting parties trade with each other. To make them trade, bitcoin takes the following measures:

  • Every node saves a complete copy of the ledger. Thus, any alteration to a transaction on the chain becomes practically not feasible.
  • Transactions are grouped into blocks. A non-genesis block is linked to its previous block by storing that block's hash. Consequently, a change to a transaction requires changes to be made to all subsequent blocks, and these changes have to be repeated on all nodes where a copy of the ledger is saved. This clever design makes hacking the distributed ledger extremely difficult.
  • Bitcoin built in a consensus mechanism to address the double-spending issue; that is, the same BTC is spent twice.
  • Hashes are extensively used to protect identities of parties, and to detect any changes that are occurring in a block.
  • It uses public/private keys and addresses to mask identities of trading parties.
  • Signs a transaction digitally.

With these measures, untrusting parties feel comfortable to trade because of the following reasons:

  • The transaction is immutable and permanent. Neither party can nullify a transaction unilaterally.
  • No double-spending is possible.
  • Transaction and settlement occur simultaneously; therefore, there is no settlement risk.
  • Identities are protected.
  • Transactions are signed by both parties, which minimizes the possibility of legal disputes in the future.

The distributed ledger

At a financial institution, the ledger is the principal book for recording all financial transactions. Bitcoin maintains a ledger for bookkeeping transactions of coins and other transactions. The difference is that a bank's ledger is centralized, and bitcoin's ledger is distributed. Consequently, a bank's ledger is at risk of being manipulated for nefarious purposes, that is, cooking the book. On the other hand, bitcoin's ledger is very difficult to be changed.

Blockchain's ledger consists of entries resulting from transactions entered by users, where users submit transactions. Each transaction contains the following information:

  • Sources (from the address) of the coins to be transferred from
  • The number of coins to be transferred
  • Destinations (send-to address) where coins should be transferred to

Both source and destination addresses are 64-character hashes. Here is an example:

979e6b063b436438105895939f4ff13d068428d2f71312cf5594c132905bfxy1

An address is like a customer's bank account number. However, there are fundamental differences between them. For example, a bank has a centralized place for saving metadata on an account, for example, the customer name, account open date, and account type, and so on. Also, the balance of an account is calculated and saved. A bitcoin address does not have metadata and maintains no balance. Addresses are referred to only in bitcoin transactions. When an address does not contain any unused coins, a new request for transferring a coin from the address will fail a transaction validation due to an insufficient fund error.

A bitcoin coin does not associate with a physical object such as a file or a physical coin. Only transactions and addresses support its existence. For example, if you want to know the total number of coins that have been minted so far, you have to go through all addresses with unused coins and add them up.

When a user enters a transaction request at a node, bitcoin software installed at the node broadcasts the transaction to all nodes. Nodes on the network will validate the transaction by retrieving all historical transactions containing the input addresses and ensuring that coins that are transferred out from these addresses are legitimate and sufficient. After that, the mining nodes start to construct a block by collecting the validated transactions. Normally, one block contains between 1,500 and 2,000 transactions. If a miner wins a race for resolving a difficult puzzle, the miner gets the role for adding the new block to the blockchain. Bitcoin takes approximately every 10 minutes to add a new block.

A bitcoin blockchain can diverge due to protocol change, software upgrade, or fixing hacked blocks. The splitting point where the divergence starts is called a fork. There are temporary forks and permanent forks. If a permanent fork occurs due to, for example, malicious attacks, it is called a hard fork. If a permanent fork occurs due to configuration or a software upgrade, it is called a soft fork. A hard fork makes previously invalid blocks/transactions valid, and a soft fork makes previously valid blocks/transactions invalid.

The consensus mechanism

The double-spending issue refers to the act of using the same coin more than once. If this problem is not resolved, a bitcoin coin loses its scarcity. Scarcity is a key feature of a currency. Without it, the coin can no longer be called a cryptocurrency. The consensus mechanism is designed for resolving the double-spending problem. To understand how the mechanism works, you need to know the concepts of proof of work (PoW) and mining.

As we learned earlier, a miner has to solve a mathematical puzzle ahead of other miners to receive the role of being a builder of the next block and receive an award for doing the work. The work of resolving the mathematical problem is called the PoW. The mathematical problem itself does not have a value. The main purpose is to give a sufficient window of time to miners validating transactions. This window of waiting time is maintained at 10 minutes. A miner can query its copy of the distributed ledger and validate the following facts:

  • The requester of a transaction has the coins
  • Any other transactions in the ledger have not spent the same coins
  • Other transactions within the candidate block do not spend the same coins

The process of repeatedly guessing an answer to the puzzle is called mining. Hardware that is manufactured and dedicated to the mining work is called a mining rig.

As per bitcoin protocol, mining is the only way to issue a new coin. Rewarding a miner serves several purposes:

  • Compensates a miner's investments on hardware
  • Covers mining operation costs such as utility bills, human salaries, site rentals, and so on
  • Gives miners incentives to safeguard the network from being attacked by malicious hackers

The total number of mintable bitcoin coins is fixed at 21 million. Currently (January 2019), close to 17.5 million coins have been issued. The bitcoin protocol defines a rule for dynamically adjusting the payout rate to the mining work, and the remaining 3.5 million coins will need another 122 years to be mined completely.

The mining payout rate is dynamically adjusted and follows the following rule:

The rate changes at every 210,000 blocks. It is a function of the block height on the chain with genesis=0, and is calculated using 64-bit integer operations as (50 * 100,000,000) >> (height / 210,000). The rate that initially started with 50 coins has fallen to 25 coins at block 210,000. It fell to 12.5 coins at block 420,000 and will eventually go down to 0 when the network reaches the size of 6,930,000 blocks.

Bitcoin adjusts the difficulty level of the puzzle for maintaining the 10-minute window. Based on the most recent rate of a new block being added, the difficulty level is calculated or adjusted accordingly. If the average rate of new blocks being added is fewer than ten minutes, the difficulty level will be increased. If the average rate takes more than ten minutes, it's decreased. The difficulty level is updated every 2,016 blocks.

With the relevant concepts being explained, we are ready to talk about the mining steps. For illustration purposes, we assume that the mathematical puzzle is to find the first hash value whose first character is 0 in order to maintain the 10-minute per new block window. Per the bitcoin protocol, a miner follows these steps to solve the puzzle:

  1. First, obtain the SHA-256 hash of a block in construction.
  2. If the resulted hash has a leading 0, the miner solves the puzzle. The miner adds the block to his/her copy of the distributed ledger on the node and claims the coin rewards. The winner broadcasts the news to other nodes. Other miners of the network check the answer and validate that the new block contains valid transactions.
  3. If passing the checks, all nodes on the network add the block to their copies of the ledger. Miners start to work on the next block.
  4. If the winner is a hacker and includes bad transactions such as double-spending a coin, the validation on transactions will fail. Other miners will not include the block in their ledger copies. They will continue to mine on the current block. As time passes, the path containing the bad block will no longer be the longest path. Per bitcoin protocol, the longest path is considered to be the blockchain and should be copied by all nodes. In other words, the path containing the bad block becomes an orphan, and will eventually be dropped. This is essentially how all nodes on the network reach a consensus to add only good blocks to the blockchain and prevent bad blocks from being included.
  1. If the resulted hash does not start with 0, per protocol, a miner adds a sequence number, known to be a nonce, starting from 0 at the end of the input text and retries the hash.
  2. If a resulted hash still does not contain a leading 0, change the nonce to 1, and obtain a new hash. Repeat the steps until a miner finds a new hash with a leading zero.

The following is a hypothetical example. The original plaintext is input string and a nonce varying from 0 to 3. Their corresponding SHA-256 hashes are as follows:

  • input string: f23f4781d6814ebe349c6b230c1f700714f4f70f735022bd4b1fb69421859993
  • input string0: 5db70bb3ae36e5b87415c1c9399100bc60f2068a2b0ec04536e92ad2598b6bbb
  • input string1: 5d0a0f2c69b88343ba44d64168b350ef62ce4e0da73044557bff451fd5df6e96
  • input string2: 7b8fe11e193f835e37301f20416c76c9cd55d962a5ad009f4302ee2607ba8d1a
  • input string3: c37e5a2e94575060277e3b1abf9d3ebbe44274e72bb86f2a526266c9c5aa3722

The algorithm for adjusting the difficulty level is to change the required number of leading 0s, along with some minor tuning. Requiring additional leading 0s will increase the average trying times, and therefore the difficulty level is higher. The current bitcoin difficulty level is 18 leading 0s to maintain the 10 minute window.

Keys and digital wallets

When a bitcoin address is created, a pair of public and private keys are generated as well. The public key is made known to the public, and the address owner keeps the private key. To spend coins associated with the address, the owner provides a digital signature that's generated with the private key and sends a transaction request to the network. In other words, you have to pose both the address and the private key to spend the corresponding coins.

If an owner loses the address and the private key, the person then permanently loses the coins. Hence, it is important to save the information at a secured place. Digital wallets are available for assisting users in managing public/private keys and addresses. You can use a wallet to do the following:

  • Generate addresses and their corresponding public/private keys
  • Save and organize information such as keys, addresses, coins owned, and so on
  • Send a transaction request to the bitcoin network

A private key is a 256-bit long hash, and a public key is 512-bit long.

They can be converted into shorter lengths in hexadecimal representation. The following screenshot is a pair of sample public/private keys, along with an address:

A private key can also be expressed in a string of 51 characters starting with a 5 and a public key in a string of 72 characters. Here are some examples:

private key:
5Jd54v5mVLvyRsjDGTFbTZFGvwLosYKayRosbLYMxZFBLfEpXnp;
public key: BFCDB2DCE28D959F2815B16F81798483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68
You have been reading a chapter from
Security Tokens and Stablecoins Quick Start Guide
Published in: Apr 2019
Publisher: Packt
ISBN-13: 9781838551063
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