Data synchronization method and device based on block chain, equipment, medium and product

By determining the block height information and changing the status information of the block data in the database when the blockchain is forked and switching, the problem of inconsistency in the database block data during the blockchain forked and switching is solved, and the consistency between the blockchain and the database data is achieved.

CN119848130APending Publication Date: 2025-04-18TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311336718.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-10-16
Publication Date
2025-04-18

AI Technical Summary

Technical Problem

When a fork switch occurs in a blockchain, how to ensure that the synchronized block data in the database is consistent with the block data on the blockchain, especially how to synchronize the block data during the fork switch.

Method used

By determining the block height information that needs to be synchronously switched, the corresponding block data is determined on the first and second chains, and the status information of the block data is changed in the database, thereby realizing synchronous switching of block data.

Benefits of technology

Ensure that when the blockchain is forked and switched, the block data synchronized in the database remains consistent with the block data on the blockchain, improving the accuracy of external queries.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119848130A_ABST
    Figure CN119848130A_ABST
Patent Text Reader

Abstract

The invention provides a block chain-based data synchronization method and device, equipment, a medium and a product. The block chain comprises a first chain (main chain) and a second chain (branched chain); the database corresponding to the block chain is used for synchronously storing block data on the block chain; the block data of the main chain has main chain state information in a database, and the block data of the branch chain has branch chain state information; the method comprises the steps of determining block height information (including at least one block height) needing synchronous switching when a block chain is subjected to bifurcation switching; based on the block height information, determining first block data corresponding to at least one block height on the first chain, and determining corresponding second block data on the second chain; in the database, the state information of the first block data is changed to branch chain state information, and the state information of the second block data is changed to main chain state information. Synchronous switching can be carried out on synchronous block data in a database when the block chain is subjected to bifurcation switching.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, in particular to the field of blockchain technology, and specifically to a blockchain-based data synchronization method, a blockchain-based data synchronization device, a computer device, a computer-readable storage medium, and a computer program product. Background Art

[0002] With the rapid development of blockchain technology, blockchain has been widely used. In the application process of blockchain, blockchain can provide external query function, but the external query function of blockchain is limited, and can only provide some relatively simple queries (for example, query transactions through transaction hash, query blocks through block hash, and query blocks through block height, etc.), and cannot realize some complex queries that require statistical analysis (for example, it is impossible to query the total number of transactions of the transaction object within a period of time, it is impossible to query the total amount of transaction resources of the transaction object within a period of time, etc.). In order to enable blockchain to provide more abundant external query functions, the block data on the blockchain can be synchronously stored in the database, and the database provides the external query function of the blockchain. Based on the powerful data query function of the database, the blockchain can provide more abundant external query functions. In the external query function of the blockchain combined with the database, in order to ensure the accuracy of the external query, the block data on the blockchain needs to be consistent with the block data synchronized in the database, and the blocks in the blockchain may fork in the process of forming the blockchain. When the blockchain forks and switches, how to synchronize the block data synchronized in the database has become a current research hotspot. Summary of the invention

[0003] The embodiments of the present application provide a blockchain-based data synchronization method, device, equipment, medium and product, which can synchronize the block data synchronized in the database when a fork switch occurs in the blockchain.

[0004] On the one hand, the embodiment of the present application provides a data synchronization method based on blockchain, wherein the blockchain includes a first chain and a second chain, wherein the first chain refers to a main chain recognized by the blockchain network, and the second chain refers to a forked chain generated by the first chain; the blockchain corresponds to a database, and the database is used to synchronously store block data on the blockchain; the block data on the blockchain has status information in the database, the status information of the block data on the main chain is the main chain status information, and the status information of the block data on the forked chain is the forked chain status information; the data synchronization method based on blockchain includes:

[0005] When a fork switch occurs in the blockchain, determine the block height information that needs to be switched synchronously; fork switching refers to the process in which the first chain on the blockchain switches from the main chain to the forked chain, and the second chain on the blockchain switches from the forked chain to the main chain; the block height information includes at least one block height;

[0006] Based on the block height information, determining first block data corresponding to at least one block height in the block height information on the first chain, and determining second block data corresponding to at least one block height in the block height information on the second chain;

[0007] In the database, the state information of the first block data is changed to the forked chain state information, and the state information of the second block data is changed to the main chain state information.

[0008] Accordingly, the embodiment of the present application provides a data synchronization device based on blockchain, wherein the blockchain includes a first chain and a second chain, wherein the first chain refers to a main chain recognized by the blockchain network, and the second chain refers to a forked chain generated by the first chain; the blockchain corresponds to a database, and the database is used to synchronously store block data on the blockchain; the block data on the blockchain has status information in the database, the status information of the block data on the main chain is the main chain status information, and the status information of the block data on the forked chain is the forked chain status information; the data synchronization device based on blockchain is set in the database, and the data synchronization device based on blockchain includes:

[0009] A determination unit, used for determining the block height information that needs to be synchronously switched when a fork switch occurs in the blockchain; the fork switch refers to the process in which the first chain on the blockchain switches from the main chain to the forked chain, and the second chain on the blockchain switches from the forked chain to the main chain; the block height information includes at least one block height;

[0010] The determining unit is further used to determine, based on the block height information, first block data corresponding to at least one block height in the block height information on the first chain, and to determine second block data corresponding to at least one block height in the block height information on the second chain;

[0011] The processing unit is used to change the state information of the first block data into the forked chain state information in the database, and change the state information of the second block data into the main chain state information.

[0012] In one implementation, the processing unit is further configured to perform the following steps:

[0013] Synchronize and store block data on the main chain in real time from the blockchain;

[0014] Get the fork switching rules of the blockchain;

[0015] Determine the fork data synchronization method corresponding to the fork switching rule;

[0016] According to the determined fork data synchronization method, the block data on the forked chain is synchronized and stored from the blockchain.

[0017] In one implementation, the fork switching rule includes: the number of blocks that need to be waited from when the blockchain forks to when the blockchain forks;

[0018] The processing unit is used to determine the fork data synchronization method corresponding to the fork switching rule, and is specifically used to perform the following steps:

[0019] If the number of blocks to be waited is greater than or equal to the block number threshold, the fork data synchronization mode corresponding to the fork switching rule is determined to be the real-time fork synchronization mode;

[0020] If the number of blocks to be waited is less than the block number threshold, it is determined that the fork data synchronization mode corresponding to the fork switching rule is a non-real-time fork synchronization mode.

[0021] In one implementation, the fork data synchronization mode corresponding to the fork switching rule includes a non-real-time fork synchronization mode or a real-time fork synchronization mode;

[0022] When the fork data synchronization mode corresponding to the fork switching rule is a non-real-time fork synchronization mode, the processing unit is used to synchronize and store the block data on the forked chain from the blockchain according to the determined fork data synchronization mode, specifically to perform the following steps:

[0023] When a fork occurs on the blockchain, the block data required for the synchronous switch is stored synchronously from the forked chain;

[0024] When the fork data synchronization mode corresponding to the fork switching rule is the real-time fork synchronization mode, the processing unit is used to synchronize and store the block data on the forked chain from the blockchain according to the determined fork data synchronization mode, specifically to perform the following steps:

[0025] When a blockchain forks, the block data on the forked chain is synchronized in real time.

[0026] In one implementation, the state information of the block data on the blockchain in the database refers to the field value of the state field;

[0027] The processing unit is used to change the state information of the first block data to the forked chain state information in the database, and change the state information of the second block data to the main chain state information, specifically to perform the following steps:

[0028] Create a first status update transaction and a second status update transaction, the first status update transaction is used to update the field value of the status field corresponding to the first block data, and the second status update transaction is used to update the field value of the status field corresponding to the second block data;

[0029] Execute a first status update transaction to change the field value of the status field corresponding to the first block data to the forked chain status information; and execute a second status update transaction to change the field value of the status field corresponding to the second block data to the main chain status information.

[0030] In one implementation, the processing unit is further configured to perform the following steps:

[0031] Receive data query requests;

[0032] Parse the data query request and obtain the data query conditions;

[0033] Based on the data query conditions, query the block data that has been synchronized and stored on the blockchain;

[0034] Returns the data query results.

[0035] In one implementation, the processing unit is used to parse the data query request and obtain the data query condition, and is specifically used to perform the following steps:

[0036] If the parsing result indicates that the data query request carries hash information, then the data query condition is generated according to the hash information; the hash information includes block hash and / or transaction hash;

[0037] If the parsing result indicates that the data query request carries block range information, then the data query condition is generated according to the block height range indicated by the block range information and the main chain status information;

[0038] If the parsing result indicates that the data query request carries time range information, the time range information is converted into a block height range, and the data query conditions are generated according to the block height range and the main chain status information.

