Note: for the sake of brevity, this article only deals with some key codes and structures. Recommended download source code, see the detailed implementation.
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.
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.
The headers table stores the data of all headers. The key is the hash of the block and the value is the
StoredHeaderContains the blockheader, the sum of the current height and difficulty value.
The chaintip table stores the latest block header.
1.5 four network modes
MainNet: main network. Real money. See the detailed parameters
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 parameters
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 parameters
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 parameters
Today we’re going to useRegTestTo test the storage and query of the local header,TestNetTo test the pow target.
# cd ./blockchain # go test . ok
TestBlockchain_CommitHeaderMethod as an example, it validates
NewBlockchainSPVCreate a new database to store headers
CommitHeaderMemory block header, block information written dead in
GetBestHeaderExtract block head from database
bestIf equal, it means success
This chapter applies the boltdb database and unit test to store headers. Next we can get the real headers.
This work adoptsCC agreementReprint must indicate the author and the link of this article