Management method of state data in block chain system and block chain node

By using multiple tree structures to organize smart contract states in the blockchain system, and using leaf nodes to store location information and tree identifiers to generate index entries, the problems of high storage costs and low access efficiency in the prior art are solved, and efficient state variable access and storage resource savings are achieved.

CN120296014APending Publication Date: 2025-07-11ANT BLOCKCHAIN TECHNOLOGY (SHANGHAI) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510349226.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-21
Publication Date
2025-07-11

AI Technical Summary

Technical Problem

In existing blockchain systems, the storage cost of the tree structure is relatively high and the data volume access efficiency is low. Especially when managing the contract status of the smart contract, it is difficult to balance the storage cost and access efficiency.

Method used

The contract status of smart contracts is organized through multiple tree structures, and the leaf nodes store the position information of the state variables instead of directly storing the value of the state variables. The pre-allocated tree identifiers are used to generate index entries, realizing separate storage of data and indexes, and reducing the amount of data in index entries.

Benefits of technology

Improves the efficiency of accessing state variables in persistent storage media, saves storage resources, and reduces storage costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120296014A_ABST
    Figure CN120296014A_ABST
Patent Text Reader

Abstract

A state data management method in a block chain system and a block chain node, the block chain system organizes contract states of a plurality of smart contracts through a plurality of tree structures, the method comprises the steps of obtaining a key-value pair included in an execution write set of a target transaction, the key in the key-value pair comprises a contract address of the target smart contract and a key of a target state variable defined in the target smart contract; obtaining a target tree identifier allocated to the target smart contract in advance according to the contract address; the value in the key-value pair is stored in a persistent storage medium; according to the contract address, a target leaf node is generated / updated in a target tree structure corresponding to the target smart contract, a directed path from a root node of the target tree structure to the target leaf node comprises a key of a target state variable, and the target leaf node comprises position information of a value in a key-value pair in a persistent storage medium; and forming an index entry by using the target leaf node and the target tree identifier, and storing the index entry to a persistent storage medium.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of this specification belong to the field of computer technology, and particularly relate to a method for managing state data in a blockchain system and a blockchain node. Background Art

[0002] A blockchain system is a new application mode of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanism, and encryption algorithms. In a blockchain system, data blocks are combined into a chain data structure in a sequential connection manner according to the time sequence, and it is a distributed ledger that is guaranteed to be tamper-proof and unforgeable by cryptographic means. Due to the characteristics of decentralization, information immutability, autonomy, etc. of the blockchain system, the blockchain system has received more and more attention and applications.

[0003] A tree structure is usually adopted to organize the contract states of smart contracts in a blockchain system. The aforementioned tree structure may include, but is not limited to, MPT (Merkle Patricia Tree) and SMT (Sparse Merkle Tree), etc. A leaf node of the tree structure stores the value of a state variable, and the key of a state variable is stored in the directed path from the root node to a leaf node. Among them, the key-value pairs of the tree nodes in the tree structure can be stored in a persistent storage medium; for the key of a tree node, the contract address of the smart contract is usually used as the prefix of the key of the tree node in the persistent storage medium.

[0004] A Log-Structured Merge-Tree (LSM) is usually adopted to manage the key-value pairs of tree nodes in a persistent storage medium. Specifically, the key-value pairs of tree nodes are scattered and stored in Sorted String Table (SSTable) files located at multiple levels, resulting in a relatively large storage cost and a low data volume access efficiency. Summary of the Invention

[0005] The purpose of the present invention is to provide a method for managing state data in a blockchain system and a blockchain node.

[0006] In a first aspect, a method for managing state data in a blockchain system is provided. The blockchain system organizes the contract states of multiple smart contracts through multiple tree structures. The method includes: obtaining the key-value pairs included in the execution write set of a target transaction, where the key in the key-value pair includes the contract address of the target smart contract and the key of the target state variable defined by the target smart contract; obtaining, according to the contract address, a target tree identifier pre-allocated to the target smart contract; storing the value in the key-value pair into a persistent storage medium; generating / updating a target leaf node in the target tree structure corresponding to the target smart contract according to the contract address, where the directed path from the root node to the target leaf node of the target tree structure includes the key of the target state variable, and the target leaf node includes the position information of the value in the key-value pair in the persistent storage medium; forming an index entry by using the target leaf node and the target tree identifier, and storing the index entry into the persistent storage medium, where the index entry is used to access the value in the key-value pair in the persistent storage medium.

[0007] In a second aspect, a blockchain node in a blockchain system is provided. The blockchain system organizes the contract states of multiple smart contracts through multiple tree structures. The blockchain node includes: a key-value pair obtaining unit configured to obtain the key-value pairs included in the execution write set of a target transaction, where the key in the key-value pair includes the contract address of the target smart contract and the key of the target state variable defined by the target smart contract; a tree identifier obtaining unit configured to obtain, according to the contract address, a target tree identifier pre-allocated to the target smart contract; a storage processing unit configured to store the value in the key-value pair into a persistent storage medium; a node updating unit configured to generate / updating a target leaf node in the target tree structure corresponding to the target smart contract according to the contract address, where the directed path from the root node to the target leaf node of the target tree structure includes the key of the target state variable, and the target leaf node includes the position information of the value in the key-value pair in the persistent storage medium; the storage processing unit is further configured to form an index entry by using the target leaf node and the target tree identifier, and store the index entry into the persistent storage medium, where the index entry is used to access the value in the key-value pair in the persistent storage medium.

