Data storage method and device, data query method and device, equipment and medium

By storing state change data of blockchain transactions in the form of key-value pairs in the blockchain system, the problems of storage redundancy and insufficient query performance in the prior art are solved, and efficient state data storage and query are achieved.

CN119739792APending Publication Date: 2025-04-01IFLYTEK CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411940030.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-26
Publication Date
2025-04-01

AI Technical Summary

Technical Problem

Existing blockchain systems have problems such as storage redundancy, insufficient query performance and excessive storage granularity when storing and managing state data, making it difficult to support state data backtracking with transaction granularity.

Method used

By obtaining the state change data corresponding to blockchain transactions, a storage key is generated based on the location of the transaction in the blockchain, the data type and address of the state change data, and the state change data is stored in the database in the form of key-value pairs.

Benefits of technology

It reduces the use of storage resources, improves storage efficiency and query performance, avoids a large number of redundant storage in traditional methods, and supports transaction-grained state data backtracking.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119739792A_ABST
    Figure CN119739792A_ABST
Patent Text Reader

Abstract

The invention provides a data storage method and device, a data query method and device, equipment and a medium. The method comprises the steps of obtaining state change data corresponding to a block chain transaction; and determining a storage key corresponding to the state change data based on the position of the block chain transaction in the block chain, the data type of the state change data and the address in the state change data, and storing the storage key and a numerical value in the state change data in a database in the form of a key-value pair. According to the method, the device, the equipment and the medium provided by the invention, by adopting a state change data recording mode of transaction granularity, a large amount of redundant storage caused by the fact that the complete world state of each block needs to be stored in a traditional method is avoided. Meanwhile, intermediate nodes in a traditional storage structure are omitted, data are directly stored in a key-value pair mode, and storage resource occupation is remarkably reduced. In addition, the historical state of the transaction granularity and the latest state data are managed independently, and storage redundancy is avoided.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of blockchain technology, and in particular to a data storage, query method, device, equipment and medium. Background Art

[0002] As a decentralized, distributed ledger technology, blockchain technology is widely used in fields such as smart contracts. The state data of the blockchain is used to record information such as account balances and smart contract storage on the chain, and is an integral part of the core data on the chain. With the popularization of blockchain applications and the rapid growth in the number of transactions, how to efficiently store and manage state data has become an urgent problem to be solved.

[0003] Existing blockchain systems (such as Ethereum) usually use the MPT (Merkle Patricia Trie) structure to store state data. Although the MPT structure has certain advantages in state verification and consistency, it has problems such as storage redundancy, insufficient query performance, and coarse storage granularity. First, the MPT structure needs to store a large number of intermediate nodes, resulting in a waste of storage space; second, state queries need to traverse multiple intermediate nodes, and the query efficiency is low, especially in the historical state query scenario; finally, the existing storage method usually stores in blocks, which is difficult to refine to the transaction level, and cannot efficiently support the state data backtracking of transaction granularity, and also causes a lot of storage redundancy.

[0004] Therefore, there is an urgent need for a state data storage method that can support transaction granularity to improve storage efficiency, optimize query performance, and reduce storage resource usage. Summary of the invention

[0005] The present invention provides a data storage and query method, device, equipment and medium, which are used to solve the defects of storage redundancy and low query efficiency in related technologies.

[0006] The present invention provides a data storage method, comprising the following steps: Get the state change data corresponding to the blockchain transaction; Based on the position of the blockchain transaction in the blockchain, the data type of the state change data, and the address in the state change data, determine the storage key corresponding to the state change data, and store the storage key and the numerical value in the state change data in the database in the form of a key-value pair.

[0007] According to a data storage method provided by the present invention, determining a storage key corresponding to the state change data based on the position of the blockchain transaction in the blockchain, the data type of the state change data, and the address in the state change data includes: Generating a timestamp based on the location of the blockchain transaction in the blockchain; Generate a type identifier based on the data type of the state change data; Based on the timestamp, the type identifier, and the address in the state change data, a storage key corresponding to the state change data is generated.

[0008] According to a data storage method provided by the present invention, when the data type is business data, generating a storage key corresponding to the state change data based on the timestamp, the type identifier and the address in the state change data includes: A storage key corresponding to the state change data is generated based on the timestamp, the type identifier, and the address and service key in the state change data.

[0009] According to a data storage method provided by the present invention, generating a timestamp based on the position of the blockchain transaction in the blockchain includes: The timestamp is generated based on the block height of the block where the blockchain transaction is located and the transaction sequence number of the blockchain transaction in the block.

[0010] According to a data storage method provided by the present invention, the determination of the data type includes: The data type is determined based on whether the business key in the state change data is empty and whether the value in the state change data is empty.

[0011] According to a data storage method provided by the present invention, obtaining state change data corresponding to a blockchain transaction includes: Determining at least one state change triggered by the blockchain transaction; Get the state change data corresponding to each state change.

[0012] The present invention provides a data query method, comprising the following steps: Receive a query request, and determine, based on the query request, a location of a blockchain transaction to be queried in the blockchain, an address of state change data corresponding to the blockchain transaction, and a data type of the state change data; Determine a query key based on the position of the blockchain transaction to be queried in the blockchain, the address of the state change data corresponding to the blockchain transaction, and the data type of the state change data; Based on the query key, the value of the state change data corresponding to each blockchain transaction stored in the form of a key-value pair is queried in the database.

[0013] According to a data query method provided by the present invention, the determination of the data type of the state change data includes: The data type of the state change data is determined based on whether the business key in the query request is empty.

[0014] According to a data query method provided by the present invention, the query key includes a timestamp, and the determination of the timestamp includes: In a case where the position of the blockchain transaction to be queried in the blockchain includes the block height of the block where the blockchain transaction is located and the transaction sequence number of the blockchain transaction in the block, generating the timestamp based on the block height and the transaction sequence number; In the case where the position of the blockchain transaction to be queried in the blockchain only includes the block height of the block at the blockchain exchange, the timestamp is generated based on the block height and the total number of transactions in the block at the blockchain exchange.

[0015] According to a data query method provided by the present invention, the query key includes a query key prefix and a timestamp; The querying, based on the query key, in the database for the value of the state change data corresponding to each blockchain transaction stored in the form of a key-value pair includes: Performing a range query in a database based on the query key prefix to obtain a key-value pair that matches the query key prefix; From the key-value pairs that match the query key prefix, candidate key-value pairs whose timestamps are before the timestamp of the query key are screened out, and the value of the candidate key-value pair with the largest timestamp is used as the query result.

[0016] According to a data query method provided by the present invention, the query key prefix includes the address of the state change data corresponding to the blockchain transaction; The performing a range query in the database based on the query key prefix to obtain a key-value pair matching the query key prefix includes: Using the deletion type identifier and the address as keys, searching the database for a key-value pair of the deletion type; If there is a key-value pair of the deletion type, and the timestamp of the key-value pair of the deletion type is before the timestamp of the query key, determining that the query result is empty; Otherwise, a range query is performed in the database based on the query key prefix to obtain key-value pairs matching the query key prefix.

