Xiangxiang's Personal Site

Machine Learning & Security Engineer
生命不息,折腾不止,留下一点活着的记录.

View on GitHub
23 April 2016

Bitcoin

by xiangxiang

Mastering bitcoin

0x00 Outline

0x01 Two basic problems

  1. Can I trust the money is authentic and not counterfeit?

  2. Can I be sure that no one else can claim that this money belongs to them and not me? (Aka the “double-spend” problem.)

0x02 Bitcoin’s four key innovations

0x03 Transactions

3.1 Transaction Lifecycle


To prevent spamming, denial-of-service attacks, or other nuisance attacks against the bitcoin system, every node independently validates every transaction before propagating it further.

Validation checklist,ch8-tx-verification

3.2 Transaction Structure

Size Field Description
4 bytes Version Specifies which rules this transaction follows
1–9 bytes (VarInt) Input Counter How many inputs are included
Variable Inputs One or more transaction inputs
1–9 bytes (VarInt) Output Counter How many outputs are included
Variable Outputs One or more transaction outputs
4 bytes Locktime A Unix timestamp or block number

A transaction is a data structure that encodes a transfer of value from a source of funds, called an input, to a destination, called an output. Transaction inputs and outputs are not related to accounts or identities

3.3 Transaction Inputs


coinbase transaction; The user’s wallet application will typically select from the user’s available UTXO various units to compose an amount greater than or equal to the desired transaction amount.

3.4 The structure of a transaction input

Size Field Description
32 bytes Transaction Hash Pointer to the transaction containing the UTXO to be spent
4 bytes Output Index The index number of the UTXO to be spent; first one is 0
1-9 bytes (VarInt) Unlocking-Script Size Unlocking-Script length in bytes\, to follow
Variable Unlocking-Script A script that fulfills the conditions of the UTXO locking script.
4 bytes Sequence Number Currently disabled Tx-replacement feature\, set to 0xFFFFFFFF

In simple terms, transaction inputs are pointers to UTXO

Transaction Outputs

Size Field Description
8 bytes Amount Bitcoin value in satoshis (10 -8 bitcoin)
1–9 bytes (VarInt) Locking-Script Size How many inputs are included
Variable Locking-Script A script defining the conditions needed to spend the output

3.5 A principle example of a Bitcoin transaction with 1 input and 1 output

Input:

Previous tx: f5d8ee39a430901c91a5917b9f2dc19d6d1a0e9cea205b009ca73dd04470b9a6

Index: 0

scriptSig : 304502206e21798a42fae0e854281abd38bacd1aeed3ee3738d9e1446618c4571d10

90db022100e2ac980643b0b82c0e88ffdfec6b64e3e6ba35e7ba5fdd7d5d6cc8d25c6b241501

Output:

Value: 5000000000

scriptPubKey : OP_DUP OP_HASH160 404371705fa9bd789a2fcd52d2c580b65d35549d

OP_EQUALVERIFY OP_CHECKSIG

3.6 Standard Transactions

3.6.1 Scripting Language

[] = stack
2->[2]
3->[2 3]
OP_ADD->[5]
5->[5 5]
OP_EQUAL->[TRUE]

3.6.2 Pay-to-Public-Key-Hash (P2PKH)

OP_DUP OP_HASH160 <Public Key Hash> OP_EQUAL OP_CHECKSIG
<Signature> <Public Key>
<Signature> <Public Key> OP_DUP OP_HASH160
<Public Key Hash> OP_EQUAL OP_CHECKSIG

3.6.3 Pay-to-Public-Key

<Public Key A> OP_CHECKSIG
<Signature from Private Key A>
<Signature from Private Key A> <Public Key A> OP_CHECKSIG

3.6.4 Pay-to-Public-Key

M <Public Key 1> <Public Key 2> ... <Public Key N> N OP_CHECKMULTISIG
OP_0 <Signature B> <Signature C>
A locking script example (2-3)
2 <Public Key A> <Public Key B> <Public Key C> 3 OP_CHECKMULTISIG

3.6.5 Data Output (OP_RETURN)

3.6.6 Pay-to-Script-Hash (P2SH)

