A blockchain data query method and device, electronic equipment and storage medium

By determining the target block range in the blockchain and generating intermediate data, the problem of low data query efficiency in blockchain is solved, and efficient data query is achieved.

CN116628021BActive Publication Date: 2026-06-26ZHEJIANG UNIV

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
ZHEJIANG UNIV
Filing Date
2023-04-28
Publication Date
2026-06-26

AI Technical Summary

Technical Problem

Existing blockchain technologies suffer from low data query efficiency, especially given the complex and heterogeneous underlying data structure of Ethereum, which makes it difficult to quickly analyze and collect data.

Method used

In the blockchain, the target block range is determined, initial data is obtained through rollback detection, intermediate data is generated, and after correlation, it is imported into the in-memory database to improve query efficiency.

Benefits of technology

By adding intermediate data, the need to query complex initial data is reduced, making it suitable for refined query scenarios and improving data query efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116628021B_ABST
    Figure CN116628021B_ABST
Patent Text Reader

Abstract

The application provides a blockchain data query method and device, electronic equipment and a storage medium. The method comprises the following steps: determining a target block interval in the case that a new block is generated in a blockchain, wherein the target block interval comprises blocks that may cause a fork and rollback; determining initial data in each block in the target block interval; analyzing the initial data according to a preset condition to generate intermediate data; associating the initial data and the intermediate data corresponding to the same block in the target block interval to obtain target data; and importing the target data into an in-memory database to enable a decentralized application to query data in the in-memory database. The application improves the data query efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of blockchain technology, and in particular to a blockchain data query method, apparatus, electronic device, and storage medium. Background Technology

[0002] With the continuous development of blockchain technology and the arrival of the Web3.0 era, decentralized applications have been deployed on the blockchain. When users interact with smart contracts deployed on the chain, the relevant logs and parameters are recorded on the Ethereum target receipt tree. However, due to the complexity and heterogeneity of Ethereum's underlying data structure, the existing collected data is difficult to analyze quickly, resulting in low data query efficiency. Summary of the Invention

[0003] To address the problem of low data query efficiency in existing technologies, this invention proposes a blockchain data query method, device, electronic device, and storage medium. The specific technical solution is as follows:

[0004] A blockchain data query method, the method comprising:

[0005] In the event that a new block is generated in the blockchain, a target block range is determined, wherein the target block range includes blocks that may generate a fork that would lead to a rollback;

[0006] Determine the initial data in each block within the target block interval;

[0007] The initial data is parsed according to preset conditions to generate intermediate data;

[0008] The initial data and intermediate data corresponding to the same block within the target block interval are associated to obtain the target data;

[0009] The target data is imported into an in-memory database so that decentralized applications can query the in-memory database.

[0010] Further, determining the initial data in each block within the target block interval includes:

[0011] By performing a rollback detection on the new block, it can be determined whether a rollback has occurred;

[0012] If it is determined that no rollback has occurred, the original data on the blocks within the target block interval will be used as the initial data.

[0013] If it is determined that a rollback has occurred, the data on the rolled-back blocks within the target block interval will be used as the initial data.

[0014] Furthermore, if it is determined that no rollback has occurred, the original data on the blocks within the target block interval will be used as the initial data, including:

[0015] If it is determined that no rollback has occurred, then each thread in the thread pool calls the event index of an event in the blockchain.

[0016] Log information is extracted from the target receipt tree of Ethereum through the event index, wherein the target receipt tree is used to record transaction-related information on blocks within the target block interval;

[0017] After byte alignment and encoding of the log information, raw data in a standard format is obtained;

[0018] The original data corresponding to each event is used as the initial data.

[0019] Furthermore, before invoking the event index of an event in the blockchain using each thread in the thread pool, the method further includes:

[0020] The smart contracts in the blockchain are compiled and converted into data files in a standard serialization format;

[0021] The events in the blockchain are obtained by analyzing the bytecode in the data file;

[0022] Extract the event index of the event.

[0023] Furthermore, if it is determined that a rollback has occurred, the method further includes:

[0024] Block the listening thread and the thread corresponding to each event in the blockchain, and initiate a rollback correction operation.

[0025] Furthermore, determining the generation of new blocks in the blockchain includes:

[0026] Start a listening thread and remotely connect to an RPC node, wherein the RPC node is used to request services from a remote computer program over a network;

[0027] The listening thread polls each block in the blockchain to determine the height of the latest block in the blockchain.

