Getting BlockChain Data:
Blocks;
Transactions;
Sending Transactions;
Cryptocurrency Functions:
Generating Private/Public keys, Hashing, Address Encoding etc;
Creating transactions;
Signing transactions;
Support functions;
Build A Block
Receive the transaction broadcase;
Verify the crypto in the transaction;
Add it to the unconfirmed Pool;
Do some hard maths on all the transactions in the pool;
Broadcast the Block to the network;
The Block is added to the blockchain;
Blockchains as Distributed Database
Everyone running the Bitcoin client is part of the network;
New blocks are boradcast to the network;
Everyone updates their local copy of the blockchain;
If you’re behind the current height of the chain, you can ask other nodes for copies of the Blocks needed to catch-up;
Implications:
Tranactions take time to ‘confirm’;
Each transaction, once it’s in an accepted block has a height;
Each increase in blockchain height is called a confirmation;
A transaction 5 blocks below the top of the chain is said to have ‘6 confirmations’;
The merchant cant decide how many confirmations is sensible to warit for;
Current default is it wait for 6 confirmations for anything of value;
Blocks store data, in Bitcoin, it’s the transactions, but it could be any digital data;
Blocks are created periodically(on average, 10mins for Bitcoin) by a process called ‘mining’;
A block represents a set of events that have occurred over a particular time frame(ususally, since the previous block);
Block aren’t identified by their height, but by their id; (may be multiple blocks at same height)
Block id is the hash of the metadata in the block;
Block id is a digial fingerprint of that block;
Some metadata:
A version number of the block format;
A link to the previous block that came immediately before it;
Merkle root of all the transactions in the block;
Timestamp of when the block was created;
Mining difficulty;
Nonce for Proof-of-Work;
All the transactions that were recorded in this block;
All the transactions in the block are used to create the Merkle Root Hash;
It’s added to the other metadata in the header, with a nonce;
We hash the whole thing, look at the resulting hash, if it starts with a required number of leading ‘0’(which means the hash need to be small enough), we’ve solved the block and broadcast it;
If not, we increment the nonce and try again;
Repeat until we find the leading zero;
Difficulty defines how many leading zeros when solveing the block;
The more zeros we need to find, the more attemps at hashing we need.
More hashing power becomes the driving force amongst miners;
Miner insert a special transaction at the beginning of block called ‘coinbase’ transaction. This ‘pays’ the miner for the work they did, and collects the transaction fees from all the transactions;
Transactions
Transactions tranfer control of coins from inputs to outputs;
Control is enforced by cryptography;
Construct A Transaction
You need the address of the person you’re going to pay;
Find UTXO’s Unspent Transaction Outputs that exceed the amount you wish to pay;
Calculate the transaction fee(optional but recommended);
Create the outputs with the correct scriptPubKey; scriptPubKey defines who can spend the coin by specifying a small verification program that is run in order to perform that verification.
Sign the transaction details;
Broadcast the transaction, see if it works;