blockchain storage

How exactly is the blockchain stored?

block data disk

blocks/blk*.dat (in the directory ~/.bitcoin.blocks') files, including: the data of the original block, these data items are packed into a block by several. The node first writes the received block to blk00000.dat, waits until it is full (128MB), and then starts writing the next one, and so on. (The file is a binary file)
File structure:
insert image description here

  1. Magic bytes: One is distributed per block, in order to distinguish the mark of one block from another block in the process of continuous data flow. The length is four bytes, which varies according to different network tags. 4 bytes
    insert image description here

  2. size: This refers to the size of the next entire file. (The size itself is 4 bytes)
    The size limit is controlled by MAX_BLOCKFILE_SIZE in the source code, usually 128MB.

  3. block header: fixed 80 bytes, the structure is as follows. Merkle hash is a kind of hash.

block header
insert image description here
4. tx count: Indicates the number of transactions in the block.
5. Transaction data: transaction structure:
insert image description here
the analysis process from the data is roughly as follows:

Read the 4-byte version number
Parse the varint, get the input quantity n,
execute 1~n cycles, and analyze the transaction input
Parse the varint, get the output quantity m,
execute 1~m cycles, and analyze the transaction output

insert image description here

blocks/index/* (metadata) Level DB

It is a leveldb database that stores the metadata of currently known blocks, and the metadata records the location on the disk corresponding to the block.

chainstate/* Level DB

It is also a leveldb database, the purpose is to verify whether the new blocks and tx are legal. The stored data is utxo: that is, each transaction has an input and output, the input records the source of funds, and the output records the funds. The output generated is "unspent transaction output", utxo, unspent transaction output. Based on account-based,
there is no concept of account in Bitcoin, and
all currently unspent transactions and their metadata are stored in a compact form. The data here is necessary to validate new incoming blocks and transactions. In theory, these data can be reconstructed from the block data, but it will take a long time. Data verification can be performed without these data, but the existing data blocks need to be scanned, which is slow.
Data structure: merkle tree. (As long as the root hash is recorded, the changes on the entire tree can be detected.)
The block is organized into a merkle tree.
The bottom layer is: data blocks, (transaction) each block is divided into two parts, block header ( root hash); block body.
Full nodes, not all
light nodes, only block headers. It needs to be proved by merkle proof. Check on the mobile phone to see if it has been written to the blockchain.
Send a request to the whole node, as long as the hash marked in red is sent, and then verify whether these hashes are consistent with the root hash stored by itself. Give me the content of the transaction, and give me the hash on this branch, and I can prove it.
The time and space complexity of verifying this, log(n).
Prove, proof of non-memberships, that the transmission of the entire number content (n) is linear.
For the page nodes, perform hash sorting. If you know that they are in the middle of these two nodes, it means that the middle of these two nodes does not exist, log(n)
The sorted merkle tree is sorted, this is a proof of non-existence, and it is faster to search. (This is not used in the current Bitcoin, but should it be used in the domain name resolution system?)
The hash must be acyclic.

Central Bank Issues Digital Currency

Private key signature to verify whether it is genuine. Asymmetric encryption algorithm, the private key is easy to leak. Digital currency is a file, but it can be copied.
The problem of double payment, double spending, double spending attack.
The central bank's record number requires the central bank to confirm the ownership of the money.
1. Who has the right to issue currency
2. How to prevent double spending.
Data structure: blockchain.
A obtains the right to mint coins and issues 10 bitcoins. —> Wind up.
Distribution: A\B, and sign, and b's address, public key hash, which is equivalent to a bank account number.
Input: Explain the source of the coin, and the hash of B. The hash of A's public key, the hash of the source, and the hash of the public key, the output contains the hash of A.
A gives B money, encrypts it with B's public key, and after receiving the money, B decrypts it with B's own private key.
Two kinds of hash pointers:
1. The hash of the entire content that is strung together.
2. The source of the currency (to place a double-spend attack)
Bitcoin, and disclose their payment QR code.
A—>B
block header

  1. version
  2. pointer to the previous block
  3. The root hash of the merkle tree
  4. Mining difficulty target threshold target
  5. nonceRandom number. (required for mining)
    H(block header) <=target

Only the hashes of the block headers are concatenated.

Full nodes and light nodes. full node and light node (cannot independently verify the legitimacy of the transaction)

Full nodes need to verify every transaction information.
We mainly target full nodes, because light nodes do not participate in the construction and maintenance of the blockchain, but only use some blockchain information to do some queries.

If you want to use Bitcoin for transactions, you just need to use light nodes. (How to correspond to the domain name resolution system)

How to make them generate a distributed consensus. distributed consensus
distributed hash table, global hashtable. The content of the consensus includes those key value pairs.
Distributed system
In an asynchronous system, asynchronous network delay is not online, there is a faulty
CAP theorem
consistency consistency availability
availability
partition tolerance part

hyperledger (consortium chain) fabric https://blog.csdn.net/ice_fire_x/article/details/104362219
membership

blocks/rev*.dat disk

Contains revoked data, as a reverse patch.
Each blockchain file will correspond to a revocation file

Total blockchain storage connections

https://www.blockchain.com/explorer/blocks/btc?page=1
https://blockchair.com/zh/bitcoin/blocks#

https://blockchair.com/zh/bitcoin

Guess you like

Origin blog.csdn.net/weixin_41523437/article/details/114867007