[0028] If the height of the latest block is greater than the height of the historical blocks already stored in the database, then it is determined that a new block has been generated in the blockchain.

[0029] Furthermore, after importing the target data into the in-memory database, the method further includes:

[0030] If another rollback is detected, the association between the current initial data and intermediate data is stopped, while the query function of the in-memory database is retained.

[0031] After initiating the rollback correction operation, the initial data, intermediate data, and target data in each block within the target block interval are deleted, and the target data is regenerated based on the initial data after rollback.

[0032] The regenerated target data is imported into the in-memory database.

[0033] A blockchain data query device, the device comprising:

[0034] The first determining module is used to determine a target block range when a new block is generated in the determined blockchain, wherein the target block range includes blocks that may generate forks that lead to rollback;

[0035] The second determining module is used to determine the initial data in each block within the target block interval;

[0036] The parsing module is used to parse the initial data according to preset conditions and generate intermediate data;

[0037] The association module is used to associate the initial data and intermediate data corresponding to the same block within the target block interval to obtain the target data;

[0038] The import module is used to import the target data into the in-memory database so that the decentralized application can query the data in the in-memory database.

[0039] An electronic device includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus;

[0040] Memory, used to store computer programs;

[0041] The processor, when executing a program stored in memory, implements any of the steps of the blockchain data query method described above.

[0042] A computer-readable storage medium storing a computer program, which, when executed by a processor, implements any of the steps of the blockchain data query method described above.

[0043] The beneficial effects of this invention are:

[0044] This application provides a blockchain data query method. When a new block is detected in the blockchain, a target block range that may cause a crossover and rollback is determined. The initial and intermediate data in each block within the target block range are then identified. The initial and intermediate data are associated with the target data and imported into an in-memory database. By adding intermediate data, this application increases the data content in the in-memory database, eliminating the need to query complex initial data. This method is suitable for more refined query scenarios and improves data query efficiency.

[0045] Of course, implementing any product or method of this application does not necessarily require achieving all of the advantages described above at the same time. Attached Figure Description

[0046] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0047] Figure 1 This is a schematic diagram of the structure of a blockchain in a blockchain network provided in an embodiment of this application;

[0048] Figure 2 A flowchart illustrating a blockchain data query method provided in this application embodiment;

[0049] Figure 3 A flowchart of a blockchain data query provided in this application embodiment;

[0050] Figure 4 This is a schematic diagram of the structure of a blockchain data query device provided in an embodiment of this application;

[0051] Figure 5 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation

[0052] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0053] In the following description, reference is made to "one specific embodiment," which describes a subset of all possible embodiments; however, it is understood that "one specific embodiment" describes the same or different subsets of all possible embodiments and may be combined with each other without conflict.

[0054] Unless otherwise defined, all techniques used herein have the same scientific or technical meaning as commonly understood by one of ordinary skill in the art to which this invention pertains. The terminology used herein is for the purpose of describing embodiments of the invention only and is not intended to limit the invention.

[0055] Before providing a further detailed description of the embodiments of the present invention, the nouns and terms involved in the embodiments of the present invention will be explained, and the nouns and terms involved in the embodiments of the present invention shall be subject to the following interpretations.

[0056] (1) Transaction, which is equivalent to the computer term "transaction", includes operations that need to be submitted to the blockchain network for execution. It does not refer solely to transactions in a business context. Given that the term "transaction" is commonly used in blockchain technology, this embodiment of the invention follows this convention.

[0057] For example, a Deploy transaction is used to install a specified smart contract on a node in the blockchain network and prepare it for invocation; an Invoke transaction is used to append a record of a transaction to the blockchain by invoking a smart contract and to operate on the blockchain's state database, including update operations (including adding, deleting, and modifying key-value pairs in the state database) and query operations (i.e., querying key-value pairs in the state database).

[0058] (2) Blockchain is an encrypted, chain-like storage structure for transactions formed by blocks.

[0059] (3) Blockchain Network is a set of nodes that incorporate new blocks into the blockchain through consensus.

[0060] (4) Smart Contracts, also known as Chaincode or Application Code, are programs deployed in nodes of a blockchain network. Nodes execute smart contracts called in received transactions to update or query key-value pairs in the state database.

[0061] To explain this application more clearly, we will first briefly introduce the working principle of smart contracts:

[0062] Building smart contracts: Smart contracts are jointly developed by multiple users within the blockchain and can be used for any transaction between any users. The protocol clearly defines the rights and obligations of both parties to the transaction. Developers program these rights and obligations electronically, and the code contains conditions that trigger the automatic execution of the contract.

