Blockchain
References
Andreas M. Antonopoulos.(2017). Mastering Bitcoin.
Imran Bashir .(2018). Mastering Blockchain.
Tutorial for building a cryptocurrency : [Link]
Blockchain Roadmap
I. Course Overview
1.1 Course overview and roadmap
1.2 What is blockchain?
1.3 The Blockchain in Practice :
+ Cryptocurrencies and Cryptography
+ Mining and Bitcoin
1.4 Roadmap to build the Blockchain
Blockchain Roadmap
II. Build the Blockchain - Type of Blocks
2.1 Set Up the Blockchain Application
2.2 Create the block
2.3 Genesis Block
2.4 Mine Blocks
2.5 SHA256 Hash Function
2.6 Test blockchain
Blockchain Roadmap
III. Build the blockchain - the Chain
3.1 Build the Blockchain Class
3.2 Test the Blockchain
3.3 Chain Validation
3.4 Test Chain Validation
3.5 Replace the Chain
3.6 Test Replace the Chain
Blockchain Roadmap
IV. Develop the Blockchain Application
4.1 Organize the Project
4.2 Blockchain API
4.3 Mine Blocks Post Request
Blockchain Roadmap
V. Create the Blockchain Network
5.1 Peer to peer server
5.2 Create the Websocket Server
5.3 Connect the Blockchain Peers
5.3 Handle messages from Peers
5.4 Synchronize the Blockchain across Peers
Blockchain Roadmap
VI. Proof Of Work, Wallet and Transaction on Blockchain
Main goal of the course
● Build a blockchain and cryptocurrency to understand these technologies.
● Learn by building
Course roadmap
● Code the course blockchain.
● Build an API around the blockchain.
● Create a dynamic peer-to-peer server for multiple contributors.
● Implement a proof-of-work system.
● Create an transaction system for a cryptocurrency.
Programming language
Uses NodeJS or any programming languages for the project.
Motivation
● The exponentially-growing need for blockchain engineers.
● Build blockchain yourself, and demonstrate understanding.
● It’s fascinating and fun.
What is the Blockchain?
The blockchain is a distributed and decentralized ledger that stores data such as
transactions between individuals and that ledger is publicly shared across all the
nodes of its network.
What is the Blockchain?
Ledger
A ledger is a record keeping book that records all the transactions of an
organization.
Distributed
Centralized vs Decentralized
Centralized Decentralized
● Only entity records the data ● Everyone records the data
● The central entity has a lot of power ● Everyone has equal power
● Full authority to fine or reward ● Fair and transparent system
● Complete trust with the entity ● Trustless
Why use the Blockchain?
● Decentralization leads to a trustless system.
● No middle men and no fees.
● High secure and no central point of failure.
● Dependable data.
The Blockchain in Practice
Cryptocurrency
● A cryptocurrency is a digital medium of exchange.
● It has three main features: a secure blockchain, wallets, and mining.
Leverages the blockchain
● How is this secure?
● Uses cryptography to generate digital signatures.
Digital Signatures
Digital Signatures
Digital Signatures
Digital Signatures
● ~1077
● ~1047
● ~1018
Wallets
● Objects that store the private and public key of an individual.
● The public key is the address of the wallet.
● Help sign transactions.
Mining
● Transactions are temporarily “unconfirmed”.
● Include blocks of transactions by solving a “proof of work”
❖ Difficult to solve, and computationally expensive.
❖ One solved, the miner can add the block and other miners will verify.
❖ Miners are rewarded for adding a block to the chain.
❖ The difficulty can adjust to control the rate of new blocks coming in.
Consensus Algorithms
● Proof of Work (PoW)
● Proof of Stake (PoS)
● Delegated Proof of Stake (DPoS)
● Proof of Elapsed Time (PoET)
● Proof of Deposit (PoD)
● Proof of Importance (PoI)
● Proof of Activity (PoA)
● Proof of Capacity (PoC)
● Proof of Storage (PoS)
Consensus Algorithms
Proof-of-Work (PoW)
Proof-of-Work (PoW)
BITCOIN
● The first decentralized cryptocurrency in 2009.
● Great growth, and widespread adoption.
BITCOIN
Mining
Mining
ETHEREUM
● Ethereum is a decentralized platform that runs smart contracts: applications
that run exactly as programmed without any possibility of downtime,
censorship, fraud or third-party interference
ETHEREUM
Block Structure
Block
● Timestamp in milliseconds.
● previvousHash – the hash of block before it.
● hash – based on its own data.
● The data to store
Block
Data Hash
Generating a block
Genesis block
Validating the integrity of blocks
Choosing the longest chain
Replace Chain
Valid Chain
Communicating with other nodes
An essential part of a node is to share and sync the blockchain with other nodes.
The following rules are used to keep the network in sync.
● When a node generates a new block, it broadcasts it to the network
● When a node connects to a new peer it querys for the latest block
● When a node encounters a block that has an index larger than the current
known block, it either adds the block the its current chain or querys for the full
blockchain.
Communicating with other nodes
Logistics
Logistics
Logistics
● Wal-Mart is focusing specifically on food tracking, traceability, and safety
● Together with partners, Wal-Mart has conducted a blockchain test designed to
trace the origin and care of food products such as pork from China and
mangoes from Mexico
E-commerce
Blockchain in VietNam
NAPAS, VietinBank, VIB, TPBank
Blockchain in VietNam
Gcaeco Exchange
Cryptocurrency
Block structure
Block hash
Proof of Work
Difficulty, nonce and the proof-of-work puzzle
Difficulty, nonce and the proof-of-work puzzle
Block with difficulty and nonce
Find block with difficulty
Consensus on the difficulty
● BLOCK_GENERATION_INTERVAL, defines how often a block should be
found. (in Bitcoin this value is 10 minutes)
● DIFFICULTY_ADJUSTMENT_INTERVAL, defines how often the difficulty
should adjust to the increasing or decreasing network hashrate. (in Bitcoin this
value is 2016 blocks)
Consensus on the difficulty
● The expected time is calculated like this: BLOCK_GENERATION_INTERVAL *
DIFFICULTY_ADJUSTMENT_INTERVAL
Consensus on the difficulty
Cumulative difficulty
Transactions
Public-key cryptography and signatures
Private-keys and public keys
● A valid private key is any random 32 byte string, eg.
19f128debc1b9122da0635954488b208b829879cf13b3d6cac5d1260c0fd967c
● A valid public key is ‘04’ concatenated with a 64 byte string, e.g
04bfcab8722991ae774db48f934ca79cfb7dd991229153b9f732ba5334aafcd8e7266e47
076996b55a14bf9913ee3145ce0cfc1372ada8ada74bd287450313534a
● The public key can be derived from the private key. The public-key will be
used as the ‘receiver’ (= address) of the coins in a transaction.
Transactions
● Transactions consists of two components: inputs and outputs.
● Outputs specify where the coins are sent and inputs give a proof that the coins
that are actually sent exists in the first place and are owned by the “sender”.
● Inputs always refer to an existing (unspent) output
Transaction outputs
● Transaction outputs (txOut) consists of an address and an amount of coins.
● The users have the private-key of the referenced public-key (=address) will be
able to access the coins.
Transaction inputs
● Transaction inputs (txIn) provide the information “where” the coins are coming
from.
● Each txIn refer to an earlier output, from which the coins are ‘unlocked’, with
the signature.
● These unlocked coins are now ‘available’ for the txOuts.
● The signature gives proof that only the user, that has the private-key of the
referred public-key ( =address) could have created the transaction.
Transactions
Transaction structure
Transaction id
● The transaction id is calculated by taking a hash from the contents of the
transaction.
Transaction signatures
● It is important that the contents of the transaction cannot be altered, after it has
been signed.
● The transactions are public, anyone can access to the transactions, even
before they are included in the blockchain.
Transaction signatures
Unspent transaction outputs
● A transaction input must always refer to an unspent transaction output (uTxO).
● When you own some coins in the blockchain, what you actually have is a list of
unspent transaction outputs whose public key matches to the private key you
own.
Unspent transaction outputs
Updating unspent transaction outputs
● Every time a new block is added to the chain, we must update our list of
unspent transaction outputs.
● This is because the new transactions will spend some of the existing
transaction outputs and introduce new unspent outputs.
Wallet
● The goal of the wallet is to create a more abstract interface for the end user.
● The end user must be able to
○ Create a new wallet (=private key in this case)
○ View the balance of his wallet
○ Send coins to other addresses
Wallet
Wallet
Wallet balance
● When you own some coins in the blockchain, what you actually have is a list of
unspent transaction outputs whose public key matches to the private key you
own.
Generating transactions
● User A has balance of 50 coins and the user wants to send 10 coins to user B
Generating transactions
● User C has initially 0 coins
● User C receives 3 transactions worth of 10, 20 and 30 coins
● User C wants to send 55 coins to user D
Generating transactions
Q&A