[0017] The present invention also provides a data storage device, comprising the following modules: An acquisition unit, used to acquire state change data corresponding to blockchain transactions; A storage unit is used to determine a storage key corresponding to the state change data based on the position of the blockchain transaction in the blockchain, the data type of the state change data, and the address in the state change data, and store the storage key and the value in the state change data in the database in the form of a key-value pair.

[0018] The present invention also provides a data query device, comprising the following modules: A query receiving unit, configured to receive a query request, and determine, based on the query request, a location of a blockchain transaction to be queried in the blockchain, an address of state change data corresponding to the blockchain transaction, and a data type of the state change data; A query key determination unit, configured to determine a query key based on a position of the blockchain transaction to be queried in the blockchain, an address of the state change data corresponding to the blockchain transaction, and a data type of the state change data; The query unit is used to query the value of the state change data corresponding to each blockchain transaction stored in the form of a key-value pair in the database based on the query key.

[0019] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, any of the above-mentioned data storage methods or data query methods is implemented.

[0020] The present invention also provides a non-transitory computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, it implements any of the data storage methods or data query methods described above.

[0021] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements any of the above-mentioned data storage methods or data query methods.

[0022] The data storage, query method, device, equipment and medium provided by the present invention only record the state data actually modified by each transaction by adopting the state change data recording method at the transaction granularity, thus avoiding the large amount of redundant storage caused by the need to save the complete world state of each block in the traditional method. At the same time, the intermediate nodes in the traditional storage structure are omitted, and the state change data is directly stored in the form of key-value pairs, which significantly reduces the storage resource usage and improves the storage efficiency. In addition, the present invention manages the historical state of the transaction granularity and the latest state change data independently, avoiding the storage redundancy caused by saving the transaction modification record and the latest state change data at the same time. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] In order to more clearly illustrate the technical solutions in the present invention or related technologies, the following briefly introduces the drawings required for use in the embodiments or related technical descriptions. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0024] Figure 1 It is a structural diagram of MPT in the related art.

[0025] Figure 2 It is a schematic diagram of the blockchain transaction execution process in the related technology.

[0026] Figure 3 It is a flow chart of the data storage method provided by the present invention.

[0027] Figure 4 It is a schematic diagram of the total transaction amount update process provided by the present invention.

[0028] Figure 5 It is a schematic diagram of the historical state storage process provided by the present invention.

[0029] Figure 6 It is a flow chart of the data storage method provided by the present invention.

[0030] Figure 7 It is a schematic diagram of the historical status query process provided by the present invention.

[0031] Figure 8 This is a coding flow chart when the query request provided by the present invention includes the block height and the transaction sequence number.

[0032] Fig. 9 This is a coding flow chart when the query request provided by the present invention only includes the block height.

[0033] Fig.10 It is a structural schematic diagram of the data storage device provided by the present invention.

[0034] Fig.11 It is a structural schematic diagram of the data query device provided by the present invention.

[0035] Fig.12 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION

[0036] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0037] Blockchain technology is a decentralized distributed ledger technology, the core of which is to ensure the security and immutability of data through distributed storage and consensus mechanisms. In the blockchain system, state data is part of the core data, which is used to record account balances, storage information of smart contracts, etc. During the operation of the blockchain, the execution of each transaction will cause state changes, and these state changes need to be stored to ensure the integrity and consistency of the blockchain.

[0038] At present, mainstream blockchain systems (such as Ethereum) usually use the MPT structure to store state data. MPT can provide data integrity verification and rapid state update capabilities, but there are some significant problems in its storage. Figure 1 It is a schematic diagram of the structure of MPT in the related art. Figure 1 As shown in the figure, MPT needs to store a large number of intermediate nodes, each of which stores additional path information and hash values. The storage space of these intermediate nodes may far exceed the size of the actual data itself, resulting in a waste of storage resources. Secondly, in blockchain systems, traditional storage methods are usually based on saving the complete world state, which means that every time a block is generated, no matter how many states have changed, the complete world state after the execution of the entire block needs to be saved. As the number of transactions and blocks increases, this storage method will lead to the accumulation of a large amount of redundant data, further exacerbating the waste of storage resources. Especially for archiving nodes, it is necessary to save records of all historical states and complete world states at the same time, and the storage demand is growing exponentially, seriously affecting the storage efficiency and scalability of the blockchain system.

[0039] In addition, the blockchain transaction execution process is essentially a state transition process. Figure 2 This is a schematic diagram of the blockchain transaction execution process in the related technology. Figure 2 As shown in Figure 1, by executing transactions in block N, the world state changes from State(N-1) to State(N). Usually, a block contains multiple transactions, which are usually executed serially, so the state transition at the block level can be further expanded into multiple transaction-level state transitions. Figure 2As shown in the figure, by executing transaction j, the world state is transformed from State{N,j-1} to State{N,j}. Generally speaking, a transaction will only access part of the world state. In order to construct a historical state with transaction granularity, it is necessary to collect each state modified by each transaction during the transaction execution process. The data format is {n, j, addr, key, value}. Among them, n represents the block height of the current transaction execution, j represents the subscript of the current transaction execution, addr is the contract address called, key is the state key modified by the transaction (such as when modifying the account status, key can be empty; when modifying the smart contract storage data, key is the business key), and value is the modified state value. A transaction may modify multiple states, and the modification records of these states are collectively referred to as History State{n, j} (which may contain multiple state modification records).

[0040] After the transaction is executed, the latest state data State{n, i} and all historical state data (such as History State{n, 0}, ..., History State{n, i}) are generated and submitted to the database for persistent storage. However, since the modification record of each transaction needs to be saved together with the latest state data, this design will introduce additional storage overhead, especially when all historical states at the block level and transaction level need to be saved, this redundant storage problem is particularly significant. This further aggravates the consumption of storage resources and reduces the storage efficiency of the blockchain system.

[0041] In view of the above problems, an embodiment of the present invention provides a data storage method. Figure 3 It is a flow chart of the data storage method provided by the present invention. Figure 3 As shown, the method includes: Step 310, obtaining the state change data corresponding to the blockchain transaction.

[0042] State change data refers to the change information of the on-chain state caused by each transaction in the blockchain system during execution, reflecting the specific impact of the transaction on the account attributes or smart contract storage data. State change data may include the following: the target address that has changed (such as the account address or contract address), the state key (identifying the specific storage field or account attribute that has been modified, such as the balance or storage key value), the modified state value, and other auxiliary information (such as the block height and transaction sequence number, which are used to identify the execution location of the transaction).

[0043] State change data can be obtained in a variety of ways, such as extracting the state changes caused by each transaction when a block is generated, or capturing relevant data when a blockchain node processes a state update. In specific implementations, blockchain nodes will monitor the state tree, database, or state change records in memory to extract the target address, state key, and value that have changed. These data are standardized and archived to provide an accurate basis for subsequent storage and query operations.

[0044] Step 320, based on the position of the blockchain transaction in the blockchain, the data type of the state change data, and the address in the state change data, determine the storage key corresponding to the state change data, and store the storage key and the value in the state change data in the database in the form of a key-value pair.