[0063] Storing smart contracts: Once the code is completed, the smart contract is uploaded to the blockchain network, meaning that every node on the network can receive the smart contract.

[0064] Executing smart contracts: Smart contracts periodically check for relevant events and triggering conditions. Events that meet the conditions are pushed to a queue for verification. Verification nodes on the blockchain first verify the event by signing it to ensure its validity. Once most verification nodes reach a consensus on the event, the smart contract will be successfully executed and the user will be notified.

[0065] As an example of blockchain, such as Figure 1 As shown, Figure 1 This is a schematic diagram of the blockchain structure in the blockchain network provided by the embodiments of the present invention. The header of each block can include the hash values ​​of all transactions in the block, as well as the hash values ​​of all transactions in the previous block. The record of a newly generated transaction is filled into the block and, after consensus among the nodes in the blockchain network, is appended to the tail of the blockchain to form a chain-like growth. The chain structure between blocks based on hash values ​​ensures the tamper-proof and anti-forgery properties of the transactions in the block.

[0066] Based on the above architecture, this invention proposes a blockchain data query method, applied to blockchain, to improve data query efficiency. Figure 2 As shown, the specific steps include the following:

[0067] Step 201: Determine the target block range if a new block is determined to be generated in the blockchain, wherein the target block range includes blocks that may generate a fork that could lead to a rollback.

[0068] The blockchain initiates a listening thread that remotely connects to an RPC (Remote Procedure Call) node. The RPC node is used to request services from a remote computer program over the network. The listening thread polls each block in the blockchain in real time to determine the height of the latest block and compares it with the historical block heights stored in the database. If the latest block height equals the historical block height, it indicates that no new block has been generated; if the latest block height is greater than the historical block height, it indicates that a new block has been generated in the blockchain.

[0069] The latest block is uncertain whether it will eventually be written into the blockchain. It may fork and roll back a certain number of blocks. The target block range consists of multiple blocks between the latest block and the blocks that will not fork. The target block range includes the blocks that may fork and cause rollback.

[0070] Step 202: Determine the initial data in each block within the target block range.

[0071] Regardless of whether a rollback occurs with the new block, it is necessary to determine the initial data in each block within the target block range and store the initial data in the first storage layer. Specifically, if no rollback occurs, the initial data is the original data in the block; if a rollback has occurred, the initial data is the data in the block after the rollback.

[0072] Step 203: Parse the initial data according to preset conditions to generate intermediate data.

[0073] Blockchain determines preset conditions based on the actual scenario, and then parses the initial data according to these preset conditions to generate a snapshot of intermediate data, which is then stored in the second storage layer. For example, if the application scenario is online shopping, and the preset conditions are to determine the current latest price and ex-factory price from the product price, then the initial data consists of various product data, such as purchase rate, click rate, ex-factory price, historical selling price, etc., while the intermediate data consists of the current latest price and ex-factory price.

[0074] For example, if the application scenario is children's health and the preset condition is to determine the cause of children's illness, then the initial data would be the air quality of the children's living space, their diet, family education, etc., and the intermediate data would be the cause of the children's illness.

[0075] Step 204: Associate the initial data and intermediate data corresponding to the same block within the target block range to obtain the target data.

[0076] The target block range includes multiple blocks, each with a block number. The blockchain associates and aggregates the initial data and intermediate data with the same block number to obtain the target data, and then stores the target data in the third storage layer.

[0077] Step 205: Import the target data into the in-memory database so that the decentralized application can query the data in the in-memory database.

[0078] The blockchain imports target data into an in-memory database, providing it to decentralized applications for data querying. For example, the in-memory database can be Redis, but other types of databases can also be used; this application does not impose specific limitations. Decentralized applications are primarily Web 3.0 applications; they do not have a backend, but their frontend interacts with the blockchain.

[0079] In this application, when a new block is detected in the blockchain, the target block range that may lead to a crossover and subsequent rollback is determined. The initial and intermediate data within each block of the target block range are then identified, and the initial and intermediate data are associated with the target data before being imported into an in-memory database. By adding intermediate data, this application increases the data content in the in-memory database, eliminating the need to query complex initial data. This approach is suitable for more refined query scenarios and improves the efficiency of querying the data to be queried.

[0080] As an optional implementation, determining the initial data in each block within the target block interval includes: determining whether a rollback has occurred by performing a rollback detection on the new block; if it is determined that no rollback has occurred, then the original data on the blocks within the target block interval is used as the initial data; if it is determined that a rollback has occurred, then the data on the rolled-back blocks within the target block interval is used as the initial data.

