Picture bitcoin Part 4: how to store blockheads?

Time:2021-1-28

Note: for the sake of brevity, this article only deals with some key codes and structures. Recommended download source code, see the detailed implementation.

1.1 INTRODUCTION

Part 1 completes the “handshake”, Part 2 completes the “heartbeat” information, and Part 3 loads the matching file.

Next, we store blockheads, which can be used to verify transactions in the future.

1.2 code address

Picture bitcoin Part 4: how to store blockheads?

1.3 boltdb database

Bolt is a pure key value storage go database, inspired by Howard Chu’s LMDB. It aims to provide a simple, fast and reliable database for those projects without a complete database server such as Postgres and mysql.

BoltDBIt’s simple enough, and it’s implemented by go. It usesKey value storage data, which can be understood as a map existing in a file.

1.4 database structure

Headers table

key|value

—|:–:

BlockHash|StoredHeader

The headers table stores the data of all headers. The key is the hash of the block and the value is theStoredHeader

StoredHeaderContains the blockheader, the sum of the current height and difficulty value.

Chaintip table

key|value

—|:–:

“KEYChainTip”|StoredHeader

The chaintip table stores the latest block header.

1.5 four network modes

MainNet: main network. Real money. See the detailed parameterschaincfg.MainNetParams

TestNet: test the network. It is another “bitcoin blockchain” on the Internet by specifying the command line parameter – testnet (or in the bitcoin.conf Testnet = 1) is added to the configuration file, and the block size is 10-20gb. It enables developers and testers to experience the bitcoin network almost realistically without using real money. See the detailed parameterschaincfg.TestNet3Params

SimNet: simulate test network. By specifying the command line parameter – SIMNET start (add SIMNET = 1 in the configuration file), the node will not communicate with other nodes. If the node is running in SIMNET, the program creates a new blockchain without block data synchronization, which is mainly used for application development and testing purposes.. See the detailed parameterschaincfg.SimNetParams

RegTest: regression test network. By specifying the command line parameter – regtest (regtest = 1 is added to the configuration file), a private node is started locally, which is mainly used for application development and testing. See the detailed parameterschaincfg.RegressionNetParams

Today we’re going to useRegTestTo test the storage and query of the local header,TestNetTo test the pow target.

1.6 testing


# cd ./blockchain

# go test .

ok

choiceTestBlockchain_CommitHeaderMethod as an example, it validatesCommitHeadermethod.

  1. NewBlockchainSPVCreate a new database to store headers

  2. CommitHeaderMemory block header, block information written dead inchainin

  3. GetBestHeaderExtract block head from database

  4. comparechainandbestIf equal, it means success

1.7 summary

This chapter applies the boltdb database and unit test to store headers. Next we can get the real headers.

reference resources:

spvwallet

BTCD

This work adoptsCC agreementReprint must indicate the author and the link of this article