- 1.
How a new Helium node with an empty blockchain acquires blocks from the Helium network and synchronizes itself with the distributed blockchain. This procedure for acquiring blocks also applies if a node goes down or if there is a temporary network partition.
- 2.
Rebuilding the Chainstate database.
- 3.
Rebuilding the blk_index database.
Blockchain Maintenance
A new node that joins the Helium peer-to-peer network will need to obtain a blockchain. Furthermore, the blockchain held by a node may need synchronization if it goes down or if there is a temporary network partition.
get_blockchain_height obtains the height of the blockchain held by a remote node. The call get_block obtains a specific block from a remote node. Both of these remote procedure calls were discussed in the previous chapter.
I will leave it to you to develop tests to obtain blocks. In a production setting and in order to improve performance, you will want to enhance the code to provide for the concurrent downloading of blocks from several remote nodes.
Reliability Engineering in Helium
An application that manages financial transactions must have extreme reliability. The cardinal principle is that the application should implement the simplest architectural patterns possible to achieve its objectives.
In the previous chapter, we saw that the distributed peer-to-peer architecture of Helium creates a network architecture that cannot be easily degraded. All of the nodes in the ecosystem are equal in capability, and anyone can instantiate a node and join the Helium network. Furthermore, Bitcoin and Helium have very modest requirements and thus can run on inexpensive commodity hardware. It is entirely feasible to run a Helium or Bitcoin node on a laptop.
Furthermore, we saw in the previous section that a Helium node can always synchronize its blockchain with the distributed blockchain in the event that the node goes down or if there is an unexpected network partition.
Aside from these reliability features, Helium and Bitcoin implement a single point of failure architecture. This point of failure is the block file, which is a simple, encoded text file. A blockchain is the collection of these files.
A Helium node can rebuild all of the data structures and databases that it needs for its operation from the blockchain (the collection of block files). Helium uses two key-value stores, the Chainstate Database and the blk_index database, and ensures by design that these databases are not critical points of failure. In other words, Helium can always rebuild these databases as long as it has all of the block files.
This simple naming scheme indexes the block files by height. The genesis block has height zero.
The collection of all of the blocks in the directory is the blockchain.
Constructing a Simulated Blockchain
- 1.
Testing blockchains during the development phase
- 2.
Examining the behavior of the blockchain when its parameters are changed
- 3.
Examining the resiliency and reliability of the blockchain
You should see a blockchain of 500 blocks in the unit_tests directory. You can build blockchains of arbitrary height by providing the requisite height parameter to the make_blocks function.
Directory File Constraints
Chainstate Maintenance
In a typical cryptocurrency transaction, an entity spends previous transaction values that have been received and not spent. The Chainstate database keeps track of all of the spent and unspent values in the cryptocurrency ecosystem. Each such value is represented as a transaction fragment. At each point in time, the Chainstate provides a snapshot of the spent and unspent fragments in the system. You can refer to Chapter 12 to review the theory of the Chainstate.
The following program code rebuilds the Chainstate database from scratch from the collection of block files. You will notice that this code leverages the hchaindb module and, in particular, uses the update_transaction function in this module to build the Chainstate database.
The build_chainstate is available in Appendix 9.
blk_index Maintenance
In Chapter 12, we discussed the blk_index database . This is a key-value store that determines the block in which a particular transaction is located. The keys are the transaction IDs and the values are the block heights.
Like the Chainstate database, the blk_index database can be rebuilt from the blockchain files.
The following module, build_blk_index, rebuilds the blk_index store. Copy the following code into the build_blk_index.py file and save it in the unit_tests directory. Delete the hblk_index sub-directory, if it exists.
Conclusion
In this chapter, we have considered some of important maintenance aspects of Helium. These have included obtaining a blockchain for a new node and synchronizing the blockchain held by a node if a node goes down or if there is a temporary network partition. We have also exposed code to rebuild the Chainstate database and the blk_index database from the blockchain files.
In the next chapter, we will develop a user wallet for Helium.