Multi-Signature
2 <PK1> <PK2> <PK3> <PK4> <PK5> 5 OP_CHECKMULTISIG (Redeem Script)
<Sig1> <Sig3>
RIPEMD160{SHA256(Locking script)} -> 20-byte hash value
P2SH locking:  OP_HASH160 <20-byte hash> OP_EQUAL
Unlocking: <Sig1> <Sig3> 2 PK1 PK2 PK3 PK4 PK5 5 OP_CHECKMULTISIG

Benefits of pay-to-script-hash

0x04 The Blockchain

Size Field Description
4 bytes Block Size The size of the block\, in bytes\, following this field
80 __ __bytes Block Header Several fields form the block header
1-9 bytes (VarInt) Transaction Counter How many transactions follow
Variable Transactions The transactions recorded in this block

In simple terms, transaction inputs are pointers to UTXO

4.1 The structure of the block header

Size Field Description
4 bytes Version A version number to track software/protocol upgrades
32 __ __bytes Previous Block Hash A reference to the hash of the previous (parent) block in the chain
__32 __ __bytes __ Merkle Root A hash of the root of the merkle tree of this block’s transactions
4 bytes Timestamp The approximate creation time of this block (seconds from Unix Epoch)
4 bytes Difficulty Target The proof-of-work algorithm difficulty target for this block
4 bytes Nonce A counter used for the proof-of-work algorithm

In simple terms, transaction inputs are pointers to UTXO

4.2 Block Identifiers

Why only header?Merkle Root

0x05 Mining


Finally, after 13.44 million blocks, in approximately 2140, almost 2,099,999,997,690,000 satoshis, or almost 21 million bitcoins, will be issued. every 4 years,1/2;init=50

5.1 Mining steps

  1. Aggregating Transactions into Blocks, While searching for a solution to the block 1
  2. Once upon receiving block 1 and validating it, check all the transactions in the memory pool, remove any that were included in block 1
  3. Immediately constructs a new empty block(a candidate for block 2)

step 1. After validating transactions->a candidate block.

5.2 Transaction Age, Fees, and Priority


To construct the candidate block, Jing’s bitcoin node selects transactions from the memory pool by applying a priority metric to each transaction and adding the highest priority transactions first

5.3 The Generation Transaction(coinbase transaction)

5.4 Transaction Structure (Recall)

Size Field Description
4 bytes Version Specifies which rules this transaction follows
1–9 bytes (VarInt) Input Counter How many inputs are included
Variable Inputs One or more transaction inputs
1–9 bytes (VarInt) Output Counter How many outputs are included
Variable Outputs One or more transaction outputs
4 bytes Locktime A Unix timestamp or block number

The structure of a normal transaction input

Size Field Description
32 bytes Transaction Hash Pointer to the transaction containing the UTXO to be spent
4 bytes Output Index The index number of the UTXO to be spent; first one is 0
1-9 bytes (VarInt) Unlocking-Script Size Unlocking-Script length in bytes\, to follow
Variable Unlocking-Script A script that fulfills the conditions of the UTXO locking script.
4 bytes Sequence Number Currently disabled Tx-replacement feature\, set to 0xFFFFFFFF

In simple terms, transaction inputs are pointers to UTXO, exception coinbase

The structure of a generation transaction input

Size Field Description
32 bytes Transaction Hash All bits are zero: Not a transaction hash reference
4 bytes Output Index The index number of the UTXO to be spent; first one is 0
1-9 bytes (VarInt) Coinbase Data Size Length of the coinbase data\, from 2 to 100 bytes
Variable Unlocking-Script Arbitrary data used for extra nonce and mining tags in v2 blocks\, must begin with block height
4 bytes Sequence Number set to 0xFFFFFFFF

In simple terms, transaction inputs are pointers to UTXO, exception coinbase

The structure of the block header

Size Field Description
4 bytes Version A version number to track software/protocol upgrades
32 bytes Previous Block Hash A reference to the hash of the previous (parent) block in the chain
32 bytes Merkle Root A hash of the root of the merkle tree of this block’s transactions
4 bytes Timestamp The approximate creation time of this block (seconds from Unix Epoch)
4 bytes Difficulty Target The proof-of-work algorithm difficulty target for this block
4 bytes Nonce A counter used for the proof-of-work algorithm

5.5 Mining the Block

Brute-force;随着时间,target原来越小

5.6 Successfully Mining the Block

0x06 Research Topics

refs

tags: bitcoin