-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBitcoin_Blockchain Notes
More file actions
57 lines (33 loc) · 4.64 KB
/
Bitcoin_Blockchain Notes
File metadata and controls
57 lines (33 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Bitcoin / Blockchain Notes
********** MAIN POINTS OF BITCOIN CODE **********
Generating the genesis block.
Generating the Merkle Root of all the transactions in a block.
********** Merkle Roots and Merkle Trees **********
The Merkle Root is one of the things that protects the integrity of Bitcoin transactions.
The Merkle Root of Bitcoin is the root node of a binary tree in which the nodes with the greatest height are the hashes of each transaction in the bitcoin block. The binary tree is the Merkle Tree. The nodes in the second highest level of the tree each only have one leaf, which are those hashed transactions. You can think of it as an inverted binary tree, with the root at the bottom - you start with the leaves. After hashing each transaction you hash each pair of hashes in order, and keep hashing each pair on up the levels of the tree until you are left with only two nodes left, when these are hashed together you get the Merkle Root.
When hashing two Merkle leafs together the hashes are just concatenated together and then hashed.
********** BLOCKS **********
Each block has an auto-incrementing block number, starting with the first.
Go to blockexplorer.com to check out data about the bitcoin blocks.
Each block is hashed and this hash expresses the difficulty in mining that block, based on the number of zeroes the has starts with. The hash is basically the ID of that block. And the next block must contain the previous block's hash in it to prove that it came after. This is how blocks are timestamped.
The nonce in each block is what a miner has to come up with to find the block and get the coinbase reward. The nonce will generate a hash of the block with the given number of leading zeroes determined by the block difficulty at that time.
********** TRANSACTIONS **********
Each Transaction has a transaction hash that represents its ID, and the sender and recipient addresses, along with the amount of bitcoins sent, the number of inputs, the number of outputs, the size in bytes, and the transaction fee.
The transaction hash is just the rest of the data of the transaction hashed.
The hash algorithm used is SHA-256. In bitcoin the hashes are actually generated by doing two SHA-256's instead of just one, so hashing the original hash. Satoshia liked double hashing.
The fundamental building block of a bitcoin transaction is a transaction output. Bitcoin full nodes track all available and spendable outputs, known as "unspent transaction outputs", or UTXO. The balance for a bitcoin address is just the combined set of all UTXO across the blockchain associated with the keys of that wallet. Most bitcoin client's maintain a database to store a quick reference to all the UTXO associated with the wallet rather having to search the whole blockchain each time the wallet is opened.
Outputs are discrete and indivisble units of value, denominated in satoshis. An unspent output can only be consumed in its entirety by a transaction.
A transaction takes previously recorded UTXO's as inputs and creates new transaction outputs made up from those. If the total is greater than the amount of bitcoin being sent, then one output will be sent back to the wallet address as change. After subtracting the outputs from the inputs, and bitcoin that is left over is used as the transaction fee.
Transaction outputs consist of two parts: an amount of bitcoin denominated in bitcoin, and a cryptographic puzzle that determines the conditions required to spend the output. The cryptographic puzzle is also known as a locking script, a witness script, or a scriptPubKey. Bitcoin's transaction scripting language is used in the locking script.
Transaction outputs are serialized (made into a byte stream) for transmissions over the network. The serialization format of a transaction output is:
Amount: 8 bytes (little-endian), bitcoin value in satoshis
Locking-Script Size: 1-9 bytes (Varint), locking-script length in bytes
Locking-Script: Variable size, a script defining the conditions needed to
spend the output
Most bitcoin libraries and frameworks don't store transactions internally as byte-streams because that would required complex parsing every time you need to access a single field, so instead they are stored in data structures. Converting from a byte-stream to a data structure is deserialization. Converting to a byte-stream for hashing and sending over the network is serialization. Most bitcoin libraries have built-in functions for transaction serialization and deserialization.
********** x **********
********** x **********
********** x **********
********** x **********
********** x **********
********** x **********