[0039] In one implementation, the data query request is used to request a query for block data within a block height range; the processing unit is used to perform the following steps when querying from the block data that has been synchronously stored on the blockchain based on the data query condition:

[0040] When the block height range requested for query overlaps with the block height range indicated by the block height information, before completing the synchronous switching, the query is performed in the main chain block data before the synchronous switching, and the main chain block data before the synchronous switching refers to the block data whose status information before the synchronous switching is the main chain status information;

[0041] After the synchronous switching is completed, the query is performed in the main chain block data after the synchronous switching is completed. The main chain block data after the synchronous switching is completed refers to the block data whose status information after the synchronous switching is completed is the main chain status information.

[0042] Accordingly, an embodiment of the present application provides a computer device, the computer device comprising:

[0043] a processor suitable for implementing a computer program;

[0044] A computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and the computer program is suitable for being loaded by a processor and executing the above-mentioned blockchain-based data synchronization method.

[0045] Accordingly, an embodiment of the present application provides a computer-readable storage medium, which stores a computer program. When the computer program is read and executed by a processor of a computer device, the computer device executes the above-mentioned blockchain-based data synchronization method.

[0046] Accordingly, an embodiment of the present application provides a computer program product, which includes a computer program stored in a computer-readable storage medium. A processor of a computer device reads the computer program from the computer-readable storage medium, and the processor executes the computer program, so that the computer device executes the above-mentioned blockchain-based data synchronization method.

[0047] In the embodiment of the present application, the database can be used to synchronously store the block data on the blockchain, and the block data on the blockchain has status information in the database. The status information of the block data on the main chain in the blockchain is the main chain status information, and the status information of the block data on the forked chain in the blockchain is the forked chain status information. The blockchain may include a first chain (the first chain is the main chain) and a second chain (the second chain forked chain). When the blockchain forks and switches, the block height information that the database needs to synchronize with can be determined, and the first block data corresponding to the block height information can be determined in the first chain, and the second block data corresponding to the block height information can be determined in the second chain. Then, the status information of the first block data can be changed to the forked chain status information, and the status information of the second block data can be changed to the main chain fork information. It can be seen that the embodiment of the present application can identify the chain (main chain or forked chain) to which the synchronized block data belongs on the blockchain in the database through the status information. When the blockchain is forked, the synchronized block data in the database can be synchronized by changing the status information of the block data. That is to say, the embodiment of the present application can synchronize the synchronized block data in the database when the blockchain is forked, so that the block data on the blockchain can be kept consistent with the block data synchronized in the database. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0049] Figure 1 This is a schematic diagram of the architecture of a blockchain network provided in an embodiment of the present application;

[0050] Figure 2 This is a schematic diagram of the structure of a blockchain provided by an embodiment of the present application;

[0051] Figure 3 It is a structural schematic diagram of a block generation process provided by an embodiment of the present application;

[0052] Figure 4 It is a schematic diagram of a blockchain fork provided in an embodiment of the present application;

[0053] Figure 5 This is a schematic diagram of a blockchain fork switch provided by an embodiment of the present application;

[0054] Figure 6 It is a schematic diagram of the architecture of a data synchronization system provided in an embodiment of the present application;

[0055] Figure 7 This is a flowchart of a data synchronization method based on blockchain provided in an embodiment of the present application;

[0056] Figure 8 This is a schematic diagram of determining block height information that needs to be synchronously switched provided by an embodiment of the present application;

[0057] Fig. 9 This is a flowchart of another blockchain-based data synchronization method provided in an embodiment of the present application;

[0058] Fig.10 It is a structural diagram of a blockchain-based data synchronization device provided in an embodiment of the present application;

[0059] Fig.11 It is a structural diagram of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0060] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0061] In order to more clearly understand the technical solution provided by the embodiments of the present application, the key terms involved in the embodiments of the present application are first introduced here:

[0062] (1) Blockchain network:

[0063] The blockchain network is a point-to-point network. Each point-to-point connected blockchain node is called a peer node. The blockchain network is based on a specific type of network protocol, so that there is no need for a central node to maintain the network status between peer nodes. Each peer node maintains the node status of the entire network and its connection status with adjacent nodes through broadcast interactions with adjacent nodes.

[0064] The blockchain network can be understood as Figure 1 The data sharing system 10 shown. The data sharing system 10 refers to a system for sharing data between blockchain nodes and blockchain nodes. The data sharing system may include multiple blockchain nodes 101, and the multiple blockchain nodes 101 may refer to each client, terminal or server in the data sharing system. Each blockchain node 101 can receive input information (for example, blockchain transactions) when performing normal work, and maintain the shared data in the data sharing system based on the received input information. In order to ensure the information intercommunication in the data sharing system, there may be an information connection between each blockchain node in the data sharing system, and information can be transmitted between blockchain nodes through the above information connection. For example, when any blockchain node in the data sharing system receives input information, other nodes in the data sharing system obtain the input information according to the consensus algorithm, and store the input information as data in the shared data, so that the data stored on all blockchain nodes in the data sharing system are consistent.

[0065] Each blockchain node in the data sharing system has a corresponding node identifier, and each blockchain node in the data sharing system can store the node identifiers of other blockchain nodes in the data sharing system, so that the generated blocks can be broadcast to other blockchain nodes in the data sharing system according to the node identifiers of other blockchain nodes. Each blockchain node can maintain a node identifier list as shown in Table 1 below, and store the node name and node identifier in the node identifier list accordingly. Among them, the node identifier can be an IP (Internet Protocol, a protocol for interconnecting networks) address and any other information that can be used to identify the blockchain node. Table 1 only uses the IP address as an example for explanation:

[0066] Table 1

[0067] Node Name Node ID Node 1 111.111.111.111 Node 2 222.222.222.222 … … Node N NNN.NNN.NNN.NNN

[0068] (2) Blockchain:

[0069] Each blockchain node in the blockchain network stores an identical blockchain. Blockchain is a distributed ledger technology in the field of information technology. It is generally composed of consensus, transaction blocks, state data storage, cryptographic identity security, etc. Since the ledger is distributed and the blocks are consensus-based, it has the characteristics of being tamper-proof, traceable, and jointly maintained. The blockchain consists of multiple blocks, which can be seen in Figure 2 As shown in the blockchain structure, the blockchain consists of multiple blocks. The genesis block in the blockchain includes a block header and a block body. The block header of the genesis block stores input information feature values, version numbers, timestamps and difficulty values, and the block body of the genesis block stores input information (i.e., transactions). The next block of the genesis block takes the genesis block as its parent block, and the next block also includes a block header and a block body. In addition to storing the input information feature values, version numbers, timestamps and difficulty values ​​of the current block, the block header of the next block also stores the feature values ​​of the block header of the parent block, and so on, so that the block data stored in each block in the blockchain is associated with the block data stored in its parent block, thereby ensuring the security of the input information in the block.

[0070] When generating each block in the blockchain, refer to Figure 3 In the block generation process shown in the figure, when the blockchain node where the blockchain is located receives the input information, it verifies the input information. After the verification is completed, the input information is stored in the transaction pool, and the hash tree used to record the input information is updated; then, the update timestamp is updated to the time when the input information is received, and different random numbers are tried, and the eigenvalue calculation is performed multiple times, so that the calculated eigenvalue can satisfy the following formula:

[0071] SHA256(SHA256(version+prev_hash+merkle_root+ntime+nbits+x)) <TARGET

[0072] Among them, SHA256 (Secure Hash Algorithm 256, a secure hash algorithm) is the eigenvalue algorithm used to calculate the eigenvalue; version (version number) is the version information of the relevant block protocol in the blockchain; prev_hash is the block header eigenvalue of the parent block of the current block; merkle_root is the eigenvalue of the input information; ntime is the update time of the update timestamp; nbits is the current difficulty, which is a fixed value within a period of time and is determined again after exceeding the fixed time period; x is a random number; TARGET is the eigenvalue threshold, which can be determined based on nbits.

[0073] In this way, when the random number that satisfies the above formula is calculated, the information can be stored accordingly, the block header and block body can be generated, and the current block can be obtained. Subsequently, the blockchain node where the blockchain is located sends the newly generated block to other blockchain nodes in the data sharing system where it is located according to the node identification of other blockchain nodes in the data sharing system. Other blockchain nodes verify the newly generated block and add the newly generated block to the blockchain stored in it after the verification is completed.

[0074] (3) Blockchain fork:

[0075] Blockchain fork refers to the phenomenon that blocks fork in the process of forming a blockchain; before the blockchain fork, there is only one chain in the blockchain, and the block height can uniquely identify the block in the blockchain; after the blockchain fork, the blockchain can include a main chain and at least one (one or more) forked chains, the block height cannot uniquely identify the block in the blockchain, and the same block height may correspond to multiple blocks, among which the main chain refers to the chain formed by the blockchain fork that is recognized by the blockchain network to which the blockchain belongs, and the forked chain refers to the chain formed by the blockchain fork that is not recognized by the blockchain network to which the blockchain belongs.