[0008] In a third aspect, a computing device is provided, including a memory and a processor. A computer program / instructions is stored in the memory. When the processor executes the computer program / instructions, the method described in the first aspect is implemented.

[0009] In a fourth aspect, a computer-readable storage medium is provided, on which a computer program / instructions is stored. When the computer program / instructions is executed in a computing device, the computing device executes the method described in the first aspect.

[0010] In the technical solution provided by the embodiments of this specification, for the tree structure used to organize the contract states of smart contracts, the position information of the value of the state variable in the persistent storage medium can be stored in the leaf nodes of the tree structure, rather than directly storing the value of the state variable. In addition, by using the leaf nodes and the tree identifier pre-assigned to the smart contract, an index entry that can be used to support accessing the value of the state variable in the persistent storage medium is formed, rather than directly using the contract address to generate the index entry. In this way, the value of the state variable is persistently stored in a data and index separation manner, which is beneficial to more efficiently accessing the value of the state variable in the persistent storage medium, and at the same time, the data volume of the index entry can be minimized as much as possible to save storage resources. Description of the Drawings

[0011] To more clearly illustrate the technical solutions of the embodiments of this specification, the drawings required for the description of the embodiments will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments recorded in this specification. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.

[0012] Figure 1 A schematic diagram of deploying a smart contract in a blockchain system provided exemplarily in the embodiments of this specification;

[0013] Figure 2 A schematic diagram of invoking a smart contract in a blockchain system provided exemplarily in the embodiments of this specification;

[0014] Figure 3 A flowchart of a method for managing state data in a blockchain system provided in the embodiments of this specification;

[0015] Figure 4 A schematic diagram of a tree structure for managing state data provided exemplarily in the embodiments of this specification;

[0016] Figure 5 A schematic diagram of the storage structure of state variables provided exemplarily in the embodiments of this specification;

[0017] Figure 6 This is a schematic structural diagram of a blockchain node in the blockchain system provided in the embodiments of this specification. Detailed implementation manners

[0018] In order to enable those skilled in the art of this technology to better understand the technical solutions in this specification, the technical solutions in the embodiments of this specification will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this specification, rather than all the embodiments. Based on the embodiments in this specification, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of this specification.

[0019] In a blockchain system that supports smart contracts, there are usually two types: contract accounts (CA) and externally owned accounts (EOA). Among them, the contract account corresponds to the smart contract, and is used to store the contract code of the relevant smart contract, as well as store the values of the state variables defined in the smart contract, and usually can only be called and activated through the external account; the external account refers to the account registered by the external user in the blockchain system.

[0020] The design of the external account and the contract account is actually a mapping from the account address to the account state. The account state of any account usually includes fields such as nonce, balance, storage Root, and codeHash. Among them, nonce and balance exist in both the external account and the contract account, and the codeHash and storage Root attributes are generally only valid on the contract account.

[0021] More specifically, for the external account, the value of nonce represents the number of transactions sent from the relevant account address; for the contract account, the value of nonce can represent the number of contracts of the smart contract created by the relevant account address, and the value is usually 1. The value of Balance represents the number of a certain digital resource / token owned by the relevant account address. The value of Storageroot is the hash value of the root node of a tree structure, such as an MPT, and the MPT is used to organize / manage the storage of the state variables of the relevant contract account. The value of CodeHash represents the hash value of the contract code of the relevant smart contract. For the external account, since it does not include the smart contract, the values of the storage Root and codeHash fields can generally be an empty string / all 0 strings.

[0022] The full name of MPT is Merkle Patricia Tree, which is a tree structure that combines Merkle Tree and Patricia Tree (a more space-saving trie). Among them, the Merkle tree algorithm can calculate a Hash value for multiple transactions respectively, and then connect them in pairs and calculate the Hash value again until the top-level Merkle root. In some blockchain systems, an improved MPT tree is usually adopted, such as a 16-ary tree structure, which is usually simply referred to as the MPT tree.

[0023] First, the basic process of deploying and invoking smart contracts in the blockchain system will be introduced exemplarily below.

[0024] Taking Ethereum as an example, users can create and invoke some complex logics in Ethereum, which is the biggest challenge that differentiates Ethereum from Bitcoin. The core of Ethereum as a programmable blockchain system is the Ethereum Virtual Machine (EVM), and each Ethereum node (i.e., blockchain node) can run the EVM. The EVM is a Turing-complete virtual machine, which means that various complex logics can be implemented through the EVM. The smart contracts published and invoked by users in Ethereum run on the EVM. In fact, what the virtual machine directly runs is the virtual machine code (virtual machine bytecode, hereinafter referred to as "bytecode" for short). The smart contracts deployed on the blockchain system can be in the form of bytecode.

[0025] Refer to Figure 1 As shown, Bob can create a transaction (Tx) for deploying a smart contract. The From field of this transaction can include Bob's account address, such as "0xf5e", the To field is empty (Null), and the Data field can include the contract code of the smart contract to be deployed. The contract code is, for example, bytecode that can be executed in the EVM. After this transaction and its corresponding digital signature, such as "0x68e12cf284…", are sent to Ethereum, if the digital signature of this transaction is verified in Ethereum and the Ethereum nodes in Ethereum reach an agreement on this transaction through the consensus mechanism, the EVM of an Ethereum node, such as node 1, can execute this transaction to complete the creation of this smart contract.

