TECHNICAL FIELD

One or more embodiments of the present specification relate to the technical field of blockchain, and more particularly, to a hierarchical storage method and a hierarchical storage apparatus based on a blockchain, and an electronic device.

BACKGROUND

Blockchain technology, also known as distributed ledger technology, is an emerging technology in which several computing devices participate in “book-keeping” and jointly maintain a complete distributed database. Since blockchain technology has the characteristics of decentralization, openness and transparency, each computing device can participate in database records, and data can be quickly synchronized between computing devices, blockchain technology has been widely used in many fields.

SUMMARY

The present specification provides a hierarchical storage method based on a blockchain, wherein account state data of the blockchain is organized into a state Merkle tree and stored in a database; the database includes multi-level data storage; and the method includes the following: when target data storage of any level in the database satisfies a data migration condition, creating an empty data table corresponding to a working data table for the target data storage; storing the state Merkle tree of a newly added block written into the target data storage in the empty data table, and migrating the state Merkle tree of a block corresponding to a target block number stored in the working data table into the empty data table, where the target block number is the largest block number among the block numbers corresponding to the state Merkle trees stored in the target data storage; and after the state Merkle tree of the block corresponding to the target block number stored in the working data table has been migrated to the empty data table, further migrating the state Merkle trees stored in the working data table to the lower level data storage of the target data storage.

Optionally, the method further includes: after the state Merkle tree of the block corresponding to the target block number stored in the working data table has been migrated to the empty data table, switching the working data table to a historical data table, and switching the empty data table to a working data table.

Optionally, further migrating the state Merkle trees stored in the working data table to the lower level data storage of the target data storage includes: after the working data table is switched to a historical data table, further migrating the state Merkle trees stored in the historical data table to a lower level data storage of the target data storage; and after the state Merkle trees stored in the historical data table has been successfully migrated to the lower level data storage of the target data storage, deleting the historical data table.

Optionally, storing the state Merkle tree of a newly added block written into the target data storage in the empty data table includes: storing the state Merkle tree of the newly added block written into the target data storage only to the empty data table; or storing the state Merkle tree of the newly added block written into the target data storage into both of the empty data table and the working data table.

Optionally, the method further includes: upon receiving a searching request for a data node on a state Merkle tree of the target data storage while the target data storage has double working tables, if the state Merkle tree of the newly added block written to the target data storage is only stored to the empty data table, in response to the searching request, searching for the node from both of the empty data table and the working data table; and if the state Merkle tree of the newly added block written to the target data storage is stored to both of the empty data table and the working data table, in response to the searching request, searching for the node only from the working data table.

Optionally, the state Merkle tree of the newly added block includes: the state Merkle tree of the latest block generated by the blockchain; or the state Merkle tree of the historical block which is migrated from the upper-level data storage to the target data storage.

Optionally, the database is a Key-Value database; and the data nodes on the state Merkle tree are stored in the database in the form of Key-Value pairs; wherein the key of the Key-Value pair is a hash value of the data content contained in the data node; the value of the Key-Value pair is the data content contained in the data node.

Optionally, the Merkle tree is a variant of a Merkle tree incorporating a Trie dictionary tree structure.

Optionally, the state Merkle tree is a Merkle Patricia Tree state tree.

Optionally, the database is a LevelDB database; or a database based on the LevelDB architecture.

Optionally, the database is a Rocksdb database based on a LevelDB architecture.

Optionally, the read-write performance of the storage medium corresponding to the multi-level data storage has a difference in performance; and the read-write performance of the storage medium corresponding to the high-level data storage is higher than the read-write performance of the storage medium corresponding the low-level data storage.

The present specification also provides a hierarchical storage apparatus based on a blockchain, wherein account state data of the blockchain is organized into a state Merkle tree and stored in a database; the database includes multi-level data storage; and the apparatus includes: a creating module, configured to: when target data storage of any level in the database satisfies a data migration condition, create an empty data table corresponding to a working data table for the target data storage; a migration module, configured to store the state Merkle tree of a newly added block written into the target data storage in the empty data table, and migrate the state Merkle tree of a block corresponding to a target block number stored in the working data table into the empty data table, where the target block number is the largest block number among the block numbers corresponding to the state Merkle trees stored in the target data storage; and after the state Merkle tree of the block corresponding to the target block number stored in the working data table has been migrated to the empty data table, further migrate the state Merkle trees stored in the working data table to the lower level data storage of the target data storage.

Optionally, the migration module is configured to after the state Merkle tree of the block corresponding to the target block number stored in the working data table has been migrated to the empty data table, switch the working data table to a historical data table, and switch the empty data table to a working data table.

Optionally, the migration module is further configured to after the working data table is switched to a historical data table, further migrate the state Merkle trees stored in the historical data table to a lower level data storage of the target data storage; and after the state Merkle trees stored in the historical data table has been successfully migrated to the lower level data storage of the target data storage, delete the historical data table.

Optionally, the migration module is configured to store the state Merkle tree of the newly added block written into the target data storage only to the empty data table; or store the state Merkle tree of the newly added block written into the target data storage into both of the empty data table and the working data table.

Optionally, the apparatus further includes: a searching module, configured to: upon receiving a searching request for a data node on a state Merkle tree of the target data storage while the target data storage has double working tables, if the state Merkle tree of the newly added block written to the target data storage is only stored to the empty data table, in response to the searching request, search for the node from both of the empty data table and the working data table; and if the state Merkle tree of the newly added block written to the target data storage is stored to both of the empty data table and the working data table, in response to the searching request, search for the node only from the working data table.

Optionally, the state Merkle tree of the newly added block includes: the state Merkle tree of the latest block generated by the blockchain; or the state Merkle tree of the historical block which is migrated from the upper-level data storage to the target data storage.

Optionally, the database is a Key-Value database; and the data nodes on the state Merkle tree are stored in the database in the form of Key-Value pairs; wherein the key of the Key-Value pair is a hash value of the data content contained in the data node; the value of the Key-Value pair is the data content contained in the data node.

Optionally, the Merkle tree is a variant of a Merkle tree incorporating a Trie dictionary tree structure.

Optionally, the state Merkle tree is a Merkle Patricia Tree state tree.

Optionally, the database is a LevelDB database; or a database based on the LevelDB architecture.

Optionally, the database is a Rocksdb database based on a LevelDB architecture.

Optionally, the read-write performance of the storage medium corresponding to the multi-level data storage has a difference in performance; and the read-write performance of the storage medium corresponding to the high-level data storage is higher than the read-write performance of the storage medium corresponding the low-level data storage.

In the above technical solutions, since the largest block number among the block numbers corresponding to the state Merkle trees stored in the working data table of the target data storage usually indicates the block number of the latest block stored in the working data table, the state Merkle tree of a newly added block written into the target data storage can be stored in the empty data table which is created for the target data storage and corresponding to the working data table, and the state Merkle tree corresponding to the largest block number stored in the working data table is also migrated into the empty data table. It can achieve storing the state Merkle tree of the latest block stored in the target data storage to the empty data table, and retaining the state Merkle trees of historical blocks in the working data table. In this way, after the state Merkle tree corresponding to the largest block number stored in the working data table has been migrated into the empty data table, by further migrating the state Merkle trees stored in the working data table to the lower level data storage of the target data storage, it can achieve hierarchical storage of the state Merkle trees of the target data storage, that is, keeping storing and retaining the state Merkle tree of the latest block in the current-level storage, and migrating the state Merkle trees of historical blocks to the lower level data storage.