[0076] like Figure 4As shown, after the blockchain forks, the blockchain includes a main chain and a forked chain. The blockchain forks after the block with a block height of 5; after the blockchain forks, the same block height may correspond to multiple blocks. For example, there are two blocks with a block height of 6 (block F1 on the main chain and block F2 on the forked chain), and there are two blocks with a block height of 7 (block G1 on the main chain and block G2 on the forked chain). In addition, the data stored in different blocks of the same block height is also different. For example, the data stored in blocks F1 and F2 with a block height of 6 are different.

[0077] Furthermore, after a fork occurs in a blockchain, the blockchain may contain at least two chains. The methods for determining the main chain and the forked chain in each chain contained in the blockchain may include but are not limited to any of the following: ① Each blockchain node in the blockchain network may vote for the newly added blocks in each chain after the blockchain forks, and the number of votes cast by each blockchain node in the blockchain network for each chain contained in the blockchain may be counted. The chain with the largest number of votes among the chains contained in the blockchain may be determined as the main chain, and the remaining chains may be determined as forked chains. In other words, among the chains formed by the fork of the blockchain, the main chain recognized by the blockchain network refers to the chain with the largest number of votes cast by the blockchain network for the newly added blocks after the fork of the blockchain; the forked chain not recognized by the blockchain network refers to the remaining chains except the main chain. ② The length of each chain contained in the blockchain after the fork of the blockchain may be counted (i.e., the number of blocks contained in each chain), and the chain with the longest length among the chains contained in the blockchain may be determined as the main chain, and the remaining chains may be determined as forked chains. That is to say, among the chains formed by the fork of the blockchain, the main chain recognized by the blockchain network refers to the longest chain; the forked chain not recognized by the blockchain network refers to the remaining chains except the longest chain. ③ Each blockchain node in the blockchain network has a node weight. The node weight corresponding to the blockchain node is related to the importance of the role of the blockchain node in the blockchain network. The more important the role of the blockchain node in the blockchain network, the higher the credibility of the blockchain node in the blockchain network, and the greater the node weight corresponding to the blockchain node. When counting the lengths of the chains contained in the blockchain after the fork of the blockchain, the node weight corresponding to the block-producing blockchain node of the block can also be introduced. Thus, the lengths of the chains contained in the blockchain after the fork of the blockchain can be counted. The length can include the original length (i.e., the number of blocks contained in each chain) and the node weight length (i.e., the node weight corresponding to the block-producing blockchain node). The longest chain among the chains contained in the blockchain can be determined as the main chain, and the remaining chains can be determined as forked chains. That is to say, among the chains formed by the fork of the blockchain, the main chain recognized by the blockchain network refers to the chain with the longest length (including the original length and the node weight length); the forked chain not recognized by the blockchain network refers to the remaining chains except the chain with the longest length (including the original length and the node weight length). Usually, after the blockchain forks, the external query function provided by the blockchain is for the main chain. It can be understood that when the block height is used for query, the block data of the block indicated by the block height on the main chain is queried, rather than the block data of the block indicated by the block height on the forked chain. For example, Figure 4 When querying data on the blockchain shown in the figure, the block data in the block with a block height of 6 is queried. What is queried is the block data in block F1 on the main chain, rather than the block data in block F2 on the forked chain.

[0078] (4) Blockchain fork switching:

[0079] Blockchain fork switching means that after a blockchain forks, if the blockchain network to which the blockchain belongs recognizes the forked chain more than the main chain (for example, the number of votes for the forked chain is higher than the number of votes for the main chain, or the length of the forked chain is higher than the length of the main chain, etc.), then it is necessary to switch the forked chain to the main chain and the main chain to the forked chain. After the blockchain forks, the newly generated blocks are connected to the switched main chain. Figure 5 As shown, in Figure 4 After the blockchain shown is forked, the newly generated blocks H, I, and J are connected to the main chain after the switch.

[0080] Based on the above-mentioned introduction to key terms such as blockchain network, blockchain, blockchain fork, and blockchain fork switching, an embodiment of the present application proposes a data synchronization method based on blockchain, which is based on synchronously storing block data on the blockchain in a database, and providing external queries of the blockchain by the database; after the blockchain forks, the database can identify the chain (main chain or forked chain) to which the synchronized block data belongs through status information, and the status information of the block data on the main chain in the database is the main chain status information, and the status information of the block data on the forked chain in the database is the forked chain status information; when the blockchain forks, the database can synchronize and switch the synchronized block data in the database by changing the status information (the main chain status information is changed to the forked chain status information, and the forked chain status information is changed to the main chain status information), so that when the blockchain forks, the block data on the blockchain can be kept consistent with the block data synchronized in the database, thereby ensuring the accuracy of external queries when providing blockchain external queries.

[0081] The data synchronization system provided in the embodiment of the present application is introduced below in conjunction with the accompanying drawings. The data synchronization system provided in the embodiment of the present application can be suitable for implementing the blockchain-based data synchronization method provided in the embodiment of the present application.

[0082] like Figure 6 As shown, the data synchronization system may include a data query device 601, a data synchronization device 602, and a blockchain node 603. The embodiment of the present application does not limit the connection method between the data query device 601, the data synchronization device 602, and the blockchain node 603. The data query device 601, the data synchronization device 602, and the blockchain node 603 may establish a direct communication connection through wired communication, or the data query device 601, the data synchronization device 602, and the blockchain node 603 may establish an indirect communication connection through wireless communication. Wherein:

[0083] (1) Data query device 601:

[0084] The data query device 601 can send a data query request to the data synchronization device 602. The data query request can be used to request to query the data in the blockchain. The data query request can be generated in response to a data query operation of a data query object, or the data query request can be generated by calling a data query service. The following describes these two situations respectively:

[0085] In the first case, a blockchain query client may be run in the data query device 601, and the data query object may perform a data query operation in the blockchain query client (for example, input a data query condition and submit a query), and the blockchain query client may generate a data query request corresponding to the data query operation based on the data query operation of the data query object, and then the blockchain query client may send a data query request to the data synchronization device 602 through the data query device 601; the embodiment of the present application does not limit the client type of the blockchain query client, and the blockchain query client may include but is not limited to any of the following: a blockchain query application, a blockchain query applet, and a blockchain query browser. In this case, after receiving the data query result returned by the data synchronization device 602, the data query device 601 may display the data query result to the data query object through the blockchain query client.

[0086] In the second case, a data query service may be deployed in the data query device 601, and the data query service may be understood as a background service of the data query device 601. When a functional module in the data query device 601 (for example, an application in the data query device 601, an operating system of the data query device 601, etc.) has a data query demand, the data query service may be called to generate a data query request corresponding to the data query demand according to the data query demand, and then the data query service may send a data query request to the data synchronization device 602 through the data query device 601. In this case, after receiving the data query result returned by the data synchronization device 602, the data query device 601 may feed back the data query result to the functional module in the data query device 601 that has the data query demand through the data query service.

[0087] (2) Data synchronization device 602:

[0088] A database may be deployed in the data synchronization device 602. The embodiment of the present application does not limit the type of database. The embodiment of the present application takes the MySQL database as an example. The MySQL database is a relational database management system that can store data in different tables, which can increase the speed and flexibility of data query. The database can synchronize and store the block data on the blockchain from the blockchain node 603. The block data on the blockchain has status information in the database. It can be understood that the database can identify the chain (main chain or forked chain) to which the synchronized block data belongs on the blockchain through the status information; the status information of the block data on the main chain in the database is the main chain status information, that is, the main chain status information can be used to identify the chain to which the corresponding block data belongs on the blockchain as the main chain, and the status information of the block data on the forked chain in the database is the forked chain status information, that is, the forked chain status information can be used to identify the chain to which the corresponding block data belongs on the blockchain as the forked chain.

[0089] The database can perform real-time detection on the blockchain, which can be understood as that the blockchain node 603 can send the blockchain's dynamic change notification information (for example, the blockchain's block notification information, the blockchain's fork notification information, and the blockchain's fork switch notification information, etc.) to the database in real time. For example, when the blockchain is forked and split, the blockchain node 603 can send the blockchain's fork switch notification information to the database in real time, and the database can synchronize the synchronized block data; the database synchronizes the synchronized block data, which can specifically refer to changing the state information of the block data, and changing the state information of the block data can include: determining the first block data on the original main chain that needs to be synchronized and the second block data on the original forked chain that needs to be synchronized, changing the state information of the first block data from the main chain state information to the forked chain state information, and changing the state information of the second block data from the forked chain state information to the main chain state information to change the state information of the block data, so that the block data on the blockchain is consistent with the block data synchronized with the database.