[0045] Specifically, the storage key is used to uniquely identify the location of the state data in the database, and its generation is based on the specific type of state change data. For account status, the storage key contains the account ID, target address, and a timestamp generated by the block height and transaction sequence number, which is used to indicate changes in account attributes (such as balance, Nonce, etc.); for smart contract storage status, the storage key consists of the storage ID, contract address, business key (such as Slot Key) and timestamp, which is used to identify changes in a storage field in the contract; and for deletion records, the storage key contains the deletion ID, target address and timestamp, indicating the deletion operation of the account or contract.

[0046] The numerical value represents the final result of the state change and is an important part of the state change data. For changes in account status, the numerical value may be the updated value of the account balance, Nonce, or account code hash; for changes in smart contract storage, the numerical value is the new value of the corresponding business key; for deletion records, the numerical value is usually empty, indicating that the target account or contract has been deleted. The numerical value directly reflects the specific content of the state change, and when used in conjunction with the storage key, it can fully describe a state change. For example, Table 1 gives the specific forms of storage keys and numerical values ​​corresponding to different types of state change data.

[0047] Table 1: Specific forms of storage keys and values ​​corresponding to different types of state change data

[0048] The storage key and value are stored in the database in the form of key-value pairs, where the storage key is used as the key to locate the location of the state data; the value is used as the value to save the specific content of the state. This storage method significantly reduces storage redundancy by omitting the intermediate nodes in the MPT storage structure, thereby improving storage efficiency. At the same time, the storage form of key-value pairs ensures the standardization and structuring of data, making it easy to quickly locate and access the target state data through the retrieval function of the database. This efficient storage design not only improves the utilization of storage space, but also provides strong support for the scalability and performance optimization of the blockchain system.

[0049] The data storage method provided by the embodiment of the present invention, by adopting the state change data recording method at the transaction granularity, only records the state data actually modified by each transaction, avoiding the large amount of redundant storage caused by the need to save the complete world state of each block in the traditional method. At the same time, the intermediate nodes in the traditional storage structure are omitted, and the state change data is directly stored in the form of key-value pairs, which significantly reduces the storage resource occupancy and improves the storage efficiency. In addition, the present invention manages the historical state and the latest state data at the transaction granularity independently, avoiding the storage redundancy caused by saving the transaction modification record and the latest state change data at the same time.

[0050] Based on the above embodiment, in step 120, determining the storage key corresponding to the state change data based on the position of the blockchain transaction in the blockchain, the data type of the state change data, and the address in the state change data includes: Generating a timestamp based on the location of the blockchain transaction in the blockchain; Generate a type identifier based on the data type of the state change data; Based on the timestamp, the type identifier, and the address in the state change data, a storage key corresponding to the state change data is generated.

[0051] Specifically, a timestamp is generated based on the location of the blockchain transaction in the blockchain. The timestamp is an important component of the storage key and is used to uniquely identify the execution time of the transaction. Its generation method combines the block height and the transaction sequence number. By associating the sequence number of the transaction in the block with the block height, it ensures that the timestamp corresponding to each transaction is unique and sequential. The introduction of timestamps provides temporal consistency for the generation of storage keys, and facilitates the subsequent management and query of state data by time dimension.

[0052] Secondly, a type identifier is generated based on the data type of the state change data. The type identifier is used to clarify the nature of the state change and distinguish different types of data. For example, when the state change data involves account status (such as balance, Nonce, or code hash), the type identifier is the account identifier; when the state change data involves the storage field of the smart contract, the type identifier is the storage identifier; and when the state change data involves the deletion operation of an account or contract, the type identifier is the deletion identifier. This type of type identifier division can accurately reflect the nature of the state change and facilitate subsequent storage and query operations.

[0053] Next, a storage key is generated based on the timestamp, type identifier, and address in the state change data. The storage key forms a storage key that can uniquely identify state changes by combining the timestamp to identify the timing characteristics of the transaction, the type identifier to distinguish the category of the state data, and the address information to determine the ownership of the state data. Through this storage key, different types of state change data can be efficiently managed and organized, ensuring the structured and standardized data storage, and providing support for subsequent queries and operations.

[0054] Based on the above embodiment, when the data type is business data, generating a storage key corresponding to the state change data based on the timestamp, the type identifier, and the address in the state change data includes: A storage key corresponding to the state change data is generated based on the timestamp, the type identifier, and the address and service key in the state change data.

[0055] Specifically, the business key is used to identify the specific storage field in the smart contract storage, and is one of the core elements of building the smart contract storage data storage key. By combining the timestamp, type identifier, address in the state change data, and the business key, a unique corresponding storage key can be generated.

[0056] The timestamp is used to identify the execution order of transactions and ensure the temporal uniqueness of the storage key; the type identifier is used to distinguish business data from other state data; the address in the state change data provides the ownership information of the storage data and is used to identify the target smart contract. The business key (Slot Key) is used as a field identifier in the smart contract storage, which further refines the granularity of the storage key, so that the storage key can accurately locate the state change of a specific storage field in the contract.

[0057] By introducing business keys, storage keys can not only uniquely identify the overall state of smart contracts, but also support accurate management of smart contract storage fields. This design effectively solves the problem of insufficiently detailed contract storage data positioning in traditional storage methods, and improves the efficiency and flexibility of data management. At the same time, the storage keys generated by combining timestamps, type identifiers, addresses, and business keys can be fully compatible with different types of state data, providing an efficient state storage and management solution for blockchain systems.

[0058] Based on the above embodiment, generating a timestamp based on the position of the blockchain transaction in the blockchain includes: The timestamp is generated based on the block height of the block where the blockchain transaction is located and the transaction sequence number of the blockchain transaction in the block.

[0059] Specifically, by obtaining the block height of the block where the blockchain transaction is located, the position of the block where the current transaction is located in the blockchain is determined. The block height is the block number arranged in sequence in the blockchain, which is used to identify the uniqueness of the block. Secondly, by obtaining the transaction sequence number of the blockchain transaction in the block, the specific execution order of the transaction in the current block is further determined. The transaction sequence number is the number inside the block, which is arranged according to the execution order of the transaction.

[0060] After obtaining the block height and transaction sequence number, a timestamp is generated in combination with the cumulative statistical information of the entire chain of transactions. Specifically, by querying the cumulative total number of transactions before the current block, CurrentTotalTx(N-1), and adding it to the transaction sequence number in the current block, a unique timestamp can be generated. The calculation formula is Timestamp=CurrentTotalTx(N-1) +k, where N is the current block height, k is the sequence number of the current transaction, and CurrentTotalTx(N-1) represents the total number of all transactions on the blockchain as of block height N-1, which is a global statistical value of the number of transactions on the entire chain. Table 2 gives the structure of CurrentTotalTx: Table 2: Structure of CuurentTotalTx

