On-chain and off-chain collaborative method and system of blockchain data, terminal and storage medium
By configuring a caching service in the blockchain system for hash value verification and data synchronization, the problem of inconsistency between on-chain and off-chain data is solved, ensuring the correctness and integrity of off-chain data and improving data query efficiency and analysis capabilities.
Patent Information
- Application Number
- CN202310268323.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-15
- Publication Date
- 2026-02-24
- Estimated Expiration
- 2043-03-15
AI Technical Summary
Existing technologies suffer from data loss, corruption, and inconsistency issues in on-chain and off-chain data collaborative processing, leading to data distortion and affecting subsequent analysis results.
The system receives blockchain data through a caching service and performs hash value verification to ensure data consistency. It also configures different modes for data synchronization and verification, detects abnormal nodes and generates alarms, and ensures the integrity and consistency of data in the off-chain database.
It achieves efficient synchronization of on-chain data to off-chain databases, ensuring data accuracy and integrity, reducing the impact on blockchain performance, and supporting the energy blockchain analysis needs of large data volumes.
Smart Images

Figure CN117290436B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of blockchain technology, specifically relating to an on-chain and off-chain collaborative method, system, terminal, and storage medium for blockchain data. Background Technology
[0002] Blockchain, through the integration of various technologies such as distributed data storage, peer-to-peer transmission, consensus mechanisms, and encryption algorithms, features encrypted and immutable data storage, and traceable operation records. It can effectively address data fraud during the transfer process in traditional transaction models, thereby building a trustworthy transaction environment and a trustworthy society. Blockchain has broad application potential in the energy sector. Through blockchain technology, distributed energy resource data can be better coordinated, data ownership can be effectively established, risk management can be facilitated, resource utilization can be significantly improved, and operating costs can be reduced.
[0003] Due to the large volume, complex data models, and lack of standardized criteria, storing energy data solely on-chain often results in low query efficiency, especially for complex queries. This significantly impacts the overall performance of the blockchain and fails to meet normal query and data analysis needs. Current on-chain and off-chain data collaborative processing mechanisms simply export data to an off-chain database for storage.
[0004] When importing on-chain data into off-chain storage, issues such as data loss or corruption may occur during data processing, or historical data in off-chain storage may become inconsistent with on-chain data due to improper processing. This can lead to data distortion, which will severely affect subsequent analysis results. Summary of the Invention
[0005] To address the discrepancies between on-chain and off-chain data caused by the inability of existing technologies to coordinate on-chain and off-chain data, this invention provides a method, system, terminal, and storage medium for on-chain and off-chain data coordination in the blockchain field, thereby resolving the aforementioned technical problems.
[0006] In a first aspect, the present invention provides an on-chain and off-chain collaboration method for blockchain data, comprising:
[0007] In the first mode, the caching service receives data pushed by the blockchain, parses the data into a block structure, performs hash value verification on each block of data, and writes the data that passes the verification into the off-chain database.
[0008] In the second mode, the caching service periodically fetches data from the blockchain. By comparing the maximum block height of the off-chain database with the maximum block height of the blockchain, if the two are inconsistent, it fetches the difference data from the blockchain and synchronizes the difference data to the off-chain database after verifying the hash value.
[0009] Furthermore, in the first mode, the caching service receives data pushed from the blockchain, parses the data into a block structure, performs hash value verification on each block, and writes the verified data into an off-chain database, including:
[0010] Configure the working mode of the cache service to the first mode and configure the maximum block transmission range. In the first mode, the cache service receives data pushed by the blockchain, and the data is within the maximum transmission range.
[0011] The caching service parses the received data into a block structure;
[0012] Obtain the block header from the block structure, and extract the first hash value from the block header;
[0013] Calculate the actual hash value of the data block corresponding to the first hash value as the second hash value, and compare the consistency between the first hash value and the second hash value;
[0014] If the first hash value matches the second hash value, the data is written to the off-chain database.
[0015] If the first hash value and the second hash value are inconsistent, the blockchain node that sent the data will be identified as an abnormal node, and an alarm message for the abnormal node will be generated.
[0016] Furthermore, in the second mode, the caching service periodically fetches data from the blockchain. It compares the maximum block height of the off-chain database with the maximum block height of the blockchain. If they are inconsistent, it fetches the difference data from the blockchain and synchronizes it to the off-chain database after hash verification. This includes:
[0017] Configure the cache service to the second working mode and configure the time interval and the number of blocks to be pulled so that the cache service periodically pulls the corresponding amount of data from the blockchain.
[0018] Periodically query the maximum block height in the off-chain database and update the latest height to the cached record file;
[0019] If the maximum block height of the blockchain is greater than the maximum block height of the record file, then calculate the target number of blocks to be synchronized in this round based on the configured number of blocks to be pulled.
[0020] Send a data synchronization request to the blockchain node to retrieve the target number of blocks.
[0021] Furthermore, when writing data to the off-chain database, the method also includes:
[0022] The data is parsed into the storage structure required by the off-chain database and then asynchronously stored in the database.
[0023] Rewrite data blocks that failed to be written to the off-chain database.
[0024] Furthermore, the method also includes:
[0025] Periodically retrieve data from the off-chain database and parse the first key field from it;
[0026] Parse the second key field from the corresponding data in the blockchain;
[0027] If the first key field does not match the second key field, the data obtained in this round is determined to be abnormal data.
[0028] Secondly, the present invention provides an on-chain and off-chain collaborative system for blockchain data, comprising:
[0029] The first collaborative unit is used to receive data pushed by the blockchain in the first mode, parse the data into a block structure, perform hash value verification on each block data, and write the data that passes the verification into the off-chain database.
[0030] The second coordination unit is used in the second mode to periodically fetch data from the blockchain for caching services. By comparing the maximum block height of the off-chain database with the maximum block height of the blockchain, if the two are inconsistent, the unit will fetch the difference data from the blockchain and synchronize the difference data to the off-chain database after hash value verification.
[0031] Furthermore, the first collaborative unit includes:
[0032] The first configuration module is used to configure the working mode of the cache service to the first mode and configure the maximum block transmission range. In the first mode, the cache service receives data pushed by the blockchain, and the data is within the maximum transmission range.
[0033] The data parsing module is used by the caching service to parse the received data into a block structure;
[0034] The hash extraction module is used to obtain the block header in the block structure and extract the first hash value from the block header;
[0035] The hash comparison module is used to calculate the actual hash value of the data block corresponding to the first hash value as the second hash value, and compare the consistency between the first hash value and the second hash value.
[0036] The data writing module is used to write the data into the off-chain database if the first hash value matches the second hash value.
[0037] The node alarm module is used to determine the blockchain node that sent the data as an abnormal node if the first hash value and the second hash value are inconsistent, and to generate an alarm message for the abnormal node.
[0038] Furthermore, the second collaborative unit includes:
[0039] The second configuration module is used to configure the working mode of the caching service to the second mode and configure the time interval and the number of blocks to be pulled, so that the caching service periodically pulls the corresponding amount of data from the blockchain.
[0040] The height caching module is used to periodically query the maximum block height of the off-chain database and update the latest height to the cached record file;
[0041] The height comparison module is used to obtain the maximum block height of the blockchain. If the maximum block height of the blockchain is greater than the maximum block height of the record file, the number of target blocks to be synchronized in this round is calculated based on the configured number of blocks to be pulled.
[0042] The data capture module is used to send data synchronization requests to blockchain nodes and capture data on the target number of blocks.
[0043] Furthermore, when writing data to the off-chain database, the system also performs the following:
[0044] The data is parsed into the storage structure required by the off-chain database and then asynchronously stored in the database.
[0045] Rewrite data blocks that failed to be written to the off-chain database.
[0046] Furthermore, the system also performs:
[0047] Periodically retrieve data from the off-chain database and parse the first key field from it;
[0048] Parse the second key field from the corresponding data in the blockchain;
[0049] If the first key field does not match the second key field, the data obtained in this round is determined to be abnormal data.
[0050] Thirdly, a terminal is provided, including:
[0051] Processor, memory, among which,
[0052] This memory is used to store computer programs.
[0053] The processor is used to retrieve and run the computer program from memory, causing the terminal to perform the terminal method described above.
[0054] Fourthly, a computer storage medium is provided, wherein instructions are stored therein, which, when executed on a computer, cause the computer to perform the methods described in the above aspects.
[0055] The beneficial effects of this invention are that the on-chain and off-chain collaborative method, system, terminal and storage medium for blockchain data provided by this invention can export on-chain data to off-chain database in real time without causing a major impact on the overall performance of the blockchain. It can also simply and effectively verify the correctness of the data, detect abnormal data and Byzantine nodes, ensure the correctness and integrity of the data, and verify the correctness and integrity of historical data stored off-chain. It can meet the basic requirements of large data volumes in energy blockchain.
[0056] Furthermore, the design principle of this invention is reliable, the structure is simple, and it has a very wide range of application prospects. Attached Figure Description
[0057] To more clearly illustrate the technical solutions in the embodiments of the present invention 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.
[0058] Figure 1 This is a schematic flowchart of a method according to an embodiment of the present invention.
[0059] Figure 2 This is an exemplary architecture diagram of a method according to an embodiment of the present invention.
[0060] Figure 3 This is another illustrative flowchart of a method according to an embodiment of the present invention.
[0061] Figure 4 This is a flowchart of a data hash value verification method according to an embodiment of the present invention.
[0062] Figure 5 This is a schematic diagram illustrating the hash value parsing principle of a method according to an embodiment of the present invention.
[0063] Figure 6 This is a schematic diagram of the structure of a terminal provided in an embodiment of the present invention. Detailed Implementation
[0064] To enable those skilled in the art to better understand the technical solutions of this invention, the technical solutions of the embodiments of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this invention, and not all embodiments. Based on the embodiments of this invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of this invention.
[0065] Figure 1This is a schematic flowchart illustrating a method according to an embodiment of the present invention. Wherein, Figure 1 The implementing entity can be an on-chain and off-chain collaborative system for blockchain data.
[0066] like Figure 1 As shown, the method includes:
[0067] Step 110: In the first mode, the caching service receives data pushed by the blockchain, parses the data into a block structure, performs hash value verification on each block, and writes the data that passes the verification into the off-chain database.
[0068] Step 120: In the second mode, the caching service periodically fetches data from the blockchain. By comparing the maximum block height of the off-chain database with the maximum block height of the blockchain, if the two are inconsistent, the service fetches the difference data from the blockchain and synchronizes the difference data to the off-chain database after verifying the hash value.
[0069] To facilitate understanding of the present invention, the following description further illustrates the on-chain and off-chain collaborative management method for blockchain data provided by the present invention, based on the principle of the method and the process of on-chain and off-chain collaborative management of blockchain data in the embodiments.
[0070] To improve data query efficiency, maintain the integrity and security of on-chain data (especially historical data), minimize the impact on blockchain performance, and facilitate the analysis of massive amounts of data, a collaborative method for on-chain and off-chain data in the energy blockchain was designed. This method provides an independent data caching service, including functions such as multi-method data acquisition, verification, parsing, storage, authentication, historical data checking, and Byzantine node discovery and alerting. It also provides a general interface for data querying and analysis, featuring rich functionality, ease of use, high availability, and loose coupling. For details, please refer to [link / reference]. Figure 2 The data caching service performs anomaly detection on both blockchain data and off-chain database data, and monitors the data exchanged between the two to ensure data collaboration. The on-chain and off-chain collaboration methods for blockchain data executed by the data caching service include:
[0071] S1. In the first mode, the caching service receives data pushed by the blockchain, parses the data into a block structure, performs hash value verification on each block, and writes the data that passes the verification into the off-chain database.
[0072] Specifically, the caching service is configured to operate in the first mode and the maximum block transmission range is configured. In the first mode, the caching service receives data pushed from the blockchain, and the data is within the maximum transmission range. The caching service parses the received data into a block structure; obtains the block header from the block structure and extracts the first hash value from the block header; calculates the actual hash value of the data block corresponding to the first hash value as the second hash value, and compares the consistency between the first hash value and the second hash value; if the first hash value and the second hash value are consistent, the data is written to the off-chain database; if the first hash value and the second hash value are inconsistent, the blockchain node that sent the data is determined to be an abnormal node, and an alarm message for the abnormal node is generated.
[0073] S2. In the second mode, the caching service periodically fetches data from the blockchain. By comparing the maximum block height of the off-chain database with the maximum block height of the blockchain, if the two are inconsistent, it fetches the difference data from the blockchain and synchronizes the difference data to the off-chain database after verifying the hash value.
[0074] Specifically, the caching service is configured to operate in the second mode, with specified time intervals and block fetching quantities. This allows the caching service to periodically fetch a corresponding amount of data from the blockchain. It periodically queries the maximum block height in the off-chain database and updates the cached record file with the latest height. If the maximum block height of the blockchain is greater than the maximum block height in the record file, the target number of blocks to be synchronized in this round is calculated based on the configured block fetching quantity. A data synchronization request is sent to the blockchain nodes to retrieve the target number of blocks. The fetched data undergoes hash value verification using the same method as in the first mode. After successful verification, the data is written to the off-chain database.
[0075] Please refer to Figure 3 In one embodiment of the present invention, the specific process of the on-chain and off-chain collaboration method for blockchain data is as follows:
[0076] (1) Configure the working mode of the data caching service. It can be configured as chain push mode or active pull mode. Configure the maximum transmission interval of blocks. If it is active pull mode, the pull time interval and the number of blocks to be pulled need to be configured. The maximum transmission interval and the number of blocks to be pulled are both limits on the amount of data transmitted in a single transmission.
[0077] (2) After the data caching service starts, select the data acquisition method according to the configuration. If it is chain push mode, skip to step 5. If it is active pull mode, skip to step 3.
[0078] (3) The data caching service queries the maximum block height of the off-chain database and updates the latest block height recorded in the cache. Then it synchronizes the latest block height to the chain and compares it with the block height in the local cache. If they are equal, this step is repeated. If the block height is greater than the block height in the local cache, the next step is performed.
[0079] (4) Calculate the block range that needs to be synchronized in this round according to the configured block transmission range, randomly select a blockchain node, and send a block synchronization request to the node.
[0080] (5) such as Figure 4 As shown, the data caching service receives the data returned from the chain, parses it into a block structure, and verifies each block. For specific verification logic, please refer to [reference needed]. Figure 5 The data caching service calculates the block's hash and compares it with the hash field in the block structure. It also retrieves the PreHash from the block structure (the block header stores the block's header information, including the hash value of the previous block (PreHash), the hash value of the current block body (Hash), and the timestamp (TimeStamp), etc.), and compares it with the hash of the previous block recorded in the database or the hash of the previous block within the current transmission interval. If they match, the verification passes, and the process proceeds to the next step. If the comparison fails, the node's data is considered to have been tampered with, the node is added to the list of abnormal nodes, and an alarm is issued.
[0081] (6) After the block data verification is successful, it will be parsed into the storage structure in the database, and asynchronous data entry will be performed. Blocks that failed to be written to the database will be collected and written again.
[0082] (7) After all blocks are successfully written in this round, update the latest block height recorded in the data cache service and enter the next round of the cycle.
[0083] (8) The data caching service periodically retrieves data from the database and compares it with the data retrieved on the blockchain based on key fields, thereby discovering abnormal data in the off-chain database.
[0084] (9) The data caching service provides relevant query interfaces, which can be directly called to perform query operations; it also provides the function of assigning read-only database roles for users to perform customized and diverse queries.
[0085] The system includes:
[0086] The first collaborative unit is used to receive data pushed by the blockchain in the first mode, parse the data into a block structure, perform hash value verification on each block data, and write the data that passes the verification into the off-chain database.
[0087] The second coordination unit is used in the second mode to periodically fetch data from the blockchain for caching services. By comparing the maximum block height of the off-chain database with the maximum block height of the blockchain, if the two are inconsistent, the unit will fetch the difference data from the blockchain and synchronize the difference data to the off-chain database after hash value verification.
[0088] Optionally, as an embodiment of the present invention, the first collaborative unit includes:
[0089] The first configuration module is used to configure the working mode of the cache service to the first mode and configure the maximum block transmission range. In the first mode, the cache service receives data pushed by the blockchain, and the data is within the maximum transmission range.
[0090] The data parsing module is used by the caching service to parse the received data into a block structure;
[0091] The hash extraction module is used to obtain the block header in the block structure and extract the first hash value from the block header;
[0092] The hash comparison module is used to calculate the actual hash value of the data block corresponding to the first hash value as the second hash value, and compare the consistency between the first hash value and the second hash value.
[0093] The data writing module is used to write the data into the off-chain database if the first hash value matches the second hash value.
[0094] The node alarm module is used to determine the blockchain node that sent the data as an abnormal node if the first hash value and the second hash value are inconsistent, and to generate an alarm message for the abnormal node.
[0095] Optionally, as an embodiment of the present invention, the second cooperative unit includes:
[0096] The second configuration module is used to configure the working mode of the caching service to the second mode and configure the time interval and the number of blocks to be pulled, so that the caching service periodically pulls the corresponding amount of data from the blockchain.
[0097] The height caching module is used to periodically query the maximum block height of the off-chain database and update the latest height to the cached record file;
[0098] The height comparison module is used to obtain the maximum block height of the blockchain. If the maximum block height of the blockchain is greater than the maximum block height of the record file, the number of target blocks to be synchronized in this round is calculated based on the configured number of blocks to be pulled.
[0099] The data capture module is used to send data synchronization requests to blockchain nodes and capture data on the target number of blocks.
[0100] Optionally, as an embodiment of the present invention, when writing data to the off-chain database, the system also performs:
[0101] The data is parsed into the storage structure required by the off-chain database and then asynchronously stored in the database.
[0102] Rewrite data blocks that failed to be written to the off-chain database.
[0103] Optionally, as an embodiment of the present invention, the system further performs:
[0104] Periodically retrieve data from the off-chain database and parse the first key field from it;
[0105] Parse the second key field from the corresponding data in the blockchain;
[0106] If the first key field does not match the second key field, the data obtained in this round is determined to be abnormal data.
[0107] Figure 6 This is a schematic diagram of the structure of a terminal 600 provided in an embodiment of the present invention. The terminal 600 can be used to execute the on-chain and off-chain collaborative method for blockchain data provided in the embodiment of the present invention.
[0108] The terminal 600 may include a processor 610, a memory 620, and a communication unit 630. These components communicate via one or more buses. Those skilled in the art will understand that the server structure shown in the figures does not constitute a limitation of the present invention. It may be a bus topology or a star topology, and may include more or fewer components than shown, or combine certain components, or have different component arrangements.
[0109] The memory 620 can be used to store the execution instructions of the processor 610. The memory 620 can be implemented by any type of volatile or non-volatile storage terminal or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk. When the execution instructions in the memory 620 are executed by the processor 610, the terminal 600 is able to perform some or all of the steps in the above method embodiments.
[0110] The processor 610 serves as the control center of the storage terminal, connecting various parts of the electronic terminal via various interfaces and lines. It executes software programs and / or modules stored in the memory 620, and calls data stored in the memory to perform various functions of the electronic terminal and / or process data. The processor can be composed of integrated circuits (ICs), such as a single packaged IC or multiple packaged ICs with the same or different functions connected together. For example, the processor 610 may only include a central processing unit (CPU). In this embodiment of the invention, the CPU may have a single processing core or include multiple processing cores.
[0111] The communication unit 630 is used to establish a communication channel, enabling the storage terminal to communicate with other terminals. It can receive user data sent by other terminals or send user data to other terminals.
[0112] The present invention also provides a computer storage medium, wherein the computer storage medium may store a program, which, when executed, may include some or all of the steps provided in the embodiments of the present invention. The storage medium may be a magnetic disk, an optical disk, read-only memory (ROM), or random access memory (RAM), etc.
[0113] Therefore, this invention can export on-chain data to off-chain database in real time without significantly impacting the overall performance of the blockchain. It can also easily and effectively verify the correctness of data, detect abnormal data and Byzantine nodes, ensure the correctness and integrity of data, and verify the correctness and integrity of historical data stored off-chain. This can meet the basic requirements of energy blockchain with large data volumes. The technical effects achieved by this embodiment can be found in the description above, and will not be repeated here.
[0114] Those skilled in the art will clearly understand that the techniques in the embodiments of the present invention can be implemented using software plus necessary general-purpose hardware platforms. Based on this understanding, the technical solutions in the embodiments of the present invention, or the parts that contribute to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium such as a USB flash drive, mobile hard drive, read-only memory (ROM), random access memory (RAM), magnetic disk, or optical disk, or other media capable of storing program code. It includes several instructions to cause a computer terminal (which may be a personal computer, server, or a second terminal, network terminal, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention.
[0115] The same or similar parts between the various embodiments in this specification can be referred to mutually. In particular, the terminal embodiments are basically similar to the method embodiments, so the description is relatively simple, and the relevant parts can be referred to the description in the method embodiments.
[0116] In the embodiments provided by this invention, it should be understood that the disclosed systems and methods can be implemented in other ways. For example, the system embodiments described above are merely illustrative. For instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between systems or units may be electrical, mechanical, or other forms.
[0117] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0118] In addition, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0119] Although the present invention has been described in detail with reference to the accompanying drawings and preferred embodiments, the invention is not limited thereto. Various equivalent modifications or substitutions can be made to the embodiments of the invention by those skilled in the art without departing from the spirit and essence of the invention, and such modifications or substitutions should all be within the scope of the invention. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the invention should also be covered within the protection scope of the invention. Therefore, the protection scope of the invention should be determined by the scope of the claims.
Claims
1. A method for on-chain and off-chain collaboration of blockchain data, characterized in that, The method is based on an independent data caching service, specifically including: In the first mode, the caching service receives data pushed by the blockchain, parses the data into a block structure, performs hash value verification on each block of data, and writes the data that passes the verification into the off-chain database. In the second mode, the caching service periodically fetches data from the blockchain. By comparing the maximum block height of the off-chain database with the maximum block height of the blockchain, if the two are inconsistent, it fetches the difference data from the blockchain and synchronizes the difference data to the off-chain database after verifying the hash value. In the first mode, the caching service receives data pushed from the blockchain, parses the data into a block structure, performs hash value verification on each block, and writes the verified data into an off-chain database, including: Configure the working mode of the cache service to the first mode and configure the maximum block transmission range. In the first mode, the cache service receives data pushed by the blockchain, and the data is within the maximum transmission range. The caching service parses the received data into a block structure; Obtain the block header from the block structure, and extract the first hash value from the block header; Calculate the actual hash value of the data block corresponding to the first hash value as the second hash value, and compare the consistency between the first hash value and the second hash value; If the first hash value matches the second hash value, the data is written to the off-chain database. If the first hash value and the second hash value are inconsistent, the blockchain node that sent the data will be identified as an abnormal node, and an alarm message for the abnormal node will be generated. In the second mode, the caching service periodically fetches data from the blockchain. It compares the maximum block height of the off-chain database with the maximum block height of the blockchain. If they are inconsistent, it fetches the difference data from the blockchain, verifies the difference data using a hash value, and then synchronizes it to the off-chain database. This includes: Configure the cache service to the second working mode and configure the time interval and the number of blocks to be pulled so that the cache service periodically pulls the corresponding amount of data from the blockchain. Periodically query the maximum block height in the off-chain database and update the latest height to the cached record file; If the maximum block height of the blockchain is greater than the maximum block height of the record file, then calculate the target number of blocks to be synchronized in this round based on the configured number of blocks to be pulled. Send a data synchronization request to the blockchain node to retrieve the target number of blocks.
2. The method according to claim 1, characterized in that, When writing data to an off-chain database, the method further includes: The data is parsed into the storage structure required by the off-chain database and then asynchronously stored in the database. Rewrite data blocks that failed to be written to the off-chain database.
3. The method according to claim 1, characterized in that, The method further includes: Periodically retrieve data from the off-chain database and parse the first key field from it; Parse the second key field from the corresponding data in the blockchain; If the first key field does not match the second key field, the data obtained in this round is determined to be abnormal data.
4. An on-chain and off-chain collaborative system for blockchain data, characterized in that, include: The first collaborative unit is used to receive data pushed by the blockchain in the first mode, parse the data into a block structure, perform hash value verification on each block data, and write the data that passes the verification into the off-chain database. The second coordination unit is used in the second mode to periodically fetch data from the blockchain for caching services. By comparing the maximum block height of the off-chain database with the maximum block height of the blockchain, if the two are inconsistent, the unit will fetch the difference data from the blockchain and synchronize the difference data to the off-chain database after verifying the hash value. The first collaborative unit includes: The first configuration module is used to configure the working mode of the cache service to the first mode and configure the maximum block transmission range. In the first mode, the cache service receives data pushed by the blockchain, and the data is within the maximum transmission range. The data parsing module is used by the caching service to parse the received data into a block structure; The hash extraction module is used to obtain the block header in the block structure and extract the first hash value from the block header; The hash comparison module is used to calculate the actual hash value of the data block corresponding to the first hash value as the second hash value, and compare the consistency between the first hash value and the second hash value. The data writing module is used to write the data into the off-chain database if the first hash value matches the second hash value. The node alarm module is used to determine the blockchain node that sent the data as an abnormal node if the first hash value and the second hash value are inconsistent, and to generate an alarm message for the abnormal node. The second collaborative unit includes: The second configuration module is used to configure the working mode of the caching service to the second mode and configure the time interval and the number of blocks to be pulled, so that the caching service periodically pulls the corresponding amount of data from the blockchain. The height caching module is used to periodically query the maximum block height of the off-chain database and update the latest height to the cached record file; The height comparison module is used to obtain the maximum block height of the blockchain. If the maximum block height of the blockchain is greater than the maximum block height of the record file, the number of target blocks to be synchronized in this round is calculated based on the configured number of blocks to be pulled. The data capture module is used to send data synchronization requests to blockchain nodes and capture data on the target number of blocks.
5. A terminal, characterized in that, include: processor; Memory used to store the processor's execution instructions; The processor is configured to perform the method according to any one of claims 1-3.
6. A computer-readable storage medium storing a computer program, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-3.
Citation Information
Patent Citations
Clinic test data checking method and system and electronic equipment
CN107657986A
A block chain data-based offline synchronization method and device
CN109558411A