[0090] The database deployed in the data synchronization device 602 can respond to the data query request sent by the data query device 601, query the data that meets the data query conditions from the synchronized block data, and return the data that meets the data query conditions as the data query result to the data query device 601.

[0091] (3) Blockchain Node 603:

[0092] The blockchain node 603 is any node device in the blockchain network, and the blockchain can be stored in the blockchain node 603. The blockchain node 603 can send dynamic change notification information of the blockchain (for example, block notification information of the blockchain, fork notification information of the blockchain, and fork switching notification information of the blockchain, etc.) to the database deployed in the data synchronization device 602 in real time.

[0093] It should be noted that the data query device 601, the data synchronization device 602 and the blockchain node 603 can all be terminals or servers. The terminals mentioned in the embodiments of the present application may include but are not limited to any of the following: smart phones, tablet computers, laptops, desktop computers, smart watches, smart home appliances, intelligent voice interaction devices, vehicle terminals and aircraft, but are not limited to this. The server mentioned in the embodiments of the present application may be a separate physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN (Content Delivery Network), and big data and artificial intelligence platforms, etc. The embodiments of the present application do not limit this.

[0094] Understandably, Figure 6 The data synchronization system of the illustrated embodiment is intended to more clearly illustrate the technical solution of the embodiment of the present application, and does not constitute a limitation on the technical solution provided by the embodiment of the present application. A person of ordinary skill in the art can appreciate that with the evolution of the system architecture and the emergence of new business scenarios, the technical solution provided by the embodiment of the present application is equally applicable to similar technical problems.

[0095] Based on the above description of key terms and data synchronization systems, the blockchain-based data synchronization method provided in the embodiments of the present application is described in detail below in conjunction with the accompanying drawings.

[0096] The present application provides a data synchronization method based on blockchain. The data synchronization method based on blockchain mainly introduces the synchronization switching of synchronized block data in the database when a fork switch occurs in the blockchain, and the synchronization method of block data on the forked chain after the blockchain forks. The data synchronization method based on blockchain can be executed by a computer device, which can be, for example, Figure 6 The data synchronization device 602 in the data synchronization system shown in FIG. Figure 7 As shown, the blockchain-based data synchronization method may include but is not limited to the following steps S701-S703:

[0097] S701, when a fork switch occurs in the blockchain, determine the block height information that needs to be synchronously switched; fork switching refers to the process in which the first chain on the blockchain switches from the main chain to the forked chain, and the second chain on the blockchain switches from the forked chain to the main chain; the block height information includes at least one block height.

[0098] The blockchain may include a first chain and a second chain. The first chain may be a main chain recognized by the blockchain network to which the blockchain belongs, and the second chain may refer to a forked chain generated by the first chain. The blockchain may correspond to a database, which may be used to synchronously store block data in the blockchain to provide external queries about the blockchain. The block data on the blockchain synchronously stored by the database may include but is not limited to at least one of the following: block headers, transaction data, and smart contracts; when synchronously storing block data on the blockchain, the database may add status information to the block data on the blockchain, and the status information may specifically refer to the field value of the status field; the status information of the block data on the main chain in the blockchain in the database may be the main chain status information, and the main chain status information may be used to identify the chain to which the synchronized corresponding block data on the blockchain belongs as the main chain, and the status information of the block data on the forked chain in the blockchain in the database may be the forked chain status information, and the forked chain status information may be used to identify the chain to which the synchronized corresponding block data on the blockchain belongs as the forked chain.

[0099] The main chain is recognized by the blockchain network to which the blockchain belongs. Therefore, blockchain queries can also be considered as main chain queries. That is to say, when providing external queries, the database can query data that meets the data query conditions from the block data on the main chain. Based on this, the database can synchronize and store the block data on the main chain from the blockchain in real time. Synchronizing and storing the block data on the main chain in real time can be understood as the database can perform real-time detection on the blockchain. When it is detected that a new block has been added to the main chain in the blockchain, the database can synchronize the block data in the new block from the main chain.

[0100] The blockchain may undergo a fork switch. A fork switch in a blockchain refers to the process in which the first chain on the blockchain switches from the main chain to the forked chain, and the second chain on the blockchain switches from the forked chain to the main chain. Therefore, the database needs to synchronize block data from the forked chain in the blockchain so that the database can be synchronized when the blockchain undergoes a fork switch. The way the database synchronizes block data from the forked chain is related to the degree of fork in the blockchain. The degree of fork refers to the number of blocks that need to be waited for from the occurrence of a fork in the blockchain to confirm that a fork switch has occurred in the blockchain. In other words, after a fork in the blockchain occurs, a certain number of blocks need to be waited for to confirm whether the blockchain needs to undergo a fork switch. The degree of fork is the number of blocks that need to be waited for. The higher the degree of fork in the blockchain, the more blocks need to be waited for from the occurrence of a fork in the blockchain to confirm that a fork switch has occurred in the blockchain. The lower the degree of fork in the blockchain, the fewer blocks need to be waited for from the occurrence of a fork in the blockchain to confirm that a fork switch has occurred in the blockchain.

[0101] Furthermore, the degree of fork of the blockchain can be represented by the fork switching rule of the blockchain. The fork switching rule may include the number of blocks that need to be waited for from the occurrence of fork in the blockchain to the determination of the fork switching of the blockchain. The fork switching rule of the blockchain can be obtained to determine the fork data synchronization method corresponding to the fork switching rule. Then, the block data on the forked chain can be synchronized and stored from the blockchain according to the determined fork data synchronization method.

[0102] Wherein, determining the fork data synchronization mode corresponding to the fork switching rule may include: if the number of blocks to be waited for from the occurrence of a fork in the blockchain to the determination that the fork switching of the blockchain occurs is greater than or equal to the block number threshold, then the data synchronization mode corresponding to the fork switching rule may be determined to be a real-time fork switching synchronization mode; if the number of blocks to be waited for from the occurrence of a fork in the blockchain to the determination that the fork switching of the blockchain occurs is less than the block number threshold, then the data synchronization mode corresponding to the fork switching rule may be determined to be a non-real-time fork switching synchronization mode. When the fork data synchronization mode corresponding to the fork switching rule is a real-time fork synchronization mode, according to the determined fork data synchronization mode, synchronizing and storing the block data on the forked chain from the blockchain may include: synchronizing the block data on the forked chain in real time after the blockchain forks, and synchronizing and storing the block data on the forked chain in real time, which can be understood as that the database can perform real-time detection on the blockchain, and when it is detected that a new block is added to the forked chain in the blockchain, the database can synchronize the new block from the forked chain. When the fork data synchronization mode corresponding to the fork switching rule is a non-real-time fork synchronization mode, the block data on the forked chain is synchronously stored from the blockchain according to the determined fork data synchronization mode, which may include: when the blockchain forks, the block data required for synchronous switching is synchronously stored from the forked chain. It can be understood that when the blockchain forks, all block data required for synchronous switching are synchronously stored from the forked chain at one time.

[0103] That is to say, the higher the degree of fork of the blockchain, the more blocks need to be waited for from the occurrence of fork of the blockchain to the determination of the fork switch of the blockchain, and the larger the amount of block data that needs to be synchronized and switched. If the block data that needs to be synchronized and switched is synchronized from the forked chain when the blockchain forks and switches, it will take a long time to synchronize. In order to avoid this situation, when the degree of fork of the blockchain is high, you can choose to synchronize the block data on the forked chain in real time after the blockchain forks. In this way, the state information can be directly changed when the blockchain forks and switches, without spending synchronization time, thereby improving the efficiency of the database in synchronizing the synchronized block data when the blockchain forks and switches. The lower the degree of forking of the blockchain, the fewer blocks need to be waited for from the occurrence of forking of the blockchain to the determination of the forking switch of the blockchain, and the smaller the amount of block data that needs to be synchronized and switched. If all block data that need to be synchronized and switched are synchronized from the forked chain at one time when the blockchain is forked, then the synchronization time can be ignored. In addition, synchronizing the block data on the forked chain before the forking switch of the blockchain may occupy more memory space of the database. When the blockchain is forked, all block data that need to be synchronized and switched are synchronized from the forked chain at one time, which can save the memory space of the database. Therefore, determining the synchronization method of the block data on the forked chain according to the degree of forking of the blockchain (i.e., the forking switching rule) is conducive to the process of the database to synchronize the synchronized block data, both in terms of efficiency and in terms of the memory usage of the database.

[0104] When a fork occurs in a blockchain, the block height information that needs to be switched synchronously can be determined. The block height information can include at least one block height. At least one block height in the block height information that needs to be switched synchronously can refer to a block height between a first block height and a second block height. The first block height can refer to the block height when the blockchain forks, and the second block height can refer to the block height when the blockchain forks. For example, Figure 8 As shown, the blockchain forks at a block height of 3, and it can be seen that the first block height is 3. The blockchain confirms that a fork switch is required at a block height of 9, and it can be seen that the second block height is 9. In other words, the block height information that needs to be synchronously switched includes at least one block height of block height 3-block height 9.