[0061] The transaction totals are updated in real time as transactions are processed. Figure 4 : is a schematic diagram of the total transaction amount update process provided by the present invention. Figure 4As shown, first input the block height N and the total number of transactions in block N at this time, which is the data basis for supporting the update of the total number of transactions. Then determine whether the block height N-1 is 0. When the block height N-1 is 0, it indicates that block N-1 is the starting block, and the total number of transactions should be 0 at this time. When the block height N-1 is not 0, query the total number of transactions on the chain when the block height is N-1. The total number of transactions at block height N is updated based on the total number of transactions on the chain of the previous block, ensuring the consistency of data and the correctness of calculations. At this time, the total number of transactions in the block with a block height of N is the total number of transactions at a height of N-1 plus the total number of transactions in block N at this time. Finally, update the result to CurrentTotalTx to complete the update of the total number of transactions.

[0062] The timestamp generated in this way can effectively distinguish each transaction in the blockchain, support data positioning and management based on the time dimension, and provide a key technical foundation for building state storage at transaction granularity.

[0063] Based on the above embodiment, the determination of the data type includes: The data type is determined based on whether the business key in the state change data is empty and whether the value in the state change data is empty.

[0064] Specifically, by checking whether the business key is empty, you can distinguish between smart contract storage data and account status or deletion data. When the business key is empty, it indicates that the state change data does not involve smart contract storage fields, but is a state change or deletion record related to the account. In this case, the account status and deletion record are further distinguished based on whether the value is empty: if the value is not empty, it indicates that it is an account status (such as a change in balance, Nonce, or code hash), and the data type is determined to be account status; if the value is empty, it indicates that the data type is a deletion record, which is used to identify that an account or contract has been deleted.

[0065] When the business key is not empty, it indicates that the state change data involves the storage field of the smart contract. The business key is used to identify the specific storage location in the contract, such as a variable or storage slot. At this time, the data type is determined to be a storage identifier, indicating that the state change data records the update information of the smart contract storage field.

[0066] By comprehensively checking whether the business key is empty and whether the value is empty, the type of state change data can be accurately determined, mainly including account status, smart contract storage data, and deletion records. This classification method provides a clear basis for the subsequent generation of storage keys, while ensuring the standardization and consistency of various state data in storage and management.

[0067] Based on the above embodiment, the step of obtaining the state change data corresponding to the blockchain transaction includes: Determining at least one state change triggered by the blockchain transaction; Get the state change data corresponding to each state change.

[0068] Specifically, the execution of a blockchain transaction may affect multiple states on the chain, such as modifying account balances, updating contract storage fields, or deleting account status in one transaction. Each state change represents a specific operation of the transaction on the on-chain data. By parsing the execution logic of the transaction, all state changes triggered by the transaction are determined. For example, a transfer transaction may involve changes in the balances of two accounts, while a smart contract call transaction may modify multiple storage fields at the same time. For these state changes, their target addresses and specific operation types need to be identified one by one.

[0069] Next, get the state change data corresponding to each state change. For each state change, the following key information needs to be extracted: the target address (identifying the account or smart contract involved in the state change); the business key identifies the specific smart contract storage field or attribute. If it is empty, it means the account state or the data related to the deletion record; the modified state value represents the final value after the state change, such as the account balance, Nonce, or the new value of the business key of the smart contract. In addition, the block height and transaction index need to be recorded to identify the source transaction of the state change.

[0070] Based on the above embodiments, Figure 5 This is a schematic diagram of the historical state storage process provided by the present invention. Figure 5As shown, taking the historical state State(n,j) as an example, the input data is the state change data set under the transaction with transaction number j in block n. Each state change data contains {n, j, addr, key, value}. Among them, n represents the block height, j represents the sequence number of the transaction in the block to which it belongs, addr is the target address, key is the business key, and value is the value in the state change data. The process first traverses all state change records in State{n,j} one by one until all records are processed. For each state change record, it is judged and classified according to the values ​​of key and value. If the business key key is empty, it indicates that the state change involves account status or deletion records; if the business key is not empty, it indicates that the state change involves smart contract storage data, and the storage key is generated according to the encoding rules of smart contract storage. Further, by judging whether the value is empty, the specific data type is distinguished. When value is not empty, the state change data is classified as account state, and the storage key is generated according to the encoding rules of account state; when value is empty, the state change data is classified as deletion record, and the storage key is generated according to the encoding rules of deletion record. All classified and encoded data are persistently stored in the database in the form of key-value pairs for subsequent management and query. Through this process, the state change data triggered by transactions can be efficiently classified and stored in a standardized manner, thereby providing structured support for blockchain state management.

[0071] In blockchain systems, query technology is one of the core functions to ensure the traceability and verifiability of on-chain data. With the continuous growth of the number of blockchain transactions, the query of state data has become increasingly complex, especially in the scenarios of historical state backtracking and fine-grained transaction query, the performance of traditional methods is particularly insufficient. In the existing technology, state data is usually stored at the block level, relying on complex data structures such as MPT. Although it can provide data integrity verification, it has many limitations in query efficiency. For example, querying the historical state of a block requires traversing multiple intermediate nodes, and each query requires loading the state snapshot of the entire block. This method significantly increases the query latency when facing large-scale on-chain data. At the same time, for transaction-level backtracking queries (such as verifying the execution results of a specific transaction), existing methods lack direct support and usually need to extract data from the entire block state, resulting in cumbersome and inefficient operations. In addition, the existing query technology also has the problem of high logical complexity when processing deleted records and special state queries. Since the deletion operation is only effective logically, but the historical records still need to be retained, additional judgment logic is required to filter the deleted state during the query, which further increases the query burden.

[0072] Based on the data storage method provided by the present invention, the query technology can be significantly optimized. The present invention stores the state change data in the form of key-value pairs in the database through the state storage method of transaction granularity, and introduces identification information such as timestamps and business keys, so that the management of state data is more standardized. Relying on this storage method, the query technology can directly locate the target state based on the prefix and timestamp of the key-value pair, avoiding the complex state tree traversal process in the traditional method. At the same time, the storage design of historical states and transaction granularity provides natural support for queries, making query operations more efficient and flexible. By combining the storage method of the present invention, the query technology has been comprehensively improved in performance and accuracy, providing the blockchain system with a more superior data query capability.

[0073] Based on the above embodiments, an embodiment of the present invention provides a data query method. Figure 6 It is a flow chart of the data storage method provided by the present invention. Figure 6 As shown, the method includes: Step 610: Receive a query request, and determine, based on the query request, the location of the blockchain transaction to be queried in the blockchain, the address of the state change data corresponding to the blockchain transaction, and the data type of the state change data.

[0074] Specifically, the query request usually contains relevant information of the data to be queried provided by the user, such as transaction identification, block height, address information or other filtering conditions. This information is the key basis for locating blockchain transactions and their state change data. First, by parsing the parameters in the query request, the location of the target blockchain transaction in the blockchain is extracted. The location of the blockchain transaction can be determined by the block height and transaction sequence number, where the block height indicates the block to which the transaction belongs, and the transaction sequence number is used to identify the specific transaction location in the block. Through the combination of block height and transaction sequence number, the time point and storage location of the transaction to be queried can be uniquely determined.

[0075] Secondly, the address of the state change data is extracted from the query request. The address is the identifier of the account or smart contract in the blockchain system. The query operation needs to clearly identify the target address involved in the state change to be queried, such as the account address or smart contract address. Through the target address, the state data range related to the query can be accurately locked to avoid interference from irrelevant data.