[0026] After completing the creation of the smart contract, a contract account corresponding to this smart contract can be generated in Ethereum and has a specific contract address. For example, "0x6f8ae93…" shown in Figure 1 represents the contract address of the created smart contract, and users can call this smart contract based on the contract address "0x6f8ae93…" of this smart contract.

[0027] The contract code and account storage of a smart contract are both stored in the contract account. The behavior of a smart contract is controlled by the contract code, and the account storage of the smart contract stores the contract state of the smart contract. More specifically, for a transaction used to deploy a smart contract, a virtual account / contract account containing the contract code and account storage (Storage) can be generated on the blockchain system.

[0028] As mentioned above, in the transaction used to deploy a smart contract, the data field includes the contract code of the smart contract, and the contract code can be bytecode that can be executed in the EVM. The bytecode consists of a series of bytes, and each byte can identify an operation. Considering various aspects such as development efficiency and readability, developers can choose to write a high-level language to write smart contract code instead of directly writing bytecode. The smart contract code written in a high-level language is compiled by a compiler to generate bytecode. There are relatively many high-level languages supported in Ethereum, such as Solidity, Serpent, and LLL languages.

[0029] Taking the Solidity language as an example, the smart contract code written in the Solidity language is similar to a class in an object-oriented programming language. Multiple members can be declared in a smart contract code, such as state variables, functions, function modifiers, events, etc. The contract state of the smart contract includes the values of multiple state variables and is stored in the account storage of the smart contract.

[0030] Please refer to the following contract code example 1 of a simple smart contract written in the Solidity language:

[0031]

[0032] Combined with the aforementioned code example 1, refer to Figure 2As shown, still taking Ethereum as an example, Bob can generate a transaction for calling the smart contract "SimpleStorage". The From field of this transaction includes Bob's account address. The To field of this transaction can include the contract address of "SimpleStorage", such as "0x6f8ae93…". The Data field of this transaction can include the call method of "SimpleStorage" (for example, the function identifier "set" of a certain method function in "SimpleStorage") and parameters (for example, the value "hello" of the state variable storedData with the data type of strings). In addition, this transaction can also include a value field, which is used to represent the value of Ether that may be transferred in this transaction. After this transaction and its corresponding digital signature, such as "0x93af05eb02…", are sent to Ethereum, if the digital signature of this transaction is verified in Ethereum and the Ethereum nodes in Ethereum reach an agreement on this transaction through the consensus mechanism, the Ethereum nodes can execute "SimpleStorage" according to this transaction. After the Ethereum nodes complete the execution of "SimpleStorage" according to this transaction, the value of the state variable storedData may change. For example, the value of the state variable storedData may be modified to "hello".

[0033] All execution records and states of smart contracts are stored on the blockchain system. Therefore, after the transaction for calling the smart contract is completed, the blockchain system stores the transaction vouchers that cannot be tampered with and will not be lost. For example, Alice can use a certain client to call a certain method function in the smart contract "SimpleStorage", such as calling "SimpleStorage.get()" in "SimpleStorage", to view the current value "hello" of storedData from a certain Ethereum node, such as node 6.

[0034] Next, the system data involved in the blockchain system and its corresponding data storage structure will be introduced exemplarily.

[0035] The system data that needs to be persistently stored in the blockchain system can be divided into two parts: block data and state data.

[0036] The block data includes one or more blocks in increasing order of block height (or block number). A single block may include a block header and a block body. The block header may include the block hash previous_Hash (or parent hash) of the previous block, timestamp Timestamp, block number BlockNum, state root hash State_Root, transaction root hash Transaction_Root, receipt root hash Receipt_Root, and nonce, etc. The block body may include a transaction sequence and a receipt sequence.

[0037] A transaction in the blockchain system refers to a task unit that is executed and recorded in the blockchain system. A single transaction usually includes a sending field (From), a receiving field (To), and a data field (Data). The From field includes the account that initiates the transaction (i.e., the sender account), and the To field may include another account involved / pointed to by the transaction.

[0038] For the block with block number N in the blockchain system, the transaction sequence included in the block with block number N can be executed based on the state data with block number (or version number) N - 1, that is, the state data corresponding to block N (i.e., the world state), to obtain the receipt sequence corresponding to the transaction sequence, and to obtain the state data with version number N.

[0039] The state data in the blockchain system can be organized through a tree structure. The value of a state variable is stored in a leaf node of the tree structure, and the key of the state variable is stored in the directed path from the root node to the leaf node of the tree structure. The state variable can be the account address of a contract account / external account, or can be a state variable in a smart contract. The aforementioned tree structure may include, for example, MPT (Merkle Patricia Tree) or SMT (Sparse Merkle Tree), etc. Specifically, the state data can be divided into the account state of the external account / contract account segment and the contract state of the smart contract.

[0040] In a blockchain system, account states can be organized through a state trie. The hash value of the root node of the state trie is stored in State_Root in the block header. The account state of an external account / contract account is stored in a leaf node of the state trie. In the directed path from the root node to a leaf node of the state trie, the account address of an external account / contract account, or part or all of the hash value calculated based on the account address, is stored. As mentioned above, the account state of a single external account / contract account usually includes fields such as Nonce, Balance, Storage root, and CodeHash. Nonce and Balance exist in both external accounts and contract accounts, while CodeHash and Storage root are generally only valid for contract accounts.