[0105] S702, based on the block height information, determining first block data corresponding to at least one block height in the block height information on the first chain, and determining second block data corresponding to at least one block height in the region height information on the second chain.

[0106] When a fork switch occurs in a blockchain, after determining the block height information (including at least one block height) that needs to be synchronously switched, the first block data corresponding to at least one block height in the block height information can be determined on the first chain based on the block height information, and the second block data corresponding to at least one block height in the block height information can be determined on the second chain. Figure 8 For example, the block height information that needs to be synchronously switched includes at least one block height of block height 3-block height 9, the first block data determined on the first chain is the block data of block C1, block D1, block E1, block F1, block G1, block H1 and block I1, and the second block data determined on the second chain is the block data of block C2, block D2, block E2, block F2, block G2, block H2 and block I2.

[0107] S703, changing the state information of the first block data to the forked chain state information in the database, and changing the state information of the second block data to the main chain state information.

[0108] When a fork switch occurs in a blockchain, after determining the block height information (including at least one block height) that needs to be synchronously switched, based on the block height information, determining the first block data corresponding to at least one block height in the block height information on the first chain, and determining the second block data corresponding to at least one block height in the block height information on the second chain, the state information of the first block data can be changed to the forked chain state information in the database, and the state information of the second block data can be changed to the main chain state information. Specifically, the state information of the first block data when synchronously stored in the database is the main chain state information, and changing the state information of the first block data to the forked chain state information means changing the state information of the first block data from the main chain state information to the forked chain state information; the state information of the second block data when synchronously stored in the database is the forked chain state information, and changing the state information of the second block data to the main chain state information means changing the state information of the second block data from the forked chain state information to the main chain state information.

[0109] Furthermore, the state information of the block data on the blockchain in the database may specifically refer to the field value of the state field, and the state information of the corresponding block data may be changed in the database through transactions, and the transaction in the database is a set of operation sequences. Changing the state information of the first block data to the forked chain state information in the database, and changing the state information of the second block data to the main chain state information, may include: creating a first state update transaction and a second state update transaction, the first state update transaction may be used to update the field value of the state field corresponding to the first block data, and the second state update transaction may be used to update the field value of the state field corresponding to the second block data; executing the first state update transaction, changing the field value of the state field corresponding to the first block data to the forked chain state information, and executing the second state update transaction, changing the field value of the state field corresponding to the second block data to the main chain state information. It can be seen that through the transactions in the database, the state information of the first block data can be batch changed at one time, and the state information of the second block data can be batch changed at one time, which can improve the efficiency of the database in synchronously switching the synchronized block data.

[0110] In the embodiment of the present application, the chain (main chain or forked chain) to which the synchronized block data belongs on the blockchain can be identified in the database through the state information. When the blockchain undergoes a fork switch, the synchronized block data in the database can be synchronized and switched by changing the state information of the block data. That is to say, the embodiment of the present application can synchronize the synchronized block data in the database when the blockchain undergoes a fork switch, so that the block data on the blockchain can be kept consistent with the synchronized block data in the database. In addition, the method of synchronizing the forked data of the blockchain can be determined according to the degree of forking of the blockchain. The blockchain with a high degree of forking can synchronize the block data on the forked chain in real time when the blockchain undergoes a fork, and can directly change the state information when the blockchain undergoes a fork switch, without spending synchronization time, and improving the efficiency of the database for synchronization switching. The blockchain with a low degree of forking can synchronize all the block data on the forked chain that need to be synchronized and split at one time when the blockchain undergoes a fork switch. The amount of block data on the forked chain that needs to be synchronized and split is small, and the synchronization time spent is acceptable, which can save the storage space of the database. In addition, the database can use transactions to batch change the block data that needs to be synchronized at one time, rather than changing each block data individually, which can further improve the efficiency of the database in synchronizing the synchronized block data.

[0111] The present application embodiment provides a data synchronization method based on blockchain, which mainly introduces the content of data query by the database in response to the data query request. The data synchronization method based on blockchain can be executed by a computer device, such as a computer device Figure 6 The data synchronization device 602 in the data synchronization system shown in FIG. Fig. 9 As shown, the blockchain-based data synchronization method may include but is not limited to the following steps S901-S907:

[0112] S901, when a fork switch occurs in the blockchain, determine the block height information that needs to be synchronously switched; fork switching refers to the process in which the first chain on the blockchain switches from the main chain to the forked chain, and the second chain on the blockchain switches from the forked chain to the main chain; the block height information includes at least one block height.

[0113] In the embodiment of the present application, the execution process of step S901 is the same as that of the above Figure 7 The execution process of step S701 in the embodiment shown is the same. For the specific execution process, please refer to the above Figure 7 The description of step S701 in the illustrated embodiment will not be repeated here.

[0114] S902, based on the block height information, determining first block data corresponding to at least one block height in the block height information on the first chain, and determining second block data corresponding to at least one block height in the block height information on the second chain.

[0115] In the embodiment of the present application, the execution process of step S902 is the same as that of the above Figure 7 The execution process of step S702 in the embodiment shown is the same. For the specific execution process, please refer to the above Figure 7 The description of step S702 in the illustrated embodiment will not be repeated here.

[0116] S903, changing the state information of the first block data to the forked chain state information in the database, and changing the state information of the second block data to the main chain state information.

[0117] In the embodiment of the present application, the execution process of step S903 is the same as that of the above Figure 7 The execution process of step S703 in the embodiment shown is the same. For the specific execution process, please refer to the above Figure 7 The description of step S703 in the illustrated embodiment will not be repeated here.

[0118] It should be noted that steps S901 to S903 in the embodiment of the present application introduce a process in which the database performs synchronous switching of the synchronously stored block data when a fork switch occurs in the blockchain (hereinafter referred to as the process of database synchronous switching); after the synchronous switching is completed, the database may, if there is insufficient memory space, choose to delete the block data whose status information is the forked chain status information to release the database memory space occupied by the block data whose status information is the forked chain status information, or the database may, if there is sufficient memory space, choose to retain the block data whose status information is the forked chain status information.

[0119] It should also be noted that in the implementation of this application, steps S904-S907 introduce the process of providing external query by the database (hereinafter referred to as the process of external query by the database); the embodiment of this application does not limit the execution order of the process of synchronous switching of the database and the process of providing external query by the database. The process of providing external query by the database can occur at any time, the process of providing external query by the database can occur before the process of synchronous switching of the database, the process of providing external query by the database can occur during the process of synchronous switching of the database, or the process of providing external query by the database can occur after the process of synchronous switching of the database. The process of providing external query by the database is introduced below in combination with steps S904-S907.

[0120] S904, receiving a data query request.

[0121] The data synchronization device can receive the data query request sent by the data query device, and forward the data query request to the database deployed by the data synchronization device, and the database processes the data query request.

[0122] S905, parsing the data query request to obtain data query conditions.

[0123] After receiving the data query request, the database in the data synchronization device can parse the data query request to obtain the data query conditions. In the actual data query process, the data query conditions can be converted into database query statements that comply with the database regulations for query.

[0124] It is easy to think that hash information has the function of unique identification on the blockchain. For example, transaction hash can be used to uniquely identify the corresponding transaction, and block hash can be used to uniquely identify the corresponding block. Therefore, based on the uniqueness of hash information, when the data query request carries hash information, the query can be directly based on the hash information. However, the blockchain may fork, and the block height on the blockchain does not have the function of unique identification. For example, after the blockchain forks, the same block height may correspond to multiple different blocks. Therefore, based on the non-uniqueness of the block height, when the data query request carries the block height, it is necessary to encapsulate the main chain condition for the data query request, and then query based on the block height and the main chain condition. After adding the main chain condition to the block height, the block data on the main chain can be filtered out for query. The block height on the main chain is unique, and the block data on the main chain is recognized by the blockchain network, which can improve the accuracy of the database's external blockchain query.

[0125] Specifically, the data query request can be parsed. If the parsing result indicates that the data query request carries hash information (for example, the transaction information may include block hash and / or transaction hash), the data query condition can be generated based on the hash information; if the parsing result indicates that the data query request carries block range information, the data query condition can be generated based on the block height range and the main chain status information indicated by the block range information, so that when performing a query, the query can be performed in the block data of the main chain status information where the filtered status information is the main chain status information; if the parsing result indicates that the data query request carries time range information, and there is a corresponding relationship between the block height and the block generation time on the blockchain, the time range information can be converted into a block height range, and the data query condition can be generated based on the block height range and the main chain status information, so that when performing a query, the query can be performed in the block data of the main chain status information where the filtered status information is the main chain status information.