[0076] Finally, the data type of the state change data is determined based on the content of the query request. The data type is used to distinguish the nature of the data to be queried, usually including three types: account data, smart contract storage data, and deletion records. By analyzing the characteristics of the state change data, such as whether it contains business keys or whether it is a deletion operation, the data type can be further clarified, thereby providing a basis for subsequent query processing.

[0077] By parsing the query request and determining the transaction location, target address and data type, it lays the foundation for subsequent efficient queries. This design ensures the accuracy and pertinence of the query, while avoiding unnecessary data traversal operations, thereby improving the efficiency and performance of the query.

[0078] Step 620, determining a query key based on the location of the blockchain transaction to be queried in the blockchain, the address of the state change data corresponding to the blockchain transaction, and the data type of the state change data; Specifically, the query key is determined based on the location of the blockchain transaction to be queried in the blockchain, the address of the state change data corresponding to the transaction, and the data type of the state change data. The query key is the unique identifier of the state change data in the database, ensuring that the required data can be retrieved efficiently and accurately.

[0079] The query key first includes the location of the blockchain transaction to be queried, which is determined by the block height and the transaction sequence number. The block height indicates the location of the block where the transaction is located in the entire blockchain, while the transaction sequence number indicates the specific location of the transaction within the block. By combining these two elements, a unique identifier can be generated for each transaction, thereby providing time sequence information for the query. In this way, the query key can clearly point to a specific block and transaction.

[0080] Secondly, the query key includes the address of the state change data. The state change in the blockchain is usually related to a specific address, such as an account address or a smart contract address. The address can determine the object to which the target data of the query belongs, ensuring that the query operation can be focused on a specific account or contract state. Therefore, the address plays a role in locating and distinguishing different data sources in the query key.

[0081] Furthermore, the data type of the state change data also occupies a key position in the query key. Different types of state change data, such as account balances, values ​​stored in smart contracts, or deleted records, need to be distinguished by data type identifiers. When querying, the data type identifier helps determine the nature of the data involved in the query and ensure the correctness of the query. The distinction between data types helps optimize query operations and ensures that each query can accurately retrieve relevant state data.

[0082] These elements are combined together in a certain encoding method to form the complete structure of the query key. Through this combination, the query key can uniquely identify each state change data and efficiently retrieve it in the database.

[0083] Step 630: Based on the query key, query the database for the value of the state change data corresponding to each blockchain transaction stored in the form of a key-value pair.

[0084] First, the query key is entered into the database, and the database will search based on the components of the key (such as block height, transaction sequence number, address, and data type). Since the state data is already stored in the form of key-value pairs, the query operation actually searches for matching keys in the database based on the query key and returns the corresponding value. The value of each query key represents the state change data corresponding to the transaction, which may be an account balance or a variable value in a smart contract. The data stored in the database is pre-indexed according to certain rules, which enables the query operation to quickly locate the target data directly through the query key without traversing the entire data set. Specifically, the database determines the location of the transaction to be queried by the location of the blockchain transaction to be queried in the query key in the blockchain, ensuring that the query process is efficient and accurate.

[0085] After finding the key-value pairs that meet the query conditions, the results are further processed. For example, if the data type corresponding to the query key is the account balance, the returned value is the balance information of the account; if the queried data type is the storage data of the smart contract, the returned value will be the latest value of a storage location in the contract. If the query is for deleted records, the returned value may be empty or the identification information may be deleted.

[0086] The data query method provided by the embodiment of the present invention adopts a transaction-granular query method to avoid the complex process of traversing multiple intermediate nodes in the traditional method. The data query directly locates the state change data to be queried based on the query key. This method improves query efficiency, reduces query delay, improves query efficiency, and makes query operations more flexible and efficient.

[0087] Based on the above embodiment, the determination of the data type of the state change data includes: The data type of the state change data is determined based on whether the business key in the query request is empty.

[0088] Specifically, when the business key is empty, it indicates that the state change data does not involve the storage field of the smart contract, but the account state data. When the business key is not empty, it indicates that the state change data involves the storage data of the smart contract. The business key usually identifies the specific storage location in the smart contract, such as a variable or storage slot, so the data type is determined to be smart contract storage data. This classification method based on whether the business key is empty can quickly and accurately distinguish the nature of the state data, providing a clear basis for subsequent storage, query or operation.

[0089] Based on the above embodiment, the query key includes a timestamp, and the determination of the timestamp includes: In a case where the position of the blockchain transaction to be queried in the blockchain includes the block height of the block where the blockchain transaction is located and the transaction sequence number of the blockchain transaction in the block, generating the timestamp based on the block height and the transaction sequence number; In the case where the position of the blockchain transaction to be queried in the blockchain only includes the block height of the block at the blockchain exchange, the timestamp is generated based on the block height and the total number of transactions in the block at the blockchain exchange.

[0090] Specifically, the query key includes a timestamp, which is an important element in the blockchain that uniquely identifies the time point of transaction execution. The method of determining the timestamp depends on the specific content of the location of the blockchain transaction to be queried in the blockchain: In the case where the position of the blockchain transaction to be queried in the blockchain includes the block height and the transaction sequence number, the generation of the timestamp is consistent with the generation method of the timestamp in the data storage method, and depends on the combination of the block height and the transaction sequence number. The block height is used to identify the absolute position of the block to which the transaction belongs in the entire blockchain, while the transaction sequence number indicates the execution order of the transaction in the current block. After obtaining the block height and transaction sequence number, the timestamp is generated in combination with the cumulative statistical information of the entire chain transaction. Specifically, by querying the cumulative total number of transactions before the current block CurrentTotalTx(N-1), and adding it to the transaction sequence number k in the current block, a unique timestamp is generated. The calculation formula is: Timestamp=CurrentTotalTx(N-1) + k. Among them, N is the current block height, and k is the sequence number of the current transaction in the current block. This method ensures the uniqueness and order of the timestamp in the entire chain, which helps to accurately identify the execution time point of each transaction.

[0091] When the position of the blockchain transaction to be queried in the blockchain only contains the block height, when the query request does not provide the transaction sequence number, the generation of the timestamp depends only on the block height and the total number of transactions in the block. By querying the total number of transactions in the current block CurrentTotalTx(N), the time point of the last transaction in the block, that is, CurrentTotalTx(N)-1, can be determined as the generated timestamp.

[0092] By combining the transaction statistics of the entire chain and the location parameters in the blockchain, the generated timestamp can be accurately mapped to a specific time point, whether it is a fine-grained query based on block height and transaction sequence number, or a coarse-grained query based on block height. This timestamp design not only improves data storage and query efficiency, but also provides a unified time management mechanism for the blockchain system, which is suitable for a variety of query scenarios.

[0093] Based on the above embodiment, the query key includes a query key prefix and a timestamp; The querying, based on the query key, in the database for the value of the state change data corresponding to each blockchain transaction stored in the form of a key-value pair includes: Performing a range query in a database based on the query key prefix to obtain a key-value pair that matches the query key prefix; From the key-value pairs that match the query key prefix, candidate key-value pairs whose timestamps are before the timestamp of the query key are screened out, and the value of the candidate key-value pair with the largest timestamp is used as the query result.