[0041] In a blockchain system, the contract states of multiple smart contracts can be organized through multiple storage tries. The hash value of the root node of the storage trie is stored in the storageroot field of the contract account corresponding to the relevant smart contract, thus locking the contract state of the smart contract to the relevant contract account through the hash value. Similarly, the value of a state variable defined in the smart contract is stored in a leaf node of the storage trie. In the directed path from the root node to a leaf node of the storage trie, the state key of a state variable defined in the relevant smart contract is stored, that is, the partial information in the directed path from the root node to the leaf node of the storage trie can be arranged in order to form the key of a state variable defined in all the relevant smart contracts. It should be noted that the key of the state variable is usually determined based on the declaration position of the state variable in the contract code.

[0042] The key-value pairs of tree nodes in the tree structure can be stored in a persistent storage medium; for the key of the tree node in the storage trie, the contract address of the smart contract is usually used as the prefix of the key. Usually, the key-value pairs of tree nodes in the persistent storage medium are managed by LSM. The key-value pairs of tree nodes are scattered and stored in Sorted String Table (SST) files at multiple levels, with a relatively high storage cost and low data access efficiency.

[0043] In the embodiments of this specification, a method for managing state data in a blockchain system, a blockchain node, a computing device, and a computer-readable storage medium are provided. In the blockchain system, the contract states of multiple smart contracts can be organized through multiple tree structures. For the key-value pairs included in the execution write set of a target transaction obtained by the blockchain system due to the execution of the target transaction, when the key in the key-value pair includes the contract address of the target smart contract and the key of the target state variable defined in the target smart contract, the target tree identifier pre-allocated to the target smart contract can be first obtained from the meta information according to the contract address; then, the value in the key-value pair is stored in the persistent storage medium, and according to the target tree identifier, in the target tree structure corresponding to the target smart contract, a target leaf node is generated / updated. The directed path from the root node to the target leaf node of the target tree structure includes the key of the target state variable, and the target leaf node includes the position information of the value in the key-value pair in the persistent storage medium; finally, an index entry is formed by using the target leaf node and the target tree identifier, and the index entry is stored in the persistent storage medium. The index entry is used to access the value in the key-value pair in the persistent storage medium.

[0044] Through the technical solution provided in the embodiments of this specification, for the tree structure used to organize the contract states of smart contracts, the position information of the value of the state variable in the persistent storage medium can be stored in the leaf node of the tree structure, rather than directly storing the value of the state variable; in addition, an index entry that can be used to support accessing the value of the state variable in the persistent storage medium is generated by using the leaf node and the tree identifier pre-allocated to the smart contract, rather than directly using the contract address to generate the index entry. In this way, the value of the state variable is persisted in a manner of separating data and index, which is beneficial to more efficiently accessing the value of the state variable in the persistent storage medium, and at the same time, the data volume of the index entry can be minimized as much as possible to save storage resources.

[0045] Figure 3 It is a flowchart of a method for managing state data in a blockchain system provided in the embodiments of this specification. This method can be executed by a blockchain node in the blockchain system, for example, executed by a storage processing module in the blockchain node; in this blockchain system, the contract states of multiple smart contracts are organized through multiple tree structures. This method exemplarily describes the process of storing the value of a target state variable in the persistent storage medium after the blockchain system obtains the value of a certain target state variable defined in the target contract due to the execution of a target transaction that directly or indirectly calls the target smart contract.

[0046] Refer to Figure 3As shown, the method may include, but is not limited to, some or all of the following steps S301 to S309.

[0047] Step S301: Obtain the key-value pairs included in the execution write set of the target transaction. The key in the key-value pair includes the contract address of the target smart contract and the key of the target state variable defined in the target smart contract.

[0048] When a blockchain node in the blockchain system executes a certain target transaction, for example, when executing a certain target transaction through the execution module in the blockchain node, if the target transaction is successfully executed, the execution write set of the target transaction will be obtained. The execution write set may include one or more key-value pairs. If the target transaction directly or indirectly invokes a certain target smart contract deployed in the blockchain system, it may occur that the value of a certain target state variable defined in the target smart contract is generated / updated in the contract state of the target smart contract. The execution write set of the target transaction may then include a key-value pair corresponding to the target state variable. The key in the key-value pair includes the contract address of the target smart contract and the key of the target state variable, and the value in the key-value pair is the value of the generated / updated target state variable. Referring to the foregoing, the key of the target state variable is usually determined based on the declaration position of the target state variable in the contract code of the target smart contract.

[0049] Exemplarily, in combination with Figure 2And code example 1 of the smart contract "SimpleStorage" in the foregoing example. The target transaction may be a transaction initiated by Bob to call the smart contract "SimpleStorage". The From field of the target transaction includes Bob's account address, the To field includes the contract address of "SimpleStorage", such as "0x6f8ae93…", and the Data field includes the call method of "SimpleStorage" (for example, the function identifier "set" of the method function "function set()" in "SimpleStorage") and parameters (for example, the value hello of the state variable storedData of data type strings). When the execution module of the blockchain node executes the target transaction, it will execute the method function "function set()" in the smart contract "SimpleStorage" according to the target transaction. Finally, the execution write set of the target transaction obtained includes the key-value pair corresponding to the state variable storedData. If the key of the state variable storedData is fa6be33 and the contract address of the smart contract "SimpleStorage" is a77d397 (here, for the convenience of example description, the contract address "0x6f8ae93…" in the foregoing example is replaced by a77d397); then, in the key-value pair corresponding to the state variable storedData, the key includes the contract address a77d397 of the smart contract "SimpleStorage" and the key of the state variable storedData, that is, fa6be33, and the value is hello.