[0126] S906, based on the data query condition, query the block data that has been synchronously stored on the blockchain.

[0127] As described above, the data query request may carry hash information or a block height range (including the block height range indicated by the block range information and the block height range corresponding to the time range information). When the data query request carries hash information, based on the data query condition, querying the block data that has been synchronously stored on the blockchain may include: querying the data corresponding to the hash information in the block data that has been synchronously stored on the blockchain based on the data query condition generated according to the hash information.

[0128] When a block height range is carried in a data query request, it can be understood that when the data query request is used to request a query on block data within a block height range, the block height range requested for query may overlap with the block height range indicated by the block height information that needs to be synchronously switched. In this case, based on the data query conditions, querying in the block data that has been synchronously stored on the blockchain may include: when the block height range requested for query overlaps with the block height range indicated by the block height information, before completing the synchronous switching, the query may be performed in the main chain block data before the synchronous switching, and the main chain block data before the synchronous switching refers to the block data whose status information before the synchronous switching is the main chain status information; after completing the synchronous switching, the query may be performed in the main chain block data after the synchronous switching is completed, and the main chain block data after the synchronous switching is completed may refer to the block data whose status information after the synchronous switching is completed is the main chain status information. From the analysis, it can be seen that before the synchronous switching is completed, the query is conducted in the main chain block data before the synchronous switching, and after the synchronous switching is completed, the query is conducted in the main chain block data after the synchronous switching. This can ensure the consistency of the database's external blockchain query, and there will be no situation where part of the data query results belong to the main chain block data before the synchronous switching, and the other part of the data query results belong to the main chain block data after the synchronous switching.

[0129] S907, return the data query result.

[0130] Based on the data query conditions, after querying from the block data that has been synchronized and stored on the blockchain, the database deployed in the data synchronization device can return the data query result to the data synchronization device, and the data synchronization device can return the data query result to the data query request.

[0131] In the embodiment of the present application, the chain (main chain or forked chain) to which the synchronized block data belongs on the blockchain can be identified in the database through the status information. When the blockchain is forked and switched, the synchronized block data in the database can be synchronized and switched by changing the status information of the block data. In other words, the embodiment of the present application can synchronize the synchronized block data in the database when the blockchain is forked and switched, so that the block data on the blockchain can be kept consistent with the block data synchronized in the database. In addition, when the block height is carried in the data query request, the main chain condition can be encapsulated for the data query request, and the query is performed based on the block height and the main chain condition. After adding the main chain condition to the block height, the block data on the main chain can be screened out for query. The block height on the main chain is unique, and the block data on the main chain is recognized by the blockchain network, which can improve the accuracy of the database's external blockchain query. In addition, when the block height range requested for query overlaps with the block height range indicated by the block height information, the query can be performed in the main chain block data before the synchronous switching is completed, and the query can be performed in the main chain block data after the synchronous switching is completed. This can ensure the consistency of the database's external blockchain queries, and there will be no situation where part of the data query results belong to the main chain block data before the synchronous switching, and the other part of the data query results belong to the main chain block data after the synchronous switching.

[0132] The method of the embodiment of the present application is described in detail above. In order to facilitate better implementation of the above scheme of the embodiment of the present application, the device of the embodiment of the present application is provided below accordingly.

[0133] See also Fig.10 , Fig.10 is a schematic diagram of a data synchronization device based on blockchain provided in an embodiment of the present application. The data synchronization device based on blockchain can be provided in a computer device provided in an embodiment of the present application. For example, the computer device can be Figure 6 A data synchronization device 602 is shown in the data synchronization system. Fig.10 The blockchain-based data synchronization device shown can be a computer program running in a computer device, and the blockchain-based data synchronization device can be used to execute Figure 7 or Fig. 9 Some or all of the steps in the method embodiment shown.

[0134] The blockchain includes the first chain and the second chain. The first chain refers to the main chain recognized by the blockchain network, and the second chain refers to the forked chain generated by the first chain. The blockchain corresponds to the database, and the database is used to synchronize and store the block data on the blockchain. The block data on the blockchain has status information in the database. The status information of the block data on the main chain is the main chain status information, and the status information of the block data on the forked chain is the forked chain status information. The data synchronization device based on the blockchain is set in the database, please refer to Fig.10 , the blockchain-based data synchronization device may include the following units:

[0135] The determining unit 1001 is used to determine the block height information that needs to be synchronously switched when a fork switch occurs in the blockchain; the fork switch refers to the process in which the first chain on the blockchain switches from the main chain to the forked chain, and the second chain on the blockchain switches from the forked chain to the main chain; the block height information includes at least one block height;

[0136] The determining unit 1001 is further used to determine, based on the block height information, first block data corresponding to at least one block height in the block height information on the first chain, and to determine second block data corresponding to at least one block height in the block height information on the second chain;

[0137] The processing unit 1002 is used to change the state information of the first block data into the forked chain state information in the database, and change the state information of the second block data into the main chain state information.

[0138] In one implementation, the processing unit 1002 is further configured to perform the following steps:

[0139] Synchronize and store block data on the main chain in real time from the blockchain;

[0140] Get the fork switching rules of the blockchain;

[0141] Determine the fork data synchronization method corresponding to the fork switching rule;

[0142] According to the determined fork data synchronization method, the block data on the forked chain is synchronized and stored from the blockchain.

[0143] In one implementation, the fork switching rule includes: the number of blocks that need to be waited from when the blockchain forks to when the blockchain forks;

[0144] The processing unit 1002 is used to determine the fork data synchronization mode corresponding to the fork switching rule, and is specifically used to perform the following steps:

[0145] If the number of blocks to be waited is greater than or equal to the block number threshold, the fork data synchronization mode corresponding to the fork switching rule is determined to be the real-time fork synchronization mode;

[0146] If the number of blocks to be waited is less than the block number threshold, it is determined that the fork data synchronization mode corresponding to the fork switching rule is a non-real-time fork synchronization mode.

[0147] In one implementation, the fork data synchronization mode corresponding to the fork switching rule includes a non-real-time fork synchronization mode or a real-time fork synchronization mode;

[0148] When the fork data synchronization mode corresponding to the fork switching rule is a non-real-time fork synchronization mode, the processing unit 1002 is used to synchronize and store the block data on the forked chain from the blockchain according to the determined fork data synchronization mode, specifically to perform the following steps:

[0149] When a fork occurs on the blockchain, the block data required for the synchronous switch is stored synchronously from the forked chain;

[0150] When the fork data synchronization mode corresponding to the fork switching rule is the real-time fork synchronization mode, the processing unit 1002 is used to synchronize and store the block data on the forked chain from the blockchain according to the determined fork data synchronization mode, specifically to perform the following steps:

[0151] When a blockchain forks, the block data on the forked chain is synchronized in real time.

[0152] In one implementation, the state information of the block data on the blockchain in the database refers to the field value of the state field;

[0153] The processing unit 1002 is used to change the state information of the first block data to the forked chain state information in the database, and change the state information of the second block data to the main chain state information, specifically to perform the following steps:

[0154] Create a first status update transaction and a second status update transaction, the first status update transaction is used to update the field value of the status field corresponding to the first block data, and the second status update transaction is used to update the field value of the status field corresponding to the second block data;

[0155] Execute a first status update transaction to change the field value of the status field corresponding to the first block data to the forked chain status information; and execute a second status update transaction to change the field value of the status field corresponding to the second block data to the main chain status information.

[0156] In one implementation, the processing unit 1002 is further configured to perform the following steps:

[0157] Receive data query requests;

[0158] Parse the data query request and obtain the data query conditions;

[0159] Based on the data query conditions, query the block data that has been synchronized and stored on the blockchain;

[0160] Returns the data query results.

[0161] In one implementation, the processing unit 1002 is used to parse the data query request and, when obtaining the data query condition, specifically to perform the following steps:

[0162] If the parsing result indicates that the data query request carries hash information, then the data query condition is generated according to the hash information; the hash information includes block hash and / or transaction hash;

[0163] If the parsing result indicates that the data query request carries block range information, then the data query condition is generated according to the block height range indicated by the block range information and the main chain status information;

[0164] If the parsing result indicates that the data query request carries time range information, the time range information is converted into a block height range, and the data query conditions are generated according to the block height range and the main chain status information.

[0165] In one implementation, the data query request is used to request a query for block data within a block height range; the processing unit 1002 is used to perform the following steps when querying from the block data that has been synchronously stored on the blockchain based on the data query condition:

[0166] When the block height range requested for query overlaps with the block height range indicated by the block height information, before completing the synchronous switching, the query is performed in the main chain block data before the synchronous switching, and the main chain block data before the synchronous switching refers to the block data whose status information before the synchronous switching is the main chain status information;

