List of articles

brief introduction

The bottom layer of bitcoin is blockchain technology , Blockchain is also known for bitcoin . Compared with other blockchain technologies , What are the characteristics of bitcoin's blockchain ? As the originator of deblocking , What's the difference between them ? Come and have a look with us .

Bitcoin's Network

Bitcoin uses P2P(peer-to-peer) The Internet , this P2P The 1 pet. P2P, Here's the peer-to-peer network architecture , It's not a person to person lending model .

P2P It means that every computer in the same network is equal to each other , Each node provides network services together , There is no such thing as “ special ” node . Each network node uses “ flat (flat)” The topology of is interconnected . stay P2P There is no server in the network (server)、 Centralized services 、 And hierarchy .

The traditional network structure is client-server The pattern of , be-all client It's all with server Interactive access to information , as long as server Hang up ,client It's no use .

And in the P2P In the network , No, server The concept of , Each node can act as a server. By contrast P2P The network is more stable than C-S The architecture of the system is much more stable .

Network discovery and synchronization

Since it is P2P The Internet , So here comes the question , This P2P How is the network established ? How are nodes discovered ?

I did P2P The students who download should have heard of the concept of seed , This seed contains the addresses of other active nodes . You can connect to the corresponding node by downloading the seed .

And each node keeps the most recently connected or active node , So it's a huge P2P The Internet .

alike , The currency of the P2P The same is true of the Internet .

How do new nodes discover peer nodes in the network ? Although there are no special nodes in the bitcoin network , But the client maintains a list , There's a list of nodes that are stable for a long time . Such a node is called “ Seed node (seed nodes)”

Nodes must continue to do two things : Discover new nodes when losing existing connections , And help other nodes when they start up .

SPV node

We introduced it earlier , In the world of bitcoin, there are no accounts , And there's no balance , Only those scattered in the blockchain UTXO(Unspent Transaction Outputs).

So if you want to verify the transaction , You need to find all the transactions related to the transaction from the historical transaction , So as to complete , Comprehensive verification .

The problem with this is , If you download all the history , So it takes hundreds of G Hard disk space for , This is unimaginable for mobile phones or other lightweight clients .

therefore SPV There is .SPV The full name is Simplified payment verification, It's called simple authentication payment .

SPV It's not the whole blockchain that's preserved , It's the head of the blockchain , Because each blockchain head has only 80 byte , So even if you download and save all the blocks, it won't be very big .

Blockchain head

The block header consists of three sets of block metadata . The first is a set of data that references the hash value of the parent block , This set of metadata is used to connect the block with the previous block in the blockchain .

The second set of metadata , That's the difficulty 、 Time stamps and nonce, Related to mining competition .

The third set of metadata is merkle The root ( A data structure to effectively summarize all transactions in a block ).

Nonce、 Difficulty targets and timestamps will be used in the mining process ,Merkle The root is used to index and organize all the transaction information of the block .

The picture above is a chain of blockchain heads .

Merkle Tree

Merkle Tree, It's a tree ( The tree in the data structure ), The Internet is mostly called Merkle Hash Tree, This is because It's constructed by Merkle Tree All nodes of are Hash value .Merkle Tree It has the following characteristics :

  1. It's a kind of tree , It could be a binary tree , It can also be multi tree , No matter how many forked trees , It has all the characteristics of tree structure ;

  2. Merkle On the leaf node of the tree value, It's up to you , It depends on your design , Such as Merkle Hash Tree The data will be Hash Value as the value of the leaf node ;

  3. Non leaf node value It's based on the values of all the leaf nodes below it , And then it is calculated according to a certain algorithm . Such as Merkle Hash Tree Non leaf nodes of value The calculation method is to combine all the child nodes of the node , Then the combination results are analyzed hash The calculated hash value.

With Merkle Tree, We just need to know other things about the transaction to be verified Merkle Tree Information in , You can calculate the whole Merkle Tree Value , In this way, the header information can be directly used for verification . This is it. SPV Principle .

Blockchain in bitcoin

Blockchain is a data structure that is orderly linked from the back to the front by blocks containing transaction information . It can be stored as flat file( A file that contains records that have no relative relationship ), Or stored in a simple database .

Bitcoin core client uses Google Of LevelDB The database stores blockchain metadata .

It consists of a block header containing metadata and a long series of transactions that constitute the main body of the block . The block is 80 byte , And the average deal is at least 250 byte , And on average, each block contains at least 500 A deal .

Block identifier

How to represent a block ? We use block markers .

The block master identifier is its encrypted hash value , One passes SHA256 The algorithm calculates the block head twice to get the digital fingerprint . Produced 32 Byte hashes are called block hashes , But the more accurate name is : Block header hash value , Because only the block head is used to calculate .

The second way to identify a block is by its position in the blockchain , namely “ Block height (block height)”. The first block , The block height is 0
And block hash value is different from , Block height is not the only identifier . Although a single block always has a clear 、 Fixed block height , But the reverse is not true , A block height does not always identify a single block . Two or more blocks may have the same block height , Competing for the same place in the blockchain .

Genesis block

The first block in the blockchain was created in 2009 year , It's called the genesis block . It is the common ancestor of all the blocks in the blockchain , That means you're from any block , Back in the chain , Eventually they will all reach the genesis block .

Because the genesis block is incorporated into the bitcoin client software , So every node starts with a blockchain that contains at least one block , This ensures that the genesis block will not be changed . Every node has “ know ” The hash value of the genesis block 、 structure 、 The time of creation and a transaction inside . therefore , Each node takes the block as the first block of the blockchain , To build a secure 、 The root of a trusted blockchain .

The hash value of the genesis block is :
0000000000 19d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f

The genesis block contains a hidden message . In its Coinbase The input of the transaction contains such a sentence “The Times 03/Jan/2009 Chancellor on brink of second bailout forbanks.” This is the headline of the front page of the times that day , Quote this sentence , It is a description of the generation time of the block , It can also be seen as a half joking reminder of the importance of an independent monetary system , And tell people that with the development of bitcoin , An unprecedented worldwide monetary revolution is about to take place . The message was embedded in the genesis block by the founder of bitcoin, Nakamoto .

coinbase The value of is :04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73

The decoding method is as follows :

stay python shell Next :


Output :

‘\x04\xff\xff\x00\x1d\x01\x04EThe Times 03/Jan/2009 Chancellor on brink of second bailout for banks’


This paper introduces the network of bitcoin and the related concepts of blockchain in bitcoin , I hope you like it .

The author of this article :flydean Program those things

Link to this article :

In this paper, the source :flydean The blog of

Welcome to my official account. : Program those things , More wonderful waiting for you !