[0094] Specifically, the query key prefix is ​​used to identify the category and target address of the state data. The query key prefix contains the data type identifier and the target address. The data type identifier is used to distinguish account state data or smart contract storage data, while the target address identifies the specific account or smart contract. Through the query key prefix, the matching key-value pair set can be quickly located in the database, thereby narrowing the query scope and reducing unnecessary data scanning.

[0095] After completing the prefix matching, the filtering results are further processed according to the timestamp in the query key. When the timestamp is generated based on the block height and transaction sequence number, the timestamp is CurrentTotalTx(N-1) + k, indicating that the query target is the state change caused by a specific transaction. At this time, the records with timestamps less than the query key timestamp are filtered out from the matching key-value pairs to ensure that only the state change data before the target transaction is retained. Among these candidate data, the key-value pair with the largest timestamp is selected as the final query result. This filtering method ensures that the returned data is the latest state before the target transaction, and accurately locates the impact of a specific transaction.

[0096] When the timestamp is generated based on the block height and the transaction sequence number is not provided, the query target is the final state of the entire block. The timestamp corresponds to the time point of the last transaction in the block. The records with timestamps less than the query key timestamp are filtered from the matching key-value pairs, and the one with the largest timestamp is further extracted as the result. This ensures that the latest state record in the block is returned without processing the details of each transaction in the block.

[0097] By combining the query key prefix and timestamp filtering logic, this query method achieves efficient data location and extraction. Whether it is a precise query at the transaction level or a range query at the block level, it can quickly lock the target data, avoid traversing a large number of intermediate nodes in traditional methods, and ensure the accuracy and timeliness of the returned results.

[0098] Based on the above embodiment, the query key prefix includes the address of the state change data corresponding to the blockchain transaction; The performing a range query in the database based on the query key prefix to obtain a key-value pair matching the query key prefix includes: Using the deletion type identifier and the address as keys, searching the database for a key-value pair of the deletion type; If there is a key-value pair of the deletion type, and the timestamp of the key-value pair of the deletion type is before the timestamp of the query key, determining that the query result is empty; Otherwise, a range query is performed in the database based on the query key prefix to obtain key-value pairs matching the query key prefix.

[0099] Specifically, first use the deletion type identifier and the target address as the key to find the deletion type key-value pair in the database. The deletion type identifier is a special field used to mark whether an account or contract has been deleted. This field is combined with the target address and the timestamp to record the specific time point when the deletion operation of the target account or contract occurred. If a deletion type key-value pair matching the prefix is ​​found, it means that the target account or contract has been deleted.

[0100] After confirming the existence of the deletion type identifier, it is necessary to further compare the timestamp of the deletion record with the timestamp of the query key. If the timestamp of the deletion record is earlier than the timestamp of the query key, it means that the target account or contract has been deleted before the query time, and the queried status data is invalid. At this time, the query result is directly determined to be empty. This processing logic can ensure that the query result reflects the actual status of the data and avoid returning outdated or invalid data due to the deletion operation.

[0101] If there is no deletion type identifier, or the timestamp of the deleted record is later than the timestamp of the query key, you can continue to perform range queries based on the query key prefix. At this time, the system will extract other key-value pairs that match the query key prefix and filter out valid status data for return. In this way, the query logic can take into account the special cases of the deletion operation while efficiently locating the target data.

[0102] Table 3 gives an example of block execution status changes. As shown in Table 3, the second transaction in block 3 deleted account addr0. Under this premise, assume that the query key for the historical status to be queried is<addr0,C,3,1> , that is, we need to query the state corresponding to the key value C at the address addr0 when the block height is 3 and the transaction number is 1. First, use the identifier of the deleted record and the address addr0 as the query key prefix to find out whether there is a related deleted record in the database. According to Table 3, there is a deleted record in the block height 3<D,addr0,5> , indicating that addr0 was deleted in the transaction with timestamp 5. The timestamp of the query key is 4 (CurrentTotalTx(N-1)=4, k=1), and the timestamp of the deletion record is also 5, which is the same as the timestamp of the query key. This shows that at the target query time, address addr0 has just been deleted, so the state change data at this time is no longer available, and the query result should return empty. Assume that the query key of the historical state to be queried is<addr0,C,3,0> , that is, the state corresponding to the key value C of the address addr0 when the block height is 3 and the transaction number is 0 needs to be queried. At this time, the timestamp of the query key is 4 (CurrentTotalTx(N-1)=4, k=0), which is earlier than the timestamp 5 of the deletion record, indicating that at the target query time, the address addr0 has not been deleted. Based on the query key prefix<S, addr0, C> Perform a range query and filter out the key-value pairs with timestamps less than 4, then select the key-value pair with the largest timestamp, that is,<S, addr0, C, 4> =4. The query returns a result of 4, which means that at query time 4, the status data of address addr0 key value C is 4.

[0103] Based on any of the above embodiments, Figure 7 This is a schematic diagram of the historical status query process provided by the present invention. Figure 7 As shown, the query request is first input. The query request generally includes the target address, business key, block height, and may also include the transaction sequence number. After obtaining the query request, the address in the query request is extracted, and a query prefix of [D, addr] is generated, and a prefix search is performed in the database. The purpose of this step is to retrieve the deleted records to prevent the query key-based search from being performed when the state change data has been deleted. If the search result is not empty, it means that there is a deleted record, and the timestamp of the deleted record is recorded as t1; if the search result is empty, it means that there is no deleted record, and t1 is recorded as infinity. After retrieving the deleted records, the query request needs to be encoded to obtain the query key. The encoding method of the query request varies depending on the content provided by the query request.

[0104] Figure 8This is a coding flow chart when the query request provided by the present invention includes the block height and transaction sequence number. Figure 8 As shown in the figure, the query request format input is [addr, Slot_Key, BlockNumber, txIndex], where addr indicates the address Slot_Key is a business key used to identify specific storage fields, BlockNumber and txIndex respectively indicate the block height and transaction sequence number. According to the input BlockNumber and txIndex, first calculate the timestamp Timestamp, which uniquely identifies the execution time of a transaction on the blockchain and provides an accurate time reference for subsequent query operations. Next, determine the type of query request based on the value of Slot_Key. If Slot_Key is empty, it indicates that the query is a request for account status. In this case, the query key format generated is [A, addr, Timestamp], where A is the account identifier, addr is the target account address, and Timestamp is a timestamp used to accurately query the status of the account at a specified time point. If Slot_key is not empty, it indicates that the query is a request for smart contract storage data. In this case, the query key format is generated as [S, addr, Slot_Key, Timestamp], where S is the smart contract storage identifier, addr is the contract address, Slot_Key represents the key of the specific storage field, and Timestamp is used to locate the state of the stored data at a specified point in time.