[0167] After the synchronous switching is completed, the query is performed in the main chain block data after the synchronous switching is completed. The main chain block data after the synchronous switching is completed refers to the block data whose status information after the synchronous switching is completed is the main chain status information.

[0168] According to another embodiment of the present application, Fig.10The various units in the blockchain-based data synchronization device shown can be separately or completely combined into one or several other units to constitute, or one (some) of the units can be further divided into multiple functionally smaller units to constitute, which can achieve the same operation without affecting the realization of the technical effects of the embodiments of the present application. The above-mentioned units are divided based on logical functions. In practical applications, the functions of one unit can also be implemented by multiple units, or the functions of multiple units can be implemented by one unit. In other embodiments of the present application, the blockchain-based data synchronization device may also include other units. In practical applications, these functions can also be implemented with the assistance of other units, and can be implemented by the collaboration of multiple units.

[0169] According to another embodiment of the present application, the program can be executed by running on a general computing device such as a computer including a central processing unit (CPU), a random access memory medium (RAM), a read-only memory medium (ROM), and other processing elements and storage elements. Figure 7 or Fig. 9 A computer program for each step involved in part or all of the method shown in the figure is used to construct a computer program for each step involved in part or all of the method shown in the figure. Fig.10 The data synchronization device based on blockchain shown in the embodiment of the present application is implemented to realize the data synchronization method based on blockchain. The computer program can be recorded on, for example, a computer-readable storage medium, and loaded into the above-mentioned computing device through the computer-readable storage medium, and run therein.

[0170] In the embodiment of the present application, the database can be used to synchronously store the block data on the blockchain, and the block data on the blockchain has status information in the database. The status information of the block data on the main chain in the blockchain is the main chain status information, and the status information of the block data on the forked chain in the blockchain is the forked chain status information. The blockchain may include a first chain (the first chain is the main chain) and a second chain (the second chain forked chain). When the blockchain forks and switches, the block height information that the database needs to synchronize with can be determined, and the first block data corresponding to the block height information can be determined in the first chain, and the second block data corresponding to the block height information can be determined in the second chain. Then, the status information of the first block data can be changed to the forked chain status information, and the status information of the second block data can be changed to the main chain fork information. It can be seen that the embodiment of the present application can identify the chain (main chain or forked chain) to which the synchronized block data belongs on the blockchain in the database through the status information. When the blockchain is forked, the synchronized block data in the database can be synchronized by changing the status information of the block data. That is to say, the embodiment of the present application can synchronize the synchronized block data in the database when the blockchain is forked, so that the block data on the blockchain can be kept consistent with the block data synchronized in the database.

[0171] Based on the above method and device embodiments, the present application embodiment provides a computer device. Fig.11 , Fig.11 It is a structural diagram of a computer device provided in an embodiment of the present application. Fig.11 The computer device shown includes at least a processor 1101, an input interface 1102, an output interface 1103, and a computer-readable storage medium 1104. The processor 1101, the input interface 1102, the output interface 1103, and the computer-readable storage medium 1104 may be connected via a bus or other means.

[0172] The computer-readable storage medium 1104 may be stored in a memory of a computer device. The computer-readable storage medium 1104 is used to store a computer program. The computer program includes computer instructions. The processor 1101 is used to execute the computer program stored in the computer-readable storage medium 1104. The processor 1101 (or CPU (Central Processing Unit)) is the computing core and control core of the computer device, which is suitable for implementing a computer program, and is specifically suitable for loading and executing a computer program to implement a corresponding method flow or a corresponding function.

[0173] The embodiment of the present application also provides a computer-readable storage medium (Memory), which is a memory device in a computer device for storing programs and data. It is understandable that the computer-readable storage medium here can include both built-in storage media in the computer device and, of course, extended storage media supported by the computer device. The computer-readable storage medium provides a storage space that stores the operating system of the computer device. In addition, a computer program suitable for being loaded and executed by a processor is also stored in the storage space. It should be noted that the computer-readable storage medium here can be a high-speed RAM memory or a non-volatile memory (Non-Volatile Memory), such as at least one disk storage; optionally, it can also be at least one computer-readable storage medium located away from the aforementioned processor.

[0174] The computer device may be, for example, Figure 6 In the data synchronization system shown in FIG. 6 , in a specific implementation, the processor 1101 may load and execute a computer program stored in a computer-readable storage medium 1104 to implement the above-mentioned Figure 7 or Fig. 9The corresponding steps in the blockchain-based data synchronization method shown. The blockchain includes a first chain and a second chain. The first chain refers to the main chain recognized by the blockchain network, and the second chain refers to the forked chain generated by the first chain. The blockchain corresponds to a database, and the database is used to synchronize and store block data on the blockchain. The block data on the blockchain has status information in the database, and the status information of the block data on the main chain is the main chain status information, and the status information of the block data on the forked chain is the forked chain status information. In a specific implementation, the computer program in the computer-readable storage medium 1104 is loaded by the processor 1101 and executes the following steps:

[0175] When a fork switch occurs in the blockchain, determine the block height information that needs to be switched synchronously; fork switching refers to the process in which the first chain on the blockchain switches from the main chain to the forked chain, and the second chain on the blockchain switches from the forked chain to the main chain; the block height information includes at least one block height;

[0176] Based on the block height information, determining first block data corresponding to at least one block height in the block height information on the first chain, and determining second block data corresponding to at least one block height in the block height information on the second chain;

[0177] In the database, the state information of the first block data is changed to the forked chain state information, and the state information of the second block data is changed to the main chain state information.

[0178] In one implementation, the computer program in the computer-readable storage medium 1104 is loaded by the processor 1101 and is further used to perform the following steps:

[0179] Synchronize and store block data on the main chain in real time from the blockchain;

[0180] Get the fork switching rules of the blockchain;

[0181] Determine the fork data synchronization method corresponding to the fork switching rule;

[0182] According to the determined fork data synchronization method, the block data on the forked chain is synchronized and stored from the blockchain.

[0183] In one implementation, the fork switching rule includes: the number of blocks that need to be waited from when the blockchain forks to when the blockchain forks;

[0184] When the computer program in the computer readable storage medium 1104 is loaded and executed by the processor 1101 to determine the fork data synchronization mode corresponding to the fork switching rule, it is specifically used to perform the following steps:

[0185] If the number of blocks to be waited is greater than or equal to the block number threshold, the fork data synchronization mode corresponding to the fork switching rule is determined to be the real-time fork synchronization mode;

[0186] If the number of blocks to be waited is less than the block number threshold, it is determined that the fork data synchronization mode corresponding to the fork switching rule is a non-real-time fork synchronization mode.

[0187] In one implementation, the fork data synchronization mode corresponding to the fork switching rule includes a non-real-time fork synchronization mode or a real-time fork synchronization mode;

[0188] When the fork data synchronization mode corresponding to the fork switching rule is a non-real-time fork synchronization mode, the computer program in the computer-readable storage medium 1104 is loaded and executed by the processor 1101 to synchronously store the block data on the forked chain from the blockchain according to the determined fork data synchronization mode, specifically for performing the following steps:

[0189] When a fork occurs on the blockchain, the block data required for the synchronous switch is stored synchronously from the forked chain;

[0190] When the fork data synchronization mode corresponding to the fork switching rule is the real-time fork synchronization mode, the computer program in the computer-readable storage medium 1104 is loaded and executed by the processor 1101 to synchronize and store the block data on the forked chain from the blockchain according to the determined fork data synchronization mode, specifically for performing the following steps:

[0191] When a blockchain forks, the block data on the forked chain is synchronized in real time.

[0192] In one implementation, the state information of the block data on the blockchain in the database refers to the field value of the state field;

[0193] When the computer program in the computer-readable storage medium 1104 is loaded and executed by the processor 1101 to change the state information of the first block data to the forked chain state information in the database, and to change the state information of the second block data to the main chain state information, it is specifically used to perform the following steps:

[0194] Create a first status update transaction and a second status update transaction, the first status update transaction is used to update the field value of the status field corresponding to the first block data, and the second status update transaction is used to update the field value of the status field corresponding to the second block data;

[0195] Execute a first status update transaction to change the field value of the status field corresponding to the first block data to the forked chain status information; and execute a second status update transaction to change the field value of the status field corresponding to the second block data to the main chain status information.

[0196] In one implementation, the computer program in the computer-readable storage medium 1104 is loaded by the processor 1101 and is further used to perform the following steps:

[0197] Receive data query requests;

[0198] Parse the data query request and obtain the data query conditions;

[0199] Based on the data query conditions, query the block data that has been synchronized and stored on the blockchain;

[0200] Returns the data query results.

[0201] In one implementation, the computer program in the computer-readable storage medium 1104 is loaded and executed by the processor 1101 to parse the data query request. When the data query condition is obtained, it is specifically used to perform the following steps:

