Lec3 Elements
Lec3 Elements
ELEMENTS
What is Blockchain?
• Spanned over multiple organizations or individuals who may not (need not)
trust each other
• In Bitcoin: A block may contain more than 500 transactions on average, the
average size of a block is around 1 MB (an upper bound proposed by Satoshi
Nakamoto in 2010)
• May grow up to 8 MB or sometime higher (several conflicting views on this!!)
• Larger blocks can help in processing large number of transactions in one go.
• But longer time for verification and propagation
Structure of a Block (Reference: Bitcoin)
• Two components:
• Block Header
• List of Transactions
H4 = Hash(H3)
Block Generation Puzzle
Merkle Root Block Hash Merkle Root Block Hash Merkle Root Block Hash
Find out the nonce which generates the desired hash (certain zero
bits at the prefix - 0000000000000000004a2b84f93a285b7a7………)
Block Header (Reference: Bitcoin)
• Mining – the mechanism to generate the hash
• The mechanism needs to be complicated enough, to make the blockchain tamper H0
proof
• Bitcoin Mining: Hk = Hash(Hk-1 || T || Nonce || Something more)
• Find the nonce such that Hk has certain predefined complexity (value less than a
target value) H1 = Hash(H0)
• Requirements
• All the replicas need to be updated with the last mined block
• All the replicas need to be consistent – the copies of the Blockchain at
different peers need to be exactly similar
Transactions in a Block (Reference: Bitcoin)
• If you change a transaction, you need to change all the subsequent block
hashes
TX 1 TX 3
TX 0
input() input()
input()
100 BTC
output1() output1()
output1()
output2()
output2()
TX 2 TX 4
input() input()
output1() output1()
Bitcoin Scripts – A Simple Example
T(A->B)
T(A->B)
KAPUB, SA(T(A->B))
T(A->B)
KAPUB, SA(T(A->B))
T(A->B)
scriptPubKey, scriptSig
T(A->B)
scriptPubKey, scriptSig
Transaction 18E14A7B6A30…
Input
scriptSig: D61967F63C7DD…
OP_DUP
OP_HASH160
Transaction scriptPubKey: 16UwLL9Risc3QfPqBUvKof…
OP_EQUALVERIFY
Output OP_CHECKSIG
<pubHash>
OP_HASH160 <pubKeyHash> OP_EQUALVERIFY
OP_CHECKSIG <pubKey>
<sig>
• Top stack item is hashed (RIPEMD-160 hashing)
OP_EQUALVERIFY OP_CHECKSIG
Bitcoin Scripts
OP_EQUALVERIFY OP_CHECKSIG
<pubKeyHash>
<pubHash>
<pubKey>
• Equality is checked between the top two items in <sig>
the stack
OP_CHECKSIG
Bitcoin Scripts
OP_CHECKSIG
<pubKey>
<sig>
TRUE
Bitcoin Script Instructions
• Anyone-can-spend outputs
scriptPubKey: {empty}
scriptSig: OP_TRUE
Source: https://en.bitcoin.it/wiki/Script
Interesting Bitcoin Scripts
Source: https://en.bitcoin.it/wiki/Script
Bitcoin P2P Network
• An ad-hoc network with random topology, Bitcoin protocol runs on TCP port
8333
• New nodes can join any time, non-responding nodes are removed after 3
hours
Joining in a Bitcoin P2P Network
Joining in a Bitcoin P2P Network
Joining in a Bitcoin P2P Network
Seed Node
Give me the
address
Joining in a Bitcoin P2P Network
Seed Node
<address list>
Joining in a Bitcoin P2P Network
Seed Node
Joining in a Bitcoin P2P Network
Start transaction
Transaction in a Bitcoin Network
Validate the
Transaction
Transaction Flooding in a Bitcoin Network
Flood the
Transaction
Transaction Flooding in a Bitcoin Network
I have already
A->B:BTC10
seen the
transaction
Which Transactions Should You Relay?
• The script matches with a pre-given set of whitelist scripts – avoid unusual
scripts, avoid infinite loops
• Does not conflict with other transactions that I have relayed after getting the
blockchain updated – avoid double spending
Transaction Flooding in a Bitcoin Network
A->B:BTC10
C->D:BTC20 A->B:BTC10
C->D:BTC20
A->B:BTC10
A->B:BTC10 A->B:BTC10
C->D:BTC20
C->D:BTC20
C->D:BTC20
A->B:BTC10
Block 3 Block 6
Block 4 Block 9
Block Propagation – Accept One of the Longest Chains
Block 3 Block 6
“Accidental” forks occur rarely. Even if they occur, eventually only one becomes part of
the longest chain
There are “intentional” forks of two type: hard forks and soft forks to come up with new
versions like Bitcoin Cash, etc., or to upgrade software versions
[ Will be further discussed during our lectures on consensus ]
Which Block to Relay
• Controlled Supply: Must be limited for the currency to have value – any
maliciously generated currency needs to be rejected by the network
• Bitcoins are generated during the mining – each time a user discovers a new
block
• The rate of block creation is adjusted every 2016 blocks to aim for a
constant two week adjustment period
• The last bitcoin will be mined in 2140 (estimated and unless changed)
Bitcoin Basics – Creation of Coins
• When multiple valid continuation to this chain appear, only the longest such
branch is accepted and it is then extended further (longest chain)
• All that is needed is a (set of) private key(s) – Public key can be generated
from the private key.
• Safely store the private key – in your desktop, on the web, mobile phone,
special hardware attachment, printed on a piece of paper as QR
• For online payment, you can use the wallet and an appropriate mode of
applying the private key
• For off line payments like in store payments or paying to your friend, you can
use your mobile phone to present the private key or use the hardcopy!! As
simple as using PayTm, Google Pay and so on.
Bitcoin Exchange