[0081] This application proposes a verification and error correction mechanism based on block header hashes, which performs rollback verification in response to possible blockchain rollbacks, forks, and other anomalies. In a blockchain, it is uncertain whether the most recently generated block will ultimately be written; therefore, rollback checks are performed on each new block to determine if a rollback has occurred. Specifically, this can be done by comparing the hash values ​​of confirmed blocks recorded in the database with the hash value of the current blockchain block to determine whether a rollback has occurred.

[0082] If it is determined that no rollback has occurred, it indicates that the original data on the blocks within the target block range is correct. Then, the smart contract parser performs SOIC automated compilation on the Solidity source file in the smart contract to obtain the ABI file. The ABI file is then parsed to obtain the contract event set, and the original data in the contract event set is used as the initial data.

[0083] If a rollback is confirmed, the data in the rolled-back block will be correct, and this data will be used as the initial data. During a block rollback, all data is contaminated. In this case, intermediate data is generated based on the correct on-chain initial data, and then the final target data is generated to provide query functionality.

[0084] As an optional implementation, the smart contracts in the blockchain are compiled and converted into data files in a standard serialization format; the bytecode in the data files is analyzed to obtain the events in the blockchain; and the event indexes of the events are extracted.

[0085] In this embodiment, the blockchain compiles the smart contract and converts it into a JSON data file in a standard serialization format. The smart contract on the chain can use various programming languages, such as Solidity and Java. For smart contracts built in different languages, the blockchain analyzes the bytecode in the data file to obtain detailed events in the blockchain, i.e., on-chain events. Then, the event function, name, and parameters are passed in to obtain a byte array, which is encrypted using SHA256 to obtain the event index topic[0]. For example, the event index of the event itself is often the method name plus the parameter type, which can be extracted through a unified data processor.

[0086] If the blockchain determines that no rollback has occurred, each thread in the thread pool calls the event index of an event in the blockchain. The log information in the target receipt tree of Ethereum is extracted through the event index. The target receipt tree is used to record transaction-related information on blocks within the target block range. After the blockchain performs byte alignment and encoding on the log information, it obtains raw data in a standard format. Finally, the raw data corresponding to each event is used as the initial data.

[0087] Once a rollback is confirmed in the blockchain, the listening thread and the thread corresponding to each event in the blockchain are blocked. Listening and event retrieval operations cease; only the rollback correction operation is initiated to roll back the block. This application achieves highly efficient real-time data collection and improves data synchronization efficiency by employing a multi-threaded mechanism for event retrieval.

[0088] As an optional implementation, after importing the target data into the in-memory database, the method further includes: if a rollback is detected again, stopping the association of the current initial data and intermediate data, while retaining the query function of the in-memory database; after initiating the rollback correction operation, deleting the initial data, intermediate data, and target data in each block within the target block interval, and regenerating the target data based on the rolled-back initial data; and importing the regenerated target data into the in-memory database.

[0089] When a block rollback is detected, the following operations are performed: the association between initial and intermediate data is stopped, and the calculation results of the first two caching layers are no longer aggregated to generate target data. However, the third caching layer still provides target data to the front end for query services. The rollback correction thread is triggered, at which point the initial and intermediate data between the rollback block header and the latest block header are deleted, and the initial data is re-acquired. Subsequently, the third caching layer is deleted, and the target data is regenerated after the rollback is complete. Finally, the newly generated target data replaces the erroneous target data, and services are resumed for decentralized applications.

[0090] This application can update the target data in the in-memory database in a timely manner by detecting the generation of new blocks in real time and performing rollback operations, so that the data that should be queried in the decentralized system is more accurate.

[0091] Simply storing on-chain event logs is insufficient; when a block rollback occurs, erroneous data cannot be corrected in time, blocking query services. Therefore, the initial data is stored as a first-layer cache, followed by intermediate data obtained from secondary calculations stored as a data snapshot as a second-layer cache, linked by block number. The intermediate and initial data are then aggregated and stored as a third-layer cache, which is provided to decentralized storage services. This application designs a three-layer storage data structure to ensure data fault tolerance in decentralized applications, providing an efficient and reliable guarantee for data storage.

[0092] Based on the same concept, this application also provides a blockchain data query flowchart in its embodiments, such as... Figure 3 As shown, the main thread includes a listening thread, a block rollback detection thread, and an event retrieval thread.