[0202] If the parsing result indicates that the data query request carries hash information, then the data query condition is generated according to the hash information; the hash information includes block hash and / or transaction hash;

[0203] If the parsing result indicates that the data query request carries block range information, then the data query condition is generated according to the block height range indicated by the block range information and the main chain status information;

[0204] If the parsing result indicates that the data query request carries time range information, the time range information is converted into a block height range, and the data query conditions are generated according to the block height range and the main chain status information.

[0205] In one implementation, the data query request is used to request a query for block data within a block height range; the computer program in the computer-readable storage medium 1104 is loaded and executed by the processor 1101. Based on the data query condition, when querying from the block data that has been synchronously stored on the blockchain, the computer program is specifically used to perform the following steps:

[0206] When the block height range requested for query overlaps with the block height range indicated by the block height information, before completing the synchronous switching, the query is performed in the main chain block data before the synchronous switching, and the main chain block data before the synchronous switching refers to the block data whose status information before the synchronous switching is the main chain status information;

[0207] After the synchronous switching is completed, the query is performed in the main chain block data after the synchronous switching is completed. The main chain block data after the synchronous switching is completed refers to the block data whose status information after the synchronous switching is completed is the main chain status information.

[0208] In the embodiment of the present application, the database can be used to synchronously store the block data on the blockchain, and the block data on the blockchain has status information in the database. The status information of the block data on the main chain in the blockchain is the main chain status information, and the status information of the block data on the forked chain in the blockchain is the forked chain status information. The blockchain may include a first chain (the first chain is the main chain) and a second chain (the second chain forked chain). When the blockchain forks and switches, the block height information that the database needs to synchronize with can be determined, and the first block data corresponding to the block height information can be determined in the first chain, and the second block data corresponding to the block height information can be determined in the second chain. Then, the status information of the first block data can be changed to the forked chain status information, and the status information of the second block data can be changed to the main chain fork information. It can be seen that the embodiment of the present application can identify the chain (main chain or forked chain) to which the synchronized block data belongs on the blockchain in the database through the status information. When the blockchain is forked, the synchronized block data in the database can be synchronized by changing the status information of the block data. That is to say, the embodiment of the present application can synchronize the synchronized block data in the database when the blockchain is forked, so that the block data on the blockchain can be kept consistent with the block data synchronized in the database.

[0209] According to one aspect of the present application, a computer program product is provided, the computer program product comprising a computer program, the computer program being stored in a computer-readable storage medium. A processor of a computer device reads the computer program from the computer-readable storage medium, and the processor executes the computer program, so that the computer device executes the blockchain-based data synchronization method provided in the above-mentioned various optional modes.

[0210] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.

Claims

1. A data synchronization method based on blockchain, characterized in that: The blockchain includes a first chain and a second chain, the first chain refers to a main chain recognized by the blockchain network, and the second chain refers to a forked chain generated by the first chain; the blockchain corresponds to a database, and the database is used to synchronously store block data on the blockchain; the block data on the blockchain has status information in the database, the status information of the block data on the main chain is the main chain status information, and the status information of the block data on the forked chain is the forked chain status information; the method includes: When a fork switch occurs in the blockchain, determining the block height information that needs to be synchronously switched; the fork switch refers to the process in which the first chain on the blockchain switches from the main chain to the forked chain, and the second chain on the blockchain switches from the forked chain to the main chain; the block height information includes at least one block height; Based on the block height information, determining, on the first chain, first block data corresponding to at least one block height in the block height information, and determining, on the second chain, second block data corresponding to at least one block height in the block height information; In the database, the state information of the first block data is changed to the forked chain state information, and the state information of the second block data is changed to the main chain state information.

2. The method according to claim 1, characterized in that The method further comprises: Synchronously storing the block data on the main chain from the blockchain in real time; Obtaining the fork switching rules of the blockchain; Determine a fork data synchronization method corresponding to the fork switching rule; According to the determined fork data synchronization method, the block data on the forked chain is synchronously stored from the blockchain.

3. The method according to claim 2, characterized in that The fork switching rule includes: the number of blocks that need to be waited from the occurrence of a fork in the blockchain to the determination of the occurrence of a fork switching in the blockchain; The step of determining the fork data synchronization mode corresponding to the fork switching rule includes: If the number of blocks to be waited is greater than or equal to the block number threshold, determining that the fork data synchronization mode corresponding to the fork switching rule is a real-time fork synchronization mode; If the number of blocks that need to be waited for is less than the block number threshold, it is determined that the fork data synchronization mode corresponding to the fork switching rule is a non-real-time fork synchronization mode.

4. The method according to claim 2 or 3, characterized in that The fork data synchronization mode corresponding to the fork switching rule includes a non-real-time fork synchronization mode or a real-time fork synchronization mode; When the fork data synchronization mode corresponding to the fork switching rule is a non-real-time fork synchronization mode, the step of synchronously storing the block data on the forked chain from the blockchain according to the determined fork data synchronization mode includes: When the blockchain undergoes a fork switch, the block data required for the synchronous switch is synchronously stored from the forked chain; When the fork data synchronization mode corresponding to the fork switching rule is a real-time fork synchronization mode, the step of synchronously storing the block data on the forked chain from the blockchain according to the determined fork data synchronization mode includes: When the blockchain is forked, the block data on the forked chain is synchronized in real time.

5. The method according to claim 1, characterized in that The state information of the block data on the blockchain on the database refers to the field value of the state field; The step of changing the state information of the first block data to the forked chain state information in the database, and changing the state information of the second block data to the main chain state information, includes: Creating a first status update transaction and a second status update transaction, wherein the first status update transaction is used to update a field value of a status field corresponding to the first block data, and the second status update transaction is used to update a field value of a status field corresponding to the second block data; Execute the first status update transaction to change the field value of the status field corresponding to the first block data to the forked chain status information; and execute the second status update transaction to change the field value of the status field corresponding to the second block data to the main chain status information.

6. The method according to claim 1, characterized in that The method further comprises: Receive data query requests; Parsing the data query request to obtain data query conditions; Based on the data query condition, query the block data that has been synchronously stored on the blockchain; Returns the data query results.

7. The method according to claim 6, characterized in that The step of parsing the data query request to obtain data query conditions includes: If the parsing result indicates that the data query request carries hash information, generating the data query condition according to the hash information; the hash information includes a block hash and / or a transaction hash; If the parsing result indicates that the data query request carries block range information, then generating the data query condition according to the block height range indicated by the block range information and the main chain status information; If the parsing result indicates that the data query request carries time range information, the time range information is converted into a block height range, and the data query condition is generated according to the block height range and the main chain status information.

8. The method according to claim 6, characterized in that The data query request is used to request to query the block data within the block height range; the query based on the data query condition is performed in the block data that has been synchronously stored on the blockchain, including: When the block height range requested for query overlaps with the block height range indicated by the block height information, before completing the synchronous switching, query is performed in the main chain block data before the synchronous switching, and the main chain block data before the synchronous switching refers to the block data whose state information before the synchronous switching is the main chain state information; After the synchronous switching is completed, a query is performed in the main chain block data after the synchronous switching is completed, and the main chain block data after the synchronous switching is completed refers to the block data whose state information after the synchronous switching is completed is the main chain state information.

9. A data synchronization device based on blockchain, characterized in that: The blockchain includes a first chain and a second chain, wherein the first chain refers to a main chain recognized by the blockchain network, and the second chain refers to a forked chain generated by the first chain; the blockchain corresponds to a database, and the database is used to synchronously store block data on the blockchain; the block data on the blockchain has status information in the database, the status information of the block data on the main chain is the main chain status information, and the status information of the block data on the forked chain is the forked chain status information; The blockchain-based data synchronization device is arranged in the database; the device comprises: a determination unit, configured to determine block height information that needs to be synchronously switched when a fork switch occurs in the blockchain; the fork switch refers to a process in which the first chain on the blockchain switches from the main chain to the forked chain, and the second chain on the blockchain switches from the forked chain to the main chain; the block height information includes at least one block height; The determining unit is further configured to determine, based on the block height information, first block data corresponding to at least one block height in the block height information on the first chain, and to determine second block data corresponding to at least one block height in the block height information on the second chain; The processing unit is used to change the state information of the first block data to the forked chain state information in the database, and change the state information of the second block data to the main chain state information.

10. A computer device, characterized in that: The computer device comprises: a processor suitable for implementing a computer program; A computer-readable storage medium storing a computer program, wherein the computer program is suitable for being loaded by the processor and executing the blockchain-based data synchronization method according to any one of claims 1 to 8.

11. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and the computer program is suitable for being loaded by a processor and executing the blockchain-based data synchronization method as described in any one of claims 1 to 8.

12. A computer program product, characterized in that The computer program product includes a computer program, and when the computer program is executed by a processor, the blockchain-based data synchronization method according to any one of claims 1 to 8 is implemented.