Blockchain data synchronization method, device, computer equipment and storage medium
By adopting tree structure and Merkel proof in the blockchain system, parallel verification and synchronization of blockchain data synchronization is realized, the problem of inefficient synchronization in traditional blockchains is solved, and data synchronization efficiency and CPU resource utilization are improved.
Patent Information
- Application Number
- CN202111166222.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-09-30
- Publication Date
- 2025-07-22
- Estimated Expiration
- 2041-09-30
AI Technical Summary
During the traditional blockchain data synchronization process, behind nodes need to verify the block hash value one by one, resulting in inefficient synchronization.
A blockchain system adopts a tree structure, by obtaining the target root hash value and target block height, and obtaining the block to be synchronized and the Merkel proof in parallel, verifying and data synchronization based on the Merkel proof.
The efficiency of blockchain data synchronization is improved. Through parallel verification and synchronization of blocks to be synchronized, the dependence on other blocks is reduced and the utilization of CPU resources is improved.
Smart Images

Figure CN113886496B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of blockchain technology, and in particular, to a method and apparatus for data synchronization of a blockchain, a computer device, and a storage medium. Background Art
[0002] In a traditional blockchain architecture, when there are lagging nodes in the blockchain system due to network latency or newly added nodes, etc., the lagging nodes need to synchronize data with other nodes.
[0003] In related technologies, a lagging node needs to first obtain the latest block (for example: the block of Block N+1), and based on this latest block, sequentially request blocks N, N-1, N-2, ……, 3, 2, 1, 0 from the peer node in reverse order, where the peer node is other nodes in the blockchain system except the lagging node. The lagging node needs to verify that the hash value of the block X requested from the peer node is equal to the parent block hash value recorded in block X+1, so as to confirm that the requested block X is the correct block (X is a positive integer less than or equal to N). And after requesting and verifying all blocks, sequentially execute blocks 0, 1, 2, 3, ……, N-2, N-1, N until finally restoring to the latest block height.
[0004] In the above data synchronization process, the verification of block X is limited by block X+1, so the block fetching phase can only fetch and verify the next block after the previous fetched block is verified, and the data synchronization efficiency is low. Summary of the Invention
[0005] Based on this, it is necessary to provide a method and apparatus for data synchronization of a blockchain, a computer device, and a storage medium that can improve the data synchronization efficiency of the blockchain for the above technical problems.
[0006] A method for data synchronization of a blockchain, for any node in the blockchain system, the blockchain in the node adopts a tree structure, the leaf nodes in the tree structure are blocks, and the root node of the tree structure is the hash value of the node state after the latest block in the node is executed. The method includes:
[0007] Obtain a target root hash value and a target block height from a peer node, where the target root hash value is the root node of the blockchain in the peer node, and the target block height is the block height of the blockchain in the peer node;
[0008] Obtain at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes according to the target block height;
[0009] Verify each of the blocks to be synchronized based on the Merkle proof of each block to be synchronized and the target root hash value;
[0010] When each of the blocks to be synchronized passes the verification, perform data synchronization on each of the blocks to be synchronized.
[0011] In one embodiment, the obtaining at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes according to the target block height includes:
[0012] Obtain at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes in parallel.
[0013] In one embodiment, the verifying each of the blocks to be synchronized based on the Merkle proof of each block to be synchronized and the target root hash value includes:
[0014] For any block to be synchronized, determine a consensus hash value according to the hash value of the block to be synchronized and the Merkle proof of the block to be synchronized;
[0015] When the consensus hash value is consistent with the target root hash value, determine that the block to be synchronized passes the verification.
[0016] In one embodiment, the performing data synchronization on each of the blocks to be synchronized when each of the blocks to be synchronized passes the verification includes:
[0017] For any block to be synchronized X, when the block to be synchronized X passes the verification, determine whether the previous block X-1 of the block to be synchronized X has been completed for execution;
[0018] When the block X-1 has been completed for execution, execute the block to be synchronized X;
[0019] Otherwise, store the block to be synchronized in local memory until the block X-1 is completed for execution, and then execute the block to be synchronized X.
[0020] In one embodiment, the executing the block to be synchronized X includes:
[0021] Add a leaf node corresponding to the block to be synchronized X to the tree structure;
[0022] Update the child nodes and the root node of the tree structure according to the hash value of the block to be synchronized X.
[0023] In one embodiment, obtaining at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes includes:
[0024] Obtaining at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes in parallel;
[0025] When the difference between the first quantity of the blocks to be synchronized obtained and the second quantity of the blocks to be synchronized that have been executed is greater than or equal to a first threshold, suspending obtaining the blocks to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes until the difference between the first quantity and the third quantity of the blocks to be synchronized that have been executed is less than or equal to a second threshold, and then continuing to obtain the blocks to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes.
[0026] In one embodiment, the method further includes:
[0027] Receiving a request for block N from a lagging node;
[0028] After obtaining block N and the Merkle proof of block N from the blockchain, sending block N and the Merkle proof of block N to the lagging node.
[0029] A data synchronization device for a blockchain, for any node in a blockchain system, where the blockchain in the node adopts a tree structure, the leaf nodes in the tree structure are blocks, and the root node of the tree structure is the hash value of the node state after the latest block in the node is executed. The device includes:
[0030] A first acquisition module, configured to acquire a target root hash value and a target block height from a peer node, where the target root hash value is the root node of the blockchain in the peer node, and the target block height is the block height of the blockchain in the peer node;
[0031] A second acquisition module, configured to acquire at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes according to the target block height;
[0032] A verification module, configured to verify each block to be synchronized according to the Merkle proof of each block to be synchronized and the target root hash value;
[0033] A synchronization module, configured to perform data synchronization on each block to be synchronized when each block to be synchronized passes the verification.
[0034] In one embodiment, the second acquisition module is further configured to:
[0035] Parallelly obtain at least one to-be-synchronized block and the Merkle proof of each to-be-synchronized block from at least one of the peer nodes.
[0036] In one embodiment, the verification module is further configured to:
[0037] For any to-be-synchronized block, determine a consensus hash value according to the hash value of the to-be-synchronized block and the Merkle proof of the to-be-synchronized block;
[0038] When the consensus hash value is consistent with the target root hash value, determine that the to-be-synchronized block passes the verification.
[0039] In one embodiment, the synchronization module is further configured to:
[0040] For any to-be-synchronized block X, when the to-be-synchronized block X passes the verification, determine whether the previous block X-1 of the to-be-synchronized block X has been completed for execution;
[0041] When the block X-1 has been completed for execution, execute the to-be-synchronized block X;
[0042] Otherwise, store the to-be-synchronized block in the local memory until the block X-1 is completed for execution, and then execute the to-be-synchronized block X.
[0043] In one embodiment, the synchronization module is further configured to:
[0044] Add a leaf node corresponding to the to-be-synchronized block X to the tree structure;
[0045] Update the child nodes and the root node of the tree structure according to the hash value of the to-be-synchronized block X.
[0046] In one embodiment, the second acquisition module is further configured to:
[0047] Parallelly obtain at least one to-be-synchronized block and the Merkle proof of each to-be-synchronized block from at least one of the peer nodes;
[0048] When the difference between the first quantity of the obtained to-be-synchronized blocks and the second quantity of the executed to-be-synchronized blocks is greater than or equal to a first threshold, suspend obtaining the to-be-synchronized blocks and the Merkle proof of each to-be-synchronized block from at least one of the peer nodes until the difference between the first quantity and the third quantity of the executed to-be-synchronized blocks is less than or equal to a second threshold, and then continue to obtain the to-be-synchronized blocks and the Merkle proof of each to-be-synchronized block from at least one of the peer nodes.
[0049] In one embodiment, the device further includes:
[0050] a receiving module, configured to receive a request for block N from a lagging node;
[0051] a sending module, configured to obtain block N and the Merkle proof of block N from the blockchain, and then send block N and the Merkle proof of block N to the lagging node.
[0052] A computer device includes a memory and a processor. The memory stores a computer program. When the processor executes the computer program, the following steps are implemented:
[0053] Obtain a target root hash value and a target block height from a peer node, where the target root hash value is the root node of the blockchain in the peer node, and the target block height is the block height of the blockchain in the peer node;
[0054] Obtain at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes according to the target block height;
[0055] Verify each block to be synchronized according to the Merkle proof of each block to be synchronized and the target root hash value;
[0056] When each block to be synchronized passes the verification, perform data synchronization on each block to be synchronized.
[0057] A computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the following steps are implemented:
[0058] Obtain a target root hash value and a target block height from a peer node, where the target root hash value is the root node of the blockchain in the peer node, and the target block height is the block height of the blockchain in the peer node;
[0059] Obtain at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes according to the target block height;
[0060] Verify each block to be synchronized according to the Merkle proof of each block to be synchronized and the target root hash value;
[0061] When each block to be synchronized passes the verification, perform data synchronization on each block to be synchronized.
[0062] The above data synchronization method, device, computer equipment, and storage medium for a blockchain. For any node in the blockchain system, the blockchain in this node adopts a tree structure, where the leaf nodes in the tree structure are blocks, and the root node of the tree structure is the hash value of the node state after the execution of the latest block in this node. A node can obtain a target root hash value and a target block height from a peer node. The target root hash value is the root node of the blockchain in the peer node, and the target block height is the block height of the blockchain in the peer node. After obtaining at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes according to the target block height, each block to be synchronized is verified based on the Merkle proof of each block to be synchronized and the target root hash value, and when each block to be synchronized passes the verification, data synchronization is performed on each block to be synchronized. The data synchronization method, device, computer equipment, and storage medium for a blockchain provided by the embodiments of the present disclosure verify the blocks to be synchronized according to the Merkle proof of the blocks to be synchronized, that is, the verification process of the blocks to be synchronized does not depend on other blocks, and the acquisition and verification of the blocks to be synchronized can be performed in parallel, which can improve the data synchronization efficiency of the blockchain. BRIEF DESCRIPTION OF THE DRAWINGS
[0063] Figure 1 FIG. is an application environment diagram of the data synchronization method for a blockchain in an embodiment;
[0064] Figures 2a to 2b FIG. is a schematic diagram of the data synchronization method for a blockchain in an embodiment;
[0065] Figure 3 FIG. is a flowchart of the data synchronization method for a blockchain in an embodiment;
[0066] Figure 4 FIG. is a flowchart of the data synchronization method for a blockchain in an embodiment;
[0067] Figure 5 FIG. is a flowchart of the data synchronization method for a blockchain in an embodiment;
[0068] Figure 6 FIG. is a flowchart of the data synchronization method for a blockchain in an embodiment;
[0069] Figures 7a to 7b FIG. is a schematic diagram of the data synchronization method for a blockchain in an embodiment;
[0070] Figure 8 FIG. is a flowchart of the data synchronization method for a blockchain in an embodiment;
[0071] Figure 9 FIG. is a flowchart of the data synchronization method for a blockchain in an embodiment;
[0072] Figure 10 The structural block diagram of the data synchronization device of the blockchain in an embodiment;
[0073] Figure 11 The internal structure diagram of a computer device in an embodiment. Detailed implementation manners
[0074] In order to make the objectives, technical solutions and advantages of the present application clearer, the present application will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0075] The data synchronization method of the blockchain provided by the present application can be applied to an application environment as Figure 1 shown. Among them, the blockchain system 100 includes a plurality of nodes 102. Inside any node 102, the blockchain is stored in a tree structure. The root node hash value recorded by the root node of the tree structure is the hash value of the node state after the execution of the latest block in this node 102, and the leaf nodes of the tree structure are blocks. Refer to Figures 2a to 2b shown, the blockchain is a tree structure of a Merkle tree. In Figure 2a the blockchain maintained by the lagging node, its latest block is marked as block N, and the root node is the hash value of the node after the execution of block N. Figure 2b the blockchain maintained by the peer node, its latest block is marked as block M, and the root node is the hash value of the node after the execution of block M, where M is greater than N, and both M and N are positive integers.
[0076] When there is a lagging node in the blockchain system 100, the blockchain maintained by the lagging node lags behind the blockchains maintained by other nodes. Therefore, it is necessary to synchronize data with other nodes to make the blockchains maintained by each node in the blockchain system consistent. The lagging node can obtain the target root hash value and the target block height from the peer node, and determine the blocks to be synchronized according to the target block height and the block height of the blockchain maintained locally. The above Figure 2a and Figure 2b in the example, the blocks to be synchronized include blocks N+1, N+2,..., M. After the lagging node requests each block to be synchronized and the Merkle proof of each block to be synchronized from at least one peer node, it can verify the correctness of the blocks to be synchronized according to the Merkle proof of each block to be synchronized and the target root hash value, and can perform data synchronization on the blocks to be synchronized when the blocks to be synchronized pass the verification.
[0077] That is, for the data synchronization method of the blockchain provided by the embodiments of the present disclosure, the block to be synchronized can be verified according to the Merkle proof of the block to be synchronized. The verification process of the block to be synchronized does not depend on other blocks. Therefore, the embodiments of the present disclosure can retrieve and verify the blocks to be synchronized in parallel, which can improve the data synchronization efficiency of the blockchain.
[0078] Among them, the node 102 can be, but is not limited to, various personal computers, laptop computers, smart phones, tablet computers, and portable wearable devices, and can also be implemented by an independent server or a server cluster composed of multiple servers.
[0079] In one embodiment, for any node in the blockchain system, the blockchain in each node adopts a tree structure. The leaf nodes in the tree structure are blocks, and the root node is the hash value of the node state after the execution of the latest block in the node. As Figure 3 shown, a data synchronization method of the blockchain is provided. Taking the method applied to Figure 1 the node 102 in it as an example for illustration, the method includes the following steps:
[0080] In step 302, obtain the target root hash value and the target block height from the peer node. The target root hash value is the root node of the blockchain in the peer node, and the target block height is the block height of the blockchain in the peer node.
[0081] In the embodiments of the present disclosure, the lagging node can obtain the target root hash value from the peer node. The target root hash value can be the hash value recorded by the root node of the blockchain in the peer node, and the target block height can be the block height of the blockchain in the peer node. In the embodiments of the present disclosure, taking Figure 2a the lagging node shown in Figure 2b and the peer node shown in
[0082] as an example, the target block height in the peer node obtained by the lagging node from the peer node is M, and the target root hash value is roothash2.
[0083] In the embodiments of the present disclosure, if the block height of the blockchain locally maintained by the lagging node is N and the target block height is M, the lagging node can request blocks N + 1, N + 2,..., M from at least one peer node. Taking block X as an example (X is an integer greater than or equal to N + 1 and less than or equal to M), the peer node can, in response to the request from the lagging node for block X, obtain block X and the Merkle proof of block X in the tree structure from the locally maintained blockchain, and then send block X and the Merkle proof of block X to the lagging node.
[0084] Taking N as 5, M as 8, and X as 6 as an example, the lagging node requests blocks 6, 7, and 8 from at least one peer node. In response to the request for block 6, one of the peer nodes obtains block 6 and the Merkle proof of block 6 from the tree structure, where the Merkle proof of block 6 includes: hash5, hash(hash7, hash8), hash(hash(hash1, hash2), hash(hash(hash3, hash4))).
[0085] It should be noted that in the embodiments of the present disclosure, the blocks can be requested in ascending order, in descending order, or in a random order. In the embodiments of the present disclosure, the order of block requests is not specifically limited.
[0086] In step 306, each block to be synchronized is verified according to the Merkle proof of each block to be synchronized and the target root hash value.
[0087] In the embodiments of the present disclosure, after obtaining each block to be synchronized, the lagging node can verify each block to be synchronized according to the Merkle proof of each block to be synchronized and the target root hash value. When the hash value obtained by performing a hash calculation on the Merkle proof and the hash value of the block to be synchronized is consistent with the target root hash value, it can be determined that the block to be synchronized is in the tree structure of the blockchain, that is, the block to be synchronized passes the verification.
[0088] In step 308, when each block to be synchronized passes the verification, data synchronization is performed on each block to be synchronized.
[0089] In the embodiments of the present disclosure, for each block to be synchronized that passes the verification, data synchronization can be performed, that is, it can be added to the tree structure of the blockchain in the lagging node as a leaf node of the tree structure.
[0090] The above blockchain data synchronization method is applicable to any node in the blockchain system. In this node, the blockchain adopts a tree structure, where the leaf nodes in the tree structure are blocks, and the root node of the tree structure is the hash value of the node state after the execution of the latest block in this node. A node can obtain the target root hash value and the target block height from a peer node. The target root hash value is the root node of the blockchain in the peer node, and the target block height is the block height of the blockchain in the peer node. After obtaining at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes according to the target block height, each block to be synchronized is verified based on the Merkle proof of each block to be synchronized and the target root hash value. When each block to be synchronized passes the verification, data synchronization is performed on each block to be synchronized. The blockchain data synchronization method provided by the embodiments of the present disclosure can verify the blocks to be synchronized according to the Merkle proof of the blocks to be synchronized, that is, the verification process of the blocks to be synchronized does not depend on other blocks. Therefore, the acquisition and verification of the blocks to be synchronized can be carried out in parallel, which can improve the data synchronization efficiency of the blockchain.
[0091] In one embodiment, step 304 may include:
[0092] Parallelly obtain at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes.
[0093] In the embodiments of the present disclosure, a lagging node can parallelly obtain at least one block to be synchronized and the Merkle proof of the block to be synchronized from at least one peer node. Exemplarily, taking the above example as an example, the lagging node can parallelly request block 6 from peer node 1, request block 7 from peer node 2, and request block 8 from peer node 3. Alternatively, the lagging node can also parallelly request block 6, block 7, and block 8 from peer node 1 or block node 2 or block node 3.
[0094] That is to say, the lagging node can parallelly select different peer nodes to request different blocks to be synchronized, which can maximize the utilization of network resources, and can parallelly verify different blocks to be synchronized, which can improve the utilization rate of CPU (central processing unit) resources and improve the data synchronization efficiency of the blockchain.
[0095] In one embodiment, referring to Figure 4 as shown, step 306 may include:
[0096] In step 402, for any block to be synchronized, determine the consensus hash value according to the hash value of the block to be synchronized and the Merkle proof of the block to be synchronized;
[0097] In step 404, when the consensus hash value is consistent with the target root hash value, it is determined that the block to be synchronized passes the verification.
[0098] In the embodiments of the present disclosure, for any block X to be synchronized, the consensus hash value can be obtained by performing a hash calculation based on the hash value of the block to be synchronized and the Merkle proof of the block X to be synchronized. Still taking the above example, when verifying block 6, the hash calculation can be performed based on the block hash value hash6 of block 6 and hash5 to obtain hash(hash5, hash6). Further, the hash calculation is performed based on hash(hash5, hash6) and hash(hash7, hash8) to obtain hash(hash(hash5, hash6), hash(hash(hash7, hash8))). Then, based on hash(hash(hash5, hash6), hash(hash(hash7, hash8))) and hash(hash(hash1, hash2), hash(hash(hash3, hash4))), the hash calculation is performed to obtain the consensus hash value.
[0099] When the consensus hash value is consistent with the target root hash value roothash2, it can be determined that the block to be synchronized is in the tree structure of the blockchain, and it is determined that the block to be synchronized passes the verification.
[0100] The blockchain data synchronization method provided by the embodiments of the present disclosure verifies the correctness of the block to be synchronized through the Merkle proof of the block to be synchronized. The verification of the block to be synchronized no longer depends on other blocks, so the verification processing of each block to be synchronized can be performed in parallel, which can improve the data synchronization efficiency and the utilization rate of CPU resources.
[0101] In one embodiment, referring to Figure 5 as shown, step 308 may include:
[0102] In step 502, for any block X to be synchronized, when the block X to be synchronized passes the verification, it is determined whether the previous block X-1 of the block X to be synchronized has been completed;
[0103] In step 504, when the block X-1 has been completed, the block X to be synchronized is executed; otherwise, the block to be synchronized is stored in the local memory until the block X-1 is completed, and then the block X to be synchronized is executed.
[0104] In the embodiments of the present disclosure, when the block X to be synchronized passes the verification, it can be determined whether the previous block X-1 of the block to be synchronized has been executed, that is, to find whether there is a leaf node corresponding to the block X-1 in the tree structure of the blockchain maintained locally, or to determine whether the current block height is the block X-1.
[0105] In one example, when there is a leaf node corresponding to the block X-1 in the tree structure or the current block height is the block X-1, it can be determined that the previous block X-1 has been executed, and then the block X to be synchronized can be executed. In another example, when there is no leaf node corresponding to the block X-1 in the tree structure or the current block height is not the block X-1, it can be determined that the previous block X-1 has not been executed, and then the current block X to be synchronized cannot be executed. The block X to be synchronized is stored in the local memory until the previous block X-1 is executed, and then the block X to be synchronized is read from the local memory for execution.
[0106] And so on. After all the blocks to be synchronized are executed, the lagging node completes the data synchronization process, and the blockchain maintained locally by the lagging node can be restored to the latest block height.
[0107] In one embodiment, referring to Figure 6 , in step 504 above, executing the block X to be synchronized includes:
[0108] In step 602, adding a leaf node corresponding to the block X to be synchronized to the tree structure;
[0109] In step 604, updating the child nodes and the root node of the tree structure according to the hash value of the block X to be synchronized.
[0110] In the embodiments of the present disclosure, when executing the block X to be synchronized (adding the block X to be synchronized to the blockchain), a leaf node corresponding to the block X to be synchronized can be added to the tree structure corresponding to the blockchain, and the child nodes and the root node associated with the leaf node corresponding to the block X to be synchronized in the tree structure are updated according to the hash value of the block X to be synchronized.
[0111] Still taking the above example as an example, after the block 6 to be synchronized passes the verification, it is determined that the block 5 is already in the tree structure, so the block 6 can be directly executed. The hash values of the block 6 and the block 5 can be hashed to obtain the corresponding child node hash(hash5, hash 6) (it should be noted that Figure 6The corresponding child nodes in are simplified as hash(5, 6), and the other nodes are analogous). Hash calculations are performed on the child nodes hash(hash5, hash 6) and hash(hash(hash 1, hash 2)) to obtain the corresponding root node roothash3. Figure 7a and Figure 7b As shown, the tree structure of the blockchain after block 6 is executed is as follows Figure 7b As shown, relative to Figure 7a In the blockchain before execution, the leaf node and child node hash(5,6) corresponding to block 6 are added to the tree structure, and the root node is updated from roothash3 to roothash4.
[0112] In one embodiment, referring to Figure 8 , step 304 may include:
[0113] In step 802, at least one block to be synchronized and the Merkle proof of each block to be synchronized are obtained from at least one peer node in parallel;
[0114] In step 804, when the difference between the first number of blocks to be synchronized obtained and the second number of blocks to be synchronized that have been executed is greater than or equal to the first threshold, the acquisition of the blocks to be synchronized and the Merkel proof of each block to be synchronized from at least one peer node is suspended until the difference between the first number and the third number of blocks to be synchronized that have been executed is less than or equal to the second threshold, and the acquisition of the blocks to be synchronized and the Merkel proof of each block to be synchronized from at least one of the peer nodes continues.
[0115] In the disclosed embodiment, the lagging node can request at least one block to be synchronized and the Merkle proof of the block to be synchronized from at least one peer node in parallel, and perform the verification and execution process of the block to be synchronized in parallel in the process of requesting the block to be synchronized. The first number of blocks to be synchronized currently obtained from the peer node and the second number of blocks to be synchronized that have been executed can be counted, and the difference between the first number and the second number can be determined. If the difference is greater than or equal to the first threshold, the acquisition of the block to be synchronized from the peer node can be suspended, and only the verification and execution process of the acquired block to be synchronized can be performed.
[0116] Until the difference between the first number and the third number of executed blocks to be synchronized is less than or equal to the second threshold, continue to obtain the blocks to be synchronized and the Merkle proof of the blocks to be synchronized from the opposite node.
[0117] Among them, the first threshold and the second threshold are preset differences, the first threshold is greater than the second threshold, and the specific values of the first threshold and the second threshold can be set by technical personnel in this field according to the memory of the lagging node, and the embodiments of the present disclosure do not make specific limitations on this.
[0118] According to the blockchain data synchronization method provided by the embodiments of the present disclosure, the backward nodes' requests for blocks to be synchronized can be controlled by preset first and second thresholds, which can reasonably control the consumption of network resources. By restricting the number of blocks cached in the local memory of the backward nodes, memory overflow can be avoided, and thus unnecessary database operations can be avoided.
[0119] In one embodiment, referring to Figure 9 , the above-mentioned blockchain data synchronization method may further include:
[0120] In step 902, receive a request from a backward node for block N;
[0121] In step 904, after obtaining block N and the Merkle proof of block N from the blockchain, send block N and the Merkle proof of block N to the backward node.
[0122] In the embodiments of the present disclosure, after the backward node completes data synchronization, the blockchain maintained by the backward node is the latest blockchain, that is, it can be used as a peer node (hereinafter simply referred to as a peer node) to provide blocks to other backward nodes (hereinafter simply referred to as backward nodes) to complete the data synchronization of the blockchains of other backward nodes.
[0123] In the case of receiving a request from a backward node for block N, block N and the Merkle proof of block N can be obtained from the tree-structured blockchain and sent to the backward node. Then, after the backward node verifies block N through the Merkle proof of block N, block N can be executed (the specific process can refer to the relevant description of the foregoing embodiments, and the embodiments of the present disclosure do not make specific limitations thereto).
[0124] To enable those skilled in the art to better understand the embodiments of the present disclosure, the following uses specific examples to illustrate the embodiments of the present disclosure.
[0125] Exemplarily, the blockchain maintained by each node in the blockchain system adopts a tree structure. In this tree structure, there is no longer a connection relationship between the blocks in the blockchain, that is, each block no longer has a parent block, and the hash value of the parent block is no longer stored in the block. All blocks will be used as leaf nodes in sequence to form a Merkle tree, and the hash value of the root will be used as the node state hash value after the nth block is executed for consensus. In this way, when any leaf node (block) of the tree is modified, it will cause the root node RootHash to be incorrect. Therefore, as long as the root node hash values are compared and found to be consistent, it can be confirmed that the hash values of all the first N blocks are consistent. And through RootHash, it can be proved by the Merkle proof method that any one of the leaf nodes (Block 0 to Block N) exists in this Merkle tree. The specific data synchronization process is as follows:
[0126] The lagging node first obtains a latest target root node hash value RootHash and the target block height, and according to the block height of the blockchain maintained locally and the target block height, sequentially requests the blocks to be synchronized from at least one peer node in parallel.
[0127] After receiving a request for block X, any peer node returns the corresponding block X and the Merkle proof from the leaf node corresponding to block X to the root node.
[0128] The lagging node can verify in parallel through the returned Merkle proof that any block X is indeed in the Merkle tree, so as to confirm that the requested block is the correct block. And after determining that the previous block X-1 of the currently requested block X has been verified and executed, it starts to execute the block X, without waiting for all the blocks to be requested before starting to execute, and finally restores to the latest block height.
[0129] During the process of the lagging node requesting blocks, cache thresholds can be preset, including a first threshold and a second threshold. When the number X of the blocks to be synchronized requested exceeds the number Y of the blocks to be synchronized that have been executed by more than the first threshold (for example, the first threshold is set to 100, X - Y >= 100), the request for blocks can be paused until the number of executed blocks reaches Z, and when the difference between the number of requested blocks and the number of executed blocks is less than the second threshold (for example, the first threshold is set to 50, X - Z <= 50), the request for blocks is restarted.
[0130] The embodiments of the present disclosure can utilize network resources and CPU resources in parallel, and request and verify blocks in parallel, which can improve the synchronization efficiency of blocks.
[0131] It should be understood that although Figures 1 - 9The steps in the flowchart are shown in sequence according to the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless specifically stated in this document, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, Figures 1 - 9 At least some of the steps may include multiple steps or multiple phases. These steps or phases are not necessarily executed at the same time, but can be executed at different times. The execution order of these steps or phases is not necessarily sequential, but can be executed alternately or in rotation with at least some of the steps or phases in other steps or other steps.
[0132] In one embodiment, for any node in the blockchain system, the blockchain in the node adopts a tree structure, the leaf nodes in the tree structure are blocks, and the root node of the tree structure is the hash value of the node state after the execution of the latest block in the node, as Figure 10 shown, a data synchronization device for a blockchain is provided, including: a first acquisition module 1002, a second acquisition module 1004, a verification module 1006, and a synchronization module 1008, where:
[0133] The first acquisition module 1002 is configured to acquire a target root hash value and a target block height from a peer node, where the target root hash value is the root node of the blockchain in the peer node, and the target block height is the block height of the blockchain in the peer node;
[0134] The second acquisition module 1004 is configured to acquire at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes according to the target block height;
[0135] The verification module 1006 is configured to verify each block to be synchronized according to the Merkle proof of each block to be synchronized and the target root hash value;
[0136] The synchronization module 1008 is configured to perform data synchronization on each block to be synchronized when each block to be synchronized passes the verification.
[0137] The above blockchain data synchronization device, for any node in the blockchain system, the blockchain in this node adopts a tree structure, the leaf nodes in this tree structure are blocks, and the root node of this tree structure is the hash value of the node state after the execution of the latest block in this node. The node can obtain the target root hash value and the target block height from the peer node. The target root hash value is the root node of the blockchain in the peer node, and the target block height is the block height of the blockchain in the peer node. After obtaining at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes according to the target block height, verify each block to be synchronized according to the Merkle proof of each block to be synchronized and the target root hash value, and perform data synchronization on each block to be synchronized when each block to be synchronized passes the verification. The blockchain data synchronization device provided by the embodiments of the present disclosure verifies the block to be synchronized according to the Merkle proof of the block to be synchronized, that is, the verification process of the block to be synchronized does not depend on other blocks, and the retrieval and verification of the blocks to be synchronized can be performed in parallel, which can improve the data synchronization efficiency.
[0138] In one embodiment, the second obtaining module 1004 is further configured to:
[0139] Obtain at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes in parallel.
[0140] In one embodiment, the verification module 1006 is further configured to:
[0141] For any block to be synchronized, determine the consensus hash value according to the hash value of the block to be synchronized and the Merkle proof of the block to be synchronized;
[0142] When the consensus hash value is consistent with the target root hash value, determine that the block to be synchronized passes the verification.
[0143] In one embodiment, the synchronization module 1008 is further configured to:
[0144] For any block to be synchronized X, when the block to be synchronized X passes the verification, determine whether the previous block X-1 of the block to be synchronized X has been completed;
[0145] When the block X-1 has been completed, execute the block to be synchronized X;
[0146] Otherwise, store the block to be synchronized in the local memory until the block X-1 is completed, and then execute the block to be synchronized X.
[0147] In one embodiment, the synchronization module 1008 is further configured to:
[0148] Add a leaf node corresponding to the to-be-synchronized block X to the tree structure;
[0149] Update the child nodes and the root node of the tree structure according to the hash value of the to-be-synchronized block X.
[0150] In one embodiment, the second acquisition module 1004 is further configured to:
[0151] Parallelly acquire at least one to-be-synchronized block and the Merkle proof of each to-be-synchronized block from at least one of the peer nodes;
[0152] When the difference between the first quantity of the acquired to-be-synchronized blocks and the second quantity of the already executed to-be-synchronized blocks is greater than or equal to a first threshold, suspend acquiring the to-be-synchronized blocks and the Merkle proof of each to-be-synchronized block from at least one of the peer nodes until the difference between the first quantity and the third quantity of the already executed to-be-synchronized blocks is less than or equal to a second threshold, and then continue to acquire the to-be-synchronized blocks and the Merkle proof of each to-be-synchronized block from at least one of the peer nodes.
[0153] In one embodiment, the device may further include:
[0154] A receiving module, configured to receive a request for block N from a lagging node;
[0155] A sending module, configured to, after acquiring block N and the Merkle proof of block N from the blockchain, send block N and the Merkle proof of block N to the lagging node.
[0156] For the specific limitations on the blockchain data synchronization device, reference may be made to the limitations on the blockchain data synchronization method in the foregoing text, which will not be elaborated here. Each module in the above blockchain data synchronization device can be implemented in whole or in part by software, hardware, and their combination. The above modules can be embedded in the processor of the computer device in hardware form or be independent of it, or be stored in the memory of the computer device in software form so that the processor can call and execute the operations corresponding to the above respective modules.
[0157] In one embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as Figure 11As shown in the figure. The computer device includes a processor, a memory, a communication interface, a display screen, and an input device connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage medium. The communication interface of the computer device is used to communicate with external terminals in a wired or wireless manner. The wireless manner can be achieved through WIFI, carrier network, NFC (Near Field Communication), or other technologies. When the computer program is executed by the processor, it realizes a data synchronization method for a blockchain. The display screen of the computer device can be a liquid crystal display screen or an electronic ink display screen. The input device of the computer device can be a touch layer covering the display screen, or a button, trackball, or touchpad set on the shell of the computer device, or an external keyboard, touchpad, or mouse, etc.
[0158] Those skilled in the art can understand that Figure 11 the structure shown in the figure is only a block diagram of some structures related to the solution of this application, and does not constitute a limitation on the computer device to which the solution of this application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine some components, or have different component arrangements.
[0159] In one embodiment, a computer device is further provided, including a memory and a processor. A computer program is stored in the memory. When the processor executes the computer program, the steps in the above method embodiments are realized.
[0160] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by the processor, the steps in the above method embodiments are realized.
[0161] Those of ordinary skill in the art can understand that all or part of the processes in the methods of the above embodiments can be completed by instructing relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above various methods. Among them, any reference to a memory, storage, database, or other medium used in the various embodiments provided in the present application can include at least one of non-volatile and volatile memories. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, or optical memory, etc. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc.
[0162] The technical features of the above embodiments can be combined arbitrarily. For the sake of brevity of description, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, it should be considered as the scope described in this specification.
[0163] The above-described embodiments merely represent several implementation manners of the present application. The description is relatively specific and detailed, but it should not be construed as a limitation on the scope of the invention patent. It should be noted that for those of ordinary skill in the art, without departing from the concept of the present application, several modifications and improvements can still be made, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application should be subject to the appended claims.
Claims
1. A data synchronization method for a blockchain, characterized in that, For any node in the blockchain system, the blockchain in the node adopts a tree structure, the leaf nodes in the tree structure are blocks, and the root node of the tree structure is the hash value of the node state after the execution of the latest block in the node. The method includes: Obtain a target root hash value and a target block height from a peer node, where the target root hash value is the root node of the blockchain in the peer node, and the target block height is the block height of the blockchain in the peer node; Parallelly obtain at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes; For any block to be synchronized, determine a consensus hash value according to the hash value of the block to be synchronized and the Merkle proof of the block to be synchronized; When the consensus hash value is consistent with the target root hash value, determine that the block to be synchronized passes the verification; When each block to be synchronized passes the verification, perform data synchronization on each block to be synchronized.
2. The method according to claim 1, characterized in that The performing data synchronization on each block to be synchronized when each block to be synchronized passes the verification includes: For any block to be synchronized X, when the block to be synchronized X passes the verification, determine whether the previous block X-1 of the block to be synchronized X has been completed for execution; When the block X-1 has been completed for execution, perform execution on the block to be synchronized X; Otherwise, store the block to be synchronized in the local memory until the block X-1 is completed for execution, and then perform execution on the block to be synchronized X.
3. The method according to claim 2, wherein The performing execution on the block to be synchronized X includes: Add a leaf node corresponding to the block to be synchronized X in the tree structure; Update the child nodes and the root node of the tree structure according to the hash value of the block to be synchronized X.
4. The method according to claim 1, wherein The parallelly obtaining at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes includes: Parallelly obtain at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes; When the difference between the first quantity of the obtained blocks to be synchronized and the second quantity of the blocks to be synchronized that have been executed is greater than or equal to a first threshold, suspend obtaining the blocks to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes until the difference between the first quantity and the third quantity of the blocks to be synchronized that have been executed is less than or equal to a second threshold, and then continue to obtain the blocks to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes.
5. The method according to any one of claims 1 to 4, characterized in that, The method further includes: Receive a request for block N from a lagging node; After obtaining the block N and the Merkle proof of the block N from the blockchain, send the block N and the Merkle proof of the block N to the lagging node.
6. A data synchronization device for a blockchain, characterized in that, For any node in the blockchain system, the blockchain in the node adopts a tree structure, the leaf nodes in the tree structure are blocks, and the root node of the tree structure is the hash value of the node state after the execution of the latest block in the node. The device includes: A first acquisition module, configured to acquire a target root hash value and a target block height from a peer node, where the target root hash value is the root node of a blockchain in the peer node, and the target block height is the block height of the blockchain in the peer node; A second acquisition module, configured to concurrently acquire at least one block to be synchronized and the Merkle proof of each block to be synchronized from at least one of the peer nodes; A verification module, configured to, for any block to be synchronized, determine a consensus hash value according to the hash value of the block to be synchronized and the Merkle proof of the block to be synchronized; and determine that the block to be synchronized passes the verification when the consensus hash value is consistent with the target root hash value; A synchronization module, configured to perform data synchronization on each block to be synchronized when each block to be synchronized passes the verification.
7. The device according to claim 6, characterized in that, The synchronization module is further configured to: For any block to be synchronized X, determine whether the previous block X-1 of the block to be synchronized X has been executed when the block to be synchronized X passes the verification; Execute the block to be synchronized X when the block X-1 has been executed; Otherwise, store the block to be synchronized in local memory until the block X-1 is executed, and then execute the block to be synchronized X.
8. The device according to claim 7, characterized in that, The synchronization module is further configured to: Add a leaf node corresponding to the block to be synchronized X to the tree structure; Update the child nodes and the root node of the tree structure according to the hash value of the block to be synchronized X.
9. A computer device, comprising a memory and a processor, the memory storing a computer program, characterized in that, The processor implements the steps of the method according to any one of claims 1 to 5 when executing the computer program.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, The computer program implements the steps of the method according to any one of claims 1 to 5 when executed by a processor.
Citation Information
Patent Citations
Block chain data updating method, device, equipment and system and readable storage medium
CN111641712A
Blockchain state data synchronization method and apparatus, and electronic device
WO2021017436A1