Blockchain Technologies
By E.M.Roopa Devi
Unit-I introduces the basic concepts of distributed computing based on which blockchain technology works.
It also covers history, definitions, features, types and benefits of blockchains along with consensus mechanisms that are at the core of blockchain technology.
Unit-II Decentralization covers the concepts of decentralization and its relationship with blockchain technology. Various methods and platforms that can be used to decentralize a process or system have also been introduced.
Cryptography and Technical Foundations introduces the theoretical foundations and cryptography which is necessary to fully understand blockchain technology. Concepts such as public and private key cryptography with practical examples are included. Finally, an introduction to financial markets is also included as there are many interesting use cases for blockchain technology in the financial sector.
Unit-III Bitcoin is the first and largest blockchain and introduces technical concepts related to bitcoin cryptocurrency in detail.
Alternative coins introduces alternative cryptocurrencies that were introduced after the invention of Bitcoin. It also presents examples of different altcoins, their properties and how they have been developed and implemented.
In-depth discussion on history, definition of smart contracts, Ricardian contracts, Oracles and the theoretical aspect of smart contracts are provided.
Unit-IV Ethereum 101 introduces the design and architecture of the Ethereum blockchain in detail. It covers various technical concepts related to the Ethereum blockchain that explains the underlying principles, features and components of this platform in depth.
Ethereum Development provides a detailed practical introduction to development of decentralized applications and smart contracts using the Ethereum blockchain.
Unit-V presents a discussion about the hyperledger project from the Linux foundation, which includes different blockchain projects introduced by its members.
Blockchain-Outside of Currencies provides a practical and detailed introduction to applications of blockchain technology in fields other than cryptocurrencies including Internet of Things, government, media and finance.
UNIT-I
Distinction between databases and blockchain ledgers
| Blockchains |
Databases have admins & centralized control | No on is the admin or in-charge |
Only entities with rights can access database | Anyone can access (public) blockchain |
Only entities entitled to read or write can do so | Anyone with right proof of work can write on the blockchain |
Databases are fast | Blockchains are slow |
No history of records & ownership of digital records | History of records & ownership of digital records |
Gartner's hype cycle for emerging technologies
Distributed systems
Design of a Distributed System
CAP theorem
Faulty nodes are of two types:
Byzantine Generals problem
Consensus
Consensus components
● Blockchain structure
o No more client/server architecture with name roles
Consensus components
● Blockchain structure
o Peer-to-peer Architecture with pseudonymous client bearing key pairs. Each node as a database copy.
Consensus components
● Blockchain structure
o Data structure:
Consensus components
● Blockchain structure
o Blocks of data:
Consensus Mechanisms
Types of Consensus mechanism
History of Blockchain
Bitcoin-2008(Satoshi Nakamoto)
Blockchain-2009
Electronic cash
The concept of Electronic Cash
Invention of bitcoin blockchain using various concepts
Introduction to blockchain
Network view of a blockchain
Structure of a block
Various technical definitions of blockchains
Generic structure of a blockchain
Generic elements of a blockchain
Addresses
Transaction
Block
Peer-to-peer network
Scripting or programming language
Virtual machine
State machine
Nodes
Smart contracts
Features of a blockchain
Distributed Consensus
Transaction Verification
Platforms for Smart Contracts
Transferring value between peers
Generating Cryptocurrency
Smart Property
Provider of security
Immutability
Uniqueness
Smart Contracts
Applications of Blockchain Technology
How blockchain accumulates blocks
Steps 4 and 5 can be considered non-compulsory as the transaction itself is finalized in step 3; however, block confirmation and further transaction reconfirmations, if required, are then carried out in steps 4 and 5.
Tiers of Blockchain Technology
Blockchain X.0
Types of blockchain
Public blockchains (permission-less ledgers)
Semi-private blockchains
Sidechains (pegged sidechains)
Permissioned ledger
Distributed ledger
Shared ledger
Fully private and proprietary blockchains
Tokenized blockchains
Tokenless blockchains
Consensus in blockchain
Consensus is basically a distributed computing concept that has been used in blockchain in order to provide a means of agreeing to a single version of truth by all peers on the blockchain network. The following two categories of consensus mechanism exist:
Proof of Work
Proof of Stake
Delegated Proof of Stake
Proof of Elapsed Time
Deposit-based consensus
Proof of importance
Federated consensus or federated Byzantine consensus
Reputation-based mechanisms
Practical Byzantine Fault Tolerance
Proof of Activity
Proof of Capacity
Proof of Storage
Proof of Authority (PoA)
CAP theorem and blockchain
Benefits and limitations of blockchain
Decentralization
Transparency and trust
Immutability
High availability
Highly secure
All transactions on a blockchain are cryptographically secured and provide integrity.
Simplification of current paradigms
Faster dealings
In the financial industry, especially in post-trade settlement functions, blockchain can play a vital role by allowing the quicker settlement of trades as it does not require a lengthy process of verification, reconciliation, and clearance because a single version of agreed upon data is already available on a shared ledger between financial organizations.
Cost saving
As no third party or clearing houses are required in the blockchain model, this can massively eliminate overhead costs in the form of fees that are paid to clearing houses or trusted third parties.
Platform for smart contracts
Challenges and limitations of blockchain technology
UNIT-II
Decentralization
Decentralization using Blockchain
Information and Communication Technology
A traditional distributed system comprises many servers performing different roles
A decentralized system (based on blockchain) where an exact replica of the applications and data is maintained across the entire network on each participating node
Methods of decentralization
Disintermediation
Through competition (contest-driven decentralization)
Scale of decentralization
Is a blockchain really needed? When is a blockchain required? In what circumstances, is blockchain preferable to traditional databases? Answers:
Routes to decentralization
How to decentralize
1. What is being decentralized?
2. What level of decentralization is required?
3. What blockchain is used?
4. What security mechanism is used?
Answers for the above questions
Example Application - a money transfer system
Blockchain and full ecosystem decentralization
Storage
Communication
Computation (computing power and decentralization)
Terminologies concerned to decentralization and its applications
Smart contract
Autonomous agents
Decentralized organization (DOs)
Decentralized autonomous organization (DAO)
Decentralized autonomous corporations
Decentralized autonomous societies
Decentralized applications
Requirements of a decentralized application
In order for an application to be considered a decentralized application, it must meet the following criteria.
Operations of a DAPP
Design of a DAPP
Traditional Application Architecture (generic client/server)
Generic DApp architecture
Examples of some decentralized DAPP applications built on Ethereum blockchain
KYC-Chain
Open Bazaar
Lazooz
Platforms for decentralization
Many platforms for decentralization are introduced to make distributed application development easy, accessible, and secure for users.
Ethereum
Maidsafe
Lisk
Cryptography and Technical Foundations
On Ubuntu Linux distribution, OpenSSL is usually already available or it can be installed using the command: $ sudo apt-get install openssl
Introduction
Mathematics
GROUP
FIELD
A FINITE FIELD
ORDER
PRIME FIELDS
RING
A CYCLIC GROUP
AN ABELIAN GROUP
MODULAR ARITHMETIC
Cryptography
Confidentiality
Integrity
Authentication
ENTITY AUTHENTICATION
Various factors are:
1. The first factor : hardware token or smart card.
2. The second factor : uses biometric features in order to identify the user.
DATA ORIGIN AUTHENTICATION
Non-repudiation
1. Message is sent directly from originator A to recipient B.
2. Message is sent to a delivery agent from originator A, which then delivers the message to recipient B.
Accountability
Cryptographic primitives
Keyless primitives
b. PNGs
2. Hash Functions
Symmetric cryptography
There are two types of symmetric ciphers, stream ciphers and block ciphers. Data Encryption Standard (DES) and Advanced Encryption Standard (AES) are common examples of block ciphers, whereas RC4 and A5 are commonly used stream ciphers.
STREAM CIPHERS
Operation of a stream cipher
Block ciphers
Block encryption mode
In this mode, plaintext is divided into blocks of fixed length depending on the type of cipher used and then the encryption function is applied on each block.
Keystream generation modes
In this mode, the encryption function generates a keystream that is then XORed with the plaintext stream in order to achieve encryption.
Message authentication modes
Cryptographic Hashes
Electronic code book
Electronic code book mode for block ciphers
Cipher block chaining
Counter mode
There are other modes, such as Cipher Feedback mode (CFB), Galois Counter mode (GCM), and Output Feedback mode, which are also used in various scenarios.
Data Encryption Standard (DES)
Advanced Encryption Standard (AES)
AES STEPS
1. In the AddRoundKey step, the state array is XORed with a subkey, which is derived from the master key.
2. This is the substitution step where a lookup table (S-box) is used to replace all bytes of the state array.
3. This step is used to shift each row except the first one in the state array to the left in a cyclic and incremental manner.
4. Finally, all bytes are mixed in this step in a linear fashion column-wise.
Asymmetric cryptography
Integer factorization
Large integers are very hard to factor. Ex.RSA
Discrete logarithm
For example, consider the following equation:
32 mod 10 = 9
Given 9, finding 2 and the exponent of the generator 3 is very hard. This hard problem is commonly used in Diffie-Hellman key exchange and digital signature algorithms.
Elliptic curves
y2 = x3 + ax + b
a, b are integers that can have various values and are elements of the field on which the elliptic curve is defined.
instead of real numbers
Pubic and private keys
RSA Encryption C = Pe mod n
Decryption P = Cd mod n
ELLIPTIC CURVE CRYPTOGRAPHY (ECC)
Mathematics behind ECC
Point Addition: P + Q
Point doubling: 2P = P + P
Discrete logarithm problem
Hash functions are typically used to provide data integrity services.
ECC
Properties:
h(x)=y
Given x and h(x), it is impossible to find any other message m
Two different input messages should not hash to the same output
RIPEMD: RIPEMD is RACE Integrity Primitives Evaluation Message Diges which is build based on MD4. 128-bit, 160-bit, 256-bit, and 320-bit are available
Whirlpool: This is based on a modified version of Rijndael cipher known as W. and uses the Miyaguchi-Preneel compression function, which is a type of one-way function used for the compression of two fixed length inputs into a single fixed length output.
It is a single block length compression function.
SHA-256 algorithm:
1. Padding of the message, which is used to make the length of a block to 512-bits if it is smaller than the required block size of 512-bits.
2. Parsing the message into message blocks that ensure that the message and its padding is divided into equal blocks of 512-bits.
3. Setting up the initial hash value, which is the eight 32-bit words obtained by taking the first 32-bits of the fractional parts of the square roots of the first eight prime numbers. These initial values are randomly chosen in order to initialize the process and gives a level of confidence that no backdoor exists in the algorithm.
• Hash computation:
1. Each message block is processed in a sequence and requires 64 rounds to compute the full hash output. Each round uses slightly different constants to ensure that no two rounds are the same.
2. First, the message schedule is prepared.
3. Then, eight working variables are initialized.
4. Then, the intermediate hash value is calculated.
5. Finally, the message is processed and the output hash is produced.
Sponge and squeeze model:
Message Authentication codes (MACs)
MACs using block ciphers
HMACs (hash-based MACs)
Secret prefix: M = MACk(x) = h(k\\x)
Secret suffix: M=MACk(x) = h(x\\k)
RSA digital signature algorithms
Two steps:
1. Calculate the hash value of the data packet
2. Sign the hash value with the signer’s private key
Properties:
a. authenticity
b. unforgeability
c. non-reusability
Sign then encrypt
The sender digitally signs the data using private key and appends the signature to the data and then encrypts the data and the digital signature using receiver’s public key. This scheme is more secure than encrypt then sign scheme.
Encrypt then sign
The sender encrypts the data using receiver’s public key and then digitally signs the encrypted data.
In practice, a digital certificate that contains the digital signature is issued by a Certificate Authority (CA) that associates a public key with an identity.
Elliptic curve digital signature algorithm
ECDSA is a DSA based on elliptic curves. It is based on modular exponentiation and discrete logarithm problem. It is used on the Bitcoin and Ethereum blockchain platforms to validate messages and provide data integrity services.
Algorithm steps:
ECDSA signing - How are signatures created?
The ECDSA signing algorithm takes a message and a private key and produces a signature, a pair of integers (r, s).
The ECDSA signing algorithm works by:
h = hash(msg)
s = k^-1 * (h + p * r) mod n
where p is the signer’s private key, and the order n
ECDSA Signature Verification
The ECDSA signature verification algorithm works by converting s back to R (R’) using the public key and message hash. The recovered R's x-coordinate r' is compared with r from the signature:
s1 = s^-1 (mod n)
R' = (h * s1) * G + (r * s1) * pubKey
Merkle Trees
PATRICIA TREES
DISTRIBUTED HASH TABLES (DHTS)
Working of DHT:
UNIT-III
Introduction
Bitcoin
Bitcoin definition
KEYS AND ADDRESSES
lANAguGG8bikEv2fYsTBnRUmx7QUcK58wt
PUBLIC KEYS IN BITCOIN
PRIVATE KEYS IN BITCOIN
BITCOIN CURRENCY UNITS
The smallest bitcoin denomination is the Satoshi.
BASE58CHECK ENCODING
VANITY ADDRESSES
Transactions
The transaction life cycle
1. A user/sender sends a transaction using wallet software or some other interface.
2. The wallet software signs the transaction using the sender's private key.
3. The transaction is broadcasted to the Bitcoin network using a flooding
algorithm.
4. Mining nodes include this transaction in the next block to be mined.
5. Mining starts once a miner who solves the Proof of Work problem broadcasts the newly mined block to the network. Proof of Work is explained in detail later in this chapter.
6. The nodes verify the block and propagate the block further, and confirmation starts to generate.
7. Finally, the confirmations start to appear in the receiver's wallet and after approximately six confirmations, the transaction is considered finalized and confirmed. However, six is just a recommended number; the transaction can be considered final even after the first confirmation. The key idea behind waiting for six confirmations is that the probability of double spending is virtually eliminated after six confirmations.
The transaction structure
A transaction at a high level contains metadata, inputs, and outputs. Transactions are combined to create a block.
• MetaData: This part of the transaction contains some values such as the size of the transaction, the number of inputs and outputs, the hash of the transaction, and a lock_time field. Every transaction has a prefix specifying the version number.
• Inputs: Generally, each input spends a previous output. Each output is considered an Unspent Transaction Output (UTXO) until an input consumes it.
• Outputs: Outputs have only two fields, and they contain instructions for the sending of bitcoins. The first field contains the amount of Satoshis, whereas the second field is a locking script that contains the conditions that need to be met in order for the output to be spent.
More information on transaction spending using locking and unlocking scripts and producing outputs is discussed later in this section.
• Verification: Verification is performed using bitcoin's scripting language.
THE SCRIPT LANGUAGE
Types of transaction
• Pay to Public Key Hash (P2PKH): P2PKH is the most commonly used transaction type and is used to send transactions to the bitcoin addresses. The format of the transaction is shown as folows:
ScriptPubKey: OP_DUP 0P_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
ScriptSig: <sig> <pubKey>
The ScriptPubKey and ScriptSig parameters are concatenated together and executed.
ScriptPubKey: 0P_HASH160 <redeemScriptHash> OP_EQUAL ScriptSig: [<sig>...<sign>] <redeemScript>
• MultiSig (Pay to MultiSig): M of n multisignature transaction script is a complex type of script where it is possible to construct a script that required multiple signatures to be valid in order to redeem a transaction. Various complex transactions such as escrow and deposits can be built using this script. The template is shown here:
ScriptPubKey: <m> <pubKey> [<pubKey> ... ] <n> OP_CHECKMULTISIG
ScriptSig: 0 [<sig > ... <sign>]
Raw multisig is obsolete, and multisig is usually part of the P2SH redeem script, mentioned in the previous bullet point.
• Pay to Pubkey: This script is a very simple script that is commonly used in coinbase transactions. It is now obsolete and was used in an old version of bitcoin. The public key is stored within the script in this case, and the unlocking script is required to sign the transaction with the private key.
The template is shown as follows:
<PubKey> OP_CHECKSIG
• Null data/OP_RETURN: This script is used to store arbitrary data on the blockchain for a fee. The limit of the message is 40 bytes. The output of this script is unredeemable because 0P_RETURN will fail the validation in any case. ScriptSig is not required in this case.
The template is very simple and is shown as follows:
0P_RETURN <data>
WHAT IS UTXO?
Unspent Transaction Output (UTXO) is an unspent transaction output that can be spent as an input to a new transaction.
Transaction fee
Contracts
Transaction malleability
Transaction pools
Transaction verification
This verification process is performed by bitcoin nodes.
1. Check the syntax and ensure that the syntax of the transaction is correct.
2. Verify that inputs and outputs are not empty.
3. Check whether the size in bytes is less than the maximum block size, which is 1 MB currently.
4. The output value must be in the allowed money range (0 to 21 million BTC).
5. All inputs must have a specified previous output, except for coinbase transactions, which should not be relayed.
6. Verify that nLockTime must not exceed 31-bits. For a transaction to be valid, it should not be less than 100 bytes. Also, the number of signature operands in a standard signature should be less than or not more than 2.
7. Reject nonstandard transactions; for example, ScriptSig is allowed to only push numbers on the stack. ScriptPubkey not passing the isStandardQ checks.
8. A transaction is rejected if there is already a matching transaction in the pool or in a block in the main branch.
9. The transaction will be rejected if the referenced output for each input exists in any other transaction in the pool.
10. For each input, there must exist a referenced output transaction. This is searched in the main branch and the transaction pool to find whether the output transaction is missing for any input, and this will be considered an orphan transaction. It will be added to the orphan transactions pool if a matching transaction is not in the pool already.
11. For each input, if the referenced output transaction is the coinbase, it must have at least 100 confirmations; otherwise, the transaction will be rejected.
12. For each input, if the referenced output does not exist or has been spent already, the transaction will be rejected.
13. Using the referenced output transactions to get input values, verify that each input value, as well as the sum, is in the allowed range of 0-21 million BTC.
14. Reject the transaction if the sum of input values is less than the sum of output values.
15. Reject the transaction if the transaction fee would be too low to get into an empty block.
Blockchain
The structure of a block
The structure of a block header
A visualization of blockchain, block, block header, transaction and script
The genesis block
Target = Previous target * Time/2016 * 10 minutes
MINING
TASK OF MINERS
SYNCHING UP WITH THE NETWORK
PROOF OF WORK
H ( N || P_hash || Tx\\Tx\\... Tx) < Target
Where N is a nonce, P_hash is a hash of the previous block, Tx represents transactions in the block, and Target is the target network difficulty value.
THE MINING ALGORITHM
• Assemble a set of potential transactions broadcasted on the network into a block.
• Compute the double hash of the block header with a nonce and the previous hash using the SHA256 algorithm.
• If the resultant hash is lower than the current difficulty level (target), then stop the process.
• If the resultant hash is greater than the current difficulty level (target), then repeat the process by incrementing the nonce. As the hash rate of the bitcoin network increased, the total amount of 32-bit nonces was exhausted too quickly. In order to address this issue, the extra nonce solution was implemented, whereby the coinbase transaction is used as a source of extra nonce to provide a larger range of nonces to be searched by the miners.
• Mining difficulty increased over time and bitcoins that could be mined by single CPU laptop computers now require dedicated mining centers to solve the hash puzzle. The current difficulty level can be queried using the bitcoin command line interface using the following command:
$ bitcoin-cli getdifficulty
THE HASHING RATE
MINING SYSTEMS
CPU
GPU
FPGA
ASICS
MINING POOLS
The bitcoin network
Version: This is the first message that a node sends out to the network, advertising its version and block count. The remote node
then replies with the same information and the connection is then established.
Verack : This is the response of the version message accepting the connection request.
Inv: This is used by nodes to advertise their knowledge of blocks and transactions.
Getdata: This is a response to inv, requesting a single block or transaction identified by its hash.
Getblocks: This returns an inv packet containing the list of all blocks starting after the last known hash or 500 blocks.
Getheaders : This is used to request block headers in a specified range.
Tx : This is used to send a transaction as a response to the getdata protocol message.
Block: This sends a block in response to the getdata protocol message.
Headers: This packet returns up to 2,000 block headers as a reply to the getheaders request.
Getaddr: This is sent as a request to get information about known peers.
Addr: This provides information about nodes on the network. It contains the number of addresses and address list in the form of IP address and port number.
Wallets
:-/.bitcoin$ file wallet.dat
wallet.dat: Berkeley DB (Btree, version 9, native byte-order)
WALLET TYPES
Bitcoin Payments
Bitcoin investment and buying and selling bitcoins
Alternative Coins
Methods of providing an initial number of altcoins are discussed as follows:
• Proof of burn: Another approach to allocating initial funds to a new altcoin is Proof of burn, also called a one-way peg or price ceiling. In this method users permanently destroy a certain quantity of bitcoins in proportion to the quantity of altcoins to be claimed. For example if 10 bitcoins were destroyed then altcoins can have a value no greater than the amount of bitcoins destroyed. This basically means that bitcoins are being converted into altcoins by burning them.
• Proof of Ownership: Instead of permanently destroying bitcoins, an alternative method is to prove that users own a certain number of bitcoins. This proof of ownership can be used to claim altcoins by tethering altcoin blocks to bitcoin blocks. For example, this can be achieved by merged mining in which effectively bitcoin miners can mine altcoin blocks while mining for bitcoin without any extra work.
• Pegged sidechains: Sidechains, as the name suggests, are blockchains separate from the bitcoin network but bitcoins can be transferred to them. Altcoins can also be transferred back to the bitcoin network. This concept is called a two-way peg.
Theoretical foundations
Alternatives to Proof of Work
PROOF OF STORAGE
PROOF OF STAKE
Proof of Coinage
Proof of deposit
Proof of burn
Proof of activity
NON-OUTSOURCEABLE PUZZLES
Difficulty adjustment and retargeting algorithms
T = Time previous * time actual/2016 *10 min
KIMOTO GRAVITY WELL
KGW = 1 + (0.7084 *pow((double(PastBlocksMass)/double(144)), -1.228))
DARK GRAVITY WAVE
2222222/ (((Difficulty+2600)/9)A2)
DIGISHIELD
MIDAS
Limitations in bitcoin
Privacy and anonymity
MIXING PROTOCOLS
THIRD-PARTY MIXING PROTOCOLS
INHERENT ANONYMITY
Extended protocols on top of bitcoin
COLORED COINS
COUNTERPARTY
Development of altcoins
CONSENSUS ALGORITHMS
HASHING ALGORITHMS
DIFFICULTY ADJUSTMENT ALGORITHMS
INTER-BLOCK TIME
BLOCK REWARDS
REWARD HALVING RATE
BLOCK SIZE AND TRANSACTION SIZE
INTEREST RATE
COIN AGE
TOTAL SUPPLY OF COINS
Namecoin
Obtaining Namecoins
Generating Namecoin records
Litecoin
Scrypt uses the following parameters to generate a derived key (Kd):
• Passphrase: This is a string of characters to hash
• Salt: This is a random string that is provided to Scrypt functions (generally all hash functions) in order to provide a defence against brute-force dictionary attacks using rainbow tables
• N: This is a memory/CPU cost parameter that must be a power of 2 > 1
• P: The parallelization parameter
• R: The block size parameter
• dkLen: The intended length of the derived key in bytes
Formally, this function can be written as follows:
Kd = scrypt (P, S, N, P, R, dkLen)
Scrypt algorithm
Primecoin
Zcash
(next difficulty) = (last difficulty) x SQRT [ (150 seconds) / (last solve time) ]
Various attributes of Zcash:
Smart Contract
• Automatically executable
• Enforceable
• Semantically sound
• Secure and unstoppable.
Unit-IV
Ethereum 101
Introduction
Ethereum clients and releases
The Ethereum stack
Web 3.0 Tech stack
Ethereum stack exchange
Components of Ethereum ecosystem
Ethereum blockchain
Ethereum State transition function
Currency (ETH and ETC)
Forks
The consensus mechanism
Bitcoin longest chain rule
The world state
THE ACCOUNT STATE
Nonce
Balance
Storageroot
Codehash
The world state and its relationship with accounts trie, accounts, and block header
Transactions
• Message call transactions: This transaction produces a message call that is used to pass messages from one account to another.
• Contract creation transactions: These transactions result in the creation of a new contract. This means that when this transaction is executed successfully, it creates an account with the associated code.
NONCE
Gas Price
Gas Limit
TO
VALUE
SIGNATURE
ECDSASIGN (Message, Private Key) = (V, R, S)
INIT
DATA
Relationship between transaction, transaction trie and block header
Contract creation transaction
There are a few essential parameters that are required when creating an account.
• Sender
• Original transactor
• Available gas
• Gas price
• Endowment, which is the amount of ether allocated initially
• A byte array of arbitrary length
• Initialization EVM code
• Current depth of the message call/contract-creation stack (current depth means the number of items that are already there in the stack)
Message call transaction
A message call requires several parameters for execution,
• Sender
• The transaction originator
• Recipient
• The account whose code is to be executed
• Available gas
• Value
• Gas price
• Arbitrary length byte array
• Input data of the call
• Current depth of the message call/contract creation stack
Elements of the Ethereum blockchain
Ethereum virtual machine (EVM)
EVM operation
EVM operation
Execution environment Tuple
• Available gas
• The program counter, which is a positive integer up to 256
• Memory contents
• Active number of words in memory
• Contents of the stack
EXECUTION ENVIRONMENT
There are some key elements that are required by the execution environment in order to execute the code.
1. The address of the account that owns the executing code.
2. The address of the sender of the transaction and the originating address of this execution.
3. The gas price in the transaction that initiated the execution.
4. Input data or transaction data depending on the type of executing agent. This is a byte array; in the case of a message call, if the execution agent is a transaction, then the transaction data is included as input data.
5. The address of the account that initiated the code execution or transaction sender. This is the address of the sender in case the code
execution is initiated by a transaction; otherwise, it's the address of the account.
6. The value or transaction value. This is the amount in Wei. If the execution agent is a transaction, then it is the transaction value.
7. The code to be executed presented as a byte array that the iterator function picks up in each execution cycle.
8. The block header of the current block
9. The number of message calls or contract creation transactions currently in execution. In other words, this is the number of CALLs or CREATES currently in execution.
The EVM is designed to handle exceptions and will halt (stop execution) in case any of the following exceptions occur: •
• Not having enough gas required for execution
• Invalid instructions
• Invalid destination of jump opcodes
• Invalid stack size (greater than 1024)
Machine state can be viewed as a tuple shown in the following diagram:
Machine state tuple
The virtual machine is also able to halt in normal conditions if STOP or SUICIDE or RETURN Opcodes are encountered during the execution cycle.
pragma solidity A0.4.0; contract Testl {
uint x=2;
function additionl(uint x) returns (uint y) { y=x+2;
This program is converted into bytecode
OPCODES:
Arithmetic operations
Logical operations
Environmental information
Push operations
Duplication operations
Exchange operations
Logging operations
System operations
Precompiled contracts
There are four precompiled contracts in Ethereum.
The elliptic curve public key recovery function
ECDSARECOVER (Elliptic curve DSA recover function) is available at address 1.
The ECDSA recovery function is shown as follows:
ECDSARECOVER(H, V, R, S) = Public Key
It takes four inputs: H, which is a 32 byte hash of the message to be signed and V, R, and S, which represent the ECDSA signature with the recovery ID and produce a 64 byte public key. V, R, and S.
The SHA-256 bit hash function
The RIPEMD-160 bit hash function
The identity function
Accounts
Accounts are one of the main building blocks of the Ethereum blockchain. The state is created or updated as a result of the interaction between accounts. Operations performed between and on the accounts represent state transitions. State transition is achieved using Ethereum state transition function as follows:
1. Confirm the transaction validity by checking the syntax, signature validity, and nonce.
2. Transaction fee is calculated and the sending address is resolved using the signature. Furthermore, sender's account balance is checked and subtracted accordingly and nonce is incremented. An error is returned if the account balance is not enough.
3. Provide enough ether (gas price) to cover the cost of the transaction. This is charged per byte incrementally according to the size of the transaction.
4. In this step, the actual transfer of value occurs. The flow is from the sender's account to receiver's account. The account is created automatically if the destination account specified in the transaction does not exist yet. Moreover, if the destination account is a contract, then the contract code is executed. This also depends on the amount of gas available. If enough gas is available, then the contract code will be executed fully; otherwise, it will run up to the point where it runs out of gas.
5. In cases of transaction failure due to insufficient account balance or gas, all state changes are rolled back with the exception of fee payment, which is paid to the miners.
6. Finally, the remainder (if any) of the fee is sent back to the sender as change and fee is paid to the miners accordingly. At this point, the function returns the resulting state.
Types of accounts
There are two types of accounts in Ethereum:
• Externally owned accounts
• Contract accounts
Externally owned accounts (EOAs) are controlled by a private key in bitcoin. Contract accounts are the accounts that have code associated with them along with the private key. An EOA has ether balance, is able to send transactions, and has no associated code.
A Contract Account (CA) has ether balance, associated code, and the ability to get triggered and execute code in response to a transaction or a message. Due to the Turing-completeness property of the Ethereum blockchain, the code within contract accounts can be of any level of complexity. The code is executed by EVM by each mining node on the Ethereum network. In addition, contract accounts are able to maintain their own permanent state and can call other contracts. In the serenity release, the distinction between externally owned accounts and contract accounts may be eliminated.
Block
Blocks are the main building blocks of a blockchain. Ethereum blocks consist of various components,
• The block header
• The transactions list
• The list of headers of Ommers or Uncles
The transaction list is a list of all transactions included in the block. In addition, the list of headers of Uncles is also included in the block.
Block header
Block headers are the most critical and detailed components of an Ethereum block. The header contains valuable information.
PARENT HASH
This is the Keccak 256-bit hash of the parent (previous) block's header.
OMMERS HASH
This is the Keccak 256-bit hash of the list of Ommers (Uncles) blocks included in the block.
BENEFICIARY
STATE ROOT
TRANSACTIONS ROOT
RECEIPTS ROOT
LOGS BLOOM
DIFFICULTY
NUMBER
GAS LIMIT
GAS USED
TIMESTAMP
EXTRA DATA
NONCE
The genesis block
Ethereum block structure with block header
Transaction receipts
MIXHASH
THE POST-TRANSACTION STATE
GAS USED
SET OF LOGS
Transaction validation and execution
Transactions are executed after verifying the transactions for validity.
Initial tests are listed as follows:
• A transaction must be well-formed and RLP-encoded without any additional trailing bytes
• The digital signature used to sign the transaction is valid
• Transaction nonce must be equal to the sender's accounts current nonce
• Gas limit must not be less than the gas used by the transaction
• The sender's account contains enough balance to cover the execution cost
THE TRANSACTION SUB STATE
A transaction sub-state is created during the execution of the transaction that is processed immediately after the execution completes.
This transaction sub-state is a tuple that is composed of three items.
Suicide set
This element contains the list of accounts that are disposed of after the transaction is executed.
Log series
Refund balance
An Ethereum block is considered valid if it passes the following checks:
BLOCK FINALIZATION
Block finalization is a process that is run by miners in order to validate the contents of the block and apply rewards. It results in four steps being executed.
Ommers validation
Validate Ommers (stale blocks also called Uncles). In the case of mining, determine Ommers. The validation process of the headers of stale blocks checks whether the header is valid and the relationship of the Uncle with the current block satisfies the maximum depth of six blocks. A block can contain a maximum of two Uncles.
Transaction validation
Validate transactions. In the case of mining, determine transactions. The process involves checking whether the total gas used in the block is equal to the final gas consumption after the final transaction.
Reward application
Apply rewards, which means updating the beneficiary's account with a reward balance. In Ethereum, a reward is also given to miners for stale blocks, which is 1/32 of the block reward. Uncles that are included in the blocks also receive 7/8 of the total block reward. The current block reward is 5 Ether. A block can have a maximum of two Uncles.
State and nonce validation
Verify the state and nonce. In the case of mining, compute a valid state and nonce.
BLOCK DIFFICULTY
block_diff = parent_diff + parent_diff // 2048 *
max(1 - (block_timestamp -parent_timestamp) // 10, -99) + int(2**((block.number // 100000) - 2))
Ether
Total cost = gasUsed * gasPrice
Each operation costs some gas; a high level fee schedule of a few operations
Based on the preceding fee schedule and the formula, an example calculation of the SHA3
operation can be calculated as follows:
• SHA3 costs 30 gas
• Current gas price is 25 GWei, which is 0.000000025 Ether
• Multiplying both: 0.000000025 *30 = 0.00000075 Ether
In total, 0.00000075 Ether is the total gas that will be charged.
Fee schedule
Gas is charged in three scenarios as a prerequisite to the execution of an operation:
• The computation of an operation
• For contract creation or message call
• Increase in the usage of memory
Messages
A message consists of the components:
1. Sender of the message
2. Recipient of the message
3. Amount of Wei to transfer and message to the contract address
4. Optional data field (Input data for the contract)
5. Maximum amount of gas that can be consumed
Calls
Mining
Mining is the process by which new currency is added to the blockchain. This is an incentive for the miners to validate and verify blocks made up of transactions. The mining process helps secure the network by verifying computations.
A miner performs the following functions:
1. Listens for the transactions broadcasted on the Ethereum network and determines the transactions to be processed.
2. Determines stale blocks called Uncles or Ommers and includes them in the block.
3. Updates the account balance with the reward earned from successfully mining the block.
4. Finally, a valid state is computed and block is finalized, which defines the result of all state transitions.
Ethash
Various types of mining
CPU mining
geth --mine --minerthreads <n>
geth attach
Miner.start(4) True
Miner.stop True
GPU mining
geth --rpc
ethminer -G
Mining rigs
MOTHERBOARD
SSD HARD DRIVE
An SSD hard drive is required. The SSD drive is recommended because of its much faster performance over the analog equivalent. This will be mainly used to store the blockchain.
GPU
The GPU is the most important component of the rig as it is the main workhorse that will be used for mining. For example, it can be a Sapphire AMD Radeon R9 380 with 4 GB RAM.
Linux Ubuntu's latest version is usually chosen as the operating system for the rig. There is also another variant of Linux available, called EthOS, that is especially built for Ethereum mining and supports mining operations natively.
Mining pools
ethminer -C -F http://ethereumpool.co/?
Clients and wallets
Geth
Eth
Pyethapp
Parity
Light clients
Installation
> sudo apt-get install -y software-properties -common
> sudo add-apt-repository -y ppa:ethereum/ethereum
> sudo apt-get update
> sudo apt-get install -y ethereum
ETH INSTALLATION
> sudo apt-get install cpp-ethereum
MIST BROWSER
Parity installation
The Ethereum network
MainNet
TestNet
Private net(s)
Supporting protocols
WHISPER
SWARM
Swarm and whisper fit together and work with blockchain
Ethereum Development
Setting up a development environment
Test Net (Ropsten)
$ geth --TestNet
Setting up a Private Net
1. Network ID.
2. Genesis file.
3. Data directory to store blockchain data.
NETWORK ID
THE GENESIS FILE
Data Directory
These flags are ,
Flags and their meaning
• --nodiscover: This flag ensures that the node is not automatically discoverable if it happens to have the same genesis file and Network ID.
• - -maxpeers: This flag is used to specify the number of peers allowed to be connected to the private net. If it is set to 0, then no one will be able to connect, which might be desirable in a few scenarios, such as private testing.
• - - rpc: This is used to enable the RPC interface in geth.
• - - rpcapi: This flag takes a list of APIs to be allowed as a parameter. For example, eth ,web3 will enable the web3 and eth interface over RPC.
• - - rpcport: This sets up the TCP RPC port; for example: 9999.
• - - rpccorsdomain: This flag specifies the URL that is allowed to connect to the private geth node and perform RPC operations.
• - - port: This specifies the TCP port that will be used to listen to the incoming connections from other peers.
• - - identity: This flag is a string that specifies the name of a private node.
STATIC NODES
RUNNING MIST ON PRIVATE NET
DEPLOYING CONTRACTS USING MIST
Mist provides an interface where contracts can be written in solidity and then deployed on the network.
Languages
Contracts can be programmed in a variety of languages.
• Mutan: This is a Go-style language, which was deprecated in early 2015 and is no longer used.
• LLL: This is a Lisp-like language, hence the name LLL. This is also not used anymore.
• Serpent: This is a simple and clean Python-like language. It is actively used for contract development.
• Solidity: This language has become a standard for contract writing for Ethereum.
Compilers
SOLC
INTEGRATED DEVELOPMENT ENVIRONMENTS (IDES)
Browser solidity
Remix
ETHEREUMJS
npm install -g ethereumjs-testrpc
Stestrpc
Contract development and deployment
Hyperledger
Projects
Fabric
Sawtooth lake
Iroha
Blockchain explorer
Fabric chaintool
Fabric SDK Py
Corda
Hyperledger as a protocol
Reference architecture
Requirements
Hyperledger architecture
MODULAR APPROACH
PRIVACY AND CONFIDENTIALITY
IDENTITY
AUDITABILITY
INTEROPERABILITY
PORTABILITY
Fabric
Hyperledger Fabric
Fabric architecture
MEMBERSHIP SERVICES
1. User identity validation.
2. User registration.
3. Assign appropriate permissions to the users depending on their roles.
Membership services are made up of various components:
• Registration authority (RA): A service that authenticates the users and assesses the identity of the fabric participants for issuance of certificates.
• Enrolment certificate authority: Enrolment certificates (Ecerts)
are long term certificates issued by ECA to registered participants in order to provide identification to the entities participating on the network.
• Transaction certificate authority:ln order to send transactions on the networks, participants are required to hold a transaction certificate. TCA is responsible for issuing transaction certificates to holders of Enrolment certificates and is derived from Ecerts.
• TLS certificate authority:In order to secure the network level communication between nodes on the Fabric, TLS certificates are used. TLS certificate authority issues TLS certificates in order to ensure security of the messages being passed between various systems on the blockchain network.
BLOCKCHAIN SERVICES
Consensus manager
Distributed ledger
Block structure fields
• Version: Used for keeping track of changes in the protocol.
• Timestamp: Timestamp in UTC epoch time, updated by block proposer.
• Transaction hash: This field contains the Merkle root hash of the transactions in the block.
• State hash: This is the Merkle root hash of the world state.
• Previous hash: This is the previous block's hash, which is calculated after serializing the block message and then creating the message digest by applying the SHA3 SHAKE256 algorithm.
• Consensus metadata: This is an optional field that can be used by the consensus protocol to provide some relevant information about the consensus.
• Non-Hash data: This is some metadata that is stored with the block but is not hashed. This feature makes it possible to have different data on different peers. It also provides the ability to discard data without any impact on the blockchain.
Peer to Peer protocol
Ledger storage
CHAINCODE SERVICES
• Secure container: Chaincode is deployed in Docker containers that provide a locked down sandboxed environment for smart contract execution. Currently Golang is supported as the main smart contract language, but any other mainstream language can be added and enabled if required.
• Secure registry: This provides a record of all images containing smart contracts.
EVENTS
APIS AND CLIS
Components of the Fabric
PEERS OR NODES
APPLICATIONS ON BLOCKCHAIN
Chaincode implementation
The following four functions are required in order to implement the chaincode:
• Init(): This function is invoked when chaincode is deployed onto the ledger. This initializes the chaincode and results in making a state change, which accordingly updates the ledger.
• Invoke(): This function is used when contracts are executed. It takes a function name as parameters along with an array of arguments. This function results in a state change and writes to the ledger.
• Query (): This function is used to query the current state of a deployed chaincode. This function does not make any changes to the ledger.
• Main(): This function is executed when a peer deploys its own copy of the chaincode. The chaincode is registered with the peer using this function.
• View logic: This is concerned with the user interface. It can be a desktop, web application or mobile frontend.
• Control logic: This is the orchestrator between user interface, data model, and APIs.
• Data model: This model is used to manage the off-chain data.
• Blockchain logic: This is used to manage the blockchain via the controller and the data model via transactions.
Hyperledger Fabric Application model
Sawtooth lake
PoET
Transaction families
Traditional smart contract paradigm
Sawtooth (transaction families) smart contract paradigm
Intel has provided three transaction families with Sawtooth: Endpoint registry, Integerkey, and Marketplace.
1. Endpoint registry is used for registering ledger services.
2. Integerkey is used for testing deployed ledgers.
3. Marketplace is used for selling, buying and trading operations and services.
Sawtooth_bond has been developed as a proof of concept to demonstrate a bond trading platform.
Consensus in Sawtooth
Corda
Architecture
STATE OBJECTS
TRANSACTIONS
CONSENSUS
FLOWS
Components
The Corda network has multiple components.
NODES
Nodes in a Corda network operated under a trust-less model and run by different organizations. Nodes run as part of an authenticated peer-to-peer network. Nodes communicate directly with each other using the Advanced Message Queuing Protocol (AMQP), which is an approved international standard (ISO/IEC 19464) and ensures that messages across different nodes are transferred safely and securely. AMQP works over Transport Layer Security (TLS) in Corda, thus ensuring privacy and integrity of data communicated between nodes.
Nodes also make use of a local relational database for storage. Messages on the network are encoded in a compact binary format. They are delivered and managed by using the Apache Artemis message broker (Active MQ). A node can serve as a network map service, notary, Oracle, or a regular node.
The following diagram shows a high-level view of two nodes communicating with each other:
Two nodes communicating in a Corda network
In the preceding diagram, Node 1 is communicating with Node 2 over a TLS communication channel using the AMQP protocol, and the nodes have a local relational database for storage.
PERMISSIONING SERVICE
Network map service
Transactions
Transactions on a Corda network are composed of the following elements: •
• Input references: This is a reference to the states the transaction is going to consume and use as an input.
• Output states: These are new states created by the transaction.
• Attachments: This is a list of hashes of attached zip files. Zip files can contain code and other relevant documentation related to the transaction. Files themselves are not made part of the transaction, instead, they are transferred and stored separately.
• Commands: A command represents the information about the intended operation of the transaction as a parameter to the contract. Each command has a list of public keys which represents all parties that are required to sign a transaction.
• Signatures: This represents the signature required by the transaction. The total number of signatures required is directly proportional to the number of public keys for commands.
• Type: There are two types of transactions namely, Normal or Notary changing. Notary changing transactions are used for reassigning a notary for a state.
• Timestamp: This field represents a bracket of time during which the transaction has taken place. These are verified and enforced by notary services. Also, it is expected that if strict timings are required, which is desirable in many financial services scenarios, notaries should be synched with an atomic clock.
• Summaries: This is a text description that describes the operations of the transaction.
Vaults
CorDapp
There are three main components in a Corda smart contract as follows:
1. Executable code that defines the validation logic to validate changes to the state objects.
2. State objects represent the current state of a contract and either can be consumed by a transaction or produced (created) by a transaction.
3. Commands are used to describe the operational and verification data that defines how a transaction can be verified.
Development environment
The development environment for Corda can be set up easily using the following steps.
Required software includes the following:
1. JDK 8 which is available
at http://www.oracle.com/technetwork/java/javase/downloads/index.ht ml.
2. IntelliJ IDEA community edition which is free and available at https://www.jetbrains.com/idea/download .
3. H2 database platform independent zip, and is available at http://www.h2database.com/html/download.html.
4. Git, available at https://git-scm.com/downloads.
5. Kotlin language, which is available for IntelliJ, and more information can be found at https://kotlinlang.org/.
6. Gradle is another component that is used to build Corda.
Once all these tools are installed, smart contract development can be started.
Blockchain-Outside of Currencies
Internet of Things
loT network
Network layer
Management layer
Application layer
loT five-layer model
Blockchain-based loT model
Application Layer
Transportation, financial, insurance and many others
Management Layer Data processing, analytics Blockchain Layer
Security, P2P (M2M) autonomous transactions, decentralization, smart contracts
Network Layer
LAN, WAN, PAN, Routers
Device Layer
Sensors, Actuators, smart devices Physical Objects People, cars, homes etc. etc.
Government
Border control
Automated Border Control using Blockchain
Voting
Citizen identification (ID cards)
Miscellaneous
Health
Finance
Insurance
Post trade settlement
Financial crime prevention