[0093] The blockchain acquires a remote RPC node instance and starts a listening thread to poll each block in the blockchain. If a block update is detected, a block rollback detection thread is started to determine if a rollback has occurred. If a rollback has occurred, rollback correction is initiated, and the main thread is blocked. The initial data in each block within the target block range is obtained based on the rollback. If no rollback has occurred, the original data in each block within the target block range is used as the initial data. Multi-threaded processing of the initial data on the chain is started to obtain a snapshot of the intermediate data. The initial data and intermediate data are associated through the block number to obtain the target data. The target data is stored in an in-memory database for querying by decentralized applications.

[0094] This application first parses the bytecode of the on-chain smart contract to extract the events that need to be monitored. Then, it polls each block, and upon detecting a newly generated block, it performs a rollback verification. After verification, a multi-threaded asynchronous mechanism is used to store the data in real-time in a layered manner. This method designs a multi-layered storage structure and improves the query efficiency of decentralized application data by synchronously monitoring blocks through multiple threads.

[0095] This application also provides a blockchain data query device. The specific implementation of this device can be found in the description of the method embodiments section; repeated details will not be repeated here. Figure 4 As shown, the device mainly includes:

[0096] The first determining module 401 is used to determine a target block range when a new block is generated in the determined blockchain, wherein the target block range includes blocks that may generate a fork and cause a rollback;

[0097] The second determining module 402 is used to determine the initial data in each block within the target block interval;

[0098] Parsing module 403 is used to parse the initial data according to preset conditions and generate intermediate data;

[0099] The association module 404 is used to associate the initial data and intermediate data corresponding to the same block within the target block range to obtain the target data;

[0100] Import module 405 is used to import target data into the in-memory database so that decentralized applications can query the data in the in-memory database.

[0101] Optionally, the second determining module 402 is used for:

[0102] By performing rollback detection on the new block, it can be determined whether a rollback has occurred;

[0103] If it is determined that no rollback has occurred, the original data in the blocks within the target block range will be used as the initial data.

[0104] If a rollback is confirmed, the data in the rolled-back blocks within the target block range will be used as the initial data.

[0105] Optionally, the second determining module 402 is used for:

[0106] If it is determined that no rollback has occurred, then each thread in the thread pool calls the event index of an event in the blockchain;

[0107] Log information is extracted from the Ethereum target receipt tree through the event index. The target receipt tree is used to record transaction-related information on blocks within the target block range.

[0108] After byte alignment and encoding of the log information, the raw data in standard format is obtained;

[0109] Use the original data corresponding to each event as the initial data.

[0110] Optionally, the device is also used for:

[0111] Compile smart contracts in the blockchain and convert them into data files in a standard serialization format;

[0112] Analyze the bytecode in the data file to obtain the events in the blockchain;

[0113] Extract the event index of the event.

[0114] Optionally, the second determining module 402 is used for:

[0115] Block the listening thread and the thread corresponding to each event in the blockchain, and initiate a rollback correction operation.

[0116] Optionally, the first determining module 401 is used for:

[0117] Start a listening thread and remotely connect to the RPC node, where the RPC node is used to request services from a remote computer program over the network;

[0118] The height of the latest block in the blockchain is determined by polling each block in the blockchain through a listening thread.

[0119] If the height of the latest block is greater than the height of the historical blocks already stored in the database, then a new block is determined to be generated in the blockchain.

[0120] Optionally, the device is also used for:

[0121] If another rollback is detected, the association between the current initial data and intermediate data will be stopped, while the query function of the in-memory database will be retained.

[0122] After initiating the rollback correction operation, delete the initial data, intermediate data, and target data in each block within the target block range, and regenerate the target data based on the initial data after rollback.

[0123] Import the regenerated target data into the in-memory database.

[0124] Based on the same concept, this application also provides an electronic device, such as... Figure 5As shown, the electronic device mainly includes: a processor 501, a communication component 502, a memory 503, and a communication bus 504. The processor 501, the communication component 502, and the memory 503 communicate with each other through the communication bus 504. The memory 503 stores programs that can be executed by the processor 501, and the processor 501 executes the programs stored in the memory 503.

[0125] The communication bus 504 mentioned in the above electronic device can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus 504 can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 5 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.

[0126] The communication component 502 is used for communication between the above-mentioned electronic device and other devices.

[0127] The memory 503 may include random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Optionally, the memory may also be at least one storage device located remotely from the aforementioned processor 501.

[0128] The processor 501 mentioned above can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc., or a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.

