WO2024021419A1 - Blockchain data storage method and apparatus, and electronic device - Google Patents

Blockchain data storage method and apparatus, and electronic device Download PDF

Info

Publication number
WO2024021419A1
WO2024021419A1 PCT/CN2022/135539 CN2022135539W WO2024021419A1 WO 2024021419 A1 WO2024021419 A1 WO 2024021419A1 CN 2022135539 W CN2022135539 W CN 2022135539W WO 2024021419 A1 WO2024021419 A1 WO 2024021419A1
Authority
WO
WIPO (PCT)
Prior art keywords
nodes
node
data
tree
blockchain
Prior art date
Application number
PCT/CN2022/135539
Other languages
French (fr)
Chinese (zh)
Inventor
陆钟豪
俞本权
卓海振
任充慧
田世坤
Original Assignee
蚂蚁区块链科技 (上海) 有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 蚂蚁区块链科技 (上海) 有限公司 filed Critical 蚂蚁区块链科技 (上海) 有限公司
Publication of WO2024021419A1 publication Critical patent/WO2024021419A1/en

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/22Indexing; Data structures therefor; Storage structures
    • G06F16/2228Indexing structures
    • G06F16/2246Trees, e.g. B+trees
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/22Indexing; Data structures therefor; Storage structures
    • G06F16/2282Tablespace storage structures; Management thereof
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/23Updating
    • G06F16/2358Change logging, detection, and notification
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/23Updating
    • G06F16/2379Updates performed during online database operations; commit processing
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/245Query processing
    • G06F16/2455Query execution
    • G06F16/24552Database cache management
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/27Replication, distribution or synchronisation of data between databases or within a distributed database system; Distributed database system architectures therefor