[0105] Fig. 9 This is a coding flow chart when the query request provided by the present invention only includes the block height. Fig. 9As shown, the query request format input is [addr, Slot_Key, BlockNumber]. According to the input BlockNumber, the timestamp Timestamp is calculated first. When the timestamp is changed to the block height of BlockNumber, the total transaction volume txTotal on the chain is reduced by one, that is, Timestamp=txTotal-1. Next, the type of query request is determined according to the value of Slot_Key. If Slot_Key is empty, it indicates that the query is a request for account status. In this case, the query key format generated is [A, addr, Timestamp], where A is the account identifier, addr is the target account address, and Timestamp is the timestamp, which is used to accurately query the status of the account at a specified time point. If Slot_key is not empty, it indicates that the query is a request for smart contract storage data. In this case, the query key format generated is [S, addr, Slot_Key, Timestamp], where S is the smart contract storage identifier, addr is the contract address, Slot_Key represents the key of the specific storage field, and Timestamp is used to locate the status of the storage data at a specified time point. Next, extract the timestamp t2 from the query key and compare it with the timestamp t1 of the deleted record. If t2 is greater than or equal to t1, it indicates that the target query history state has been deleted at the time of query, and the query result returns a null value. If t2 is less than t1, it indicates that the target query history state has not been deleted at the time of query. At this time, based on the encoded query key prefix, search in the database to obtain candidate key-value pairs that match the query prefix. Among all candidate key-value pairs, select the candidate key-value pair with the largest timestamp and return it as the query result.

[0106] The data storage device and the data query device provided by the present invention are described below. The data storage device and the data query device described below can correspond to the data storage method and the data query method described above.

[0107] Fig.10 Schematic diagram of the structure of the data storage device provided by the present invention. Fig.10 As shown, the device comprises: An acquisition unit 1010 is used to acquire state change data corresponding to a blockchain transaction; The storage unit 1020 is used to determine the storage key corresponding to the state change data based on the position of the blockchain transaction in the blockchain, the data type of the state change data, and the address in the state change data, and store the storage key and the value in the state change data in the database in the form of a key-value pair.

[0108] The data storage device provided by the embodiment of the present invention only records the state data actually modified by each transaction by adopting the state change data recording method at the transaction granularity, thereby avoiding the large amount of redundant storage caused by the need to save the complete world state of each block in the traditional method. At the same time, the intermediate nodes in the traditional storage structure are omitted, and the state change data is directly stored in the form of key-value pairs, which significantly reduces the storage resource occupancy and improves the storage efficiency. In addition, the present invention manages the historical state and the latest state data at the transaction granularity independently, avoiding the storage redundancy caused by saving the transaction modification record and the latest state change data at the same time.

[0109] Based on any of the above embodiments, the acquisition unit is specifically used for: Determining at least one state change triggered by the blockchain transaction; Get the state change data corresponding to each state change.

[0110] Based on any of the above embodiments, the storage unit is specifically used for: Generating a timestamp based on the location of the blockchain transaction in the blockchain; Generate a type identifier based on the data type of the state change data; Based on the timestamp, the type identifier, and the address in the state change data, a storage key corresponding to the state change data is generated.

[0111] A storage key corresponding to the state change data is generated based on the timestamp, the type identifier, and the address and service key in the state change data.

[0112] The timestamp is generated based on the block height of the block where the blockchain transaction is located and the transaction sequence number of the blockchain transaction in the block.

[0113] The data type is determined based on whether the business key in the state change data is empty and whether the value in the state change data is empty.

[0114] Fig.11 Schematic diagram of the structure of the data query device provided by the present invention. Fig.11 As shown, the device comprises: A query receiving unit 1110 is used to receive a query request, and determine, based on the query request, a location of a blockchain transaction to be queried in the blockchain, an address of state change data corresponding to the blockchain transaction, and a data type of the state change data; A query key determination unit 1120, configured to determine a query key based on a position of the blockchain transaction to be queried in the blockchain, an address of the state change data corresponding to the blockchain transaction, and a data type of the state change data; The query unit 1130 is used to query the database for the value of the state change data corresponding to each blockchain transaction stored in the form of a key-value pair based on the query key.

[0115] The data query device provided by the embodiment of the present invention adopts a transaction-granular query method to avoid the complex process of traversing multiple intermediate nodes in the traditional method. The data query directly locates the state change data to be queried based on the query key. This method improves query efficiency, reduces query delay, improves query efficiency, and makes query operations more flexible and efficient.

[0116] Based on any of the above embodiments, the query key determination unit is specifically used for: The data type of the state change data is determined based on whether the business key in the query request is empty.

[0117] In a case where the position of the blockchain transaction to be queried in the blockchain includes the block height of the block where the blockchain transaction is located and the transaction sequence number of the blockchain transaction in the block, generating the timestamp based on the block height and the transaction sequence number; In the case where the position of the blockchain transaction to be queried in the blockchain only includes the block height of the block at the blockchain exchange, the timestamp is generated based on the block height and the total number of transactions in the block at the blockchain exchange.

[0118] The querying, based on the query key, in the database for the value of the state change data corresponding to each blockchain transaction stored in the form of a key-value pair includes: Performing a range query in a database based on the query key prefix to obtain a key-value pair that matches the query key prefix; From the key-value pairs that match the query key prefix, candidate key-value pairs whose timestamps are before the timestamp of the query key are screened out, and the value of the candidate key-value pair with the largest timestamp is used as the query result.

[0119] Based on any of the above embodiments, the query unit is specifically used for: The performing a range query in the database based on the query key prefix to obtain a key-value pair matching the query key prefix includes: Using the deletion type identifier and the address as keys, searching the database for a key-value pair of the deletion type; If there is a key-value pair of the deletion type, and the timestamp of the key-value pair of the deletion type is before the timestamp of the query key, determining that the query result is empty; Otherwise, a range query is performed in the database based on the query key prefix to obtain key-value pairs matching the query key prefix.

[0120] Fig.12 An example of a physical structure diagram of an electronic device is shown in FIG. Fig.12 As shown, the electronic device may include: a processor 1210, a communication interface 1220, a memory 1230 and a communication bus 1240, wherein the processor 1210, the communication interface 1220, and the memory 1230 communicate with each other through the communication bus 1240. The processor 1210 may call the logic instructions in the memory 1230 to execute the data storage method or the data query method. The data storage method includes: Get the state change data corresponding to the blockchain transaction; Based on the position of the blockchain transaction in the blockchain, the data type of the state change data, and the address in the state change data, determine the storage key corresponding to the state change data, and store the storage key and the numerical value in the state change data in the database in the form of a key-value pair.

[0121] Data query methods include: Receive a query request, and determine, based on the query request, a location of a blockchain transaction to be queried in the blockchain, an address of state change data corresponding to the blockchain transaction, and a data type of the state change data; Determine a query key based on the position of the blockchain transaction to be queried in the blockchain, the address of the state change data corresponding to the blockchain transaction, and the data type of the state change data; Based on the query key, the value of the state change data corresponding to each blockchain transaction stored in the form of a key-value pair is queried in the database.

[0122] In addition, the logic instructions in the above-mentioned memory 1230 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention can be essentially or partly embodied in the form of a software product that contributes to the relevant technology. The computer software product is stored in a storage medium, including several instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.