[0050] In the foregoing example, the target smart contract is directly or indirectly called by the target transaction for exemplary description. In a specific technical scenario, the target transaction may also be a transaction for deploying the target smart contract. The key-value pair in step S301 may be generated during the execution of the target transaction, that is, during the initialization of the contract state of the target smart contract.

[0051] Step S303, obtain the target tree identifier pre-allocated to the target smart contract according to the contract address.

[0052] In a possible implementation, the target tree identifier can be allocated by a blockchain node in the blockchain system during the deployment of the target smart contract. Continuing with the previous example, for the smart contract "SimpleStorage", when the blockchain node, such as the execution module in the blockchain node, executes the transaction for deploying the smart contract "SimpleStorage", it will obtain the contract address a77d397 of the smart contract "SimpleStorage". In addition, the execution module may also initialize the smart contract "SimpleStorage", for example, initialize the contract state of the smart contract "SimpleStorage" and obtain the execution write set of the transaction for deploying "SimpleStorage". After the blockchain node completes the initialization of the contract state of the smart contract "SimpleStorage", that is, after obtaining the execution write set of the transaction for deploying the smart contract "SimpleStorage", it can allocate / determine the target tree identifier for the smart contract "SimpleStorage", and store the allocated target tree identifier for the smart contract "SimpleStorage" as a component of the meta-information in a specific meta-file. For example, the contract address a77d397 and the target tree identifier of the smart contract "SimpleStorage" can be stored in association in a specific meta-file metadata file, and the associated contract address a77d397 and target tree identifier are used as components of the meta-information.

[0053] During the process of deploying the target smart contract, the blockchain node can determine the target tree identifier corresponding to the target smart contract based on the number of contracts of the smart contracts already deployed in the blockchain system. Exemplarily, here it is assumed that the smart contract "SimpleStorage" is the x-th smart contract deployed in the blockchain system, then the blockchain node, such as the storage and processing module of the blockchain node, can use x of a certain predetermined length (for example, 4 bytes, 32 bits) to represent the tree identifier corresponding to the smart contract "SimpleStorage".

[0054] In other words, if the target transaction is used to deploy the target smart contract, then before executing step S303, the blockchain node, such as the storage and processing module in the blockchain node, will also allocate the target tree identifier for the target smart contract and store the target tree identifier as a component of the meta-information in the persistent storage medium. If the target transaction is used to directly or indirectly call the target smart contract, then the blockchain node, such as the storage and processing module of the blockchain node, will directly execute step S303.

[0055] In another possible implementation, during the process of deploying a target smart contract, the blockchain system may not initialize the contract state of the target smart contract. Instead, the contract state of the target smart contract is initialized only when the target smart contract is first called. Correspondingly, the target tree identifier can be assigned by a blockchain node in the blockchain system when the target smart contract is first called. For example, the storage and processing module in the blockchain node can associate the target tree identifier assigned to the target smart contract with the contract address of the target smart contract and store them in the meta-information.

[0056] Step S305: Store the value in the key-value pair into the persistent storage medium.

[0057] The account state of an external account / contract account and the value of the state variables defined by the smart contract can be stored in multiple data files. That is, in the aforementioned step S305, specifically, the value in the key-value pair included in the execution write set of the target transaction can be stored in the data file in the persistent storage medium. In some embodiments, in order to more effectively distinguish the values of the state variables defined in different smart contracts in the persistent storage medium, the target tree identifier, the key of the target state variable, and the value in the key-value pair can be combined into a storage object and stored in the persistent storage medium. Continuing with the previous example, please continue to refer to Figure 5 As shown, for example, the tree identifier x corresponding to the smart contract "SimpleStorage", the key fa6be33 of the state variable storedData, and the value hello of the state variable storedData can be combined into a storage object and stored in a certain data file in the persistent storage medium, such as the data file Datafile F1.

[0058] Step S307: Generate / update the target leaf node in the target tree structure corresponding to the target smart contract according to the target tree identifier. The directed path from the root node to the target leaf node of the target tree structure includes the key of the target state variable, and the target leaf node includes the location information of the value in the key-value pair in the persistent storage medium.

[0059] Exemplarily, refer to Figure 4As shown, it is assumed here that the target transaction belongs to block N. The storage and processing module of the blockchain node can build a tree structure, i.e., the storage trie, for managing the contract state of the smart contract "SimpleStorage" in memory according to the contract address of the target smart contract. For example, according to the value s1 included in the storage root field of the leaf node, such as leaf node A4, corresponding to the contract address a77d397 in the state trie corresponding to block N-1. In this process, the root node A10 of the tree structure, i.e., the storage trie, corresponding to "SimpleStorage" will be loaded first according to s1, and then all the nodes in the directed path from the root node to the target leaf node corresponding to the state variable storedData will be loaded in sequence. Except for the root node and the target leaf node, the directed path may also include a relay node, and the type of the relay node can be an Extension Node or a Branch Node. For example, the root node A10, the extension node A11, the branch node A14, and the leaf node A15 will be loaded in sequence in memory. The leaf node A15 may be a newly generated leaf node or a leaf node to be updated. In short, after loading the leaf node A15, the location information L1 of the value in the key-value pair corresponding to the target state variable in the persistent storage medium will be written into the leaf node A15, such as the location field of the leaf node A15. The aforementioned location information may include the file identifier of the data file to which the value of the target state variable belongs in the persistent storage medium, the address offset in the data file, and the data length, etc.