Definitions

  • One or more embodiments of this specification relate to the field of blockchain technology, and in particular, to a blockchain data storage method and device, and electronic equipment.
  • Blockchain technology also known as distributed ledger technology, is an emerging technology in which several node devices jointly participate in "bookkeeping" and jointly store and maintain a complete distributed database.
  • the blockchain data that needs to be stored and maintained usually includes block data and account status data corresponding to the blockchain account in the blockchain; and the block data can further include area data.
  • the above various blockchain data can usually be organized into a logical tree structure in the database in the form of key-value pairs. storage.
  • blockchain data can be organized into a Merkle tree in the form of key-value pairs and stored in the database.
  • This specification proposes a blockchain data storage method.
  • the key-value pairs of the blockchain data are stored in the database in the form of root nodes, intermediate nodes and leaf nodes on a logical tree structure; so The root node and the intermediate node are used to store the characters in the key of the blockchain data; the leaf nodes are used to store the value of the blockchain data; any node on the tree structure passes its hash The value is linked to the node of the previous layer; the method includes:
  • Cache at least some of the root nodes and intermediate nodes to a storage medium that supports overwriting data, so as to modify and update at least some of the nodes in the storage medium; and, generate a record for all the nodes.
  • a data record of the modification update details of at least part of the nodes is recorded, and the data record and other nodes among the root node, intermediate node and leaf node except the at least part of the nodes are written into the database for persistent storage.
  • This specification also proposes a blockchain data storage method.
  • the key-value pairs of the blockchain data are stored in the database in the form of root nodes, intermediate nodes and leaf nodes on a logical tree structure;
  • the root node and intermediate node are used to store the characters in the key of the blockchain data;
  • the leaf nodes are used to store the value of the blockchain data; any node on the tree structure passes through its
  • the hash value is linked to the nodes of the upper layer; wherein, at least some of the root nodes and intermediate nodes are cached in a storage medium that supports overwriting data, and are modified and updated in the storage medium; and, using Data records that record modification and update details for at least some of the nodes, and other nodes among the root nodes, intermediate nodes and leaf nodes other than at least some of the nodes are persistently stored in the database;
  • the method include:
  • This specification also proposes a blockchain data storage device.
  • the key-value pairs of the blockchain data are stored in the database in the form of root nodes, intermediate nodes and leaf nodes on a logical tree structure;
  • the root node and intermediate node are used to store the characters in the key of the blockchain data;
  • the leaf nodes are used to store the value of the blockchain data; any node on the tree structure passes through its
  • the hash value is linked to the node of the previous layer; the method includes:
  • a conversion module that converts key-value pairs of the blockchain data into root nodes, intermediate nodes and leaf nodes in a logical tree structure
  • a storage module that caches at least part of the root node and intermediate nodes to a storage medium that supports overwriting data, so as to modify and update the at least part of the nodes in the storage medium; and, generate Record data records of modification update details for at least part of the nodes, and write the data records and other nodes among the root node, intermediate node and leaf node except the at least part of the nodes into the database for persistence storage.
  • This specification also proposes a blockchain data storage device.
  • the key-value pairs of the blockchain data are stored in the database in the form of root nodes, intermediate nodes and leaf nodes on a logical tree structure;
  • the root node and intermediate node are used to store the characters in the key of the blockchain data;
  • the leaf nodes are used to store the value of the blockchain data; any node on the tree structure passes through its
  • the hash value is linked to the nodes of the upper layer; wherein, at least some of the root nodes and intermediate nodes are cached in a storage medium that supports overwriting data, and are modified and updated in the storage medium; and, using Data records that record modification and update details for at least some of the nodes, and other nodes among the root nodes, intermediate nodes and leaf nodes except for at least some of the nodes are persistently stored in the database;
  • the device include:
  • a determination module that determines whether at least some of the nodes cached in the storage medium meet persistent storage conditions
  • a writing module if at least some of the nodes cached in the storage medium meet persistent storage conditions, write at least some of the nodes cached in the storage medium into the database for persistent storage.
  • Figure 1 is a tree structure diagram of an MPT tree provided by an exemplary embodiment
  • Figure 2 is a schematic diagram of organizing the account status data of each blockchain account in the blockchain into an MPT status tree in the form of key-value pairs according to an exemplary embodiment
  • Figure 3 is a schematic diagram of organizing the contract data stored in the storage space corresponding to the contract account into an MPT storage tree provided by an exemplary embodiment
  • Figure 4 is a tree structure diagram of an FDMT tree provided by an exemplary embodiment
  • Figure 5 is a structural diagram of a Tree node provided by an exemplary embodiment
  • Figure 6 is a structural diagram of a bucket data bucket provided by an exemplary embodiment
  • Figure 7 is a flow chart of a blockchain data storage method provided by an exemplary embodiment
  • Figure 8 is a schematic structural diagram of an electronic device provided by an exemplary embodiment
  • Figure 9 is a block diagram of a blockchain data storage device provided by an exemplary embodiment.
  • the steps of the corresponding method are not necessarily performed in the order shown and described in this specification.
  • methods may include more or fewer steps than described in this specification.
  • a single step described in this specification may be broken down into multiple steps for description in other embodiments; and multiple steps described in this specification may also be combined into a single step in other embodiments. describe.
  • Blockchain is a new application model of computer technology such as distributed data storage, point-to-point transmission, consensus mechanism, and encryption algorithm.
  • data blocks are combined into a chained data structure in a chronological manner and are cryptographically guaranteed to be an untamperable and unforgeable distributed ledger. Due to the characteristics of blockchain, such as decentralization, non-tamperable information, and autonomy, blockchain has also received more and more attention and applications.
  • Blockchains are generally divided into three types: Public Blockchain, Private Blockchain and Consortium Blockchain.
  • Public Blockchain Private Blockchain
  • Consortium Blockchain there can also be a combination of the above types, such as private chain + alliance chain, alliance chain + public chain, etc.
  • the public chain is the most decentralized. Participants who join the public chain (also called nodes in the blockchain) can read data records on the chain, participate in transactions, and compete for the accounting rights of new blocks, etc. Moreover, each node can freely join or exit the network and perform related operations.
  • the private chain has the writing permission of the network controlled by an organization or institution, and the data reading permission is regulated by the organization.
  • a private chain can be a weakly centralized system with strict restrictions on nodes and a small number of nodes. This type of blockchain is more suitable for internal use within specific organizations.
  • the alliance chain is a blockchain between the public chain and the private chain, which can achieve "partial decentralization".
  • Each node in the alliance chain usually has a corresponding entity or organization; the nodes join the network through authorization and form an alliance of interests to jointly maintain the operation of the blockchain.
  • blockchain accounts can usually be divided into the following two types:
  • Contract account stores the executed smart contract code and the value of the state in the smart contract code. It can usually only be activated through external account calls;
  • External account (Externally owned account): It is an account directly controlled by the user, also called a user account.
  • the design of external accounts and contract accounts is actually the mapping of account addresses to account status.
  • the status of an account is usually represented by a structure.
  • the status of the account associated with the transaction in the blockchain usually changes.
  • the account structure usually includes fields such as Balance, Nonce, Codehash, and Storageroot. in:
  • Nonce field used to maintain the number of transactions for this account. It is a counter used to ensure that each transaction can and can only be processed once, effectively avoiding replay attacks;
  • Codehash field used to maintain the contract code of the account. In practical applications, only the hash value of the contract code is usually maintained in the Codehash field.
  • the Storageroot field is used to maintain the storage content of this account.
  • an independent persistent storage space is usually allocated to store the contract data corresponding to the contract account. This independent storage space is usually called the account storage of the contract account.
  • the storage content of contract accounts is usually constructed into a logical tree structure and stored in the form of key-value pairs.
  • MPT Merkle Patricia Trie
  • This tree structure usually includes the root node, Intermediate nodes and leaf nodes.
  • the logical tree structure constructed based on the storage content of the contract account is usually called the Storage tree.
  • the Storageroot field usually only maintains the hash value of the root node of the Storage tree.
  • the field values of the Codehash field and the Storageroot field shown above are both null.
  • the MPT tree is a variant of the Merkle tree that is used to store and maintain blockchain data and incorporates the tree structure of a Trie dictionary tree.
  • FDMT Field-Fixed Depth Merkle Tree
  • the following is an example of using an MPT tree to store blockchain data.
  • the blockchain data that needs to be stored and maintained in the blockchain usually includes account status (state) data, transaction data, and receipt data. Therefore, in practical applications, the above account status data, transaction data and receipt data can be organized into MPT state tree (also called world state), MPT transaction tree and MPT receipt in the form of key-value pairs. tree and three MPT trees, which are stored and maintained separately.
  • account status state
  • transaction data transaction data
  • receipt data receipt data
  • MPT state tree also called world state
  • MPT transaction tree also called world state
  • MPT transaction tree and MPT receipt in the form of key-value pairs.
  • tree and three MPT trees which are stored and maintained separately.
  • the contract data stored in the storage space corresponding to the contract account is usually constructed into an MTP Storage tree (hereinafter referred to as the Storage tree).
  • the hash value of the root node of the Storage tree will be added to the Storage field in the above structure of the contract account corresponding to the Storage tree.
  • the MPT state tree is an MPT tree organized in the form of key-value pairs by the account status (state) data of all accounts in the blockchain (including external accounts and contract accounts).
  • the MPT transaction tree is an MPT tree organized by the transaction data in the blockchain in the form of key-value pairs.
  • the MPT receipt tree is the transaction receipt corresponding to each transaction generated after the transaction in the block is executed. It is an MPT tree organized in the form of key-value pairs.
  • the hash values of the root nodes of the MPT status tree, MPT transaction tree and MPT receipt tree shown above will eventually be added to the block header of the corresponding block.
  • MPT transaction tree and MPT receipt tree both correspond to blocks, that is, each block has its own MPT transaction tree and MPT receipt tree.
  • the MPT status tree is a global MPT tree, which does not correspond to a specific block, but covers the account status data of all accounts in the blockchain. Every time the blockchain generates a latest block, after the transactions in the latest block are successfully executed, the blockchain accounts related to these executed transactions in the blockchain (can be external accounts or contract accounts) )'s account status will usually change accordingly.
  • the node device After a "transfer transaction" in the block is executed, the balances of the transfer-out account and the transfer-in account related to the "transfer transaction" (that is, the field values of the Balance fields of these accounts) are usually also Changes will occur.
  • the node device After the node device completes the execution of the transaction in the latest block generated by the blockchain, since the account status in the current blockchain has changed, the node device needs to construct based on the current account status data of all accounts in the blockchain. MPT status tree is used to maintain the latest status of all accounts in the blockchain.
  • the latest account status data is used to reconstruct an MPT status tree.
  • each block in the blockchain has a corresponding MPT state tree.
  • the MPT status tree maintains the latest account status of all accounts in the blockchain after the transactions in the block are executed.
  • Figure 1 is a tree structure diagram of an MPT tree shown in this specification.
  • MPT tree is a more traditional modified Merkle tree variant, which combines the advantages of two tree structures, Merkle tree and Trie dictionary tree (also called prefix tree).
  • the root node of the MPT tree can usually be an extension node.
  • the intermediate nodes of the MPT tree can usually be branch nodes or other extension nodes.
  • the extension node and the branch node can be collectively referred to as character nodes, which are used to store the character prefix part of the string corresponding to the key (that is, the account address) of the account status data.
  • the above character prefix part usually refers to shared character prefixes (shared nibbles).
  • the shared character prefix refers to the same prefix of one or more characters that the keys of all account status data (that is, the blockchain account address) have.
  • the above leaf nodes are used to store the character suffix part (key-end) and Value (that is, specific account status data) of the string corresponding to the key of the blockchain data.
  • the extension node is used to store one or more characters in the shared character prefix of the account address (that is, the shared nibble shown in Figure 1), and the hash value of the node at the next level linked to the extension node (that is, the hash value shown in Figure 1 Next node).
  • the branch node contains 17 slots.
  • the first 16 slots correspond to the 16 possible hexadecimal characters in the key.
  • One character corresponds to a nibble (half byte).
  • Each slot in the first 16 slots bits each representing a character in the shared character prefix of an account address. These slots are used to fill the hash value of the node at the next level linked to the branch node.
  • the last slot is the value slot, which is usually a null value.
  • Leaf nodes are used to store the character suffix of the account address (i.e., the key-end shown in Figure 1), and the value of the account status data (i.e., the account structure described above). Among them, the character suffix of the account address and the shared character prefix of the account address together form a complete account address.
  • the character suffix refers to the suffix consisting of the last or multiple characters other than the shared character prefix of the account address.
  • Figure 2 is a schematic diagram of organizing the account status data of each blockchain account in the blockchain into an MPT state tree in the form of key-value pairs as shown in this specification.
  • the blockchain accounts corresponding to the account addresses in the first three rows are external accounts, and the Codehash and Storage root fields are empty.
  • the blockchain account corresponding to the account address in line 4 is a contract account.
  • the Codehash field maintains the hash value of the contract code corresponding to the contract account;
  • the Storage root field maintains the root node of the Storage tree composed of the storage content of the contract account. hash value.
  • the MPT status tree is finally organized according to the account status data in Table 1, as shown in Figure 3.
  • the MPT state tree is composed of 4 leaf nodes, 2 branch nodes, and 2 extension nodes (one extension node serves as the root node).
  • the prefix field is a prefix field shared by extension nodes and leaf nodes. Different field values of the prefix field can be used to represent different node types.
  • the value of the prefix field is 0, indicating an extended node containing an even number of nibbles; as mentioned above, a nibble represents a nibble and is composed of 4-bit binary. One nibble can correspond to a character that constitutes an account address.
  • the value of the prefix field is 1, indicating an extended node containing an odd number of nibbles; the value of the prefix field is 2, indicating a leaf node containing an even number of nibbles; the value of the prefix field is 3, indicating an odd number of nibbles.
  • the branch node since it is a parallel single nibble character node, the branch node does not have the above prefix field.
  • the Shared nibble field in the extension node corresponds to the key value of the key-value pair contained in the extension node, indicating the common character prefix between account addresses; for example, all account addresses in the above table have the common character prefix a7.
  • the Next Node field is filled with the hash value (hash pointer) of the next node.
  • the hexadecimal character 0 ⁇ f fields in the branch node correspond to the key value of the key-value pair contained in the branch node; if the branch node is an intermediate node on the search path of the account address on the MPT tree, then the branch node
  • the Value field can be empty.
  • the Key-end in the leaf node corresponds to the key value of the key-value pair contained in the leaf node, and represents the last few characters of the account address (the character suffix of the account address).
  • the key values of each node on the search path from the root node to the leaf node constitute a complete account address.
  • the Value field of the leaf node is filled with the account status data corresponding to the account address; for example, the structure composed of the above fields such as Balance, Nonce, Code, and storage can be encoded and then filled in the Value field of the leaf node.
  • Figure 3 is a schematic diagram of organizing the contract data stored in the storage space corresponding to the contract account into an MPT storage tree as shown in this specification.
  • the account with the account address "a77d397" shown in Table 1 is a contract account, so the contract data stored in the storage space corresponding to the contract account will be organized into a storage tree.
  • the root node of the storage tree will also be linked to the leaf node corresponding to the contract account in the MTP status tree shown in Figure 1 based on its hash value.
  • the hash value S1 of the root node of the storage tree will be added to the storage root field in the account status stored in the leaf node corresponding to the contract account in the MTP status tree shown in Figure 1.
  • the storage tree can be called a subtree extended from the leaf node corresponding to the contract account in the MTP status tree shown in Figure 1.
  • the contract data stored in the storage space of the contract account can usually be in the form of state variables.
  • state variables can be organized into a storage tree as shown in Figure 3 in the form of key-value pairs for storage.
  • the hash value of the account address of the contract account and the storage location of the state variable in the account storage of the contract account can be used as the key, and the value of the variable corresponding to the state variable can be used as the value.
  • the above-mentioned database can usually be stored in a persistent storage medium (such as a storage disk) mounted on the above-mentioned node device.
  • a persistent storage medium such as a storage disk mounted on the above-mentioned node device.
  • the above storage medium is the physical storage corresponding to the above database.
  • the key in the Key-Value pair corresponding to the node in the above-mentioned MPT state tree or the above-mentioned storage tree can be the hash value of the data content contained in the node; the value in the key-value pair of the node can be specifically The data content contained by node.
  • the hash value of the data content contained in the node can be calculated (that is, hash calculation is performed on the entire node), and the calculated hash value is used as the key. Use the data content contained in the node as value to generate a Key-Value pair. Then, store the generated Key-Value pair in the database.
  • you need to query the node on the above-mentioned MPT status tree or the above-mentioned storage tree you can perform content addressing based on the hash value of the data content contained in the node as the key.
  • Figure 4 is a tree structure diagram of an FDMT tree shown in this specification.
  • the above-mentioned FDMT tree is also a Merkle tree variant that incorporates the tree structure of a Trie dictionary tree.
  • blockchain data can also be organized into FDMT trees in the form of key-value pairs and stored in the database.
  • the tree structure of the FDMT tree can include the first N layers of Tree nodes (Figure 4 shows 3 layers, which is only schematic), and the last layer of Leaf nodes (i.e. leaves). node).
  • the Tree nodes in the first N layers will serve as the root node, and the Tree nodes in other layers except the first layer will serve as intermediate nodes.
  • each Tree node that is, the root node and the intermediate node
  • each Tree node of the first N layers of the above-mentioned FDMT tree will adopt a unified data structure.
  • the first N layers of Tree nodes on the above-mentioned FDMT tree can each include multiple blocks representing different characters; the above-mentioned blocks are the "positions" of characters in the key used to store blockchain data. Each block can further include multiple slots representing different characters. The above slot is also used to store the characters in the key of the blockchain data.
  • each Tree node includes N blocks.
  • Each block further includes N slots.
  • the nodes of each layer on the above-mentioned FDMT tree can still be linked by filling the hash value (hash pointer) of the node of the next layer into the node of the upper layer. That is, the nodes in the above FDMT tree are linked to the nodes in the upper layer through their own hash values.
  • the above slots can be used to fill the hash value of the next-level node linked to the current Tree node.
  • the node at the next level of the Tree node can still be a Tree node or a Leaf node.
  • Each Tree node on the above-mentioned FDMT tree shown in Figure 4 can be used to store at least some characters in the key of the above-mentioned blockchain data.
  • the string corresponding to the key of the above blockchain data can still include character prefixes and character suffixes.
  • the above-mentioned Tree node can be used to store the characters in the character prefix of the key of the blockchain data.
  • the above leaf node can be used to store the character suffix of the key of the blockchain data and the value of the above blockchain data.
  • the actual stored characters can specifically be the characters represented by the block in the Tree node (that is, a non-empty block with at least one slot filled with a hash value), A string generated by concatenating the characters represented by the slots filled with hash values (that is, non-empty slots) in the block.
  • each block in the Tree node can represent only one character. That is, based on the storage format of the Tree node shown in Figure 4, some of the characters in the character prefix of the key of the above-mentioned blockchain data actually stored by each Tree node are strings with a length of 2 characters.
  • Figure 5 is a structural diagram of a Tree node shown in this specification.
  • the Tree node contains 16 blocks representing different hexadecimal characters. Each block further includes 16 slots that represent different hexadecimal characters (only the 16 slots included in block 6 are shown in Figure 5).
  • block6 represents hexadecimal character 6) in the Tree node is a non-empty block
  • slot4 represents hexadecimal character 4
  • slot6 represents hexadecimal character 6
  • slot9 represents the hexadecimal character 9
  • Hexadecimal strings "64", "66” and "69”.
  • the number of blocks contained in the above-mentioned Tree node and the number of slots contained in each block are not specifically limited in this specification.
  • the number of sub-blocks contained in the above-mentioned Tree node can be determined based on the number of types of character elements contained in the string corresponding to the key of the above-mentioned blockchain data; and, the number of slots contained in the sub-blocks quantity.
  • the key corresponding to the above-mentioned blockchain data is a hexadecimal string.
  • the number of character element types contained in the string corresponding to the key of the above-mentioned blockchain data is 16; then the number of character elements contained in the above-mentioned Tree node
  • the number of blocks and the number of slots contained in each block can be 16.
  • the number of layers of Tree nodes contained in the above-mentioned FDMT tree can be a fixed value; in practical applications, the value of the above-mentioned N can be an integer greater than or equal to 1. That is to say, the above-mentioned FDMT tree can be a Merkle tree that contains at least one layer of Tree nodes, and the number of layers of Tree nodes contained is relatively fixed.
  • the first 6 address characters can be the same.
  • the length of the characters stored in the Tree node is 2 characters; therefore, the above FDMT tree can be designed as a tree structure containing three layers of Tree nodes.
  • the Tree node and Leaf node on the FDMT tree shown in Figure 4 can also be persistently stored in the database in the form of Key-Value pairs.
  • the key in the Key-Value pair corresponding to the Tree node or Leaf node on the above-mentioned FDMT tree can specifically be the hash value of the data content contained in the Tree node or Leaf node.
  • the Value in the key-value pair of Tree node or Leaf node can be the data content contained in Tree node or Leaf node.
  • the tree structure of the FDMT tree shown in Figure 4 can be used to store the account status data of each blockchain account in the blockchain, and can also be used to store the storage space corresponding to a certain contract account. Stored contract data.
  • the FDMT tree used to store the account status data of each blockchain account in the blockchain can be called the FDMT status tree.
  • the FDMT tree used to store contract data stored in the storage space corresponding to a certain contract account can be called an FDMT storage tree.
  • the root node on the above-mentioned FDMT storage tree can also be linked to the leaf node corresponding to the above-mentioned contract account in the above-mentioned FDMT status tree through its hash value.
  • the hash value of the root node on the FDMT storage tree can also be added to the storage root field in the account status stored in the leaf node corresponding to the contract account in the above FDMT status tree, which will not be described again.
  • the actual data stored in the Leaf node on its tree structure usually has a larger size than other types of nodes.
  • Data capacity For example, the value of the above-mentioned blockchain data actually stored by the above-mentioned Leaf node is usually the original content of the above-mentioned blockchain data.
  • the original content of the above-mentioned blockchain data is relative to the character prefix of the above-mentioned blockchain data. It also takes up more storage space. Therefore, in order to ensure that the above-mentioned Leaf nodes can have larger data capacity, whether it is the MPT tree shown in Figure 1 or the Leaf node on the FDMT tree shown in Figure 4, they are usually designed in the form of large data blocks.
  • the above leaf nodes may be in the form of bucket data buckets.
  • the above-mentioned bucket data bucket may specifically be a container or storage space used to store data.
  • Figure 6 is a structural diagram of a bucket data bucket shown in this specification.
  • the above bucket data bucket (ie, the bucker node shown in Figure 6) can include several data records.
  • each data record corresponds to a piece of blockchain data, which is used to store the character suffix (ie, the key-end shown in Figure 6) and value of the key of the above-mentioned blockchain data. That is, a data record refers to a storage record that includes the character suffix and value of the key of the above blockchain data.
  • the structure of the bucket data bucket shown in Figure 6 is specifically explained by taking the Leaf node on the FDMT tree shown in Figure 4 as an example. In practical applications, the structure of the bucker node shown in Figure 6 can also be used as the Leaf node of the MPT tree shown in Figure 1, which will not be described again in this specification.
  • write amplification refers to the effect of writing bandwidth amplification caused by the data to be written being smaller than the data actually written.
  • the Tree nodes in the first N layers of the FDMT tree all use a data structure containing multiple slots, the write amplification effect is particularly obvious when using the FDMT tree to store blockchain data.
  • this specification proposes a technical solution to optimize the write amplification effect produced when using a logical tree structure to store blockchain data.
  • the key-value pairs of blockchain data can be stored in the database in the form of root nodes, intermediate nodes and leaf nodes in a logical tree structure; the root nodes and intermediate nodes are used to store blocks. Characters in the key of the chain data; leaf nodes are used to store the value of the blockchain data; any node on the above tree structure is linked to the node on the previous level through its hash value.
  • the blockchain data to be stored can be obtained, and the blockchain data can be converted into root nodes, intermediate nodes and leaf nodes on the logical tree structure;
  • At least some of the above-mentioned root nodes and intermediate nodes can be cached to a storage medium that supports overwriting data, so that at least some of the above-mentioned nodes can be modified and updated in the storage medium;
  • a data record for recording modification and update details for at least part of the above-mentioned nodes may also be generated, and the above-mentioned data records may be recorded with other nodes among the above-mentioned root nodes, intermediate nodes and leaf nodes except for the above-mentioned at least part of the nodes. Write to the database for persistent storage.
  • At least part of the root nodes and intermediate nodes on the above-mentioned logical tree structure are cached in a storage medium that supports overwriting data, and the at least part of the nodes are processed in the storage medium.
  • Modification and update can alleviate the write amplification effect caused by repeatedly writing at least some of the root nodes and intermediate nodes in the above-mentioned logical tree structure to the database, thereby improving the storage performance of the above-mentioned database.
  • nodes containing multiple slots in the root node and intermediate nodes of the above logical tree structure if there are nodes containing multiple slots in the root node and intermediate nodes of the above logical tree structure, then these nodes will be cached in a storage medium that supports overwriting data. Overwrite writing can be used in the storage medium to modify and update at least some of the nodes, and these overwritten nodes will no longer need to be written to the database in the disk, thus avoiding the need to write these to the database. The entire node writes to the database on disk, causing the write amplification effect described above.
  • Figure 7 is a flow chart of a blockchain data storage method provided by an exemplary embodiment.
  • the method is applied to blockchain node equipment; the key-value pairs of the blockchain data are stored in the database in the form of root nodes, intermediate nodes and leaf nodes on a logical tree structure; the The root node and the intermediate node are used to store the characters in the key of the blockchain data; the leaf nodes are used to store the value of the blockchain data; any node on the tree structure passes its hash value Link with the node of the previous layer; the method includes the following steps:
  • Step 702 Obtain the key-value pair of the blockchain data to be stored
  • the above-mentioned blockchain data to be stored may specifically include any type of data that needs to be persistently stored in the blockchain.
  • the above-mentioned blockchain data to be stored may specifically include account status data corresponding to the blockchain account on the blockchain.
  • blockchain accounts in the blockchain can usually include external accounts and contract accounts. Therefore, the account status data corresponding to the blockchain account on the blockchain can be specifically Including account status data corresponding to user accounts on the blockchain (for example, it can be account balance data), and state variable data stored in contract accounts on the blockchain (for example, it can be certificate data stored in smart contracts) .
  • the above-mentioned blockchain data to be stored may also include transaction data published to the blockchain network, receipt data corresponding to the transaction data generated after the transaction data is executed, and so on.
  • the node device in the blockchain when the node device in the blockchain obtains the key-value pair of the blockchain data to be stored, the node device can locally store the area after obtaining the blockchain data to be stored. Blockchain data is processed into key-value pairs.
  • the step of processing the blockchain data to be stored into key-value pairs can also be completed by a third party.
  • the node device can directly obtain the data processed by the third party from the third party.
  • the key-value pair of the blockchain data to be stored can also be completed by a third party.
  • the key in the key-value pair of the blockchain data may refer to the primary key of the blockchain data in the database.
  • This primary key can specifically serve as a query index.
  • the value in the key-value pair of blockchain data specifically refers to the data content of the above-mentioned blockchain data.
  • the key in the key-value pair of the account status data can specifically be the account of the blockchain account. address.
  • the key in the key-value pair of the transaction data or receipt data corresponding to the transaction data may specifically be: Transaction identifier; for example, in practical applications, the transaction identifier can be the hash value of the transaction, or it can also be the transaction ID assigned to the transaction when consensus is reached on the transaction.
  • Step 704 Convert the blockchain data key-value pairs into root nodes, intermediate nodes and leaf nodes in a logical tree structure
  • the key-value pairs of the blockchain data For the key-value pairs of the blockchain data to be stored, they can be organized into a logical tree structure and stored in the database in the form of nodes on the logical tree structure.
  • the so-called logical tree structure refers to a tree structure constructed on a logical level based on the nodes stored in the database and the link relationships between the nodes.
  • the above logical tree structure may specifically include multi-layer nodes, and the multi-layer nodes may be stored in node units in the underlying physical storage (such as a disk) hosting the database.
  • the multi-layer nodes may be stored in node units in the underlying physical storage (such as a disk) hosting the database.
  • the above-mentioned logical tree structure may be a Merkle tree that is integrated with the tree structure of a dictionary tree; for example, it may be the above-mentioned MPT tree or the above-mentioned FDMT tree.
  • the above logical tree structure may include root nodes, intermediate nodes and leaf nodes.
  • the node device in the blockchain can convert the key-value pair of the blockchain data to be stored into the above logical tree shape. Root nodes, intermediate nodes and leaf nodes on the structure.
  • the updated root node, intermediate nodes, and leaf nodes are the root nodes, intermediate nodes, and leaf nodes that are converted from the key-value pairs of the above blockchain data.
  • intermediate nodes and leaf nodes used to store the key-value pairs of the blockchain data are found.
  • intermediate nodes and leaf nodes for storing the key-value pairs of the blockchain data can be created based on the key-value pairs of the blockchain data, and then based on the The key-value pair of the blockchain data updates the queried root node, and the newly created intermediate nodes and leaf nodes.
  • the key-value pairs of the above blockchain data are converted into the root node, intermediate node and node of the logical tree structure.
  • leaf nodes you can also initially create root nodes, intermediate nodes, and leaf nodes that store the key-value pairs of the blockchain data.
  • the root node, intermediate node, and leaf node created by initialization at this time are the root node, intermediate node, and leaf node that are converted from the key-value pairs of the above blockchain data.
  • the nodes in the above logical tree structure can still be linked to the nodes in the upper layer through their own hash values.
  • the above-mentioned root node and intermediate node are specifically used to store at least one character in the key corresponding to the key-value key value of the blockchain data.
  • the above leaf nodes are specifically used to store the value of blockchain data (that is, the specific content of blockchain data).
  • the number of layers of intermediate nodes may be one layer or multiple layers, and is not particularly limited in this specification.
  • the key of the above blockchain data can still include a character prefix part (Shared nibble) and a character suffix part (key-end); in this case, the root node and the intermediate node can be used for storage characters in the above character prefix.
  • the above leaf nodes can be used to store the above character suffix and the value of the blockchain data.
  • the root node and intermediate nodes in the above logical tree structure can store the characters in the key of the blockchain data; therefore, the above logical tree structure has the characteristics of a Trie dictionary tree.
  • the nodes in the above logical tree structure can be linked to the nodes in the upper layer through their own hash values. Therefore, the above logical tree structure also has the characteristics of Merkle tree. It is easy to understand that the logical tree structure described in this specification can be a Merkle tree variant similar to an MPT tree or an FDMT tree that incorporates the tree structure of a Trie dictionary tree.
  • the above-mentioned blockchain data is account status data corresponding to a blockchain account on the blockchain
  • the above-mentioned logical tree structure may be based on each block in the blockchain.
  • the above-mentioned Merkle state tree is usually split into a current Merkle state tree (Current State Tree) and a historical Merkle state tree (History State Tree).
  • the current Merkle state tree is a Merkle state tree organized by the latest account status of each blockchain account
  • the historical Merkle state tree is a Merkle state tree organized by the historical account status of each blockchain account.
  • Each block has a corresponding current Merkle state tree and historical Merkle state tree.
  • the nodes on the current Merkle state tree since the current Merkle state tree maintains the latest account status of each blockchain account, based on this feature, the nodes on the current Merkle state tree usually perform frequent writing, modification and update operations. .
  • the logical tree structure described in step 702 and step 706 may specifically refer to the above-mentioned current Merkle state tree. That is, for the above-mentioned current Merkle state tree, the technical solution described in steps 702 to 706 can be adopted, while for the historical Merkle state tree, the storage solution in the existing technology can still be adopted. Of course, in practical applications, the technical solution described in steps 702 to 706 can also be used for the historical Merkle state tree.
  • Step 706 Cache at least part of the root node and intermediate nodes to a storage medium that supports overwriting data, so as to modify and update the at least part of the nodes in the storage medium; and, generate Record data records of modification update details for at least part of the nodes, and write the data records and other nodes among the root node, intermediate node and leaf node except the at least part of the nodes into the database for persistence storage.
  • At least some of the above-mentioned root nodes and intermediate nodes can no longer be written to the above-mentioned database for persistent storage by default, but can be cached to the storage on the node device of the blockchain that supports overwriting data. medium, and modify and update at least some of the above-mentioned nodes in the above-mentioned storage medium.
  • the above-mentioned storage medium that supports overwriting data may be a memory mounted on a node device in the blockchain.
  • the above storage medium can also be other forms of storage media that support overwriting data, which are not specifically limited in this specification.
  • the above-mentioned storage medium may also be a solid-state hard drive.
  • nodes among the above-mentioned root nodes, intermediate nodes and leaf nodes, except for at least some of the above-mentioned nodes are still stored by writing to the above-mentioned database for persistent storage by default.
  • a data record for recording modification and update details for at least some of the above nodes can also be generated, and then the data record and the above other nodes are written into the above database for persistent storage.
  • the execution of these transactions will usually cause the data content stored in some nodes in the logical tree structure to change.
  • it is usually necessary to re- Calculate the hash value (i.e. roothash) of the root node of the logical tree structure fill the hash value of the root node into the block header, and then write the updated nodes in the logical tree structure to the database for persistent storage .
  • the process of recalculating the hash value of the root node of the logical tree structure and writing the updated nodes in the logical tree structure to the database for persistent storage is called logical tree structure.
  • the commit operation of the structure In this specification, specifically, when performing a commit operation on the logical tree structure, the data record and the above-mentioned other nodes can be written into the above-mentioned database for persistent storage.
  • At least some of the nodes may specifically be nodes including a plurality of slots for storing characters in the key of the blockchain data among the root nodes and intermediate nodes.
  • at least some of the above-mentioned nodes may also include all root nodes and intermediate nodes in the above-mentioned logical tree structure by default.
  • the above-mentioned logical tree structure is an MPT tree
  • at least some of the above-mentioned nodes may be branch nodes (branch nodes) on the MPT tree.
  • branch nodes since only the branch node on the MPT tree has multiple slots, only the branch node on the MPT tree can be cached to the above storage medium (that is, only the branch node of the MPT tree can be cached to the storage medium).
  • the branch nodes in the root node and intermediate nodes are cached to the above storage medium).
  • the above-mentioned logical tree structure is an FDMT tree
  • at least some of the above-mentioned nodes may be Tree nodes of the first N layers of FDMT.
  • the Tree nodes of the first N layers of the FDMT tree have multiple slots, only the Tree nodes of the first N layers of the FDMT tree can be cached to the above storage medium. (That is, all the root nodes and intermediate nodes of the FDMT tree are cached in the above storage medium).
  • the above-mentioned data record may specifically be a data record used to record modification and update details for each slot in at least some of the above-mentioned nodes.
  • the above data record may specifically be a WAL (write ahead log) log.
  • WAL logging technology is an efficient logging algorithm.
  • all data modifications to the storage system will be written to the WAL log before being submitted to the storage system; then, the WAL log will be updated through checkpoint events that are triggered periodically or manually by the user.
  • the data stored in the log file is modified and written to the storage system.
  • the at least some nodes cached in the storage medium can be updated based on the WAL logs persistently stored in the database. Perform data recovery.
  • the user can restore the cached data in the storage medium through exception recovery instructions.
  • the node device in the blockchain can receive the user's abnormal recovery instruction for at least some of the nodes cached in the above storage medium, and then can respond to the above abnormal recovery instruction based on the WAL log stored in the database, cache the storage medium Perform data recovery on at least some of the above nodes.
  • a persistent storage condition may be set for at least some of the nodes cached in the storage medium.
  • the persistent storage condition specifically refers to at least some of the nodes cached in the storage medium. Write conditions to the database for persistent storage.
  • the above-mentioned nodes cached in the storage medium can be determined periodically based on a preset period. Whether at least some nodes meet the persistent storage conditions. If at least some of the nodes cached in the storage medium satisfy the persistent storage condition, at least some of the nodes cached in the storage medium can be further written into the database for persistent storage.
  • the above-mentioned persistent storage conditions are not particularly limited in this specification, and can be flexibly set based on actual storage requirements in actual applications.
  • the above-mentioned persistent storage conditions may specifically include one or a combination of more of the following conditions:
  • Condition 1 The number of the above data records persistently stored in the above database reaches the threshold
  • Condition 2 The storage capacity of persistently stored data records in the above database reaches the threshold
  • Condition 3 Receive the user's persistent storage instruction for at least some of the nodes stored in the storage medium.
  • the storage space may be further stored in the database for persistent storage. At least some of the above nodes cached in the media are deleted. In addition, after at least some of the nodes cached in the storage medium are successfully written into the database for persistent storage, based on the same consideration, in order to improve the storage space utilization of the database, the above-mentioned nodes can be further written into the database for persistent storage. Delete the data records persistently stored in the database corresponding to at least some of the above nodes.
  • a node device in the blockchain when a node device in the blockchain receives a read instruction for a node on the above-mentioned logical tree structure, the node can be read from the above-mentioned storage medium by default, And when the node is not read from the storage medium, the node is further read from the database.
  • At least part of the root nodes and intermediate nodes on the above-mentioned logical tree structure are cached in a storage medium that supports overwriting data, and the at least part of the nodes are processed in the storage medium.
  • Modification and update can alleviate the write amplification effect caused by repeatedly writing at least some of the root nodes and intermediate nodes in the above-mentioned logical tree structure to the database, thereby improving the storage performance of the above-mentioned database.
  • the FDMT tree is significantly different from the traditional MPT tree.
  • the Tree node on the FDMT tree usually has more slots than the branch node of the MPT tree. For example, if a Tree node contains 16 blocks, and each block contains 16 slots, a Tree node will contain 256 slots, and the number of slots is much larger than the branch nodes on the MPT tree.
  • the Tree node on the FDMT tree will have larger data capacity than the branch node on the MPT tree.
  • the write amplification effect caused by it will be more obvious compared to the MPT tree. For example, if the data stored in any of the 256 slots contained in a certain Tree node on the FDMT tree is updated, when the Tree node containing 256 slots is rewritten into the database for persistent storage, its consumption The write bandwidth is obviously much greater than the write bandwidth consumed when writing the entire branch node containing only 16 slots in the MPT tree to the database.
  • the Tree nodes of the first N layers of the FDMT tree are cached in the memory, and modified and updated in the memory, it can avoid repeatedly writing the Tree nodes of the first N layers of the FDMT tree. into the database on disk, thus mitigating the resulting write amplification effect.
  • this application also provides device embodiments.
  • this specification also provides an embodiment of a blockchain data storage device.
  • the embodiments of the blockchain data storage device in this specification can be applied to electronic devices.
  • the device embodiments may be implemented by software, or may be implemented by hardware or a combination of software and hardware.
  • Taking software implementation as an example as a logical device, it is formed by reading the corresponding computer program instructions in the non-volatile memory into the memory and running them through the processor of the electronic device where it is located.
  • FIG 8 it is a hardware structure diagram of the electronic device where the blockchain data storage device of this specification is located.
  • the electronic device where the device in the embodiment is located may also include other hardware based on the actual functions of the electronic device, which will not be described again.
  • Figure 9 is a block diagram of a blockchain data storage device according to an exemplary embodiment of this specification.
  • the blockchain data storage device 90 can be applied in the electronic device shown in Figure 8, wherein the key-value pairs of the blockchain data are based on the root of the logical tree structure.
  • the form of nodes, intermediate nodes and leaf nodes are stored in the database; the root nodes and intermediate nodes are used to store the characters in the key of the blockchain data; the leaf nodes are used to store the blockchain data value; any node on the tree structure is linked to the node on the previous level through its hash value; the device 90 includes:
  • the acquisition module 901 obtains the key-value pair of the blockchain data to be stored
  • the conversion module 902 converts the key-value pairs of the blockchain data into root nodes, intermediate nodes and leaf nodes in a logical tree structure
  • the storage module 903 caches at least part of the root node and intermediate nodes to a storage medium that supports overwriting data, so as to modify and update the at least part of the nodes in the storage medium; and, generate Data records that record modification and update details for at least part of the nodes, and write the data records and other nodes among the root node, intermediate node and leaf node except the at least part of the nodes into the database. Persistent storage.
  • a typical implementation device is a computer, which may be in the form of a personal computer, a laptop, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email transceiver, or a game controller. desktop, tablet, wearable device, or a combination of any of these devices.
  • a computer includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
  • processors CPUs
  • input/output interfaces network interfaces
  • memory volatile and non-volatile memory
  • Memory may include non-permanent storage in computer-readable media, random access memory (RAM) and/or non-volatile memory in the form of read-only memory (ROM) or flash memory (flash RAM). Memory is an example of computer-readable media.
  • RAM random access memory
  • ROM read-only memory
  • flash RAM flash random access memory
  • Computer-readable media includes both persistent and non-volatile, removable and non-removable media that can be implemented by any method or technology for storage of information.
  • Information may be computer-readable instructions, data structures, modules of programs, or other data.
  • Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), and read-only memory.
  • PRAM phase change memory
  • SRAM static random access memory
  • DRAM dynamic random access memory
  • RAM random access memory
  • read-only memory read-only memory
  • ROM read-only memory
  • EEPROM electrically erasable programmable read-only memory
  • flash memory or other memory technology
  • compact disc read-only memory CD-ROM
  • DVD digital versatile disc
  • Magnetic tape cartridges magnetic disk storage, quantum memory, graphene-based storage media or other magnetic storage devices, or any other non-transmission medium, can be used to store information that can be accessed by computing devices.
  • computer-readable media does not include transitory media, such as modulated data signals and carrier waves.
  • first, second, third, etc. may use the terms first, second, third, etc. to describe various information, the information should not be limited to these terms. These terms are only used to distinguish information of the same type from each other.
  • first information may also be called second information, and similarly, the second information may also be called first information.
  • word “if” as used herein may be interpreted as "when” or “when” or “in response to determining.”