[0123] On the other hand, the present invention further provides a computer program product, the computer program product includes a computer program, the computer program can be stored on a non-transitory computer-readable storage medium, and when the computer program is executed by a processor, the computer can execute the data storage method or data query method provided by the above methods. The data storage method includes: Get the state change data corresponding to the blockchain transaction; Based on the position of the blockchain transaction in the blockchain, the data type of the state change data, and the address in the state change data, determine the storage key corresponding to the state change data, and store the storage key and the numerical value in the state change data in the database in the form of a key-value pair.

[0124] Data query methods include: Receive a query request, and determine, based on the query request, a location of a blockchain transaction to be queried in the blockchain, an address of state change data corresponding to the blockchain transaction, and a data type of the state change data; Determine a query key based on the position of the blockchain transaction to be queried in the blockchain, the address of the state change data corresponding to the blockchain transaction, and the data type of the state change data; Based on the query key, the value of the state change data corresponding to each blockchain transaction stored in the form of a key-value pair is queried in the database.

[0125] In another aspect, the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, which is implemented when the computer program is executed by a processor to perform the data storage method or data query method provided by the above methods. The data storage method includes: Get the state change data corresponding to the blockchain transaction; Based on the position of the blockchain transaction in the blockchain, the data type of the state change data, and the address in the state change data, determine the storage key corresponding to the state change data, and store the storage key and the numerical value in the state change data in the database in the form of a key-value pair.

[0126] Data query methods include: Receive a query request, and determine, based on the query request, a location of a blockchain transaction to be queried in the blockchain, an address of state change data corresponding to the blockchain transaction, and a data type of the state change data; Determine a query key based on the position of the blockchain transaction to be queried in the blockchain, the address of the state change data corresponding to the blockchain transaction, and the data type of the state change data; Based on the query key, the value of the state change data corresponding to each blockchain transaction stored in the form of a key-value pair is queried in the database.

[0127] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0128] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the relevant technology can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiment.

[0129] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A data storage method, characterized in that: include: Get the state change data corresponding to the blockchain transaction; Based on the position of the blockchain transaction in the blockchain, the data type of the state change data, and the address in the state change data, determine the storage key corresponding to the state change data, and store the storage key and the numerical value in the state change data in the database in the form of a key-value pair.

2. The data storage method according to claim 1, characterized in that: The determining, based on the position of the blockchain transaction in the blockchain, the data type of the state change data, and the address in the state change data, a storage key corresponding to the state change data includes: Generating a timestamp based on the location of the blockchain transaction in the blockchain; Generate a type identifier based on the data type of the state change data; Based on the timestamp, the type identifier, and the address in the state change data, a storage key corresponding to the state change data is generated.

3. The data storage method according to claim 2, characterized in that: In the case where the data type is business data, generating a storage key corresponding to the state change data based on the timestamp, the type identifier, and the address in the state change data includes: A storage key corresponding to the state change data is generated based on the timestamp, the type identifier, and the address and service key in the state change data.

4. The data storage method according to claim 2, characterized in that: The generating a timestamp based on the position of the blockchain transaction in the blockchain includes: The timestamp is generated based on the block height of the block where the blockchain transaction is located and the transaction sequence number of the blockchain transaction in the block.

5. The data storage method according to any one of claims 1 to 4, characterized in that: The determination of the data type includes: The data type is determined based on whether the business key in the state change data is empty and whether the value in the state change data is empty.

6. The data storage method according to any one of claims 1 to 4, characterized in that: The obtaining of state change data corresponding to a blockchain transaction includes: Determining at least one state change triggered by the blockchain transaction; Get the state change data corresponding to each state change.

7. A data query method, characterized in that: include: Receive a query request, and determine, based on the query request, a location of a blockchain transaction to be queried in the blockchain, an address of state change data corresponding to the blockchain transaction, and a data type of the state change data; Determine a query key based on the position of the blockchain transaction to be queried in the blockchain, the address of the state change data corresponding to the blockchain transaction, and the data type of the state change data; Based on the query key, the value of the state change data corresponding to each blockchain transaction stored in the form of a key-value pair is queried in the database.

8. The data query method according to claim 7, characterized in that: The determination of the data type of the state change data includes: The data type of the state change data is determined based on whether the business key in the query request is empty.

9. The data query method according to claim 7, characterized in that: The query key includes a timestamp, and the determination of the timestamp includes: In a case where the position of the blockchain transaction to be queried in the blockchain includes the block height of the block where the blockchain transaction is located and the transaction sequence number of the blockchain transaction in the block, generating the timestamp based on the block height and the transaction sequence number; In the case where the position of the blockchain transaction to be queried in the blockchain only includes the block height of the block at the blockchain exchange, the timestamp is generated based on the block height and the total number of transactions in the block at the blockchain exchange.

10. The data query method according to claim 7, characterized in that: The query key includes a query key prefix and a timestamp; The querying, based on the query key, in the database for the value of the state change data corresponding to each blockchain transaction stored in the form of a key-value pair includes: Performing a range query in a database based on the query key prefix to obtain a key-value pair that matches the query key prefix; From the key-value pairs that match the query key prefix, candidate key-value pairs whose timestamps are before the timestamp of the query key are screened out, and the value of the candidate key-value pair with the largest timestamp is used as the query result.

11. The data query method according to claim 10, characterized in that: The query key prefix includes the address of the state change data corresponding to the blockchain transaction; The performing a range query in the database based on the query key prefix to obtain a key-value pair matching the query key prefix includes: Using the deletion type identifier and the address as keys, searching the database for a key-value pair of the deletion type; If there is a key-value pair of the deletion type, and the timestamp of the key-value pair of the deletion type is before the timestamp of the query key, determining that the query result is empty; Otherwise, a range query is performed in the database based on the query key prefix to obtain key-value pairs matching the query key prefix.

12. A data storage device, characterized in that: include: An acquisition unit, used to acquire state change data corresponding to blockchain transactions; A storage unit is used to determine a storage key corresponding to the state change data based on the position of the blockchain transaction in the blockchain, the data type of the state change data, and the address in the state change data, and store the storage key and the value in the state change data in the database in the form of a key-value pair.

13. A data query device, characterized in that: include: A query receiving unit, configured to receive a query request, and determine, based on the query request, a location of a blockchain transaction to be queried in the blockchain, an address of state change data corresponding to the blockchain transaction, and a data type of the state change data; A query key determination unit, configured to determine a query key based on a position of the blockchain transaction to be queried in the blockchain, an address of the state change data corresponding to the blockchain transaction, and a data type of the state change data; The query unit is used to query the value of the state change data corresponding to each blockchain transaction stored in the form of a key-value pair in the database based on the query key.

14. An electronic device comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that: When the processor executes the program, the data storage method according to any one of claims 1 to 6 or the data query method according to any one of claims 7 to 11 is implemented.

15. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the data storage method according to any one of claims 1 to 6 or the data query method according to any one of claims 7 to 11 is implemented.

16. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the data storage method according to any one of claims 1 to 6 or the data query method according to any one of claims 7 to 11 is implemented.