[0060] The foregoing Figure 4 In the node composition of the exemplary tree structure, the prefix "prefix" is used to represent the type of the tree node. For example, 0 represents an Extension Node containing an even number of shared nibbles, 1 represents an Extension Node containing an odd number of shared nibbles, 2 represents a Leaf Node containing an even number of nibbles, and 3 represents a Leaf Node containing an odd number of nibbles. In addition, Figure 4 Each tree node included in the exemplary tree structure is exemplary. For example, each leaf node may only include a location field for storing location information, and the key of the state variable corresponding to the leaf node is included in the directed path from the root node to the parent node of the leaf node; for another example, each leaf node includes the key of its corresponding state variable and a location field for storing location information, etc.

[0061] It is understandable that if the tree identifier x corresponding to the smart contract "SimpleStorage", the key "fa6be33" of the state variable storedData, and the value "hello" of the state variable storedData are formed into a storage object and stored in a certain data file, the location information L1 written in the target leaf node is the location information of the storage object in the persistent storage medium.

[0062] Step S309: Use the target leaf node and the target tree identifier to form an index entry, and store the index entry in the persistent storage medium. The index entry is used to access the value in the key-value pair in the persistent storage medium.

[0063] The target leaf node may include other contents in addition to the location information. For example, referring to Figure 4 the structure of the example leaf node, the leaf node may also include a prefix field and a key-end field; in this case, an index entry can be formed using the target tree identifier, the hash value of all the contents of the target leaf node, and all the contents of the target leaf node. For example, referring to Figure 5 as shown, an index entry that can be used to access the value "hello" of the state variable storedData in the persistent storage medium can be formed using the tree identifier x corresponding to the smart contract "SimpleStorage", the hash value H(A15) of all the contents of the leaf node A15, and all the contents of the leaf node A15. When the key of the target leaf node is included in the directed path from the root node to the parent node of the target leaf node, and the target leaf node only includes the location information of the relevant state variable in the persistent storage medium, an index entry can be formed using the target tree identifier, the key of the target state variable, and the location information included in the target leaf node; for example, referring to Figure 5 as shown, an index entry that can be used to access the value "hello" of the state variable storedData in the persistent storage medium can be formed using the tree identifier x corresponding to the smart contract "SimpleStorage", the key "fa6be33" of the state variable storedData corresponding to the leaf node A15, and the location information L1.

[0064] Similarly, the index entry can be stored in a certain index file in the persistent storage medium.

[0065] To save storage space, the target tree identifier can also be compressed according to a preset compression strategy, and then an index entry can be formed by using the compression result and the target leaf node. Exemplarily, the data type of the target tree identifier is a fixed-length integer, for example, 4 bytes or 32 bits. Then, the minimum number of bytes required for the target tree identifier can be determined first, and then a flag bit with a predetermined length and the target tree identifier represented by the minimum number of bytes are used to form the compression result, where the value represented by the flag bit is the minimum number of bytes. The foregoing compression strategy is only exemplary, and in a specific technical scenario, it can be selected by the developer himself.

[0066] Based on the same concept as the foregoing method embodiment, an embodiment of the present specification also provides a blockchain node 600 in a blockchain system. The blockchain system organizes the contract states of multiple smart contracts through multiple tree structures. Refer to Figure 6 As shown, the blockchain node 600 includes: a key-value pair acquisition unit 601, configured to acquire the key-value pairs included in the execution write set of a target transaction, where the key in the key-value pair includes the contract address of the target smart contract and the key of the target state variable defined by the target smart contract; a tree identifier acquisition unit 603, configured to acquire a target tree identifier pre-allocated for the target smart contract according to the contract address; a storage processing unit 605, configured to store the value in the key-value pair into a persistent storage medium; a node update unit 607, configured to generate / update a target leaf node in the target tree structure corresponding to the target smart contract according to the contract address, where the directed path from the root node to the target leaf node of the target tree structure includes the key of the target state variable, and the target leaf node includes the position information of the value in the key-value pair in the persistent storage medium; the storage processing unit 605 is further configured to form an index entry by using the target leaf node and the target tree identifier, and store the index entry into the persistent storage medium, where the index entry is used to access the value in the key-value pair in the persistent storage medium.

[0067] An embodiment of the present specification also provides a computer-readable storage medium, on which a computer program / instructions are stored. When the computer program / instructions are executed on a computer, the computer is made to execute a method for managing state data in a blockchain system provided in each of the foregoing embodiments.

[0068] An embodiment of the present specification also provides a computing device, including a memory and a processor. A computer program / instructions are stored in the memory, and when the processor executes the computer program / instructions, a method for managing state data in a blockchain system provided in each of the foregoing embodiments is implemented.