Abstract

A blockchain data storage method, wherein a key-value pair of blockchain data is stored in a database in the form of a root node, an intermediate node and a leaf node in a logical tree structure. The root node and the intermediate node are used for storing characters in the key of the blockchain data, and the leaf node is used for storing the value of the blockchain data. The method comprises: obtaining a key-value pair of blockchain data to be stored; converting the key-value pair of the blockchain data into a root node, an intermediate node and a leaf node in a logical tree structure; caching at least part of nodes among the root node and the intermediate node into a storage medium which supports data overwrite; and generating a data record for recording modification and update details with respect to the at least part of the nodes, and writing the data record and the nodes among the root node, the intermediate node and the leaf node other than the at least part of the nodes into the database for permanent storage.

Description

区块链数据存储方法及装置、电子设备Blockchain data storage method and device, electronic equipment
本申请要求于2022年07月29日提交中国专利局、申请号为202210908663.8、发明名称为“区块链数据存储方法及装置、电子设备”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。This application claims priority to the Chinese patent application filed with the China Patent Office on July 29, 2022, with the application number 202210908663.8 and the invention title "Blockchain data storage method and device, electronic equipment", the entire content of which is incorporated by reference. in this application.
技术领域Technical field
本说明书一个或多个实施例涉及区块链技术领域,尤其涉及一种区块链数据存储方法及装置、电子设备。One or more embodiments of this specification relate to the field of blockchain technology, and in particular, to a blockchain data storage method and device, and electronic equipment.
背景技术Background technique
区块链技术,也被称之为分布式账本技术,是一种由若干台节点设备共同参与“记账”,共同存储和维护一份完整的分布式数据库的新兴技术。对于区块链的节点设备来说,需要存储和维护的区块链数据,通常包括区块数据、区块链中的区块链账户对应的账户状态数据;而区块数据又可以进一步包括区块头数据、区块中的区块交易数据、以及与区块中的区块交易数据对应的交易收据,等等。Blockchain technology, also known as distributed ledger technology, is an emerging technology in which several node devices jointly participate in "bookkeeping" and jointly store and maintain a complete distributed database. For blockchain node equipment, the blockchain data that needs to be stored and maintained usually includes block data and account status data corresponding to the blockchain account in the blockchain; and the block data can further include area data. Block header data, block transaction data in the block, and transaction receipts corresponding to the block transaction data in the block, etc.
区块链的节点设备在存储以上示出的各种区块链数据时,通常可以将上述各种区块链数据以key-value键值对的形式,组织成逻辑的树状结构在数据库中存储。例如,在实际应用中,可以将区块链数据以key-value键值对的形式,组织成Merkle树在数据库中存储。When the blockchain node device stores the various blockchain data shown above, the above various blockchain data can usually be organized into a logical tree structure in the database in the form of key-value pairs. storage. For example, in practical applications, blockchain data can be organized into a Merkle tree in the form of key-value pairs and stored in the database.
发明内容Contents of the invention
本说明书提出一种区块链数据存储方法,所述区块链数据的key-value键值对以逻辑的树形结构上的根节点、中间节点和叶子节点的形式,存储在数据库中;所述根节点、中间节点用于存放所述区块链数据的key中的字符;所述叶子节点用于存放所述区块链数据的value;所述树形结构上的任一节点通过其hash值与上一层的节点链接;所述方法包括:This specification proposes a blockchain data storage method. The key-value pairs of the blockchain data are stored in the database in the form of root nodes, intermediate nodes and leaf nodes on a logical tree structure; so The root node and the intermediate node are used to store the characters in the key of the blockchain data; the leaf nodes are used to store the value of the blockchain data; any node on the tree structure passes its hash The value is linked to the node of the previous layer; the method includes:
获取待存储的区块链数据的key-value键值对;Obtain the key-value pair of the blockchain data to be stored;
将所述区块链数据的key-value键值对转换成逻辑的树形结构上的根节点、中间节点和叶子节点;Convert the key-value pairs of the blockchain data into root nodes, intermediate nodes and leaf nodes in a logical tree structure;
将所述根节点、中间节点中的至少部分节点缓存至支持覆盖式写入数据的存储介质,以在所述存储介质中针对所述至少部分节点进行修改更新;以及,生成用于记录针对所述至少部分节点的修改更新详情的数据记录,并将所述数据记录和所述根节点、中间节点和叶子节点中除所述至少部分节点以外的其他节点写入所述数据库进行持久化存储。Cache at least some of the root nodes and intermediate nodes to a storage medium that supports overwriting data, so as to modify and update at least some of the nodes in the storage medium; and, generate a record for all the nodes. A data record of the modification update details of at least part of the nodes is recorded, and the data record and other nodes among the root node, intermediate node and leaf node except the at least part of the nodes are written into the database for persistent storage.
本说明书还提出一种区块链数据存储方法,所述区块链数据的key-value键值对以逻辑的树形结构上的根节点、中间节点和叶子节点的形式,存储在数据库中;所述根节点、中间节点用于存放所述区块链数据的key中的字符;所述叶子节点用于存放所述区块链数据的value;所述树形结构上的任一节点通过其hash值与上一层的节点链接;其中,所述根节点、中间节点中的至少部分节点缓存在支持覆盖式写入数据的存储介质,并在所述存储介质中进行修改更新;以及,用于记录针对所述至少部分节点的修改更新详情的数据记录,和所述根节点、中间节点和叶子节点中除所述至少部分节点以外的其他节点在所述数据库进行持久化存储;所述方法包括:This specification also proposes a blockchain data storage method. The key-value pairs of the blockchain data are stored in the database in the form of root nodes, intermediate nodes and leaf nodes on a logical tree structure; The root node and intermediate node are used to store the characters in the key of the blockchain data; the leaf nodes are used to store the value of the blockchain data; any node on the tree structure passes through its The hash value is linked to the nodes of the upper layer; wherein, at least some of the root nodes and intermediate nodes are cached in a storage medium that supports overwriting data, and are modified and updated in the storage medium; and, using Data records that record modification and update details for at least some of the nodes, and other nodes among the root nodes, intermediate nodes and leaf nodes other than at least some of the nodes are persistently stored in the database; the method include:
确定所述存储介质中缓存的所述至少部分节点是否满足持久化存储条件;Determine whether at least some of the nodes cached in the storage medium meet persistent storage conditions;
如果所述存储介质中缓存的所述至少部分节点满足持久化存储条件,将所述存储介质中缓存的所述 至少部分节点写入所述数据库中进行持久化存储。本说明书还提出一种区块链数据存储装置,所述区块链数据的key-value键值对以逻辑的树形结构上的根节点、中间节点和叶子节点的形式,存储在数据库中;所述根节点、中间节点用于存放所述区块链数据的key中的字符;所述叶子节点用于存放所述区块链数据的value;所述树形结构上的任一节点通过其hash值与上一层的节点链接;所述方法包括:If at least some of the nodes cached in the storage medium meet persistent storage conditions, the at least some of the nodes cached in the storage medium are written into the database for persistent storage. This specification also proposes a blockchain data storage device. The key-value pairs of the blockchain data are stored in the database in the form of root nodes, intermediate nodes and leaf nodes on a logical tree structure; The root node and intermediate node are used to store the characters in the key of the blockchain data; the leaf nodes are used to store the value of the blockchain data; any node on the tree structure passes through its The hash value is linked to the node of the previous layer; the method includes:
获取模块,获取待存储的区块链数据的key-value键值对;Get the module to get the key-value pairs of the blockchain data to be stored;
转换模块,将所述区块链数据的key-value键值对转换成逻辑的树形结构上的根节点、中间节点和叶子节点;A conversion module that converts key-value pairs of the blockchain data into root nodes, intermediate nodes and leaf nodes in a logical tree structure;
存储模块,将所述根节点、中间节点中的至少部分节点缓存至支持覆盖式写入数据的存储介质,以在所述存储介质中针对所述至少部分节点进行修改更新;以及,生成用于记录针对所述至少部分节点的修改更新详情的数据记录,并将所述数据记录和所述根节点、中间节点和叶子节点中除所述至少部分节点以外的其他节点写入所述数据库进行持久化存储。A storage module that caches at least part of the root node and intermediate nodes to a storage medium that supports overwriting data, so as to modify and update the at least part of the nodes in the storage medium; and, generate Record data records of modification update details for at least part of the nodes, and write the data records and other nodes among the root node, intermediate node and leaf node except the at least part of the nodes into the database for persistence storage.
本说明书还提出一种区块链数据存储装置,所述区块链数据的key-value键值对以逻辑的树形结构上的根节点、中间节点和叶子节点的形式,存储在数据库中;所述根节点、中间节点用于存放所述区块链数据的key中的字符;所述叶子节点用于存放所述区块链数据的value;所述树形结构上的任一节点通过其hash值与上一层的节点链接;其中,所述根节点、中间节点中的至少部分节点缓存在支持覆盖式写入数据的存储介质,并在所述存储介质中进行修改更新;以及,用于记录针对所述至少部分节点的修改更新详情的数据记录,和所述根节点、中间节点和叶子节点中除所述至少部分节点以外的其他节点在所述数据库进行持久化存储;所述装置包括:This specification also proposes a blockchain data storage device. The key-value pairs of the blockchain data are stored in the database in the form of root nodes, intermediate nodes and leaf nodes on a logical tree structure; The root node and intermediate node are used to store the characters in the key of the blockchain data; the leaf nodes are used to store the value of the blockchain data; any node on the tree structure passes through its The hash value is linked to the nodes of the upper layer; wherein, at least some of the root nodes and intermediate nodes are cached in a storage medium that supports overwriting data, and are modified and updated in the storage medium; and, using Data records that record modification and update details for at least some of the nodes, and other nodes among the root nodes, intermediate nodes and leaf nodes except for at least some of the nodes are persistently stored in the database; the device include:
确定模块,确定所述存储介质中缓存的所述至少部分节点是否满足持久化存储条件;A determination module that determines whether at least some of the nodes cached in the storage medium meet persistent storage conditions;
写入模块,如果所述存储介质中缓存的所述至少部分节点满足持久化存储条件,将所述存储介质中缓存的所述至少部分节点写入所述数据库中进行持久化存储。以上技术方案具有如下的技术效果:A writing module, if at least some of the nodes cached in the storage medium meet persistent storage conditions, write at least some of the nodes cached in the storage medium into the database for persistent storage. The above technical solutions have the following technical effects:
在将区块链数据的key-value键值对以逻辑的树形结构上的根节点、中间节点和叶子节点的形式,在数据库中进行存储时,通过将上述逻辑的树形结构上的根节点、中间节点中的至少部分节点缓存在支持覆盖式写入数据的存储介质,并在该存储介质中针对该至少部分节点进行修改更新,可以缓解在将上述逻辑的树形结构上的根节点、中间节点中的至少部分节点反复写入数据库的过程中而造成的写放大效应,进而可以提升上述数据库的存储性能。When storing the key-value pairs of blockchain data in the database in the form of root nodes, intermediate nodes, and leaf nodes on a logical tree structure, by converting the root nodes on the above logical tree structure Caching at least some of the nodes and intermediate nodes in a storage medium that supports overwriting data, and modifying and updating at least some of the nodes in the storage medium can alleviate the problem of converting the root node to the above logical tree structure. , the write amplification effect caused by the repeated writing of at least some nodes among the intermediate nodes to the database, thereby improving the storage performance of the above-mentioned database.
附图说明Description of drawings
图1是一示例性实施例提供的一种MPT树的树形结构图;Figure 1 is a tree structure diagram of an MPT tree provided by an exemplary embodiment;
图2是一示例性实施例提供的一种将区块链中的各个区块链账户的账户状态数据以key-value键值对的形式组织成MPT状态树的示意图;Figure 2 is a schematic diagram of organizing the account status data of each blockchain account in the blockchain into an MPT status tree in the form of key-value pairs according to an exemplary embodiment;
图3是一示例性实施例提供的一种将合约账户对应的存储空间中存储的合约数据组织成MPT storage树的示意图;Figure 3 is a schematic diagram of organizing the contract data stored in the storage space corresponding to the contract account into an MPT storage tree provided by an exemplary embodiment;
图4是一示例性实施例提供的一种FDMT树的树形结构图;Figure 4 is a tree structure diagram of an FDMT tree provided by an exemplary embodiment;
图5是一示例性实施例提供的一种Tree node的结构图;Figure 5 is a structural diagram of a Tree node provided by an exemplary embodiment;
图6是一示例性实施例提供的一种bucket数据桶的结构图;Figure 6 is a structural diagram of a bucket data bucket provided by an exemplary embodiment;
图7是一示例性实施例提供的一种区块链数据存储方法的流程图;Figure 7 is a flow chart of a blockchain data storage method provided by an exemplary embodiment;
图8是一示例性实施例提供的一种电子设备的结构示意图;Figure 8 is a schematic structural diagram of an electronic device provided by an exemplary embodiment;
图9是一示例性实施例提供的一种区块链数据存储装置的框图。Figure 9 is a block diagram of a blockchain data storage device provided by an exemplary embodiment.
具体实施方式Detailed ways
这里将详细地对示例性实施例进行说明,其示例表示在附图中。下面的描述涉及附图时,除非另有表示,不同附图中的相同数字表示相同或相似的要素。以下示例性实施例中所描述的实施方式并不代表与本说明书一个或多个实施例相一致的所有实施方式。相反,它们仅是与如所附权利要求书中所详述的、本说明书一个或多个实施例的一些方面相一致的装置和方法的例子。Exemplary embodiments will be described in detail herein, examples of which are illustrated in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings refer to the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with one or more embodiments of this specification. Rather, they are merely examples of apparatus and methods consistent with some aspects of one or more embodiments of this specification as detailed in the appended claims.
需要说明的是:在其他实施例中并不一定按照本说明书示出和描述的顺序来执行相应方法的步骤。在一些其他实施例中,其方法所包括的步骤可以比本说明书所描述的更多或更少。此外,本说明书中所描述的单个步骤,在其他实施例中可能被分解为多个步骤进行描述;而本说明书中所描述的多个步骤,在其他实施例中也可能被合并为单个步骤进行描述。It should be noted that in other embodiments, the steps of the corresponding method are not necessarily performed in the order shown and described in this specification. In some other embodiments, methods may include more or fewer steps than described in this specification. In addition, a single step described in this specification may be broken down into multiple steps for description in other embodiments; and multiple steps described in this specification may also be combined into a single step in other embodiments. describe.
区块链(Blockchain)是分布式数据存储、点对点传输、共识机制、加密算法等计算机技术的新型应用模式。区块链系统中按照时间顺序将数据区块以顺序相连的方式组合成链式数据结构,并以密码学方式保证的不可篡改和不可伪造的分布式账本。由于区块链具有去中心化、信息不可篡改、自治性等特性,区块链也受到人们越来越多的重视和应用。Blockchain is a new application model of computer technology such as distributed data storage, point-to-point transmission, consensus mechanism, and encryption algorithm. In the blockchain system, data blocks are combined into a chained data structure in a chronological manner and are cryptographically guaranteed to be an untamperable and unforgeable distributed ledger. Due to the characteristics of blockchain, such as decentralization, non-tamperable information, and autonomy, blockchain has also received more and more attention and applications.
区块链一般被划分为三种类型:公有链(Public Blockchain),私有链(Private Blockchain)和联盟链(Consortium Blockchain)。此外,还可以有上述多种类型的结合,比如私有链+联盟链、联盟链+公有链等。Blockchains are generally divided into three types: Public Blockchain, Private Blockchain and Consortium Blockchain. In addition, there can also be a combination of the above types, such as private chain + alliance chain, alliance chain + public chain, etc.
其中,去中心化程度最高的是公有链。加入公有链的参与者(也可称为区块链中的节点)可以读取链上的数据记录、参与交易、以及竞争新区块的记账权等。而且,各节点可自由加入或者退出网络,并进行相关操作。Among them, the public chain is the most decentralized. Participants who join the public chain (also called nodes in the blockchain) can read data records on the chain, participate in transactions, and compete for the accounting rights of new blocks, etc. Moreover, each node can freely join or exit the network and perform related operations.
私有链则相反,该网络的写入权限由某个组织或者机构控制,数据读取权限受组织规定。简单来说,私有链可以为一个弱中心化系统,其对节点具有严格限制且节点数量较少。这种类型的区块链更适合于特定机构内部使用。On the contrary, the private chain has the writing permission of the network controlled by an organization or institution, and the data reading permission is regulated by the organization. Simply put, a private chain can be a weakly centralized system with strict restrictions on nodes and a small number of nodes. This type of blockchain is more suitable for internal use within specific organizations.
联盟链则是介于公有链以及私有链之间的区块链,可实现“部分去中心化”。联盟链中各个节点通常有与之相对应的实体机构或者组织;节点通过授权加入网络并组成利益相关联盟,共同维护区块链运行。The alliance chain is a blockchain between the public chain and the private chain, which can achieve "partial decentralization". Each node in the alliance chain usually has a corresponding entity or organization; the nodes join the network through authorization and form an alliance of interests to jointly maintain the operation of the blockchain.
在区块链领域,有一个重要的概念就是账户(Account)。对于支持智能合约的区块链网络中,区块链账户通常可以分为以下两种类型:In the field of blockchain, an important concept is account. For blockchain networks that support smart contracts, blockchain accounts can usually be divided into the following two types:
合约账户(contract account):存储执行的智能合约代码以及智能合约代码中状态的值,通常只能通过外部账户调用激活;Contract account: stores the executed smart contract code and the value of the state in the smart contract code. It can usually only be activated through external account calls;
外部账户(Externally owned account):就是由用户直接控制的账户,也称之为用户账户。External account (Externally owned account): It is an account directly controlled by the user, also called a user account.
外部账户和合约账户的设计,实际上是账户地址到账户状态的映射。账户的状态通常会通过一个结构体来表示。当区块中的交易被执行后,区块链中与该交易相关的账户的状态通常也会发生变化。The design of external accounts and contract accounts is actually the mapping of account addresses to account status. The status of an account is usually represented by a structure. When a transaction in a block is executed, the status of the account associated with the transaction in the blockchain usually changes.
在一个例子中,账户的结构体通常包括Balance,Nonce,Codehash和Storageroot等字段。其中:In an example, the account structure usually includes fields such as Balance, Nonce, Codehash, and Storageroot. in:
Balance字段,用于维护账户目前的账户余额;Balance field, used to maintain the current account balance of the account;
Nonce字段,用于维护该账户的交易次数。它是用于保障每笔交易能且只能被处理一次的计数器,有效避免重放攻击;Nonce field, used to maintain the number of transactions for this account. It is a counter used to ensure that each transaction can and can only be processed once, effectively avoiding replay attacks;
Codehash字段,用于维护该账户的合约代码。在实际应用中,Codehash字段中通常仅维护合约代码的hash值。Codehash field, used to maintain the contract code of the account. In practical applications, only the hash value of the contract code is usually maintained in the Codehash field.
Storageroot字段,用于维护该账户的存储内容。对于合约账户而言,通常会分配一个独立的持久化的存储空间,用以存储该合约账户对应的合约数据。该独立的存储空间通常称之为该合约账户的账 户存储。The Storageroot field is used to maintain the storage content of this account. For contract accounts, an independent persistent storage space is usually allocated to store the contract data corresponding to the contract account. This independent storage space is usually called the account storage of the contract account.
合约账户的存储内容通常会以key-value键值对的形式,被构建成逻辑的树形结构进行存储。例如,MPT(Merkle Patricia Trie)树树就是区块链领域一种比较常用的用于存储和维护区块链数据的一种逻辑的树形结构,在这种树形结构中通常包括根节点、中间节点、叶子节点。The storage content of contract accounts is usually constructed into a logical tree structure and stored in the form of key-value pairs. For example, MPT (Merkle Patricia Trie) tree is a logical tree structure commonly used in the blockchain field for storing and maintaining blockchain data. This tree structure usually includes the root node, Intermediate nodes and leaf nodes.
其中,基于合约账户的存储内容构建成的逻辑的树形结构,通常也称之为Storage树。而Storageroot字段通常仅维护该Storage树的根节点的hash值。其中,对于外部账户而言,以上示出的Codehash字段和Storageroot字段的字段值均为空值。Among them, the logical tree structure constructed based on the storage content of the contract account is usually called the Storage tree. The Storageroot field usually only maintains the hash value of the root node of the Storage tree. Among them, for external accounts, the field values of the Codehash field and the Storageroot field shown above are both null.
在实际应用中,大多数的区块链模型,通常都会采用Merkle树;或者,在Merkle树的数据结构的基础上设计的Merkle树变种等逻辑的树形结构,来存储和维护数据。In practical applications, most blockchain models usually use Merkle trees; or logical tree structures such as Merkle tree variants designed based on the Merkle tree data structure to store and maintain data.
例如,MPT树,就是一种用来存储和维护区块链数据的,融合了Trie字典树的树形结构的Merkle树变种。For example, the MPT tree is a variant of the Merkle tree that is used to store and maintain blockchain data and incorporates the tree structure of a Trie dictionary tree.
又如,FDMT(Fixed Depth Merkle Tree)树,也是一种用来存储和维护区块链数据的,融合了Trie字典树的树形结构的Merkle树变种。Another example is the FDMT (Fixed Depth Merkle Tree) tree, which is also a variant of the Merkle tree that is used to store and maintain blockchain data and incorporates the tree structure of the Trie dictionary tree.
以下以使用MPT树,来存储区块链数据为例进行说明。The following is an example of using an MPT tree to store blockchain data.
在一个例子中,区块链中需要存储和维护的区块链数据,通常包括账户状态(state)数据、交易数据和收据数据。因此,在实际应用中,可以分别将上述账户状态数据、交易数据和收据数据以key-value键值对的形式,组织成MPT状态树(也称之为world state)、MPT交易树和MPT收据树等三棵MPT树,分别进行存储和维护。In one example, the blockchain data that needs to be stored and maintained in the blockchain usually includes account status (state) data, transaction data, and receipt data. Therefore, in practical applications, the above account status data, transaction data and receipt data can be organized into MPT state tree (also called world state), MPT transaction tree and MPT receipt in the form of key-value pairs. tree and three MPT trees, which are stored and maintained separately.
其中,除了以上三棵MPT树以外,合约账户对应的存储空间中存储的合约数据,通常也会被构建成为一棵MTP Storage树(以下简称为Storage树)。该Storage树的根节点的hash值,会被添加到与该Storage树对应的合约账户的上述结构体中的Storage字段。Among them, in addition to the above three MPT trees, the contract data stored in the storage space corresponding to the contract account is usually constructed into an MTP Storage tree (hereinafter referred to as the Storage tree). The hash value of the root node of the Storage tree will be added to the Storage field in the above structure of the contract account corresponding to the Storage tree.
MPT状态树,是由区块链中所有账户(包括外部账户和合约账户)的账户状态(state)数据,以key-value键值对的形式组织成的MPT树。The MPT state tree is an MPT tree organized in the form of key-value pairs by the account status (state) data of all accounts in the blockchain (including external accounts and contract accounts).
MPT交易树,是由区块链中的交易(transaction)数据,以key-value键值对的形式组织成的MPT树。The MPT transaction tree is an MPT tree organized by the transaction data in the blockchain in the form of key-value pairs.
MPT收据树,是区块中的交易在执行完毕后生成的与每笔交易对应的交易(receipt)收据,以key-value键值对的形式组织成的MPT树。The MPT receipt tree is the transaction receipt corresponding to each transaction generated after the transaction in the block is executed. It is an MPT tree organized in the form of key-value pairs.
以上示出的MPT状态树、MPT交易树和MPT收据树的根节点的hash值,最终都会被添加至对应区块的区块头中。The hash values of the root nodes of the MPT status tree, MPT transaction tree and MPT receipt tree shown above will eventually be added to the block header of the corresponding block.
其中,MPT交易树和MPT收据树均与区块相对应,即每一个区块都有自己的MPT交易树和MPT收据树。而MPT状态树是一个全局的MPT树,并不与某一个特定的区块相对应,而是涵盖了区块链中所有账户的账户状态数据。区块链每产生一个最新区块,则在该最新区块中的交易被执行成功之后,区块链中与这些被执行的交易相关的区块链账户(可以是外部账户也可以是合约账户)的账户状态,通常也会随之发生变化。Among them, MPT transaction tree and MPT receipt tree both correspond to blocks, that is, each block has its own MPT transaction tree and MPT receipt tree. The MPT status tree is a global MPT tree, which does not correspond to a specific block, but covers the account status data of all accounts in the blockchain. Every time the blockchain generates a latest block, after the transactions in the latest block are successfully executed, the blockchain accounts related to these executed transactions in the blockchain (can be external accounts or contract accounts) )'s account status will usually change accordingly.
例如,当区块中的一笔“转账交易”执行完毕后,与该“转账交易”相关的转出方账户和转入方账户的余额(即这些账户的Balance字段的字段值),通常也会随之发生变化。节点设备在区块链产生的最新区块中的交易执行完毕后,由于当前区块链中的账户状态发生了变化,因此节点设备需要根据区块链中所有账户当前的账户状态数据,来构建MPT状态树,用于维护区块链中所有账户的最新状态。For example, after a "transfer transaction" in the block is executed, the balances of the transfer-out account and the transfer-in account related to the "transfer transaction" (that is, the field values of the Balance fields of these accounts) are usually also Changes will occur. After the node device completes the execution of the transaction in the latest block generated by the blockchain, since the account status in the current blockchain has changed, the node device needs to construct based on the current account status data of all accounts in the blockchain. MPT status tree is used to maintain the latest status of all accounts in the blockchain.
每当区块链中产生一个最新区块,并且该最新区块中的交易执行完毕后,导致区块链中的部分账户的账户状态发生了变化,节点设备都需要基于区块链中所有账户最新的账户状态数据,重新构建一棵MPT状态树。换句话说,区块链中每一个区块,都有一个与之对应的MPT状态树。该MPT状态树,维护了在该区块中的交易在执行完毕后,区块链中所有账户最新的账户状态。Whenever a latest block is generated in the blockchain and the transaction in the latest block is executed, causing the account status of some accounts in the blockchain to change, the node device needs to be based on all accounts in the blockchain. The latest account status data is used to reconstruct an MPT status tree. In other words, each block in the blockchain has a corresponding MPT state tree. The MPT status tree maintains the latest account status of all accounts in the blockchain after the transactions in the block are executed.
请参见图1,图1为本说明书示出的一种MPT树的树形结构图。Please refer to Figure 1, which is a tree structure diagram of an MPT tree shown in this specification.
其中,需要说明的是,图1中各个节点的连接关系仅为示意性的。Among them, it should be noted that the connection relationship of each node in Figure 1 is only schematic.
MPT树,是一种较为传统的经过改良的Merkle树变种,其融合了Merkle树和Trie字典树(也称之为前缀树)两种树形结构的优点。MPT tree is a more traditional modified Merkle tree variant, which combines the advantages of two tree structures, Merkle tree and Trie dictionary tree (also called prefix tree).
在MPT树中通常包括三种节点,分别为叶子节点(leaf node),扩展节点(extension node)和分支节点(branch node)。其中,MPT树的根节点通常可以是扩展节点。MPT树的中间节点通常可以是分支节点或者其它的扩展节点。There are usually three types of nodes in the MPT tree, namely leaf node, extension node and branch node. Among them, the root node of the MPT tree can usually be an extension node. The intermediate nodes of the MPT tree can usually be branch nodes or other extension nodes.
其中,扩展节点和分支节点可以统称为字符节点,用于存储账户状态数据的key(即账户地址)对应的字符串的字符前缀部分。对于MPT树而言,上述字符前缀部分通常是指共享字符前缀(shared nibbles)。所述共享字符前缀,是指所有账户状态数据的key(即区块链账户地址)所具有的相同的一个或者多个字符组成的前缀。而上述叶子节点,用于存储区块链数据的key对应的字符串的字符后缀部分(key-end)和Value(即具体的账户状态数据)。Among them, the extension node and the branch node can be collectively referred to as character nodes, which are used to store the character prefix part of the string corresponding to the key (that is, the account address) of the account status data. For MPT trees, the above character prefix part usually refers to shared character prefixes (shared nibbles). The shared character prefix refers to the same prefix of one or more characters that the keys of all account status data (that is, the blockchain account address) have. The above leaf nodes are used to store the character suffix part (key-end) and Value (that is, specific account status data) of the string corresponding to the key of the blockchain data.
扩展节点,用于存储账户地址的共享字符前缀中一个或者多个字符(即图1示出的shared nibble),和该扩展节点链接的下一层的节点的hash值(即图1示出的Next node)。The extension node is used to store one or more characters in the shared character prefix of the account address (that is, the shared nibble shown in Figure 1), and the hash value of the node at the next level linked to the extension node (that is, the hash value shown in Figure 1 Next node).
分支节点,包含17个槽位,前16个槽位对应着key中的16个可能的十六进制字符,一个字符对应一个nibble(半字节),前16个槽位中的每一个槽位,分别表示一个账户地址的共享字符前缀中的一个字符,这些槽位用于填充该分支节点链接的下一层的节点的hash值。最后一个槽位为value槽位,一般为空值。The branch node contains 17 slots. The first 16 slots correspond to the 16 possible hexadecimal characters in the key. One character corresponds to a nibble (half byte). Each slot in the first 16 slots bits, each representing a character in the shared character prefix of an account address. These slots are used to fill the hash value of the node at the next level linked to the branch node. The last slot is the value slot, which is usually a null value.
叶子节点,用于存储账户地址的字符后缀(即图1示出的key-end),和账户状态数据的value(即以上描述的账户的结构体)。其中,账户地址的字符后缀和账户地址的共享字符前缀共同组成了一个完整的账户地址。所述字符后缀,是指除了账户地址的共享字符前缀以外的最后一个或者多个字符组成的后缀。Leaf nodes are used to store the character suffix of the account address (i.e., the key-end shown in Figure 1), and the value of the account status data (i.e., the account structure described above). Among them, the character suffix of the account address and the shared character prefix of the account address together form a complete account address. The character suffix refers to the suffix consisting of the last or multiple characters other than the shared character prefix of the account address.
请参见图2,图2为本说明书示出的一种将区块链中的各个区块链账户的账户状态数据以key-value键值对的形式组织成MPT状态树的示意图。Please refer to Figure 2. Figure 2 is a schematic diagram of organizing the account status data of each blockchain account in the blockchain into an MPT state tree in the form of key-value pairs as shown in this specification.
假设需要组织成MTP状态树的账户状态数据的key-value键值对如下表1所示:Assume that the key-value pairs of account status data that need to be organized into an MTP status tree are as shown in Table 1 below:
Figure PCTCN2022135539-appb-000001
Figure PCTCN2022135539-appb-000001
表1Table 1
其中,需要说明的是,在表1中,前三行的账户地址对应的区块链账户为外部账户,Codehash和Storage root字段为空值。第4行的账户地址对应的区块链账户为合约账户,Codehash字段维护了该合约账户对应的合约代码的hash值;Storage root字段维护了该合约账户的存储内容构成的Storage树的根节点的hash值。Among them, it should be noted that in Table 1, the blockchain accounts corresponding to the account addresses in the first three rows are external accounts, and the Codehash and Storage root fields are empty. The blockchain account corresponding to the account address in line 4 is a contract account. The Codehash field maintains the hash value of the contract code corresponding to the contract account; the Storage root field maintains the root node of the Storage tree composed of the storage content of the contract account. hash value.
最终按照表1中的账户状态数据组织成的MPT状态树,如图3所示。The MPT status tree is finally organized according to the account status data in Table 1, as shown in Figure 3.
该MPT状态树是由4个叶子节点,2个分支节点,和2个扩展节点(其中一个扩展节点作为根节点)构成。The MPT state tree is composed of 4 leaf nodes, 2 branch nodes, and 2 extension nodes (one extension node serves as the root node).
在图2中,prefix字段为扩展节点和叶子节点共同具有的前缀字段。该prefix字段的不同字段值可以用于表示不同的节点类型。In Figure 2, the prefix field is a prefix field shared by extension nodes and leaf nodes. Different field values of the prefix field can be used to represent different node types.
例如,prefix字段的取值为0,表示包含偶数个nibbles的扩展节点;如前所述,nibble表示半字节,由4位二进制组成,一个nibble可以对应一个组成账户地址的字符。prefix字段的取值为1,表示包含奇数个nibble(s)的扩展节点;prefix字段的取值为2,表示包含偶数个nibbles的叶子节点;prefix字段的取值为3,表示包含奇数个nibble(s)的叶子节点。而分支节点,由于其是并列单nibble的字符节点,因此分支节点不具有上述prefix字段。For example, the value of the prefix field is 0, indicating an extended node containing an even number of nibbles; as mentioned above, a nibble represents a nibble and is composed of 4-bit binary. One nibble can correspond to a character that constitutes an account address. The value of the prefix field is 1, indicating an extended node containing an odd number of nibbles; the value of the prefix field is 2, indicating a leaf node containing an even number of nibbles; the value of the prefix field is 3, indicating an odd number of nibbles. The leaf node of (s). As for the branch node, since it is a parallel single nibble character node, the branch node does not have the above prefix field.
扩展节点中的Shared nibble字段,对应该扩展节点所包含的键值对的key值,表示账户地址之间的共同字符前缀;比如,上表中的所有账户地址均具有共同的字符前缀a7。Next Node字段中填充下一个节点的hash值(hash指针)。The Shared nibble field in the extension node corresponds to the key value of the key-value pair contained in the extension node, indicating the common character prefix between account addresses; for example, all account addresses in the above table have the common character prefix a7. The Next Node field is filled with the hash value (hash pointer) of the next node.
分支节点中的16进制字符0~f字段,对应该分支节点所包含的键值对的key值;如果该分支节点为账户地址在MPT树上的搜索路径上的中间节点,则该分支节点的Value字段可以为空值。0~f字段中用于填充下一层节点的hash值。The hexadecimal character 0~f fields in the branch node correspond to the key value of the key-value pair contained in the branch node; if the branch node is an intermediate node on the search path of the account address on the MPT tree, then the branch node The Value field can be empty. The hash value used to fill the next layer node in the 0~f field.
叶子节点中的Key-end,对应该叶子节点所包含的键值对的key值,表示账户地址的最后几个字符(账户地址的字符后缀)。从根节点搜索到叶子节点的搜索路径上的各个节点的key值,构成了一个完整的账户地址。该叶子节点的Value字段填充账户地址对应的账户状态数据;例如,可以对上述Balance,Nonce,Code和storage等字段构成的结构体进行编码后,填充至叶子节点的Value字段。The Key-end in the leaf node corresponds to the key value of the key-value pair contained in the leaf node, and represents the last few characters of the account address (the character suffix of the account address). The key values of each node on the search path from the root node to the leaf node constitute a complete account address. The Value field of the leaf node is filled with the account status data corresponding to the account address; for example, the structure composed of the above fields such as Balance, Nonce, Code, and storage can be encoded and then filled in the Value field of the leaf node.
请参见图3,图3为本说明书示出的一种将合约账户对应的存储空间中存储的合约数据组织成MPT storage树的示意图。Please refer to Figure 3. Figure 3 is a schematic diagram of organizing the contract data stored in the storage space corresponding to the contract account into an MPT storage tree as shown in this specification.
请继续参见表1,表1中示出的账户地址为“a77d397”的账户为合约账户,因此该合约账户对应的存储空间中存储的合约数据会被组织成一棵storage树。其中,该storage树的根节点,也会基于其hash值链接到图1示出的MTP状态树中与该合约账户对应的叶子节点。该storage树的根节点的hash值S1,会被添加到图1示出的MTP状态树中与该合约账户对应的叶子节点中存储的账户状态中的storage root字段里。此时该storage树可以称之为是图1示出的MTP状态树中与该合约账户对应的叶子节点延伸出的一棵子树。Please continue to refer to Table 1. The account with the account address "a77d397" shown in Table 1 is a contract account, so the contract data stored in the storage space corresponding to the contract account will be organized into a storage tree. Among them, the root node of the storage tree will also be linked to the leaf node corresponding to the contract account in the MTP status tree shown in Figure 1 based on its hash value. The hash value S1 of the root node of the storage tree will be added to the storage root field in the account status stored in the leaf node corresponding to the contract account in the MTP status tree shown in Figure 1. At this time, the storage tree can be called a subtree extended from the leaf node corresponding to the contract account in the MTP status tree shown in Figure 1.
假设该合约账户的存储空间中存储的合约数据的key-value键值对如下表2所示:Assume that the key-value pair of contract data stored in the storage space of the contract account is as shown in Table 2 below:
Figure PCTCN2022135539-appb-000002
Figure PCTCN2022135539-appb-000002
表2Table 2
需要说明的是,合约账户的存储空间中存储的合约数据,通常可以是状态变量的形式。在进行存储时,可以将状态变量以key-value键值对的形式组织成如图3所示的storage树进行存储。例如,在一个例子中,可以将合约账户的账户地址和状态变量在合约账户的账户存储中的存储位置的hash值作为key,将状态变量对应的变量取值作为value。It should be noted that the contract data stored in the storage space of the contract account can usually be in the form of state variables. When storing, state variables can be organized into a storage tree as shown in Figure 3 in the form of key-value pairs for storage. For example, in one example, the hash value of the account address of the contract account and the storage location of the state variable in the account storage of the contract account can be used as the key, and the value of the variable corresponding to the state variable can be used as the value.
其中,图3示出的storage树的基本结构与图2示出的MTP状态树相似,在本说明书中不再赘 述。The basic structure of the storage tree shown in Figure 3 is similar to the MTP status tree shown in Figure 2, and will not be described again in this specification.
进一步的,无论是如图2所示的MPT状态树上的node,还是如图3所示的storage树上的node,都可以以Key-Value键值对的形式存储在数据库中进行持久化存储。Furthermore, whether it is a node on the MPT state tree as shown in Figure 2 or a node on the storage tree as shown in Figure 3, it can be stored in the database in the form of Key-Value pairs for persistent storage. .
例如,上述数据库通常可以存储在上述节点设备上搭载的持久化存储介质(比如存储磁盘)中。上述存储介质为与上述数据库对应的物理存储。For example, the above-mentioned database can usually be stored in a persistent storage medium (such as a storage disk) mounted on the above-mentioned node device. The above storage medium is the physical storage corresponding to the above database.
其中,上述MPT状态树或者上述storage树上的node对应的Key-Value键值对中的key,具体可以是node所包含的数据内容的hash值;node的键值对中的Value,具体可以是node所包含的数据内容。Among them, the key in the Key-Value pair corresponding to the node in the above-mentioned MPT state tree or the above-mentioned storage tree can be the hash value of the data content contained in the node; the value in the key-value pair of the node can be specifically The data content contained by node.
在将上述MPT状态树或者上述storage树上的node存储至数据库时,可以计算该node所包含的数据内容的hash值(即对node整体进行hash计算),并将计算出的hash值作为key,将该node所包含的数据内容作为value,生成Key-Value键值对。然后,将生成的Key-Value键值对存储至数据库中。而在需要查询上述MPT状态树或者上述storage树上的node时,可以基于node所包含的数据内容的hash值作为key来进行内容寻址。When storing the node on the above-mentioned MPT state tree or the above-mentioned storage tree into the database, the hash value of the data content contained in the node can be calculated (that is, hash calculation is performed on the entire node), and the calculated hash value is used as the key. Use the data content contained in the node as value to generate a Key-Value pair. Then, store the generated Key-Value pair in the database. When you need to query the node on the above-mentioned MPT status tree or the above-mentioned storage tree, you can perform content addressing based on the hash value of the data content contained in the node as the key.
请参见图4,图4为本说明书示出的一种FDMT树的树形结构图。Please refer to Figure 4, which is a tree structure diagram of an FDMT tree shown in this specification.
上述FDMT树也是一种融合了Trie字典树的树形结构的Merkle树变种。The above-mentioned FDMT tree is also a Merkle tree variant that incorporates the tree structure of a Trie dictionary tree.
在实际应用中,也可以将区块链数据以key-value键值对的形式组织成FDMT树的形式在数据库中进行存储。In practical applications, blockchain data can also be organized into FDMT trees in the form of key-value pairs and stored in the database.
如图4所示,在FDMT树的树形结构中,可以包含前N层(图4示出的为3层,仅为示意性的)的Tree node,和最后一层的Leaf node(即叶子节点)。其中,在前N层的Tree node中,第一层的Tree node将作为根节点,除了第一层以外的其它层的Tree node将作为中间节点。As shown in Figure 4, the tree structure of the FDMT tree can include the first N layers of Tree nodes (Figure 4 shows 3 layers, which is only schematic), and the last layer of Leaf nodes (i.e. leaves). node). Among the Tree nodes in the first N layers, the Tree node in the first layer will serve as the root node, and the Tree nodes in other layers except the first layer will serve as intermediate nodes.
其中,与以上描述的MPT树不同的是,上述FDMT树前N层的各个Tree node(即根节点和中间节点),将采用统一的数据结构。Among them, what is different from the MPT tree described above is that each Tree node (that is, the root node and the intermediate node) of the first N layers of the above-mentioned FDMT tree will adopt a unified data structure.
如图4所示,上述FDMT树上的前N层的Tree node,均可以包括分别代表不同字符的多个block;上述block为用于存放区块链数据的key中的字符的“位置”。而每一个block可以进一步包括多个分别代表不同字符的槽位。上述槽位也用于存放区块链数据的key中的字符。As shown in Figure 4, the first N layers of Tree nodes on the above-mentioned FDMT tree can each include multiple blocks representing different characters; the above-mentioned blocks are the "positions" of characters in the key used to store blockchain data. Each block can further include multiple slots representing different characters. The above slot is also used to store the characters in the key of the blockchain data.
例如,图4示出的为每一个Tree node包括N个block。每个block进一步包括N个slot。其中,上述FDMT树上各层的节点之间,仍然可以采用在上一层的节点中填充下一层的节点的hash值(hash指针)的方式,来进行节点间的链接。也即,上述FDMT树中的节点,通过其自身的hash值,链接至上一层的节点。相应的,上述槽位具体可以用于填充当前的Tree node所链接的下一层节点的hash值。Tree node的下一层节点,具体仍然可以是Tree node,也可以是Leaf node。For example, Figure 4 shows that each Tree node includes N blocks. Each block further includes N slots. Among them, the nodes of each layer on the above-mentioned FDMT tree can still be linked by filling the hash value (hash pointer) of the node of the next layer into the node of the upper layer. That is, the nodes in the above FDMT tree are linked to the nodes in the upper layer through their own hash values. Correspondingly, the above slots can be used to fill the hash value of the next-level node linked to the current Tree node. The node at the next level of the Tree node can still be a Tree node or a Leaf node.
需要解释的是,图4中示出的上述FDMT树上各层的节点之间的链接关系仅为示意性的,并不是对上述FDMT树上各层的节点之间的链接关系的一种特殊限定。It should be explained that the link relationship between the nodes at each layer on the above-mentioned FDMT tree shown in Figure 4 is only schematic and is not a special link relationship between the nodes at each layer on the above-mentioned FDMT tree. limited.
请继续参见图4,图4示出的上述FDMT树上的各Tree node,均可以用于存储上述区块链数据的key中的至少部分字符。Please continue to refer to Figure 4. Each Tree node on the above-mentioned FDMT tree shown in Figure 4 can be used to store at least some characters in the key of the above-mentioned blockchain data.
上述区块链数据的key对应的字符串,仍然可以包括字符前缀和字符后缀。在这种情况下,上述Tree node可以用于存储所述区块链数据的key的字符前缀中的字符。上述叶子节点可以用于存储所述区块链数据的key的字符后缀和上述区块链数据的Value。The string corresponding to the key of the above blockchain data can still include character prefixes and character suffixes. In this case, the above-mentioned Tree node can be used to store the characters in the character prefix of the key of the blockchain data. The above leaf node can be used to store the character suffix of the key of the blockchain data and the value of the above blockchain data.
对于图4示出的上述FDMT树上的各Tree node,实际存储的字符,具体可以是该Tree node中的block(即至少有一个槽位填充了hash值的非空block)所代表的字符,与该block中填充了hash值的槽位(即非空槽位)所代表的字符,进行拼接生成的字符串。For each Tree node on the above-mentioned FDMT tree shown in Figure 4, the actual stored characters can specifically be the characters represented by the block in the Tree node (that is, a non-empty block with at least one slot filled with a hash value), A string generated by concatenating the characters represented by the slots filled with hash values (that is, non-empty slots) in the block.
其中,需要说明的是,在实际应用中,Tree node中的每一个block,可以仅代表一位字符。也即,基于图4示出的Tree node的存储格式,每一个Tree node实际存储的上述区块链数据的key的字符前缀中的部分字符,为长度是2位字符的字符串。Among them, it should be noted that in actual applications, each block in the Tree node can represent only one character. That is, based on the storage format of the Tree node shown in Figure 4, some of the characters in the character prefix of the key of the above-mentioned blockchain data actually stored by each Tree node are strings with a length of 2 characters.
例如,请参见图5,图5为本说明书示出的一种Tree node的结构图;For example, please refer to Figure 5, which is a structural diagram of a Tree node shown in this specification;
如图5所示,该Tree node包含16个代表不同的16进制字符的block。每一个block进一步包括16个分别代表不同的16进制字符的slot(图5中只示出了block6包含的16个槽位)。假设该Tree node中的block6(代表了16进制字符6)为非空block,该block中的slot4(代表了16进制字符4)、slot6(代表了16进制字符6)和slot9(代表了16进制字符9)为填充了该Tree node链接的下一层节点的hash值的非空slot;则该Tree node存储的上述区块链数据的key的字符前缀中的部分字符,分别为16进制字符串“64”、“66”和“69”。As shown in Figure 5, the Tree node contains 16 blocks representing different hexadecimal characters. Each block further includes 16 slots that represent different hexadecimal characters (only the 16 slots included in block 6 are shown in Figure 5). Assume that block6 (represents hexadecimal character 6) in the Tree node is a non-empty block, and slot4 (represents hexadecimal character 4), slot6 (represents hexadecimal character 6) and slot9 (represents The hexadecimal character 9) is a non-empty slot filled with the hash value of the next-level node linked to the Tree node; then some of the characters in the character prefix of the key of the above-mentioned blockchain data stored in the Tree node are: Hexadecimal strings "64", "66" and "69".
其中,上述Tree node中所包含的block的数量,以及每一个block所包含的槽位的数量,在本说明书中不进行特别限定。在实际应用中,可以基于上述区块链数据的key对应的字符串所包含的字符元素的类型数,来确定上述Tree node所包含的子block的数量;以及,子block所包含的槽位的数量。Among them, the number of blocks contained in the above-mentioned Tree node and the number of slots contained in each block are not specifically limited in this specification. In practical applications, the number of sub-blocks contained in the above-mentioned Tree node can be determined based on the number of types of character elements contained in the string corresponding to the key of the above-mentioned blockchain data; and, the number of slots contained in the sub-blocks quantity.
例如,假设上述区块链数据对应的key为16进制字符串,此时上述区块链数据的key对应的字符串所包含的字符元素的类型数为16;则上述Tree node中所包含的block的数量,以及每一个block所包含的槽位的数量都可以为16。For example, assume that the key corresponding to the above-mentioned blockchain data is a hexadecimal string. At this time, the number of character element types contained in the string corresponding to the key of the above-mentioned blockchain data is 16; then the number of character elements contained in the above-mentioned Tree node The number of blocks and the number of slots contained in each block can be 16.
其中,上述FDMT树包含的Tree node的层数,具体可以是一个固定值;在实际应用中,上述N的取值具体可以是一个大于或者等于1的整数。也即,上述FDMT树具体可以是一棵包含至少一层Tree node,并且包含的Tree node的层数相对固定的Merkle树。Among them, the number of layers of Tree nodes contained in the above-mentioned FDMT tree can be a fixed value; in practical applications, the value of the above-mentioned N can be an integer greater than or equal to 1. That is to say, the above-mentioned FDMT tree can be a Merkle tree that contains at least one layer of Tree nodes, and the number of layers of Tree nodes contained is relatively fixed.
例如,在一个例子中,以上述区块链数据的key为区块链账户地址为例,假设区块链系统支持的区块链账户地址被设计为,前6位地址字符可以相同。那么,在这种情况下,由于Tree node存储的字符的长度为2位字符;因此,上述FDMT树可以被设计成包含三层Tree node的树状结构。For example, in one example, taking the key of the above blockchain data as a blockchain account address, assuming that the blockchain account address supported by the blockchain system is designed, the first 6 address characters can be the same. Well, in this case, since the length of the characters stored in the Tree node is 2 characters; therefore, the above FDMT tree can be designed as a tree structure containing three layers of Tree nodes.
进一步的,对于图4上示出的FDMT树上的Tree node和Leaf node,也可以以Key-Value键值对的形式在数据库中进行持久化存储。其中,上述FDMT树上的Tree node或Leaf node对应的Key-Value键值对中的key,具体可以是Tree node或Leaf nod所包含的数据内容的hash值。Tree node或Leaf node的键值对中的Value,具体可以是Tree node或Leaf node所包含的数据内容。Furthermore, the Tree node and Leaf node on the FDMT tree shown in Figure 4 can also be persistently stored in the database in the form of Key-Value pairs. Among them, the key in the Key-Value pair corresponding to the Tree node or Leaf node on the above-mentioned FDMT tree can specifically be the hash value of the data content contained in the Tree node or Leaf node. The Value in the key-value pair of Tree node or Leaf node can be the data content contained in Tree node or Leaf node.
在将FDMT树上的Tree node或Leaf node存储至数据库时,也可以计算该Tree node或Leaf node所包含的数据内容的hash值(即对node整体进行hash计算),并将计算出的hash值作为key,将该Tree node或Leaf node所包含的数据内容作为value,生成Key-Value键值对。然后,将生成的Key-Value键值对存储至数据库中。而在需要查询上述FDMT树上的node时,可以基于node所包含的数据内容的hash值作为key来进行内容寻址。When storing the Tree node or Leaf node on the FDMT tree to the database, you can also calculate the hash value of the data content contained in the Tree node or Leaf node (that is, perform hash calculation on the entire node), and use the calculated hash value As the key, use the data content contained in the Tree node or Leaf node as the value to generate a Key-Value pair. Then, store the generated Key-Value pair in the database. When you need to query the node on the above FDMT tree, you can perform content addressing based on the hash value of the data content contained in the node as the key.
需要说明的是,图4示出的FDMT树的树形结构,可以用于存储区块链中的各个区块链账户的账户状态数据,也可以用于存储某一合约账户对应的存储空间中存储的合约数据。It should be noted that the tree structure of the FDMT tree shown in Figure 4 can be used to store the account status data of each blockchain account in the blockchain, and can also be used to store the storage space corresponding to a certain contract account. Stored contract data.
其中,用于存储区块链中的各个区块链账户的账户状态数据的FDMT树,可以称之为FDMT状态树。用于存储某一合约账户对应的存储空间中存储的合约数据的FDMT树,可以称之为FDMT storage树。Among them, the FDMT tree used to store the account status data of each blockchain account in the blockchain can be called the FDMT status tree. The FDMT tree used to store contract data stored in the storage space corresponding to a certain contract account can be called an FDMT storage tree.
上述FDMT storage树上的根节点,具体也可以通过其hash值链接到上述FDMT状态树中与上述合约账户对应的叶子节点中。该FDMT storage树上的根节点的hash值,也可以被添加到上述FDMT状态树中与该合约账户对应的叶子节点中存储的账户状态中的storage root字段里,不再赘述。The root node on the above-mentioned FDMT storage tree can also be linked to the leaf node corresponding to the above-mentioned contract account in the above-mentioned FDMT status tree through its hash value. The hash value of the root node on the FDMT storage tree can also be added to the storage root field in the account status stored in the leaf node corresponding to the contract account in the above FDMT status tree, which will not be described again.
其中,需要说明的是,无论是图1示出的MPT树,还是图4示出的FDMT树,其树形结构上的Leaf node实际存储的数据,相对于其它类型的节点通常具有更大的数据容量。比如,上述Leaf node实际存储的上述区块链数据的value,通常为上述区块链数据的原始内容,上述区块链数据的原始内容相对于上述区块链数据的字符前缀而言,其所占用的存储空间也更大。因此,为了确保上述Leaf node能够具有更大的数据容量,无论是图1示出的MPT树,还是图4示出的FDMT树上的Leaf node,通常都被设计成大数据块的形式。Among them, it should be noted that, whether it is the MPT tree shown in Figure 1 or the FDMT tree shown in Figure 4, the actual data stored in the Leaf node on its tree structure usually has a larger size than other types of nodes. Data capacity. For example, the value of the above-mentioned blockchain data actually stored by the above-mentioned Leaf node is usually the original content of the above-mentioned blockchain data. The original content of the above-mentioned blockchain data is relative to the character prefix of the above-mentioned blockchain data. It also takes up more storage space. Therefore, in order to ensure that the above-mentioned Leaf nodes can have larger data capacity, whether it is the MPT tree shown in Figure 1 or the Leaf node on the FDMT tree shown in Figure 4, they are usually designed in the form of large data blocks.
其中,上述数据块的具体形式以及存储结构,在本说明书中不进行特别限定。The specific form and storage structure of the above-mentioned data blocks are not particularly limited in this specification.
例如,在实际应用中,上述叶子节点具体可以是bucket数据桶的形式。其中,上述bucket数据桶具体可以是用于存储数据的容器或者存储空间。For example, in practical applications, the above leaf nodes may be in the form of bucket data buckets. The above-mentioned bucket data bucket may specifically be a container or storage space used to store data.
例如,请参见图6,图6为本说明书示出的一种bucket数据桶的结构图。For example, please refer to Figure 6, which is a structural diagram of a bucket data bucket shown in this specification.
如图6所示,在上述bucket数据桶(即图6中示出的bucker node)中,可以包括若干条数据记录。其中,每一条数据记录都分别对应一条区块链数据,用于存储上述区块链数据的key的字符后缀(即图6中示出的key-end)和value。也即,一条数据记录,是指一条包括上述区块链数据的key的字符后缀和value的存储记录。As shown in Figure 6, the above bucket data bucket (ie, the bucker node shown in Figure 6) can include several data records. Among them, each data record corresponds to a piece of blockchain data, which is used to store the character suffix (ie, the key-end shown in Figure 6) and value of the key of the above-mentioned blockchain data. That is, a data record refers to a storage record that includes the character suffix and value of the key of the above blockchain data.
需要说明的是,图6示出的bucket数据桶的结构,具体是以作为图4示出的FDMT树上的Leaf node为例来进行说明的。实际应用中,图6示出的bucker node的结构具体也可以作为图1示出的MPT树的Leaf node,在本说明书不再赘述。It should be noted that the structure of the bucket data bucket shown in Figure 6 is specifically explained by taking the Leaf node on the FDMT tree shown in Figure 4 as an example. In practical applications, the structure of the bucker node shown in Figure 6 can also be used as the Leaf node of the MPT tree shown in Figure 1, which will not be described again in this specification.
通过以上的描述可知无论是图1示出的MPT树,还是如图4示出的FDMT树,都会存在一些包含若干个槽位的节点。比如,图1示出的MPT树上的分支节点(branch node),图4示出的FDMT的前N层的Tree node,都包含若干个用于存放区块链数据的key中的字符的槽位。It can be seen from the above description that whether it is the MPT tree shown in Figure 1 or the FDMT tree shown in Figure 4, there will be some nodes containing several slots. For example, the branch node on the MPT tree shown in Figure 1 and the Tree node of the first N layers of FDMT shown in Figure 4 both contain several slots for storing characters in the key of the blockchain data. Bit.
在实际应用中,对于MPT树或者FDMT树上那些包含多个槽位的节点来说,如果该节点中仅部分槽位里存放的内容发生更新,那么即便除了这部分槽位以外的其他槽位里存放的内容并未发生更新,通常也需要在对这部分槽位更新完成后,将整个节点重新写入数据库进行持久化存储,从而造成明显的写放大效应。In practical applications, for those nodes in the MPT tree or FDMT tree that contain multiple slots, if the content stored in only part of the slots in the node is updated, then even if other slots except this part of the slots are updated, The content stored in the slot has not been updated. Usually, after the update of this part of the slot is completed, the entire node needs to be rewritten to the database for persistent storage, resulting in an obvious write amplification effect.
其中,需要解释的是,所谓的写放大(Write amplification),是指需要写入的数据小于最终实际写入的数据,而产生的写入带宽放大的效应。Among them, it needs to be explained that the so-called write amplification refers to the effect of writing bandwidth amplification caused by the data to be written being smaller than the data actually written.
例如,对于MPT树或者FDMT树上包含多个槽位的节点,如果该节点中仅部分槽位里存放的内容发生更新,则需要写入的是这部分槽位里存放的内容,然而由于MPT树或者FDMT树上的节点是一个完整的不可再分的数据整体,因此即便只需要写入部分槽位里存放的内容,也不得不将整个节点写入到磁盘的数据库中,从而导致需要写入的数据小于最终实际写入的数据,产生上述的写放大效应,造成写入带宽浪费。For example, for a node containing multiple slots in an MPT tree or FDMT tree, if only the content stored in some slots in the node is updated, the content stored in this part of the slots needs to be written. However, due to the MPT The nodes on the tree or FDMT tree are a complete and non-divisible whole of data. Therefore, even if only the content stored in some slots needs to be written, the entire node has to be written to the database on the disk, resulting in the need to write The input data is smaller than the final actual written data, resulting in the above-mentioned write amplification effect, resulting in a waste of write bandwidth.
而且,由于FDMT树的前N层的Tree node,均采用了包含多个槽位的数据结构,因此在利用FDMT树存储区块链数据时,造成的写放大效应尤其明显。Moreover, since the Tree nodes in the first N layers of the FDMT tree all use a data structure containing multiple slots, the write amplification effect is particularly obvious when using the FDMT tree to store blockchain data.
有鉴于此,本说明书提出一种优化在利用逻辑的树形结构来存储区块链数据时产生的写放大效应的技术方案。In view of this, this specification proposes a technical solution to optimize the write amplification effect produced when using a logical tree structure to store blockchain data.
在实现时,区块链数据的key-value键值对可以以逻辑的树形结构上的根节点、中间节点和叶子节点的形式,存储在数据库中;根节点、中间节点用于存放区块链数据的key中的字符;叶子节点用于存放所述区块链数据的value;上述树形结构上的任一节点通过其hash值与上一层的节点链接。During implementation, the key-value pairs of blockchain data can be stored in the database in the form of root nodes, intermediate nodes and leaf nodes in a logical tree structure; the root nodes and intermediate nodes are used to store blocks. Characters in the key of the chain data; leaf nodes are used to store the value of the blockchain data; any node on the above tree structure is linked to the node on the previous level through its hash value.
在利用上述逻辑的树形结构来存储区块链数据时,可以获取待存储的区块链数据,将该区块链数据转换成逻辑的树形结构上的根节点、中间节点和叶子节点;When using the above logical tree structure to store blockchain data, the blockchain data to be stored can be obtained, and the blockchain data can be converted into root nodes, intermediate nodes and leaf nodes on the logical tree structure;
一方面,可以将上述根节点、中间节点中的至少部分节点缓存至支持覆盖式写入数据的存储介质,以在该存储介质中针对上述至少部分节点进行修改更新;On the one hand, at least some of the above-mentioned root nodes and intermediate nodes can be cached to a storage medium that supports overwriting data, so that at least some of the above-mentioned nodes can be modified and updated in the storage medium;
另一方面,还可以生成用于记录针对上述至少部分节点的修改更新详情的数据记录,并将上述数据记,录和上述根节点、中间节点和叶子节点中除上述至少部分节点以外的其他节点写入所述数据库进行持久化存储。On the other hand, a data record for recording modification and update details for at least part of the above-mentioned nodes may also be generated, and the above-mentioned data records may be recorded with other nodes among the above-mentioned root nodes, intermediate nodes and leaf nodes except for the above-mentioned at least part of the nodes. Write to the database for persistent storage.
在以上技术方案中,通过将上述逻辑的树形结构上的根节点、中间节点中的至少部分节点缓存在支持覆盖式写入数据的存储介质,并在该存储介质中针对该至少部分节点进行修改更新,可以缓解在将上述逻辑的树形结构上的根节点、中间节点中的至少部分节点反复写入数据库的过程中而造成的写放大效应,进而可以提升上述数据库的存储性能。In the above technical solution, at least part of the root nodes and intermediate nodes on the above-mentioned logical tree structure are cached in a storage medium that supports overwriting data, and the at least part of the nodes are processed in the storage medium. Modification and update can alleviate the write amplification effect caused by repeatedly writing at least some of the root nodes and intermediate nodes in the above-mentioned logical tree structure to the database, thereby improving the storage performance of the above-mentioned database.
例如,在实际应用中,上述逻辑的树形结构上的根节点、中间节点中,如果存在包含多个槽位的节点,那么将这些节点在支持覆盖式写入数据的存储介质中进行缓存,就可以在该存储介质中采用覆盖式写入的方式,针对该至少部分节点进行修改更新,而这些被覆盖式写入的节点将不再需要写入到磁盘中的数据库,从而可以避免将这些节点整体写入磁盘中的数据库而造成以上描述的写放大效应。For example, in practical applications, if there are nodes containing multiple slots in the root node and intermediate nodes of the above logical tree structure, then these nodes will be cached in a storage medium that supports overwriting data. Overwrite writing can be used in the storage medium to modify and update at least some of the nodes, and these overwritten nodes will no longer need to be written to the database in the disk, thus avoiding the need to write these to the database. The entire node writes to the database on disk, causing the write amplification effect described above.
请参见图7,图7是一示例性实施例提供的一种区块链数据存储方法的流程图。所述方法应用于区块链节点设备;所述区块链数据的key-value键值对以逻辑的树形结构上的根节点、中间节点和叶子节点的形式,存储在数据库中;所述根节点、中间节点用于存放所述区块链数据的key中的字符;所述叶子节点用于存放所述区块链数据的value;所述树形结构上的任一节点通过其hash值与上一层的节点链接;所述方法包括以下步骤:Please refer to Figure 7, which is a flow chart of a blockchain data storage method provided by an exemplary embodiment. The method is applied to blockchain node equipment; the key-value pairs of the blockchain data are stored in the database in the form of root nodes, intermediate nodes and leaf nodes on a logical tree structure; the The root node and the intermediate node are used to store the characters in the key of the blockchain data; the leaf nodes are used to store the value of the blockchain data; any node on the tree structure passes its hash value Link with the node of the previous layer; the method includes the following steps:
步骤702,获取待存储的区块链数据的key-value键值对;Step 702: Obtain the key-value pair of the blockchain data to be stored;
上述待存储的区块链数据,具体可以包括需要在区块链中进行持久化存储的任意类型的数据。The above-mentioned blockchain data to be stored may specifically include any type of data that needs to be persistently stored in the blockchain.
在示出的一种实施方式中,上述待存储的区块链数据,具体可以包括与区块链上的区块链账户对应的账户状态数据。In one embodiment shown, the above-mentioned blockchain data to be stored may specifically include account status data corresponding to the blockchain account on the blockchain.
例如,如前所述,在实际应用中,区块链中的区块链账户通常可以包括外部账户和合约账户,因而与区块链上的区块链账户对应的账户状态数据,则具体可以包括与区块链上的用户账户对应的账户状态数据(比如可以是账户余额数据),以及区块链上的合约账户中存储的状态变量数据(比如可以是智能合约中存储的存证数据)。For example, as mentioned above, in practical applications, blockchain accounts in the blockchain can usually include external accounts and contract accounts. Therefore, the account status data corresponding to the blockchain account on the blockchain can be specifically Including account status data corresponding to user accounts on the blockchain (for example, it can be account balance data), and state variable data stored in contract accounts on the blockchain (for example, it can be certificate data stored in smart contracts) .
当然,在实际应用中,上述待存储的区块链数据也可以包括发布至区块链网络中的交易数据,以及与交易数据在执行完毕后产生的与交易数据对应的收据数据,等等。Of course, in practical applications, the above-mentioned blockchain data to be stored may also include transaction data published to the blockchain network, receipt data corresponding to the transaction data generated after the transaction data is executed, and so on.
在一个例子中,区块链中的节点设备,在获取待存储的区块链数据的key-value键值对时,具体可以在获取到待存储的区块链数据之后,在本地将该区块链数据处理成key-value键值对。In one example, when the node device in the blockchain obtains the key-value pair of the blockchain data to be stored, the node device can locally store the area after obtaining the blockchain data to be stored. Blockchain data is processed into key-value pairs.
在另一个例子中,将待存储的区块链数据处理成key-value键值对的步骤,也可以由第三方来完成,节点设备可以从该第三方直接获取由该第三方处理完成的该待存储的区块链数据的key-value键值对。In another example, the step of processing the blockchain data to be stored into key-value pairs can also be completed by a third party. The node device can directly obtain the data processed by the third party from the third party. The key-value pair of the blockchain data to be stored.
其中,区块链数据的key-value键值对中的key,可以是指该区块链数据在数据库中的主键。该主键具体可以充当查询索引的作用。区块链数据的key-value键值对中的value,具体是指上述区块链数据的数据内容。Among them, the key in the key-value pair of the blockchain data may refer to the primary key of the blockchain data in the database. This primary key can specifically serve as a query index. The value in the key-value pair of blockchain data specifically refers to the data content of the above-mentioned blockchain data.
需要说明的是,对于不同类型的区块链数据,其key-value键值对中的key,通常也可能会存在一定的差异。It should be noted that for different types of blockchain data, there may usually be certain differences in the keys in the key-value pairs.
例如,如果上述区块链数据具体为区块链中的区块链账户对应的账户状态数据,则该账户状态数据的key-value键值对中的key,具体可以是区块链账户的账户地址。For example, if the above blockchain data is specifically the account status data corresponding to the blockchain account in the blockchain, then the key in the key-value pair of the account status data can specifically be the account of the blockchain account. address.
如果上述区块链数据具体为区块链中的交易数据或者与交易数据对应的收据数据时,该交易数据或者与交易数据对应的收据数据的key-value键值对中的key,具体可以是交易标识;比如,在实际应用中,该交易标识具体可以是交易的hash值,或者也可以是在对交易进行共识时为该交易分配的交易ID。If the above blockchain data is specifically transaction data in the blockchain or receipt data corresponding to the transaction data, the key in the key-value pair of the transaction data or receipt data corresponding to the transaction data may specifically be: Transaction identifier; for example, in practical applications, the transaction identifier can be the hash value of the transaction, or it can also be the transaction ID assigned to the transaction when consensus is reached on the transaction.
步骤704,将所述区块链数据key-value键值对转换成逻辑的树形结构上的根节点、中间节点和叶子节点;Step 704: Convert the blockchain data key-value pairs into root nodes, intermediate nodes and leaf nodes in a logical tree structure;
对于待存储的区块链数据的key-value键值对,具体可以将其组织成逻辑的树状结构,并以该逻辑的树状结构上的节点的形式,在数据库中进行存储。For the key-value pairs of the blockchain data to be stored, they can be organized into a logical tree structure and stored in the database in the form of nodes on the logical tree structure.
其中,所谓逻辑的树形结构,是指基于数据库中存储的节点,以及节点之间的链接关系,在逻辑层面上构建出的树形结构。Among them, the so-called logical tree structure refers to a tree structure constructed on a logical level based on the nodes stored in the database and the link relationships between the nodes.
例如,上述逻辑的树形结构中具体可以包括多层节点,该多层节点具体可以以节点为单位存储在承载数据库的底层物理存储(比如磁盘)中。在需要使用上述逻辑的树形结构上存储的区块链数据 时,具体可以将数据库中存储的多层节点加载到内存中,并在内存中按照各个节点之间的链接关系,在逻辑层面还原出具体的树形结构。For example, the above logical tree structure may specifically include multi-layer nodes, and the multi-layer nodes may be stored in node units in the underlying physical storage (such as a disk) hosting the database. When you need to use the blockchain data stored in the tree structure of the above logic, you can specifically load the multi-layer nodes stored in the database into the memory, and restore them at the logical level in the memory according to the link relationship between each node. Produce a specific tree structure.
在示出的一种实施方式中,上述逻辑的树形结构,具体可以是融合了字典树的树形结构的Merkle树;例如,可以是上述的MPT树,也可以上述的FDMT树。In an embodiment shown, the above-mentioned logical tree structure may be a Merkle tree that is integrated with the tree structure of a dictionary tree; for example, it may be the above-mentioned MPT tree or the above-mentioned FDMT tree.
在实际应用中,上述逻辑的树形结构可以包括根节点、中间节点和叶子节点。区块链中的节点设备在获取到待存储的区块链数据的key-value键值对之后,可以将该待存储的区块链数据的key-value键值对转换成上述逻辑的树形结构上的根节点、中间节点和叶子节点。In practical applications, the above logical tree structure may include root nodes, intermediate nodes and leaf nodes. After obtaining the key-value pair of the blockchain data to be stored, the node device in the blockchain can convert the key-value pair of the blockchain data to be stored into the above logical tree shape. Root nodes, intermediate nodes and leaf nodes on the structure.
其中,在将区块链数据的key-value键值对,转换成为逻辑的树形结构的根节点、中间节点和叶子节点时,具体可以从该逻辑的树形结构的根节点开始,查找用于存储该区块链数据的key-value键值对的根节点、中间节点和叶子节点,然后基于该区块链数据的key-value键值对对查询到的根节点、中间节点和叶子节点进行更新。在这种情况下,更新后的根节点、中间节点和叶子节点,即为将上述区块链数据的key-value键值对转换成为的根节点、中间节点和叶子节点。Among them, when converting the key-value pairs of the blockchain data into the root nodes, intermediate nodes and leaf nodes of the logical tree structure, you can specifically start from the root node of the logical tree structure and search for To store the root node, intermediate node and leaf node of the key-value pair of the blockchain data, and then query the root node, intermediate node and leaf node based on the key-value pair of the blockchain data Make an update. In this case, the updated root node, intermediate nodes, and leaf nodes are the root nodes, intermediate nodes, and leaf nodes that are converted from the key-value pairs of the above blockchain data.
需要说明的是,如果从该逻辑的树形结构的根节点开始,除了根节点以外,并未查找到用于存储该区块链数据的key-value键值对的中间节点和叶子节点,也可以在该逻辑的树形结构中,基于该区块链数据的key-value键值对来创建用于存储该区块链数据的key-value键值对的中间节点和叶子节点,然后基于该区块链数据的key-value键值对对查询到的根节点、和新创建的中间节点和叶子节点进行更新。It should be noted that if starting from the root node of the logical tree structure, in addition to the root node, no intermediate nodes and leaf nodes used to store the key-value pairs of the blockchain data are found. In the logical tree structure, intermediate nodes and leaf nodes for storing the key-value pairs of the blockchain data can be created based on the key-value pairs of the blockchain data, and then based on the The key-value pair of the blockchain data updates the queried root node, and the newly created intermediate nodes and leaf nodes.
当然,如果是向逻辑的树形结构中首次写入区块链数据,此时在将上述区块链数据的key-value键值对,转换成为逻辑的树形结构的根节点、中间节点和叶子节点时,具体也可以初始化的创建用于存储该区块链数据的key-value键值对的根节点、中间节点和叶子节点。此时初始化创建的该根节点、中间节点和叶子节点,即为将上述区块链数据的key-value键值对转换成为的根节点、中间节点和叶子节点。Of course, if the blockchain data is written to the logical tree structure for the first time, the key-value pairs of the above blockchain data are converted into the root node, intermediate node and node of the logical tree structure. For leaf nodes, you can also initially create root nodes, intermediate nodes, and leaf nodes that store the key-value pairs of the blockchain data. The root node, intermediate node, and leaf node created by initialization at this time are the root node, intermediate node, and leaf node that are converted from the key-value pairs of the above blockchain data.
其中,上述逻辑的树状结构上的节点,仍然可以通过其自身的hash值与上一层的节点进行链接。上述根节点和中间节点,具体用于存储区块链数据的key-value键值对应的key中的至少一个字符。而上述叶子节点具体用于存储区块链数据的value(即区块链数据的具体内容)。中间节点的层数可以为一层也可以为多层,在本说明书中不进行特别限定。Among them, the nodes in the above logical tree structure can still be linked to the nodes in the upper layer through their own hash values. The above-mentioned root node and intermediate node are specifically used to store at least one character in the key corresponding to the key-value key value of the blockchain data. The above leaf nodes are specifically used to store the value of blockchain data (that is, the specific content of blockchain data). The number of layers of intermediate nodes may be one layer or multiple layers, and is not particularly limited in this specification.
例如,在一个例子中,上述区块链数据的key,仍然可以包括字符前缀部分(Shared nibble)和字符后缀部分(key-end);在这种情况下,根节点和中间节点可以用于存储上述字符前缀中的字符。而上述叶子节点则可以用于存储上述字符后缀和区块链数据的value。For example, in one example, the key of the above blockchain data can still include a character prefix part (Shared nibble) and a character suffix part (key-end); in this case, the root node and the intermediate node can be used for storage characters in the above character prefix. The above leaf nodes can be used to store the above character suffix and the value of the blockchain data.
一方面,由于上述逻辑的树形结构中的根节点和中间节点,可以存放区块链数据的key中的字符;因此,上述逻辑的树形结构具有Trie字典树的特点。另一方面,上述逻辑的树状结构上的节点,可以通过其自身的hash值与上一层的节点进行链接。因此,上述逻辑的树形结构也具有Merkle树的特点。不难理解,本说明书描述的逻辑的树形结构,可以是一种类似于MPT树或者FDMT树的融合了Trie字典树的树形结构的Merkle树变种。需要补充说明的是,当上述区块链数据为与区块链上的区块链账户对应的账户状态数据时,上述逻辑的树状结构具体可以是基于所述区块链中的各个区块链账户对应的账户状态数据的key-value键值对生成的Merkle树。此时该Merkle树可以称之为Merkle状态树。On the one hand, the root node and intermediate nodes in the above logical tree structure can store the characters in the key of the blockchain data; therefore, the above logical tree structure has the characteristics of a Trie dictionary tree. On the other hand, the nodes in the above logical tree structure can be linked to the nodes in the upper layer through their own hash values. Therefore, the above logical tree structure also has the characteristics of Merkle tree. It is easy to understand that the logical tree structure described in this specification can be a Merkle tree variant similar to an MPT tree or an FDMT tree that incorporates the tree structure of a Trie dictionary tree. It should be supplemented that when the above-mentioned blockchain data is account status data corresponding to a blockchain account on the blockchain, the above-mentioned logical tree structure may be based on each block in the blockchain. A Merkle tree generated from the key-value pairs of the account status data corresponding to the chain account. At this time, the Merkle tree can be called a Merkle state tree.
而在实际应用中,为了提升Merkle状态树的访问性能,通常会将上述Merkle状态树拆分成一棵当前Merkle状态树(Current State Tree)和一棵历史Merkle状态树(History State Tree)。其中,当前Merkle状态树,是由各个区块链账户的最新账户状态组织成的Merkle状态树;历史Merkle状态树是由各个区块链账户的历史账户状态组织成的Merkle状态树。每一个区块都有一棵与之对应的当前Merkle状态树和历史Merkle状态树。In actual applications, in order to improve the access performance of the Merkle state tree, the above-mentioned Merkle state tree is usually split into a current Merkle state tree (Current State Tree) and a historical Merkle state tree (History State Tree). Among them, the current Merkle state tree is a Merkle state tree organized by the latest account status of each blockchain account; the historical Merkle state tree is a Merkle state tree organized by the historical account status of each blockchain account. Each block has a corresponding current Merkle state tree and historical Merkle state tree.
在这种场景下,由于当前Merkle状态树,维护的是各个区块链账户的最新账户状态,因此基于这 一特点,当前Merkle状态树上的节点通常会频繁的进行写入和修改更新的操作。基于此,在本说明书中,步骤702和步骤706中描述的逻辑的树状结构,具体可以是指上述当前Merkle状态树。也即,对于上述当前Merkle状态树,可以采用如步骤702-步骤706所描述的技术方案,而对于历史Merkle状态树仍然可以采用现有技术中的存储方案。当然,在实际应用中,对于历史Merkle状态树,也可以采用如步骤702-步骤706所描述的技术方案。In this scenario, since the current Merkle state tree maintains the latest account status of each blockchain account, based on this feature, the nodes on the current Merkle state tree usually perform frequent writing, modification and update operations. . Based on this, in this specification, the logical tree structure described in step 702 and step 706 may specifically refer to the above-mentioned current Merkle state tree. That is, for the above-mentioned current Merkle state tree, the technical solution described in steps 702 to 706 can be adopted, while for the historical Merkle state tree, the storage solution in the existing technology can still be adopted. Of course, in practical applications, the technical solution described in steps 702 to 706 can also be used for the historical Merkle state tree.
步骤706,将所述根节点、中间节点中的至少部分节点缓存至支持覆盖式写入数据的存储介质,以在所述存储介质中针对所述至少部分节点进行修改更新;以及,生成用于记录针对所述至少部分节点的修改更新详情的数据记录,并将所述数据记录和所述根节点、中间节点和叶子节点中除所述至少部分节点以外的其他节点写入所述数据库进行持久化存储。Step 706: Cache at least part of the root node and intermediate nodes to a storage medium that supports overwriting data, so as to modify and update the at least part of the nodes in the storage medium; and, generate Record data records of modification update details for at least part of the nodes, and write the data records and other nodes among the root node, intermediate node and leaf node except the at least part of the nodes into the database for persistence storage.
在将上述区块链数据的key-value键值对转换成逻辑的树形结构上的根节点、中间节点和叶子节点,转换成为上述逻辑的树形结构上的根节点、中间节点和叶子节点之后,可以将上述根节点、中间节点和叶子节点在数据库中进行存储。Convert the key-value pairs of the above blockchain data into root nodes, intermediate nodes and leaf nodes on the logical tree structure, and convert them into root nodes, intermediate nodes and leaf nodes on the above logical tree structure. Afterwards, the above root nodes, intermediate nodes and leaf nodes can be stored in the database.
其中,需要说明的是,在实际应用中,上述根节点、中间节点和叶子节点通常都会写入到数据库中进行持久化存储,而在本说明书中,为了缓解在将上述根节点、中间节点写入数据库中持久化存储而造成的写放大效应,在对上述根节点、中间节点进行存储时,具体可以采用与叶子节点完全不同的存储策略。Among them, it should be noted that in actual applications, the above-mentioned root nodes, intermediate nodes and leaf nodes are usually written into the database for persistent storage. However, in this specification, in order to ease the need to write the above-mentioned root nodes, intermediate nodes Due to the write amplification effect caused by persistent storage in the database, when storing the above-mentioned root nodes and intermediate nodes, a completely different storage strategy can be adopted from that of leaf nodes.
一方面,对于上述根节点、中间节点中的至少部分节点,可以不再默认写入上述数据库中进行持久化存储,而是缓存至区块链的节点设备搭载的支持覆盖式写入数据的存储介质中,并在上述存储介质中针对上述至少部分节点进行修改更新。On the one hand, at least some of the above-mentioned root nodes and intermediate nodes can no longer be written to the above-mentioned database for persistent storage by default, but can be cached to the storage on the node device of the blockchain that supports overwriting data. medium, and modify and update at least some of the above-mentioned nodes in the above-mentioned storage medium.
其中,在一个例子中,上述支持覆盖式写入数据的存储介质,具体可以是区块链中的节点设备搭载的内存。当然,除了可以是内存以外,上述存储介质,具体也可以是其它形式的支持覆盖式写入数据的存储介质,在本说明书中不进行具体限定。例如,在实际应用中,上述存储介质具体也可以是固态硬盘。Among them, in one example, the above-mentioned storage medium that supports overwriting data may be a memory mounted on a node device in the blockchain. Of course, in addition to being a memory, the above storage medium can also be other forms of storage media that support overwriting data, which are not specifically limited in this specification. For example, in practical applications, the above-mentioned storage medium may also be a solid-state hard drive.
另一方面,对于上述根节点、中间节点和叶子节点中除了上述至少部分节点以外的其他节点,仍然采用默认写入上述数据库进行持久化存储的方式进行存储。除此之外,还可以生成用于记录针对上述至少部分节点的修改更新详情的数据记录,然后将该数据记录和上述其他节点一起写入上述数据库进行持久化存储。On the other hand, other nodes among the above-mentioned root nodes, intermediate nodes and leaf nodes, except for at least some of the above-mentioned nodes, are still stored by writing to the above-mentioned database for persistent storage by default. In addition, a data record for recording modification and update details for at least some of the above nodes can also be generated, and then the data record and the above other nodes are written into the above database for persistent storage.
例如,在实际应用中,当一个最新的区块中包含的所有交易执行完毕后,这些交易的执行通常会导致逻辑的树状结构中的部分节点存储的数据内容发生变化,此时通常需要重新计算逻辑的树状结构的根节点的hash值(即roothash),将该根节点的hash值填充到区块头中,然后再将逻辑的树状结构中发生更新的节点写入数据库进行持久化存储。在相关技术中,将重新计算逻辑的树状结构的根节点的hash值,以及将逻辑的树状结构中发生更新的节点写入数据库进行持久化存储的过程,称之为针对逻辑的树状结构的commit操作。而在本说明书中,具体也可以在针对该逻辑的树状结构进行commit操作时,将该数据记录和上述其他节点一起写入上述数据库进行持久化存储。For example, in practical applications, when all transactions included in a latest block are executed, the execution of these transactions will usually cause the data content stored in some nodes in the logical tree structure to change. In this case, it is usually necessary to re- Calculate the hash value (i.e. roothash) of the root node of the logical tree structure, fill the hash value of the root node into the block header, and then write the updated nodes in the logical tree structure to the database for persistent storage . In related technologies, the process of recalculating the hash value of the root node of the logical tree structure and writing the updated nodes in the logical tree structure to the database for persistent storage is called logical tree structure. The commit operation of the structure. In this specification, specifically, when performing a commit operation on the logical tree structure, the data record and the above-mentioned other nodes can be written into the above-mentioned database for persistent storage.
在示出的一种实施方式中,上述至少部分节点具体可以是上述根节点、中间节点中,包括多个用于存放该区块链数据的key中的字符的槽位的节点。当然,在实际应用中,上述至少部分节点具体也可以默认包含上述逻辑的树状结构中的所有的根节点和中间节点。In an embodiment shown, at least some of the nodes may specifically be nodes including a plurality of slots for storing characters in the key of the blockchain data among the root nodes and intermediate nodes. Of course, in practical applications, at least some of the above-mentioned nodes may also include all root nodes and intermediate nodes in the above-mentioned logical tree structure by default.
例如,在一个例子中,如果上述逻辑的树状结构是MPT树,则上述至少部分节点可以是MPT树上的分支节点(branch node)。在这种情况下,由于MPT树上只有分支节点(branch node)具有多个槽位,因此可以仅将上述MPT树上的分支节点(branch node)缓存到上述存储介质(即只将MPT树的根节点和中间节点中的分支节点缓存到上述存储介质)。For example, in one example, if the above-mentioned logical tree structure is an MPT tree, then at least some of the above-mentioned nodes may be branch nodes (branch nodes) on the MPT tree. In this case, since only the branch node on the MPT tree has multiple slots, only the branch node on the MPT tree can be cached to the above storage medium (that is, only the branch node of the MPT tree can be cached to the storage medium). The branch nodes in the root node and intermediate nodes are cached to the above storage medium).
在另一个例子中,如果上述逻辑的树状结构为FDMT树,则上述至少部分节点可以是FDMT的前N层的Tree node。在这种情况下,由于FDMT树上前N层的Tree node均具有多个槽位,因此可以仅 将上述FDMT树上的前N层的Tree node均缓存到上述存储介质。(即将FDMT树的根节点和中间节点全部缓存到上述存储介质)。In another example, if the above-mentioned logical tree structure is an FDMT tree, then at least some of the above-mentioned nodes may be Tree nodes of the first N layers of FDMT. In this case, since the Tree nodes of the first N layers of the FDMT tree have multiple slots, only the Tree nodes of the first N layers of the FDMT tree can be cached to the above storage medium. (That is, all the root nodes and intermediate nodes of the FDMT tree are cached in the above storage medium).
相应的,上述数据记录,则具体可以是用于记录针对上述至少部分节点中的各个槽位的修改更新详情的数据记录。Correspondingly, the above-mentioned data record may specifically be a data record used to record modification and update details for each slot in at least some of the above-mentioned nodes.
其中,上述数据记录的具体形式,在本说明书中不进行特别限定。The specific form of the above data recording is not particularly limited in this specification.
在示出的一种实施方式中,上述数据记录具体可以是WAL(write ahead log)日志。其中,WAL日志技术,是一种高效的日志算法。在开启WAL模式的存储系统中,所有针对存储系统的数据修改,在提交到存储系统中之前,都会先写入WAL日志中;然后,再通过周期性触发或者用户手动触发的checkpoint事件,将WAL日志文件中存储的数据修改,写入到存储系统中。In an embodiment shown, the above data record may specifically be a WAL (write ahead log) log. Among them, WAL logging technology is an efficient logging algorithm. In a storage system with WAL mode turned on, all data modifications to the storage system will be written to the WAL log before being submitted to the storage system; then, the WAL log will be updated through checkpoint events that are triggered periodically or manually by the user. The data stored in the log file is modified and written to the storage system.
在这种情况下,当上述存储介质中缓存的上述至少部分节点由于设备异常(比如设备端点)发生数据丢失,可以基于数据库中持久化存储的WAL日志对上述存储介质中缓存的上述至少部分节点进行数据恢复。In this case, when at least some of the nodes cached in the storage medium experience data loss due to device abnormalities (such as device endpoints), the at least some nodes cached in the storage medium can be updated based on the WAL logs persistently stored in the database. Perform data recovery.
例如,当设备异常导致缓存的上述至少部分节点发生数据丢失,用户可以通过异常恢复指令来对上述存储介质中缓存的数据进行恢复。而区块链中的节点设备可以接收用户针对上述存储介质中缓存的上述至少部分节点的异常恢复指令,然后可以响应于上述异常恢复指令,基于数据库中存储的WAL日志,对该存储介质中缓存的上述至少部分节点进行数据恢复。For example, when a device abnormality causes data loss in at least some of the cached nodes, the user can restore the cached data in the storage medium through exception recovery instructions. The node device in the blockchain can receive the user's abnormal recovery instruction for at least some of the nodes cached in the above storage medium, and then can respond to the above abnormal recovery instruction based on the WAL log stored in the database, cache the storage medium Perform data recovery on at least some of the above nodes.
在示出的一种实施方式中,对于在上述存储介质中缓存的上述至少部分节点,可以为其设置一个持久存储条件,该持久化存储条件具体是指上述存储介质中缓存的上述至少部分节点写入数据库进行持久化存储的条件。In an embodiment shown, a persistent storage condition may be set for at least some of the nodes cached in the storage medium. The persistent storage condition specifically refers to at least some of the nodes cached in the storage medium. Write conditions to the database for persistent storage.
在这种情况下,可以确定存储介质中缓存的上述至少部分节点是否满足了该持久化存储条件;例如,在实际应用中,可以基于预设的周期,周期性来确定存储介质中缓存的上述至少部分节点是否满足了该持久化存储条件。如果存储介质中缓存的上述至少部分节点满足了该持久化存储条件,则可以进一步将该存储介质中缓存的上述至少部分节点写入数据库中进行持久化存储。In this case, it can be determined whether at least some of the above-mentioned nodes cached in the storage medium satisfy the persistent storage condition; for example, in practical applications, the above-mentioned nodes cached in the storage medium can be determined periodically based on a preset period. Whether at least some nodes meet the persistent storage conditions. If at least some of the nodes cached in the storage medium satisfy the persistent storage condition, at least some of the nodes cached in the storage medium can be further written into the database for persistent storage.
其中,上述持久化存储的条件,在本说明书中不进行特别的限定,在实际应用中可以基于实际的存储需求进行灵活的设置。Among them, the above-mentioned persistent storage conditions are not particularly limited in this specification, and can be flexibly set based on actual storage requirements in actual applications.
例如,在示出的一种实施方式,上述持久化存储的条件具体可以包括以下示出的条件中的一个或者多个的组合:For example, in one embodiment shown, the above-mentioned persistent storage conditions may specifically include one or a combination of more of the following conditions:
条件1:上述数据库中持久化存储的上述数据记录的条数达到阈值;Condition 1: The number of the above data records persistently stored in the above database reaches the threshold;
在这种情况下,可以在上述数据库中持久化存储的上述数据记录的条数达到阈值时,触发将缓存的上述至少部分节点回写到磁盘中的数据库进行持久化存储。In this case, when the number of the data records persistently stored in the database reaches a threshold, writing back at least some of the cached nodes to the database in the disk can be triggered for persistent storage.
条件2,上述数据库中持久化存储的数据记录的存储容量达到阈值;Condition 2: The storage capacity of persistently stored data records in the above database reaches the threshold;
在这种情况下,可以在上述数据库中持久化存储的上述数据记录的存储容量达到阈值时,触发将缓存的上述至少部分节点回写到磁盘中的数据库进行持久化存储。In this case, when the storage capacity of the data records persistently stored in the database reaches a threshold, writing back at least part of the cached nodes to the database in the disk can be triggered for persistent storage.
条件3:接收到用户针对上述存储介质中存储的上述至少部分节点的持久化存储指令。Condition 3: Receive the user's persistent storage instruction for at least some of the nodes stored in the storage medium.
在这种情况下,可以在接收到用户输入的针对上述存储介质中存储的上述至少部分节点的持久化存储指令时,触发将缓存的上述至少部分节点回写到磁盘中的数据库进行持久化存储。In this case, upon receiving a persistent storage instruction input by the user for at least part of the nodes stored in the storage medium, it may be triggered to write back the cached at least part of the nodes to the database in the disk for persistent storage. .
在示出的一种实施方式中,在将上述存储介质中缓存的上述至少部分节点成功写入数据库中进行持久化存储之后,为了提高上述存储介质的存储空间利用率,还可以进一步将上述存储介质中缓存的上述至少部分节点删除。除此之外,在将上述存储介质中缓存的上述至少部分节点成功写入所述数据库中进行持久化存储之后,基于相同的考虑,为了提高上述数据库的存储空间利用率,还可以进一步将该数据库中持久化存储的与上述至少部分节点对应的数据记录删除。In an embodiment shown, after at least some of the nodes cached in the storage medium are successfully written into the database for persistent storage, in order to improve the storage space utilization of the storage medium, the storage space may be further stored in the database for persistent storage. At least some of the above nodes cached in the media are deleted. In addition, after at least some of the nodes cached in the storage medium are successfully written into the database for persistent storage, based on the same consideration, in order to improve the storage space utilization of the database, the above-mentioned nodes can be further written into the database for persistent storage. Delete the data records persistently stored in the database corresponding to at least some of the above nodes.
在本说明书,由于在对上述逻辑的树状结构上的根节点、中间节点和叶子节点进行存储时,采用了与相关技术中完全不同的存储策略;例如,如前所述,在本说明书中,与相关技术不同的是,上述 逻辑的树状结构上的根节点、中间节点中的至少部分节点,默认将不再持久化存储在数据库中,而是会缓存在上述存储介质中;因此,在读取上述逻辑的树状结构上的根节点、中间节点和叶子节点时,具体可以采用分别从上述存储介质以及上述数据库中分别读取的读取方式。In this specification, when storing the root node, intermediate node and leaf node on the above logical tree structure, a completely different storage strategy is adopted from that in related technologies; for example, as mentioned above, in this specification , different from related technologies, at least some of the root nodes and intermediate nodes on the above-mentioned logical tree structure will no longer be persistently stored in the database by default, but will be cached in the above-mentioned storage medium; therefore, When reading the root node, the intermediate node and the leaf node on the above logical tree structure, specifically, a reading method of reading from the above storage medium and the above database respectively may be adopted.
例如,在示出的一种实施方式中,当区块链中的节点设备接收到针对上述逻辑的树状结构上的节点的读取指令,可以默认先从上述存储介质中读取该节点,并在从该存储介质中未读取到该节点时,再进一步从数据库中读取该节点。For example, in an embodiment shown, when a node device in the blockchain receives a read instruction for a node on the above-mentioned logical tree structure, the node can be read from the above-mentioned storage medium by default, And when the node is not read from the storage medium, the node is further read from the database.
在以上技术方案中,通过将上述逻辑的树形结构上的根节点、中间节点中的至少部分节点缓存在支持覆盖式写入数据的存储介质,并在该存储介质中针对该至少部分节点进行修改更新,可以缓解在将上述逻辑的树形结构上的根节点、中间节点中的至少部分节点反复写入数据库的过程中而造成的写放大效应,进而可以提升上述数据库的存储性能。In the above technical solution, at least part of the root nodes and intermediate nodes on the above-mentioned logical tree structure are cached in a storage medium that supports overwriting data, and the at least part of the nodes are processed in the storage medium. Modification and update can alleviate the write amplification effect caused by repeatedly writing at least some of the root nodes and intermediate nodes in the above-mentioned logical tree structure to the database, thereby improving the storage performance of the above-mentioned database.
例如,以上述逻辑的树形结构为FDMT树为例,由于传统的MPT树中只有branch node会包含16个用于区块链数据的key中的字符的slot,故在将MPT树中的节点在数据库中进行持久化存储时,虽然也会造成写放大效应,但造成的写放大效应通常并不明显。For example, taking the above logical tree structure as an FDMT tree, since only the branch node in the traditional MPT tree will contain 16 slots for characters in the key of blockchain data, the nodes in the MPT tree When persisting storage in the database, although the write amplification effect will also be caused, the write amplification effect is usually not obvious.
而FDMT树与传统的MPT树相比则明显不同。The FDMT tree is significantly different from the traditional MPT tree.
由于FDMT树的前N层的Tree node会包括多个用于存放区块链数据的key中的字符的block,每一个位置又会包括多个用于存放区块链数据的key中的字符的slot;因此,基于这种特殊的数据结构,FDMT树上的Tree node,相较于MPT树的branch node,通常会存在更多的slot。比如,以一个Tree node包含16个block,每个block包含16个slot为例,一个Tree node会包含256个slot,slot数量远大于MPT树上的branch node。Since the Tree nodes of the first N layers of the FDMT tree will include multiple blocks used to store the characters in the key of the blockchain data, each position will include multiple blocks used to store the characters in the key of the blockchain data. slot; therefore, based on this special data structure, the Tree node on the FDMT tree usually has more slots than the branch node of the MPT tree. For example, if a Tree node contains 16 blocks, and each block contains 16 slots, a Tree node will contain 256 slots, and the number of slots is much larger than the branch nodes on the MPT tree.
而具有更多的slot,也就意味着FDMT树上的Tree node相较于MPT树上的branch node,将具有更大的数据容量。在这种情况下,在将FDMT树上的Tree node在写入数据库进行持久化存储时,其造成的写放大效应相较于MPT树也就会更加的明显。比如,如果FDMT树上的某一个Tree node包含的256个slot中任意一个slot中存放的数据发生更新,在将包含256个slot的该Tree node重新写入数据库中进行持久化存储时,其消耗的写入带宽,显然是远远大于将MPT树上的一个仅包含16个slot的branch node整个写入数据库时所消耗的写入带宽。Having more slots means that the Tree node on the FDMT tree will have larger data capacity than the branch node on the MPT tree. In this case, when the Tree node on the FDMT tree is written to the database for persistent storage, the write amplification effect caused by it will be more obvious compared to the MPT tree. For example, if the data stored in any of the 256 slots contained in a certain Tree node on the FDMT tree is updated, when the Tree node containing 256 slots is rewritten into the database for persistent storage, its consumption The write bandwidth is obviously much greater than the write bandwidth consumed when writing the entire branch node containing only 16 slots in the MPT tree to the database.
而如果采用以上实施例描述的存储方案,将FDMT树的前N层的Tree node缓存到内存中,并在内存中进行修改更新,就可以避免将FDMT树的前N层的Tree node反复的写入磁盘中的数据库,从而可以缓解由此造成的写放大效应。If the storage solution described in the above embodiment is used, the Tree nodes of the first N layers of the FDMT tree are cached in the memory, and modified and updated in the memory, it can avoid repeatedly writing the Tree nodes of the first N layers of the FDMT tree. into the database on disk, thus mitigating the resulting write amplification effect.
与上述方法实施例相对应,本申请还提供了装置的实施例。Corresponding to the above method embodiments, this application also provides device embodiments.
与上述方法实施例相对应,本说明书还提供了一种区块链数据存储装置的实施例。Corresponding to the above method embodiments, this specification also provides an embodiment of a blockchain data storage device.
本说明书的区块链数据存储装置的实施例可以应用在电子设备上。装置实施例可以通过软件实现,也可以通过硬件或者软硬件结合的方式实现。以软件实现为例,作为一个逻辑意义上的装置,是通过其所在电子设备的处理器将非易失性存储器中对应的计算机程序指令读取到内存中运行形成的。The embodiments of the blockchain data storage device in this specification can be applied to electronic devices. The device embodiments may be implemented by software, or may be implemented by hardware or a combination of software and hardware. Taking software implementation as an example, as a logical device, it is formed by reading the corresponding computer program instructions in the non-volatile memory into the memory and running them through the processor of the electronic device where it is located.
从硬件层面而言,如图8所示,为本说明书的区块链数据存储装置所在电子设备的一种硬件结构图,除了图8所示的处理器、内存、网络接口、以及非易失性存储器之外,实施例中装置所在的电子设备通常根据该电子设备的实际功能,还可以包括其他硬件,对此不再赘述。From the hardware level, as shown in Figure 8, it is a hardware structure diagram of the electronic device where the blockchain data storage device of this specification is located. In addition to the processor, memory, network interface, and non-volatile components shown in Figure 8 In addition to the permanent memory, the electronic device where the device in the embodiment is located may also include other hardware based on the actual functions of the electronic device, which will not be described again.
图9是本说明书一示例性实施例示出的一种区块链数据存储装置的框图。Figure 9 is a block diagram of a blockchain data storage device according to an exemplary embodiment of this specification.
请参考图9,所述区块链数据存储装置90可以应用在前述图8所示的电子设备中,其中所述区块链数据的key-value键值对以逻辑的树形结构上的根节点、中间节点和叶子节点的形式,存储在数据库中;所述根节点、中间节点用于存放所述区块链数据的key中的字符;所述叶子节点用于存放所述区块链数据的value;所述树形结构上的任一节点通过其hash值与上一层的节点链接;所述装置90包括:Please refer to Figure 9. The blockchain data storage device 90 can be applied in the electronic device shown in Figure 8, wherein the key-value pairs of the blockchain data are based on the root of the logical tree structure. The form of nodes, intermediate nodes and leaf nodes are stored in the database; the root nodes and intermediate nodes are used to store the characters in the key of the blockchain data; the leaf nodes are used to store the blockchain data value; any node on the tree structure is linked to the node on the previous level through its hash value; the device 90 includes:
获取模块901,获取待存储的区块链数据的key-value键值对;The acquisition module 901 obtains the key-value pair of the blockchain data to be stored;
转换模块902,将所述区块链数据的key-value键值对转换成逻辑的树形结构上的根节点、中间节点和叶子节点;The conversion module 902 converts the key-value pairs of the blockchain data into root nodes, intermediate nodes and leaf nodes in a logical tree structure;
存储模块903,将所述根节点、中间节点中的至少部分节点缓存至支持覆盖式写入数据的存储介质,以在所述存储介质中针对所述至少部分节点进行修改更新;以及,生成用于记录针对所述至少部分节点的修改更新详情的数据记录,并将所述数据记录和所述根节点、中间节点和叶子节点中除所述至少部分节点以外的其他节点写入所述数据库进行持久化存储。The storage module 903 caches at least part of the root node and intermediate nodes to a storage medium that supports overwriting data, so as to modify and update the at least part of the nodes in the storage medium; and, generate Data records that record modification and update details for at least part of the nodes, and write the data records and other nodes among the root node, intermediate node and leaf node except the at least part of the nodes into the database. Persistent storage.
上述实施例阐明的系统、装置、模块或单元,具体可以由计算机芯片或实体实现,或者由具有某种功能的产品来实现。一种典型的实现设备为计算机,计算机的具体形式可以是个人计算机、膝上型计算机、蜂窝电话、相机电话、智能电话、个人数字助理、媒体播放器、导航设备、电子邮件收发设备、游戏控制台、平板计算机、可穿戴设备或者这些设备中的任意几种设备的组合。The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer, which may be in the form of a personal computer, a laptop, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email transceiver, or a game controller. desktop, tablet, wearable device, or a combination of any of these devices.
在一个典型的配置中,计算机包括一个或多个处理器(CPU)、输入/输出接口、网络接口和内存。In a typical configuration, a computer includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
内存可能包括计算机可读介质中的非永久性存储器,随机存取存储器(RAM)和/或非易失性内存等形式,如只读存储器(ROM)或闪存(flash RAM)。内存是计算机可读介质的示例。Memory may include non-permanent storage in computer-readable media, random access memory (RAM) and/or non-volatile memory in the form of read-only memory (ROM) or flash memory (flash RAM). Memory is an example of computer-readable media.
计算机可读介质包括永久性和非永久性、可移动和非可移动媒体可以由任何方法或技术来实现信息存储。信息可以是计算机可读指令、数据结构、程序的模块或其他数据。计算机的存储介质的例子包括,但不限于相变内存(PRAM)、静态随机存取存储器(SRAM)、动态随机存取存储器(DRAM)、其他类型的随机存取存储器(RAM)、只读存储器(ROM)、电可擦除可编程只读存储器(EEPROM)、快闪记忆体或其他内存技术、只读光盘只读存储器(CD-ROM)、数字多功能光盘(DVD)或其他光学存储、磁盒式磁带、磁盘存储、量子存储器、基于石墨烯的存储介质或其他磁性存储设备或任何其他非传输介质,可用于存储可以被计算设备访问的信息。按照本文中的界定,计算机可读介质不包括暂存电脑可读媒体(transitory media),如调制的数据信号和载波。Computer-readable media includes both persistent and non-volatile, removable and non-removable media that can be implemented by any method or technology for storage of information. Information may be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), and read-only memory. (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, Magnetic tape cartridges, magnetic disk storage, quantum memory, graphene-based storage media or other magnetic storage devices, or any other non-transmission medium, can be used to store information that can be accessed by computing devices. As defined in this article, computer-readable media does not include transitory media, such as modulated data signals and carrier waves.
还需要说明的是,术语“包括”、“包含”或者其任何其他变体意在涵盖非排他性的包含,从而使得包括一系列要素的过程、方法、商品或者设备不仅包括那些要素,而且还包括没有明确列出的其他要素,或者是还包括为这种过程、方法、商品或者设备所固有的要素。在没有更多限制的情况下,由语句“包括一个……”限定的要素,并不排除在包括所述要素的过程、方法、商品或者设备中还存在另外的相同要素。It should also be noted that the terms "comprises," "comprises," or any other variation thereof are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that includes a list of elements not only includes those elements, but also includes Other elements are not expressly listed or are inherent to the process, method, article or equipment. Without further limitation, an element defined by the statement "comprises a..." does not exclude the presence of additional identical elements in a process, method, article, or device that includes the stated element.
上述对本说明书特定实施例进行了描述。其它实施例在所附权利要求书的范围内。在一些情况下,在权利要求书中记载的动作或步骤可以按照不同于实施例中的顺序来执行并且仍然可以实现期望的结果。另外,在附图中描绘的过程不一定要求示出的特定顺序或者连续顺序才能实现期望的结果。在某些实施方式中,多任务处理和并行处理也是可以的或者可能是有利的。The foregoing describes specific embodiments of this specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in a different order than in the embodiments and still achieve desired results. Additionally, the processes depicted in the figures do not necessarily require the specific order shown, or sequential order, to achieve desirable results. Multitasking and parallel processing are also possible or may be advantageous in certain implementations.
在本说明书一个或多个实施例使用的术语是仅仅出于描述特定实施例的目的,而非旨在限制本说明书一个或多个实施例。在本说明书一个或多个实施例和所附权利要求书中所使用的单数形式的“一种”、“所述”和“该”也旨在包括多数形式,除非上下文清楚地表示其他含义。还应当理解,本文中使用的术语“和/或”是指并包含一个或多个相关联的列出项目的任何或所有可能组合。The terminology used in one or more embodiments of this specification is for the purpose of describing particular embodiments only and is not intended to limit the one or more embodiments of this specification. As used in one or more embodiments of this specification and the appended claims, the singular forms "a," "the" and "the" are intended to include the plural forms as well, unless the context clearly dictates otherwise. It will also be understood that the term "and/or" as used herein refers to and includes any and all possible combinations of one or more of the associated listed items.
应当理解,尽管在本说明书一个或多个实施例可能采用术语第一、第二、第三等来描述各种信息,但这些信息不应限于这些术语。这些术语仅用来将同一类型的信息彼此区分开。例如,在不脱离本说明书一个或多个实施例范围的情况下,第一信息也可以被称为第二信息,类似地,第二信息也可以被称为第一信息。取决于语境,如在此所使用的词语“如果”可以被解释成为“在……时”或“当……时”或“响应于确定”。It should be understood that although one or more embodiments of this specification may use the terms first, second, third, etc. to describe various information, the information should not be limited to these terms. These terms are only used to distinguish information of the same type from each other. For example, without departing from the scope of one or more embodiments of this specification, the first information may also be called second information, and similarly, the second information may also be called first information. Depending on the context, the word "if" as used herein may be interpreted as "when" or "when" or "in response to determining."
以上所述仅为本说明书一个或多个实施例的较佳实施例而已,并不用以限制本说明书一个或多个实施例,凡在本说明书一个或多个实施例的精神和原则之内,所做的任何修改、等同替换、改进等,均应包含在本说明书一个或多个实施例保护的范围之内。The above are only preferred embodiments of one or more embodiments of this specification, and are not intended to limit one or more embodiments of this specification. Within the spirit and principles of one or more embodiments of this specification, Any modifications, equivalent substitutions, improvements, etc. shall be included in the scope of protection of one or more embodiments of this specification.

Claims (17)

  1. 一种区块链数据存储方法,所述区块链数据的key-value键值对以逻辑的树形结构上的根节点、中间节点和叶子节点的形式,存储在数据库中;所述根节点、中间节点用于存放所述区块链数据的key中的字符;所述叶子节点用于存放所述区块链数据的value;所述树形结构上的任一节点通过其hash值与上一层的节点链接;所述方法包括:A method of storing blockchain data. The key-value pairs of the blockchain data are stored in a database in the form of root nodes, intermediate nodes and leaf nodes on a logical tree structure; the root node , the intermediate node is used to store the characters in the key of the blockchain data; the leaf node is used to store the value of the blockchain data; any node on the tree structure passes its hash value and the above One layer of node links; the method includes:
    获取待存储的区块链数据的key-value键值对;Obtain the key-value pair of the blockchain data to be stored;
    将所述区块链数据的key-value键值对转换成逻辑的树形结构上的根节点、中间节点和叶子节点;Convert the key-value pairs of the blockchain data into root nodes, intermediate nodes and leaf nodes in a logical tree structure;
    将所述根节点、中间节点中的至少部分节点缓存至支持覆盖式写入数据的存储介质,以在所述存储介质中针对所述至少部分节点进行修改更新;以及,生成用于记录针对所述至少部分节点的修改更新详情的数据记录,并将所述数据记录和所述根节点、中间节点和叶子节点中除所述至少部分节点以外的其他节点写入所述数据库进行持久化存储。Cache at least some of the root nodes and intermediate nodes to a storage medium that supports overwriting data, so as to modify and update at least some of the nodes in the storage medium; and, generate a record for all the nodes. A data record of the modification update details of at least part of the nodes is recorded, and the data record and other nodes among the root node, intermediate node and leaf node except the at least part of the nodes are written into the database for persistent storage.
  2. 根据权利要求1所述的方法,所述至少部分节点包括多个用于存放所述区块链数据的key中的字符的槽位;所述槽位用于存储与该节点链接的下一层节点的hash值;所述数据记录用于记录针对所述至少部分节点中的各个槽位的修改更新详情。According to the method of claim 1, the at least some nodes include a plurality of slots for storing characters in the key of the blockchain data; the slots are used to store the next layer linked to the node. The hash value of the node; the data record is used to record modification and update details for each slot in at least some of the nodes.
  3. 根据权利要求2所述的方法,所述逻辑的树状结构包括融合了字典树的树形结构的Merkle树。According to the method of claim 2, the logical tree structure includes a Merkle tree fused with the tree structure of a dictionary tree.
  4. 根据权利要求3所述的方法,所述区块链数据包括与所述区块链上的区块链账户对应的账户状态数据;According to the method of claim 3, the blockchain data includes account status data corresponding to the blockchain account on the blockchain;
    基于所述区块链中的各个区块链账户对应的账户状态数据的key-value键值对生成的Merkle树包括:基于所述各个区块链账户的最新账户状态数据生成的当前Merkle状态树;以及,基于所述各个区块链账户的历史账户状态数据组织成的历史Merkle状态树;所述逻辑的树形结构为所述当前Merkle状态树。The Merkle tree generated based on the key-value pairs of the account status data corresponding to each blockchain account in the blockchain includes: the current Merkle status tree generated based on the latest account status data of each blockchain account ; And, a historical Merkle state tree organized based on the historical account status data of each blockchain account; the logical tree structure is the current Merkle state tree.
  5. 根据权利要求3所述的方法,所述逻辑的树状结构包括MPT树;The method according to claim 3, the logical tree structure includes an MPT tree;
    所述根节点为所述MPT树上的扩展节点extension node;所述中间节点为所述MPT树上的所述扩展节点extension node或者分支节点branch node;所述至少部分节点为所述MPT树上的branch node。The root node is an extension node on the MPT tree; the intermediate node is the extension node or a branch node on the MPT tree; and at least some of the nodes are on the MPT tree. branch node.
  6. 根据权利要求3所述的方法,所述逻辑的树状结构包括FDMT树;The method according to claim 3, the logical tree structure includes an FDMT tree;
    其中,所述FDMT树上的根节点、中间节点均包括多个用于存放所述区块链数据的key中的字符的位置;每个位置中进一步包括多个用于存放所述区块链数据的key中的字符的槽位;所述槽位用于存放链接至该节点的下一层的节点的hash;所述至少部分节点为所述FDMT树上的根节点和中间节点。Among them, the root node and the intermediate node on the FDMT tree include multiple locations for storing characters in the key of the blockchain data; each location further includes multiple locations for storing the characters in the blockchain data key. The slot of the character in the key of the data; the slot is used to store the hash of the node at the next level linked to the node; at least some of the nodes are the root node and intermediate nodes on the FDMT tree.
  7. 根据权利要求1所述的方法,所述方法还包括:The method of claim 1, further comprising:
    确定所述存储介质中缓存的所述至少部分节点是否满足持久化存储条件;Determine whether at least some of the nodes cached in the storage medium meet persistent storage conditions;
    如果所述存储介质中缓存的所述至少部分节点满足持久化存储条件,将所述存储介质中缓存的所述至少部分节点写入所述数据库中进行持久化存储。If at least some of the nodes cached in the storage medium meet persistent storage conditions, the at least some of the nodes cached in the storage medium are written into the database for persistent storage.
  8. 根据权利要求7所述的方法,所述方法还包括:The method of claim 7, further comprising:
    在将所述存储介质中缓存的所述至少部分节点成功写入所述数据库中进行持久化存储之后,进一步将所述存储介质中缓存的所述至少部分节点删除。After the at least part of the nodes cached in the storage medium is successfully written into the database for persistent storage, the at least part of the nodes cached in the storage medium is further deleted.
  9. 根据权利要求7所述的方法,所述方法还包括:The method of claim 7, further comprising:
    在将所述存储介质中缓存的所述至少部分节点成功写入所述数据库中进行持久化存储之后,进一步将所述数据库中持久化存储的与所述至少部分节点对应的所述数据记录删除。After the at least part of the nodes cached in the storage medium is successfully written into the database for persistent storage, the data records corresponding to the at least part of the nodes that are persistently stored in the database are further deleted. .
  10. 根据权利要求1所述的方法,所述持久化存储条件,包括以下示出的任一或者多个的组合:The method according to claim 1, the persistent storage conditions include any one or a combination of more of the following:
    所述数据库中持久化存储的数据记录的条数达到阈值;The number of persistently stored data records in the database reaches a threshold;
    所述数据库中持久化存储的数据记录的存储容量达到阈值;The storage capacity of persistently stored data records in the database reaches a threshold;
    接收到了针对所述存储介质中存储的所述至少部分节点的持久化存储指令。Persistent storage instructions for the at least part of the nodes stored in the storage medium are received.
  11. 根据权利要求1所述的方法,所述方法还包括:The method of claim 1, further comprising:
    响应于针对所述逻辑的树状结构上的节点的读取指令,从所述存储介质中读取所述节点,并在从所述存储介质中未读取到所述节点时,进一步从所述数据库中读取所述节点。In response to a read instruction for a node on the logical tree structure, read the node from the storage medium, and when the node is not read from the storage medium, further read the node from the storage medium. Read the node from the database.
  12. 根据权利要求1所述的方法,所述数据记录包括WAL日志。The method of claim 1, the data records include WAL logs.
  13. 根据权利要求12所述的方法,所述方法还包括:The method of claim 12, further comprising:
    接收针对所述存储介质中缓存的所述至少部分节点的异常恢复指令;Receive exception recovery instructions for at least some of the nodes cached in the storage medium;
    响应于所述异常恢复指令,基于所述数据库中存储的所述WAL日志,对所述存储介质中缓存的所述至少部分节点进行数据恢复。In response to the exception recovery instruction, data recovery is performed on at least some of the nodes cached in the storage medium based on the WAL log stored in the database.
  14. 根据权利要求1所述的方法,所述存储介质包括内存。The method of claim 1, the storage medium includes memory.
  15. 一种区块链数据存储装置,所述区块链数据的key-value键值对以逻辑的树形结构上的根节点、中间节点和叶子节点的形式,存储在数据库中;所述根节点、中间节点用于存放所述区块链数据的key中的字符;所述叶子节点用于存放所述区块链数据的value;所述树形结构上的任一节点通过其hash值与上一层的节点链接;所述装置包括:A blockchain data storage device, the key-value pairs of the blockchain data are stored in the database in the form of root nodes, intermediate nodes and leaf nodes on a logical tree structure; the root node , the intermediate node is used to store the characters in the key of the blockchain data; the leaf node is used to store the value of the blockchain data; any node on the tree structure passes its hash value and the above One layer of node links; the device includes:
    获取模块,获取待存储的区块链数据的key-value键值对;Get the module to get the key-value pairs of the blockchain data to be stored;
    转换模块,将所述区块链数据的key-value键值对转换成逻辑的树形结构上的根节点、中间节点和叶子节点;A conversion module that converts key-value pairs of the blockchain data into root nodes, intermediate nodes and leaf nodes in a logical tree structure;
    存储模块,将所述根节点、中间节点中的至少部分节点缓存至支持覆盖式写入数据的存储介质,以在所述存储介质中针对所述至少部分节点进行修改更新;以及,生成用于记录针对所述至少部分节点的修改更新详情的数据记录,并将所述数据记录和所述根节点、中间节点和叶子节点中除所述至少部分节点以外的其他节点写入所述数据库进行持久化存储。A storage module that caches at least part of the root node and intermediate nodes to a storage medium that supports overwriting data, so as to modify and update the at least part of the nodes in the storage medium; and, generate Record data records of modification update details for at least part of the nodes, and write the data records and other nodes among the root node, intermediate node and leaf node except the at least part of the nodes into the database for persistence storage.
  16. 一种电子设备,包括:An electronic device including:
    处理器;processor;
    用于存储处理器可执行指令的存储器;Memory used to store instructions executable by the processor;
    其中,所述处理器通过运行所述可执行指令以实现如权利要求1-14中任一项所述的方法的步骤。Wherein, the processor executes the steps of the method according to any one of claims 1-14 by running the executable instructions.
  17. 一种计算机可读存储介质,其上存储有计算机指令,该指令被处理器执行时实现如权利要求1-14中任一项所述方法的步骤。A computer-readable storage medium having computer instructions stored thereon, which when executed by a processor, implements the steps of the method according to any one of claims 1-14.
PCT/CN2022/135539 2022-07-29 2022-11-30 Blockchain data storage method and apparatus, and electronic device WO2024021419A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202210908663.8A CN115221176A (en) 2022-07-29 2022-07-29 Block chain data storage method and device and electronic equipment
CN202210908663.8 2022-07-29

Publications (1)

Publication Number Publication Date
WO2024021419A1 true WO2024021419A1 (en) 2024-02-01

Family

ID=83614655

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2022/135539 WO2024021419A1 (en) 2022-07-29 2022-11-30 Blockchain data storage method and apparatus, and electronic device

Country Status (2)

Country Link
CN (1) CN115221176A (en)
WO (1) WO2024021419A1 (en)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115221176A (en) * 2022-07-29 2022-10-21 蚂蚁区块链科技(上海)有限公司 Block chain data storage method and device and electronic equipment

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110175188A (en) * 2019-05-31 2019-08-27 杭州复杂美科技有限公司 A kind of block chain state data buffer storage and querying method, equipment and storage medium
CN110347660A (en) * 2019-06-28 2019-10-18 阿里巴巴集团控股有限公司 Based on the classification storage method and device of block chain, electronic equipment
US20210243009A1 (en) * 2020-01-31 2021-08-05 International Business Machines Corporation Correlation-based hash tree verification
CN114706848A (en) * 2022-02-25 2022-07-05 蚂蚁区块链科技(上海)有限公司 Block chain data storage, updating and reading method and device and electronic equipment
CN115221176A (en) * 2022-07-29 2022-10-21 蚂蚁区块链科技(上海)有限公司 Block chain data storage method and device and electronic equipment

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110175188A (en) * 2019-05-31 2019-08-27 杭州复杂美科技有限公司 A kind of block chain state data buffer storage and querying method, equipment and storage medium
CN110347660A (en) * 2019-06-28 2019-10-18 阿里巴巴集团控股有限公司 Based on the classification storage method and device of block chain, electronic equipment
US20210243009A1 (en) * 2020-01-31 2021-08-05 International Business Machines Corporation Correlation-based hash tree verification
CN114706848A (en) * 2022-02-25 2022-07-05 蚂蚁区块链科技(上海)有限公司 Block chain data storage, updating and reading method and device and electronic equipment
CN115221176A (en) * 2022-07-29 2022-10-21 蚂蚁区块链科技(上海)有限公司 Block chain data storage method and device and electronic equipment

Also Published As

Publication number Publication date
CN115221176A (en) 2022-10-21

Similar Documents

Publication Publication Date Title
TWI731595B (en) Block chain state data storage method and device, and electronic equipment
WO2021017436A1 (en) Blockchain state data synchronization method and apparatus, and electronic device
TWI732463B (en) Block chain state data recovery method and device, and electronic equipment
US11113272B2 (en) Method and apparatus for storing blockchain state data and electronic device
TWI737152B (en) Block chain-based hierarchical storage method and device, and electronic equipment
TWI737157B (en) Block chain-based hierarchical storage method and device, and electronic equipment
WO2020238254A1 (en) Data storage method and node
CN102057366B (en) Distributed cache arrangement
WO2020258856A1 (en) Hierarchical storage method and apparatus based on blockchain, and electronic device
WO2022237596A1 (en) Traversal method and apparatus for smart contract storage content, and electronic device
US10761948B1 (en) Method, apparatus, and electronic device for restoring state data of blockchain
CN112988761B (en) Block chain data storage method and device and electronic equipment
US20170286477A1 (en) Database system recovery using non-volatile system memory
CN102821138A (en) Metadata distributed storage method applicable to cloud storage system
CN107784121A (en) Lowercase optimization method of log file system based on nonvolatile memory
CN112988912B (en) Block chain data storage method and device and electronic equipment
WO2024021419A1 (en) Blockchain data storage method and apparatus, and electronic device
US20210263905A1 (en) Blockchain based hierarchical data storage
CN109388585A (en) For providing the technology of cache coherence based on cache types
CN114706848A (en) Block chain data storage, updating and reading method and device and electronic equipment
WO2016188280A1 (en) Write-in method and device for database sub-tables
WO2022233274A1 (en) Block chain data storage method and apparatus, and electronic device
CN114153848A (en) Block chain data storage method and device and electronic equipment
CN112905607B (en) Block chain data storage method and device and electronic equipment
CN116737609A (en) Method and device for selecting replacement cache line

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 22952853

Country of ref document: EP

Kind code of ref document: A1