[0129] In another embodiment of this application, a computer-readable storage medium is provided, which stores a computer program that, when run on a computer, causes the computer to perform the blockchain data query method steps described in the above embodiments.

[0130] In the above embodiments, implementation can be achieved, in whole or in part, through software, hardware, firmware, or any combination thereof. When implemented in software, it can be implemented, in whole or in part, as a computer program product. This computer program product includes one or more computer instructions. When these computer instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of this application are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another, for example, computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, microwave, etc.) means. The computer-readable storage medium can be any available medium accessible to a computer or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape, etc.), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid-state drive), etc.

[0131] It should be noted that, in this document, relational terms such as "first" and "second" are used merely to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0132] The above description is merely a specific embodiment of the present invention, enabling those skilled in the art to understand or implement the invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the invention. Therefore, the present invention is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features claimed herein.

Claims

1. A blockchain data query method, characterized in that, The method includes: In the event that a new block is generated in the blockchain, a target block range is determined, wherein the target block range includes blocks that may generate a fork that would lead to a rollback; Determine the initial data in each block within the target block interval; The initial data is parsed according to preset conditions to generate intermediate data; The initial data and intermediate data corresponding to the same block within the target block interval are associated to obtain the target data; The target data is imported into an in-memory database so that decentralized applications can query the data in the in-memory database. Initial data is stored in the first storage layer, intermediate data in the second storage layer, and target data in the third storage layer; the third-layer cache is provided for decentralized storage services to access.

2. The blockchain data query method according to claim 1, characterized in that, Determining the initial data in each block within the target block interval includes: By performing a rollback detection on the new block, it can be determined whether a rollback has occurred; If it is determined that no rollback has occurred, the original data on the blocks within the target block interval will be used as the initial data. If it is determined that a rollback has occurred, the data on the rolled-back blocks within the target block interval will be used as the initial data.

3. The blockchain data query method according to claim 2, characterized in that, If it is determined that no rollback has occurred, then the original data on the blocks within the target block interval will be used as the initial data, including: If it is determined that no rollback has occurred, then each thread in the thread pool calls the event index of an event in the blockchain. Log information is extracted from the target receipt tree of Ethereum through the event index, wherein the target receipt tree is used to record transaction-related information on blocks within the target block interval; After byte alignment and encoding of the log information, raw data in a standard format is obtained; The original data corresponding to each event is used as the initial data.

4. The blockchain data query method according to claim 3, characterized in that, Before invoking the event index of an event in the blockchain using each thread in the thread pool, the method further includes: The smart contracts in the blockchain are compiled and converted into data files in a standard serialization format; The events in the blockchain are obtained by analyzing the bytecode in the data file; Extract the event index of the event.

5. The blockchain data query method according to claim 2, characterized in that, If it is determined that a rollback has occurred, the method further includes: Block the listening thread and the thread corresponding to each event in the blockchain, and initiate a rollback correction operation.

6. The blockchain data query method according to claim 1, characterized in that, Determining the generation of a new block in the blockchain includes: Start a listening thread and remotely connect to an RPC node, wherein the RPC node is used to request services from a remote computer program over a network; The listening thread polls each block in the blockchain to determine the height of the latest block in the blockchain. If the height of the latest block is greater than the height of the historical blocks already stored in the database, then a new block is determined to have been generated in the blockchain.

7. The blockchain data query method according to claim 1, characterized in that, After importing the target data into the in-memory database, the method further includes: If another rollback is detected, the association between the current initial data and intermediate data is stopped, while the query function of the in-memory database is retained. After initiating the rollback correction operation, the initial data, intermediate data, and target data in each block within the target block interval are deleted, and the target data is regenerated based on the initial data after rollback. The regenerated target data is imported into the in-memory database.

8. A blockchain data query device, characterized in that, The device includes: The first determining module is used to determine a target block range when a new block is generated in the determined blockchain, wherein the target block range includes blocks that may generate forks that lead to rollback; The second determining module is used to determine the initial data in each block within the target block interval; The parsing module is used to parse the initial data according to preset conditions and generate intermediate data; The association module is used to associate the initial data and intermediate data corresponding to the same block within the target block interval to obtain the target data; The import module is used to store initial data in the first storage layer, intermediate data in the second storage layer, and import the target data into the third storage layer of the in-memory database, so that the decentralized application can query the in-memory database.

9. An electronic device, characterized in that, It includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, when executing a program stored in memory, implements the method described in any one of claims 1-7.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the method described in any one of claims 1-7.