[0069] In the 1990s, improvements to a technology could be clearly distinguished as either hardware improvements (e.g., improvements to circuit structures such as diodes, transistors, switches, etc.) or software improvements (improvements to method flows). However, with the development of technology, many method flow improvements today can be regarded as direct improvements to hardware circuit structures. Almost all designers obtain the corresponding hardware circuit structure by programming the improved method flow into the hardware circuit. Therefore, it cannot be said that an improvement to a method flow cannot be implemented with a hardware entity module. For example, a Programmable Logic Device (PLD) (such as a Field Programmable Gate Array (FPGA)) is such an integrated circuit whose logical function is determined by a user's programming of the device. Designers can program on their own to "integrate" a digital system onto a single PLD, without having to ask a chip manufacturer to design and fabricate a dedicated integrated circuit chip. Moreover, nowadays, instead of manually fabricating integrated circuit chips, this programming is mostly implemented using "logic compiler" software, which is similar to the software compiler used in program development and writing. The original code before compilation also has to be written in a specific programming language, which is called a Hardware Description Language (HDL), and there is not just one kind of HDL, but many kinds, such as ABEL (Advanced Boolean Expression Language), AHDL (Altera Hardware Description Language), Confluence, CUPL (Cornell University Programming Language), HDCal, JHDL (Java Hardware Description Language), Lava, Lola, MyHDL, PALASM, RHDL (Ruby Hardware Description Language), etc. Currently, the most commonly used are VHDL (Very-High-Speed Integrated Circuit Hardware Description Language) and Verilog. Those skilled in the art should also be clear that as long as the method flow is slightly logically programmed with the above-mentioned several hardware description languages and programmed into the integrated circuit, it is easy to obtain the hardware circuit that implements the logical method flow.

[0070] The controller can be implemented in any suitable manner. For example, the controller can take the form of, for example, a microprocessor or a processor and a computer-readable medium storing computer-readable program code (such as software or firmware) executable by the (micro)processor, logic gates, switches, an application specific integrated circuit (ASIC), a programmable logic controller, and an embedded microcontroller. Examples of the controller include, but are not limited to, the following microcontrollers: ARC 625D, Atmel AT91SAM, Microchip PIC18F26K20, and Silicone Labs C8051F320. The memory controller can also be implemented as part of the control logic of the memory. Those skilled in the art also know that in addition to implementing the controller in the form of pure computer-readable program code, it is entirely possible to logically program the method steps to enable the controller to be implemented in the form of logic gates, switches, application specific integrated circuits, programmable logic controllers, embedded microcontrollers, etc. to achieve the same function. Therefore, such a controller can be considered a hardware component, and the devices included therein for implementing various functions can also be regarded as the structures within the hardware component. Or even, the devices for implementing various functions can be regarded as either software modules for implementing the method or structures within the hardware component.

[0071] The systems, devices, modules, or units illustrated in the above embodiments can be specifically implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a server system. Of course, this application does not exclude that with the development of future computer technologies, the computers for implementing the functions of the above embodiments can be, for example, personal computers, laptop computers, in-vehicle human-machine interaction devices, cellular phones, camera phones, smart phones, personal digital assistants, media players, navigation devices, email devices, game consoles, tablet computers, wearable devices, or any combination of these devices.

[0072] Although one or more embodiments of this specification provide method operation steps as described in the embodiments or flowcharts, more or fewer operation steps may be included based on conventional or non-creative means. The order of steps listed in the embodiments is only one way among many execution orders of the steps and does not represent the only execution order. When the actual device or terminal product is executed, it may be executed in the order of the method shown in the embodiments or the drawings or in parallel (for example, in an environment of parallel processors or multi-threaded processing, or even in a distributed data processing environment). The terms "comprise", "include" or any other variant thereof are intended to cover non-exclusive inclusion, so that a process, method, product or device comprising a series of elements not only includes those elements, but also includes other elements not expressly listed, or also includes elements inherent to such process, method, product or device. Without further limitation, it does not exclude the existence of additional identical or equivalent elements in the process, method, product or device comprising the said elements. For example, if terms such as first and second are used to denote names, they do not denote any particular order.

[0073] For the convenience of description, when describing the above device, it is divided into various modules according to functions and described separately. Of course, when implementing one or more of this specification, the functions of each module can be implemented in the same or multiple software and / or hardware, or the modules implementing the same function can be realized by a combination of multiple sub-modules or sub-units, etc. The device embodiments described above are only illustrative. For example, the division of the units is only a logical function division, and there may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed coupling or direct coupling or communication connection to each other can be through some interfaces. The indirect coupling or communication connection of the device or unit can be in electrical, mechanical or other forms.

[0074] The present invention is described with reference to the flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each flow and / or block in the flowchart and / or block diagram, and the combination of flows and / or blocks in the flowchart and / or block diagram can be implemented by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate a device for realizing the functions specified in Figure 1 one flow or multiple flows and / or blocks Figure 1 one block or multiple blocks.

[0075] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to operate in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including an instruction means that implements the functions specified in the flowchart(s) Figure 1 of one or more flowcharts and / or block(s) Figure 1 of one or more blocks.

[0076] These computer program instructions can also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer-implemented process, so that the instructions executed on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart(s) Figure 1 of one or more flowcharts and / or block(s) Figure 1 of one or more blocks.

[0077] In a typical configuration, a computing device includes one or more processors (CPUs), an input / output interface, a network interface, and memory.

[0078] The memory may include non-permanent memory in the form of computer-readable media, random access memory (RAM) and / or non-volatile memory such as read only memory (ROM) or flash memory (flash RAM). Memory is an example of computer-readable media.

[0079] Computer-readable media includes both permanent and non-permanent, removable and non-removable media implemented by any method or technology for storing information. The information can be computer-readable instructions, data structures, program modules, 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), read only memory (ROM), electrically erasable programmable read only memory (EEPROM), flash memory or other memory technologies, compact disc read only memory (CD-ROM), digital versatile discs (DVD) or other optical storage, magnetic cassettes, magnetic tape disk storage, graphene storage or other magnetic storage devices, or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include transitory media such as modulated data signals and carrier waves.

[0080] Those skilled in the art should understand that one or more embodiments of this specification can be provided as a method, a system, or a computer program product. Therefore, one or more embodiments of this specification can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, one or more embodiments of this specification can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.

[0081] One or more embodiments of this specification can be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. One or more embodiments of this specification can also be practiced in a distributed computing environment, where tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules can be located in local and remote computer storage media including storage devices.

[0082] Each embodiment in this specification is described in a progressive manner. For the same or similar parts among the embodiments, reference can be made to each other. Each embodiment focuses on the differences from other embodiments. In particular, for system embodiments, since they are basically similar to method embodiments, the description is relatively simple. For related parts, reference can be made to the description of the method embodiments. In the description of this specification, the description of reference terms such as "one embodiment", "some embodiments", "example", "specific example", or "some examples" means that the specific features, structures, materials, or characteristics described in connection with the embodiment or example are included in at least one embodiment or example of this specification. In this specification, the schematic expressions of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials, or characteristics described can be combined in a suitable manner in any one or more embodiments or examples. In addition, without contradiction, those skilled in the art can combine and combine different embodiments or examples described in this specification and the features of different embodiments or examples.

[0083] The above is only the embodiment of one or more embodiments of this specification and is not used to limit one or more embodiments of this specification. For those skilled in the art, one or more embodiments of this specification can have various changes and modifications. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of this specification should be included within the scope of the claims.

Claims

1. A method for managing state data in a blockchain system, where the blockchain system organizes the contract states of multiple smart contracts through multiple tree structures, and the method includes: Obtain the key-value pairs included in the execution write set of a target transaction, where the key in the key-value pair includes the contract address of the target smart contract and the key of the target state variable defined in the target smart contract; According to the contract address, obtain the target tree identifier pre-allocated for the target smart contract; Store the value in the key-value pair into a persistent storage medium; According to the contract address, generate / update a target leaf node in the target tree structure corresponding to the target smart contract. The directed path from the root node to the target leaf node of the target tree structure includes the key of the target state variable, and the target leaf node includes the position information of the value in the key-value pair in the persistent storage medium; Use the target leaf node and the target tree identifier to form an index entry, and store the index entry into the persistent storage medium. The index entry is used to access the value in the key-value pair in the persistent storage medium.

2. The method according to claim 1, where the target tree identifier is allocated by a blockchain node in the blockchain system during the deployment process of the target smart contract.

3. The method according to claim 2, where the target tree identifier is determined based on the cumulative number of smart contracts already deployed in the blockchain system during the deployment process of the target smart contract.

4. The method according to claim 1, where storing the value in the key-value pair to a persistent storage medium includes: Form a storage object with the target tree identifier, the key of the target state variable, and the value in the key-value pair, and store it into the persistent storage medium; where the position information is the position information of the storage object.

5. The method according to claim 1, wherein the use of the target leaf node and the target tree identifier to form an index entry includes: Use the target tree identifier, the target leaf node, and its hash value to form an index entry.

6. The method according to claim 1, wherein the forming of the index entry by using the target leaf node and the target tree identifier comprises: Perform compression processing on the target tree identifier according to a preset compression strategy, and use the compression result and the target leaf node to form an index entry.

7. The method according to claim 6, where the data type of the target tree identifier is a fixed-length integer; Among them, The performing compression processing on the target tree identifier according to a preset compression strategy includes: Determine the minimum number of bytes required for the target tree identifier; Use a flag bit with a predetermined length and the target tree identifier represented by the minimum number of bytes to form a compression result, where the value represented by the flag bit is the minimum number of bytes.

8. A blockchain node in a blockchain system, where the blockchain system organizes the contract states of multiple smart contracts through multiple tree structures, and the blockchain node includes: A key-value pair acquisition unit, configured to obtain the key-value pairs included in the execution write set of a target transaction, where the key in the key-value pair includes the contract address of the target smart contract and the key of the target state variable defined by the target smart contract; A tree identifier acquisition unit, configured to acquire a target tree identifier pre-assigned to the target smart contract according to the contract address; A storage processing unit, configured to store the value in the key-value pair into a persistent storage medium; A node update unit, configured to generate / update a target leaf node in a target tree structure corresponding to the target smart contract according to the contract address, where a directed path from the root node to the target leaf node of the target tree structure includes the key of the target state variable, and the target leaf node includes the position information of the value in the key-value pair in the persistent storage medium; The storage processing unit is further configured to form an index entry by using the target leaf node and the target tree identifier, and store the index entry into the persistent storage medium, where the index entry is used to access the value in the key-value pair in the persistent storage medium.

9. A computing device, including a memory and a processor, where a computer program is stored in the memory, and when the processor executes the computer program, the method according to any one of claims 1-7 is implemented.

10. A computer-readable storage medium, on which a computer program is stored, and when the computer program is executed in a computing device, the computing device executes the method according to any one of claims 1-7.