Merkle tree updating method, system, device and storage medium
By calculating the hash value of the Merkel tree child node in parallel and updating the hash value of the parent node, the time-consuming problem of Merkel tree hash calculation in the blockchain system is solved, and transaction efficiency and system performance are improved.
Patent Information
- Application Number
- CN202111680630.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-29
- Publication Date
- 2024-11-26
- Estimated Expiration
- 2041-12-29
AI Technical Summary
The hashing calculation of Merkel tree in the blockchain system requires traversing the entire tree, resulting in serial single-threaded calculation, which consumes time and affects system performance.
By calculating the hash value of the child node on the Merkel tree in parallel, using the CPU's parallel computer system, selecting uncalculated child nodes to calculate their hash value in parallel, and update the hash value of the parent node after the calculation is completed, keeping it Sequentiality of calculations.
It shortens the hash computing time of Merkel tree, improves the transaction efficiency of blockchain systems, and solves the performance bottleneck caused by serial single-thread computing.
Smart Images

Figure CN114331439B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of blockchain technology, and in particular to a Merkle tree updating method, system, device and storage medium. Background Art
[0002] The Merkle tree is a binary hash tree, or a multi-branch tree, usually called a Merkle Hash Tree. All nodes that make up the Merkle tree are hash values. The leaf nodes of the Merkle tree store data files, while the non-leaf nodes store the hash values of their child nodes. Its characteristic is that any changes in the underlying data will be passed to its parent node. In the blockchain system, it is mainly used for fast induction and verification of the integrity of blocks. It groups the data in the blockchain for hash operations, and continuously recursively operates upward to generate new hash nodes. In the end, only one Merkle root hash is left to be stored in the block header.
[0003] Currently, the hash calculation of the Merkle tree requires traversing the entire tree. This hash calculation method is a serial single-threaded calculation process. When the number of nodes on the Merkle tree is large, the hash calculation of the Merkle tree is quite time-consuming, which seriously affects the overall performance of the blockchain system. Summary of the invention
[0004] The embodiments of the present invention provide a Merkle tree updating method, system, device and storage medium, aiming to solve the technical problem that the current Merkle tree serial single-thread hash calculation method is time-consuming.
[0005] An embodiment of the present invention provides a Merkle tree updating method, which is applied to a blockchain system. The Merkle tree updating method includes:
[0006] Determine the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree;
[0007] Selecting one uncalculated child node corresponding to each parent node from the node layer to be calculated each time, and calculating the first hash value of each selected child node in parallel;
[0008] When it is determined that the calculation of the first hash value of each child node in the node layer to be calculated is completed, the second hash value of each parent node is updated according to the first hash value of the child node corresponding to each parent node.
[0009] In one embodiment, the Merkle tree updating method further includes:
[0010] Set the count value of the counter corresponding to each parent node;
[0011] After the parallel calculation of the first hash value of each selected child node is completed each time, the count value of the counter corresponding to each parent node is reduced;
[0012] When the count value of the counter corresponding to each of the parent nodes is 0, it is determined that the calculation of the first hash value of each child node in the node layer to be calculated is completed.
[0013] In one embodiment, when the child nodes in the node layer to be calculated are not leaf nodes of the Merkle tree, the step of setting the count value of the counter corresponding to each parent node includes:
[0014] Get the number of all child nodes corresponding to one of the parent nodes;
[0015] The number of nodes is set to the count value of the counter corresponding to each of the parent nodes.
[0016] In one embodiment, when the child nodes in the node layer to be calculated are leaf nodes of the Merkle tree, the step of setting the count value of the counter corresponding to each parent node further includes:
[0017] The count value of the counter corresponding to each parent node is set to 1.
[0018] In one embodiment, the step of updating the second hash value of each parent node according to the first hash value of the child node corresponding to each parent node includes:
[0019] Combining the first hash values of all child nodes corresponding to each of the parent nodes to obtain combined data corresponding to each of the parent nodes;
[0020] Performing a hash operation on the combined data corresponding to each of the parent nodes to obtain a hash value of the combined data;
[0021] The second hash value of each of the parent nodes is modified using the hash value of the combined data.
[0022] In one embodiment, the Merkle tree updating method further includes:
[0023] When the child nodes in the node layer to be calculated are root nodes of the Merkle tree, first hash values of the child nodes in the node layer to be calculated are calculated in parallel.
[0024] In one embodiment, the Merkle tree updating method further includes:
[0025] When a transaction is generated in the blockchain system, searching whether there is an account to be involved in the transaction on the Merkle tree;
[0026] When there are accounts to be involved in transactions on the Merkle tree, the step of determining the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree is performed;
[0027] When the account to be involved in the transaction does not exist on the Merkle tree, obtaining the account to be involved in the transaction from a database of the blockchain system;
[0028] Insert the transaction account to be participated in into the Merkle tree, and execute the step of determining the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree.
[0029] In addition, to achieve the above-mentioned purpose, the present invention also provides a blockchain system, which includes:
[0030] A node determination module is used to determine the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree;
[0031] A node calculation module, used for selecting an uncalculated child node corresponding to each parent node from the node layer to be calculated each time, and calculating the first hash value of each selected child node in parallel;
[0032] The node updating module is used to update the second hash value of each parent node according to the first hash value of the child node corresponding to each parent node when determining that the calculation of the first hash value of each child node in the node layer to be calculated is completed.
[0033] In addition, to achieve the above-mentioned purpose, the present invention also provides a terminal device including: a memory, a processor, and a Merkel tree update method program stored on the memory and executable on the processor, wherein the Merkel tree update method program implements the steps of the above-mentioned Merkel tree update method when executed by the processor.
[0034] In addition, to achieve the above-mentioned purpose, the present invention also provides a storage medium on which a Merkle tree update method program is stored. When the Merkle tree update method program is executed by a processor, the steps of the above-mentioned Merkle tree update method are implemented.
[0035] The technical solution of a Merkle tree updating method, system, device and storage medium provided in the embodiments of the present invention has at least the following technical effects or advantages:
[0036] The present invention utilizes the parallel computing mechanism of the CPU. When a transaction is generated in a blockchain system, the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree is determined, and an uncalculated child node corresponding to each parent node is selected from the node layer to be calculated each time, and the first hash value of each selected child node is calculated in parallel. When it is determined that the calculation of the first hash value of each child node in the node layer to be calculated is completed, the second hash value of each parent node is updated according to the first hash value of the child node corresponding to each parent node. The technical solution solves the technical problem that the current Merkle tree serial single-thread hash calculation method is time-consuming, which not only shortens the hash calculation time of the Merkle tree in the blockchain system, which is beneficial to improving the transaction efficiency of the user account, but also maintains the sequentiality of the hash calculation of the Merkle tree. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] Figure 1 A schematic diagram of a flow chart of an embodiment of a Merkle tree updating method of the present invention;
[0038] Figure 2 This is a schematic diagram of the Merkle tree;
[0039] Figure 3 A schematic diagram of the structure of the hardware operating environment involved in the embodiment of the present invention;
[0040] Figure 4 This is a functional module diagram of the blockchain system of the present invention. DETAILED DESCRIPTION
[0041] In order to better understand the above technical solution, exemplary embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although exemplary embodiments of the present invention are shown in the accompanying drawings, it should be understood that the present invention can be implemented in various forms and should not be limited by the embodiments described herein. On the contrary, these embodiments are provided to enable a more thorough understanding of the present invention and to fully convey the scope of the present invention to those skilled in the art.
[0042] The embodiment of the present invention provides an embodiment of the Merkle tree updating method. It should be noted that although the logical order is shown in the flow chart, in some cases, the steps shown or described may be performed in an order different from that shown here.
[0043] like Figure 1 As shown, in one embodiment of the present invention, a Merkle tree update method of the present invention is applied to a blockchain system, and the Merkle tree update method includes the following steps:
[0044] Step S210: Determine the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree.
[0045] Step S220: Select one uncalculated child node corresponding to each parent node from the to-be-calculated node layer each time, and calculate the first hash value of each selected child node in parallel.
[0046] In this embodiment, the Merkle tree is a tree data structure composed of hash values, which is used to verify the integrity and accuracy of data in a decentralized blockchain system. Figure 2 As shown, Figure 2 This is a schematic diagram of a Merkle tree. Account A to Account H are all transaction accounts mounted on the Merkle tree, Node 1 is the root node of the Merkle tree, Node 10, Node 11, Node 12, Node 20, Node 21 and Node 22 are the middle nodes of the Merkle tree, Node 111, Node 112, Node 121, Node 122, Node 211, Node 212, Node 221 and Node 222 are all leaf nodes of the Merkle tree. In two adjacent layers of nodes on the Merkle tree, the upper node layer is the parent node of the lower node layer, and the lower node layer is the child node of the upper node layer, that is, node 11 is the parent node of node 111 and node 112, and node 111 and node 112 are both child nodes of node 11; node 12 is the parent node of node 121 and node 122, and node 121 and node 122 are both child nodes of node 12; node 21 is the parent node of node 211 and node 212, and node 211 and node 212 are both child nodes of node 21; node 22 is the parent node of node 221 and node 222, and node 221 and node 222 are both child nodes of node 22. Node 10 is the parent node of node 11 and node 12, and node 11 and node 12 are child nodes of node 10; node 20 is the parent node of node 21 and node 22, and node 21 and node 22 are child nodes of node 20; node 1 is the parent node of node 10 and node 20, and node 10 and node 20 are child nodes of node 1.
[0047] The parent node on the Merkle tree is the hash value of its child node after hashing. Among them, node 111 is the hash value of account A's data after hashing, that is, node 111 = Hash (A); node 112 is the hash value of account B's data after hashing, that is, node 112 = Hash (B); ...; node 221 is the hash value of account G's data after hashing, that is, node 221 = Hash (G); node 222 is the hash value of account H's data after hashing, that is, node 222 = Hash (H).
[0048] Node 11 is the hash value obtained by hashing nodes 111 and 112, that is, node 11 = Hash (AB) = Hash (Hash (A) + Hash (B)); ...; node 22 is the hash value obtained by hashing nodes 221 and 222, that is, node 22 = Hash (GH) = Hash (Hash (G) + Hash (H)).
[0049] Node 10 is the hash value obtained by hashing nodes 11 and 12, that is, node 10 = Hash (ABCD) = Hash (Hash (AB) + Hash (CD));
[0050] Node 20 is a hash value obtained by hashing nodes 21 and 22, that is, node 20 = Hash (EFGH) = Hash (Hash (EF) + Hash (GH)).
[0051] Node 1 is a hash value obtained by hashing nodes 10 and 20, that is, node 1 = Hash (AH) = Hash (Hash (ABCD) + Hash (EFGH)), where AH represents ABCDEFGH.
[0052] Specifically, when a transaction is generated in the blockchain system, once the transaction is successful, the account data of the accounts of both parties involved in the transaction will change. Due to the change in account data, the Merkle tree will be updated. The update mechanism of the Merkle tree is: for the upper node layer with a parent-child node relationship on the Merkle tree and the lower node layer adjacent to the upper node layer, the hash value of the node in the lower node layer needs to be calculated, so the upper node layer with a parent-child node relationship and the lower node layer in the lower node layer can be understood as the node layer to be calculated. Since the upper node layer and the lower node layer adjacent to the upper node layer have a parent-child node relationship, the nodes in the upper node layer are the parent nodes of the nodes in the lower node layer, and the nodes in the lower node layer are the child nodes of the nodes in the upper node layer.
[0053] When the Merkle tree needs to be updated, the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree is first determined. Figure 2In the node layer where nodes 111-node 222 are located, the node layer where nodes 11-node 22 are located, the node layer where nodes 10-node 20 are located, and the node layer where node 1 is located can all be understood as node layers to be calculated. For example, when the node layer where nodes 111-node 222 are located is the node layer to be calculated, it is necessary to calculate the first hash value of nodes 111-node 222, and the node layer where nodes 11-node 22 are located is the upper layer of the node layer where nodes 111-node 222 are located, so node 11 is the parent node of nodes 111 and node 112, node 12 is the parent node of nodes 121 and node 122, node 21 is the parent node of nodes 211 and node 212, and node 22 is the parent node of nodes 221 and node 222. When the node layer where other nodes are located is the node layer to be calculated, the relationship between the node layer where other nodes are located and their adjacent upper layer is the same as the relationship between the node layer where nodes 11-node 22 are located and the node layer where nodes 111-node 222 are located, and will not be repeated here.
[0054] Further, after determining the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree, select an uncalculated child node corresponding to each parent node from the node layer to be calculated each time, and then use the parallel computing mechanism of the CPU to parallelly calculate the first hash value of each child node selected each time. Wherein, parallelly calculating the first hash value of each child node selected each time can be understood as calculating the first hash value of each child node selected each time at the same time, and each child node selected each time is a node that has not been hashed.
[0055] like Figure 2 As shown in the figure, when a transaction is generated in the blockchain system, account A and account B are the trading parties, that is, account A and account B can trade with each other, account C and account D are the trading parties, that is, account C and account D can trade with each other, account E and account F are the trading parties, that is, account E and account F can trade with each other, account G and account H are the trading parties, that is, account G and account H can trade with each other. When any two accounts complete a transaction, the account data of the two accounts that have completed the transaction will change. When the account data of the two accounts that have completed the transaction changes, the Merkle tree will be updated. The Merkle tree is updated from the bottom node to the root node, that is, from the bottom to the top.
[0056] For the node layer where nodes 11-node 22 are located and the node layer where nodes 111-node 222 are located, there is a parent-child node relationship. The node layer where nodes 111-node 222 are located is used as the node layer to be calculated, and when the child nodes in the node layer to be calculated are not the root nodes of the Merkle tree, node 111 is selected from all child nodes corresponding to node 11, node 121 is selected from all child nodes corresponding to node 12, node 211 is selected from all child nodes corresponding to node 21, and node 221 is selected from all child nodes corresponding to node 22. The child nodes selected and not calculated this time are: node 111, node 121, node 211, and node 221. Then, the first hash value of node 111, the first hash value of node 121, the first hash value of node 211, and the first hash value of node 221 are calculated in parallel, that is, the first hash value of node 111, the first hash value of node 121, the first hash value of node 211, and the first hash value of node 221 are calculated at the same time. Afterwards, one uncalculated child node is selected from the remaining and uncalculated child nodes corresponding to nodes 11-node 22 respectively in the above manner, and then the first hash value of each selected child node is calculated in parallel. The selection is performed in this way until all child nodes corresponding to nodes 11-node 22 are selected and the first hash values are calculated in the above manner.
[0057] When there is a parent-child node relationship between the node layer where nodes 10-node 20 are located and the node layer where nodes 11-node 22 are located, and the node layer where nodes 11-node 22 are located is used as the node layer to be calculated, and the child nodes in the node layer to be calculated are not the root nodes of the Merkle tree, node 11 is selected from all the child nodes corresponding to node 10, and node 21 is selected from all the child nodes corresponding to node 20. The child nodes selected and not calculated this time are: node 11 and node 21, and then the first hash value of node 11 and the first hash value of node 21 are calculated in parallel, that is, the first hash value of node 11 and the first hash value of node 21 are calculated at the same time. After that, one uncalculated child node is selected from the remaining and uncalculated child nodes corresponding to nodes 10-node 20 again in the above manner, and then the first hash value of each selected child node is calculated in parallel, and the selection is performed in this way until all the child nodes corresponding to nodes 10-node 20 are selected and the first hash values are calculated in the above manner.
[0058] For the node layer where node 1 is located and the node layer where nodes 10-node 20 are located, there is a parent-child node relationship. The node layer where nodes 10-node 20 are located is used as the node layer to be calculated, and when the child nodes in the node layer to be calculated are not the root nodes of the Merkle tree, node 10 is selected from all the child nodes corresponding to node 1. The child node selected and not calculated this time is: node 10, and then the first hash value of node 10 is calculated in parallel. Although there is only one child node selected this time, the first hash value of node 10 is still calculated according to the parallel calculation mechanism. After that, a child node is selected from the remaining and uncalculated child nodes corresponding to node 1 again in the above manner, and then the first hash value of each selected child node is calculated in parallel, and the selection is carried out in this way until all the child nodes corresponding to node 1 are selected and the first hash value is calculated in the above manner.
[0059] When the node layer where node 1 is located is used as the node layer to be calculated, and the child nodes in the node layer to be calculated are the root nodes of the Merkle tree, and the child nodes in the node to be calculated have no parent nodes, that is, node 1 has no parent node, the first hash value of node 1 is calculated in parallel.
[0060] Step S230: when it is determined that the calculation of the first hash value of each child node in the node layer to be calculated is completed, the second hash value of each parent node is updated according to the first hash value of the child node corresponding to each parent node.
[0061] In this embodiment, the first hash values of all child nodes in the node layer to be calculated are not calculated once, but are calculated multiple times. The number of times the first hash values of all child nodes in the node layer to be calculated is calculated is related to the number of all child nodes corresponding to each parent node, and the number of nodes determines the number of calculations. Figure 2 In the calculation, when the child nodes in the node layer to be calculated are not leaf nodes or root nodes of the Merkle tree, the calculation times are twice. When the child nodes in the node layer to be calculated are leaf nodes or root nodes of the Merkle tree, the calculation times are once. If the calculation times are reached, it means that the calculation of the first hash values of all child nodes in the node layer to be calculated is completed.
[0062] Since the update of the second hash value of the parent node needs to be performed after the calculation of the first hash value of each child node in the node layer to be calculated is completed, after the first hash value of each child node selected each time is calculated in parallel, the calculation progress of the first hash value of each child node in the node layer to be calculated is detected, that is, it is determined whether the calculation of the first hash value of each child node in the node layer to be calculated is completed. If it is determined that the calculation of the first hash value of each child node in the node layer to be calculated is completed, the second hash value of each parent node is updated according to the first hash value of the child node corresponding to each parent node.
[0063] Specifically, updating the second hash value of each parent node according to the first hash value of the child node corresponding to each parent node includes:
[0064] Combine the first hash values of all child nodes corresponding to each parent node to obtain combined data corresponding to each parent node;
[0065] Perform hash operation on the combined data corresponding to each parent node to obtain the hash value of the combined data;
[0066] The second hash value of each parent node is modified using the hash value of the combined data.
[0067] For the upper node layer and the lower node layer adjacent to the upper node layer that have a parent-child node relationship on the Merkle tree, the first hash values of all child nodes corresponding to each parent node are combined to obtain a combined data, which is generated by combining the first hash values of all child nodes corresponding to each parent node. Then, a hash operation is performed on the combined data to obtain the hash value of the combined data. The hash value of the combined data is the new second hash value of the parent node. The second hash value of the parent node is modified using the hash value of the combined data, that is, the second hash value of the parent node will be replaced by the hash value of the combined data. After the modification, the second hash value of the parent node is the hash value of the combined data. Figure 2 , the calculated first hash value of node 111 is Hash(A), the first hash value of node 112 is Hash(B), the second hash value of node 11 before update is Hash(AB), after Hash(A) and Hash(B) are combined and hashed, the hash value of the combined data is Hash(AB), Hash(AB) is obtained by hashing Hash(A) and Hash(B), that is, Hash(AB)=Hash(Hash(A)+Hash(B)), after Hash(AB) is modified, the second hash value of node 11 after modification is Hash(AB). In this way, when nodes 12, 21, 22, 10, 20 and 1 are used as parent nodes, the update method of their own second hash values is the same as the update method of the second hash value of node 11, which will not be repeated here.
[0068] Among them, when the child node in the node layer to be calculated is the root node of the Merkle tree, the first hash value of the child node in the node layer to be calculated is calculated in parallel. It can be understood that the child node in the node layer to be calculated is the root node of the Merkle tree, the root node has no parent node, and the first hash value of the child node in the node layer to be calculated is the hash value of the root node.
[0069] like Figure 2As shown, the Merkle tree is a binary tree, and this embodiment is described by taking the binary tree as an example. There are 8 accounts mounted on the Merkle tree, namely, account A to account H. All accounts in account A to account H have completed transactions, and all transactions are successful. The account data of each account in account A to account H has changed, so the Merkle tree needs to be updated. In order from bottom to top, the Merkle tree is updated from the bottom node to the root node. The update process of the Merkle tree is as follows:
[0070] After the transaction between account A and account H is successful, the account data of each account is changed. First, the hash values of node 111, node 121, node 211 and node 221 are calculated in parallel, and then the hash values of node 112, node 122, node 212 and node 222 are calculated in parallel. After the hash values of node 111 and node 222 are calculated, the original hash values of node 111 and node 222 are updated to Hash(A), Hash(B), Hash(C), Hash(D), Hash(E), Hash(F), Hash(G) and Hash(H) respectively. Among them, Hash(A)-Hash(H) are obtained by hashing the account data of account A-account H after the changes.
[0071] Next, the hash value of node 11 and the hash value of node 21 are calculated in parallel, that is, the hash value of node 11 is Hash(AB), Hash(AB)=Hash(Hash(A)+Hash(B)), the hash value of node 21 is Hash(EF), Hash(EF)=Hash(HashE)+Hash(F)), the original hash value of node 11 is updated to Hash(AB), and the original hash value of node 21 is updated to Hash(EF); then, the hash value of node 12 and the hash value of node 22 are calculated in parallel, the hash value of node 12 is Hash(CD), Hash(CD)=Hash(Hash(C)+Hash(D)), the hash value of node 22 is Hash(GH)), Hash(GH))=Hash(Hash(G)+Hash(H)), the original hash value of node 12 is updated to Hash(CD), and the original hash value of node 22 is updated to Hash(GH).
[0072] Next, the hash value of node 10 is calculated in parallel. The hash value of node 10 is Hash(ABCD), Hash(ABCD) = Hash(Hash(AB) + Hash(CD)), and the original hash value of node 10 is updated to Hash(ABCD); then the hash value of node 20 is calculated in parallel. The hash value of node 20 is Hash(EFGH), Hash(EFGH) = Hash(Hash(EF) + Hash(GH)), and the original hash value of node 20 is updated to Hash(EFGH). Among them, the hash value of a node is calculated each time, but the blockchain system still opens parallel computing threads, one of which participates in the hash value calculation of the node, and the other threads do not participate in the calculation.
[0073] Finally, the hash value of node 1 is calculated. The hash value of node 1 is also the hash value of the root node, that is, the hash value of node 1 is Hash(AH), Hash(AH)=Hash(Hash(ABCD)+Hash(EFGH)), and the original hash value of node 1 is updated to Hash(AH). According to the above sequence, the Merkle tree update is completed.
[0074] According to the above technical solution, this embodiment utilizes the parallel computing mechanism of the CPU to perform parallel computing on the hash value of an uncalculated child node corresponding to each parent node in the same node layer, thereby shortening the hash computing time of the Merkle tree in the blockchain system, which is beneficial to improving the transaction efficiency of the user account, and maintaining the sequentiality of the hash computing of the Merkle tree.
[0075] Furthermore, the Merkle tree updating method of the present invention further comprises the following steps:
[0076] Step a: setting the count value of the counter corresponding to each parent node;
[0077] Specifically, when a node on the Merkle tree is used as a parent node of a child node in a node layer to be calculated, a counter is pre-set in each parent node, and the count value of the counter corresponding to each parent node is set according to the number of child nodes corresponding to each parent node and whether the child node in the node layer to be calculated is a leaf node of the Merkle tree. The step of setting the count value of the counter corresponding to each parent node includes:
[0078] Since the number of all child nodes corresponding to each parent node on the Merkle tree is the same, if the child nodes in the node layer to be calculated are not leaf nodes of the Merkle tree, the number of all child nodes corresponding to one of the parent nodes can be obtained, and the number of nodes can be set as the count value of the counter corresponding to each parent node. Figure 2In the figure, the root node has 2 child nodes. When the root node is used as the parent node, the count value of the counter in the root node is set to 2. In the intermediate nodes, node 10 and node 20 each have 2 corresponding child nodes, and the count values of the counters in node 10 and node 20 are set to 2; node 11, node 12, point 21 and node 22 each have 2 corresponding child nodes, and the count values of the counters in node 11, node 12, point 21 and node 22 are set to 2.
[0079] Since the leaf node is the bottom node of the Merkle tree, its hash value is calculated through account data. The leaf node can be understood as the parent node of the account, and the account is the child node of the leaf node, such as node 111 is the parent node of account A, and account A is the child node of node 111. The hash values of the account data of each account can be calculated together once, and the hash value of the leaf node can be updated, so multiple calculations are not required. Therefore, the step of setting the count value of the counter corresponding to each parent node also includes: if the child node in the node layer to be calculated is a leaf node of the Merkle tree, the count value of the counter corresponding to each parent node is set to 1.
[0080] Step b: after the parallel calculation of the first hash value of each selected child node is completed each time, the count value of the counter corresponding to each parent node is reduced;
[0081] Step c: Determine whether the count value of the counter corresponding to each of the parent nodes is 0; if so, determine whether the calculation of the first hash value of each child node in the node layer to be calculated is completed; if not, determine that the calculation of the first hash value of each child node in the node layer to be calculated is not completed, then continue to execute step S232 and make a judgment.
[0082] After the parallel calculation of the first hash value of each selected child node is completed each time, the count value of the counter corresponding to each parent node is subtracted by 1. If the count value is set to 2, after the first hash value of each child node selected for the first time is calculated, the count value is reduced to 1, and then the first hash value of each child node selected for the second time is calculated in parallel. After the first hash value of each child node selected for the second time is calculated, the count value is reduced to 0. When the count value is 0, it is determined that the calculation of the first hash value of each child node in the node layer to be calculated is completed, which realizes the accurate judgment of whether the first hash values of all child nodes corresponding to each parent node are calculated. Then, according to the first hash values of all child nodes corresponding to each parent node, the second hash value of each parent node is updated, which is conducive to improving the update efficiency of the second hash value of each parent node.
[0083] If the count value is set to 1, after the first hash value calculation of each child node selected for the first time is completed, the count value is reduced to 0, that is, the count value is 0, and it is determined that the first hash value calculation of each child node in the node layer to be calculated is completed.
[0084] Furthermore, the Merkle tree updating method of the present invention further includes the following steps:
[0085] When a transaction is generated in the blockchain system, searching whether there is an account to be involved in the transaction on the Merkle tree;
[0086] When there are accounts to be involved in transactions on the Merkle tree, the step of determining the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree is performed;
[0087] When the account to be involved in the transaction does not exist on the Merkle tree, obtaining the account to be involved in the transaction from a database of the blockchain system;
[0088] Insert the transaction account to be participated in into the Merkle tree, and execute the step of determining the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree.
[0089] The account to be traded refers to the account to be traded. Figure 2 In the example, if accounts A to H need to participate in the transaction, then accounts A to H are all accounts to participate in the transaction. Usually, accounts to participate in the transaction for the first time do not exist in the Merkle tree, but will be recorded in the database of the blockchain system. Before the transaction is carried out, they will be found from the database and mounted on the Merkle tree, so that they exist in the Merkle tree. If the account to participate in the transaction is not the first time to participate in the transaction, but has participated in the transaction before, the account to participate in the transaction exists in the Merkle tree, and can directly participate in the transaction.
[0090] Specifically, when a transaction is generated in the blockchain system, the account to be involved in the transaction is searched from the Merkle tree. If the account to be involved in the transaction exists on the Merkle tree, the account to be involved in the transaction is allowed to conduct the transaction. After the transaction is successful, the step of determining the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree is executed. For example, Figure 2 Account A-Account H are the accounts to be involved in the transaction, and accounts A-Account H are all accounts to be involved in the transaction that have been mounted on the Merkle tree. When the blockchain system generates a transaction, it is determined that account A-Account H exists on the Merkle tree, and then account A-Account H is allowed to trade. After the transaction between account A and account H is successful, the step of determining the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree is executed. Figure 2Account A-Account H are the accounts to be traded, and Account A-Account D have been mounted on the Merkle tree. Account E-Account H does not exist on the Merkle tree. Then, Account E-Account H is first found from the database, and then Account E-Account H is mounted on the Merkle tree. In this way, Account A-Account H are all on the Merkle tree, and Account A-Account H is allowed to trade. After the transaction between Account A and Account H is successful, the step of determining the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree is executed.
[0091] like Figure 3 As shown, Figure 3 It is a schematic diagram of the structure of the hardware operating environment involved in the embodiment of the present invention.
[0092] It should be noted that Figure 3 This can be a structural diagram of the hardware operating environment of the terminal device.
[0093] like Figure 3 As shown, the terminal device may include: a processor 1001, such as a CPU, a memory 1005, a user interface 1003, a network interface 1004, and a communication bus 1002. Among them, the communication bus 1002 is used to realize the connection and communication between these components. The user interface 1003 may include a display screen (Display), an input unit such as a keyboard (Keyboard), and the optional user interface 1003 may also include a standard wired interface and a wireless interface. The network interface 1004 may optionally include a standard wired interface and a wireless interface (such as a WI-FI interface). The memory 1005 may be a high-speed RAM memory, or a stable memory (non-volatile memory), such as a disk memory. The memory 1005 may also be a storage device independent of the aforementioned processor 1001.
[0094] Those skilled in the art will understand that Figure 3 The terminal device structure shown in the figure does not constitute a limitation on the terminal device, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.
[0095] like Figure 3 As shown, the memory 1005 as a storage medium may include an operating system, a network communication module, a user interface module, and a Merkle tree update method program. Among them, the operating system is a program for managing and controlling the hardware and software resources of the terminal device, the Merkle tree update method program and the operation of other software or programs.
[0096] exist Figure 3In the terminal device shown, the user interface 1003 is mainly used to connect to the terminal and communicate data with the terminal; the network interface 1004 is mainly used for the background server and communicates data with the background server; the processor 1001 can be used to call the Merkle tree update method program stored in the memory 1005.
[0097] In this embodiment, the terminal device includes: a memory 1005, a processor 1001, and a Merkle tree update method program stored in the memory 1005 and executable on the processor, wherein:
[0098] When the processor 1001 calls the Merkle tree update method program stored in the memory 1005, the following operations are performed:
[0099] Determine the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree;
[0100] Selecting one uncalculated child node corresponding to each parent node from the node layer to be calculated each time, and calculating the first hash value of each selected child node in parallel;
[0101] When it is determined that the calculation of the first hash value of each child node in the node layer to be calculated is completed, the second hash value of each parent node is updated according to the first hash value of the child node corresponding to each parent node.
[0102] When the processor 1001 calls the Merkle tree update method program stored in the memory 1005, it also performs the following operations:
[0103] Set the count value of the counter corresponding to each parent node;
[0104] After the parallel calculation of the first hash value of each selected child node is completed each time, the count value of the counter corresponding to each parent node is reduced;
[0105] When the count value of the counter corresponding to each of the parent nodes is 0, it is determined that the calculation of the first hash value of each child node in the node layer to be calculated is completed.
[0106] When the processor 1001 calls the Merkle tree update method program stored in the memory 1005, it also performs the following operations:
[0107] Get the number of all child nodes corresponding to one of the parent nodes;
[0108] The number of nodes is set to the count value of the counter corresponding to each of the parent nodes.
[0109] When the processor 1001 calls the Merkle tree update method program stored in the memory 1005, it also performs the following operations:
[0110] The count value of the counter corresponding to each parent node is set to 1.
[0111] When the processor 1001 calls the Merkle tree update method program stored in the memory 1005, it also performs the following operations:
[0112] Combine the first hash values of all child nodes corresponding to each parent node to obtain combined data corresponding to each parent node;
[0113] Performing a hash operation on the combined data corresponding to each of the parent nodes to obtain a hash value of the combined data;
[0114] The second hash value of each of the parent nodes is modified using the hash value of the combined data.
[0115] When the processor 1001 calls the Merkle tree update method program stored in the memory 1005, it also performs the following operations:
[0116] When the child nodes in the node layer to be calculated are root nodes of the Merkle tree, first hash values of the child nodes in the node layer to be calculated are calculated in parallel.
[0117] When the processor 1001 calls the Merkle tree update method program stored in the memory 1005, it also performs the following operations:
[0118] When a transaction is generated in the blockchain system, searching whether there is an account to be involved in the transaction on the Merkle tree;
[0119] When there are accounts to be involved in transactions on the Merkle tree, the step of determining the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree is performed;
[0120] When the account to be involved in the transaction does not exist on the Merkle tree, obtaining the account to be involved in the transaction from a database of the blockchain system;
[0121] Insert the transaction account to be participated in into the Merkle tree, and execute the step of determining the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree.
[0122] The present invention utilizes the parallel computing mechanism of the CPU. When a transaction is generated in a blockchain system, the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree is determined, and an uncalculated child node corresponding to each parent node is selected from the node layer to be calculated each time, and the first hash value of each selected child node is calculated in parallel. When it is determined that the calculation of the first hash value of each child node in the node layer to be calculated is completed, the second hash value of each parent node is updated according to the first hash value of the child node corresponding to each parent node. The technical solution solves the technical problem that the current Merkle tree serial single-thread hash calculation method is time-consuming, which not only shortens the hash calculation time of the Merkle tree in the blockchain system, which is beneficial to improving the transaction efficiency of the user account, but also maintains the sequentiality of the hash calculation of the Merkle tree.
[0123] like Figure 4 As shown, the present invention also provides a blockchain system, the blockchain system comprising:
[0124] A node determination module 310 is used to determine the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree;
[0125] The node calculation module 320 is used to select one uncalculated child node corresponding to each parent node from the node layer to be calculated each time, and calculate the first hash value of each selected child node in parallel;
[0126] The node updating module 330 is used to update the second hash value of each parent node according to the first hash value of the child node corresponding to each parent node when it is determined that the calculation of the first hash value of each child node in the node layer to be calculated is completed.
[0127] The specific implementation methods of the blockchain system of the present invention are basically the same as the embodiments of the Merkle tree update method described above, and will not be repeated here.
[0128] Furthermore, the present invention also provides a terminal device comprising: a memory, a processor, and a Merkel tree update method program stored in the memory and executable on the processor, wherein the Merkel tree update method program implements the steps of the above-mentioned Merkel tree update method when executed by the processor.
[0129] Furthermore, the present invention also provides a storage medium on which a Merkle tree updating method program is stored, and when the Merkle tree updating method program is executed by a processor, the steps of the above-mentioned Merkle tree updating method are implemented.
[0130] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0131] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0132] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0133] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process in the computer or other programmable device. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0134] It should be noted that in the claims, any reference signs placed between brackets shall not be construed as limiting the claims. The word "comprising" does not exclude the presence of components or steps not listed in the claim. The word "a" or "an" preceding a component does not exclude the presence of a plurality of such components. The invention may be implemented by means of hardware comprising several different components and by means of a suitably programmed computer. In a unit claim enumerating several means, several of these means may be embodied by the same item of hardware. The use of the words first, second, and third etc. does not indicate any order. These words may be interpreted as names.
[0135] Although the preferred embodiments of the present invention have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention.
[0136] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.
Claims
1. A Merkle tree updating method, characterized in that: Applied to the blockchain system, the Merkle tree update method includes: When a transaction is generated in the blockchain system, searching whether there is an account to be involved in the transaction on the Merkle tree; When there is a transaction account to be participated in on the Merkle tree, determine the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree, the node layer to be calculated is the upper node layer and the lower node layer in the lower node layer with a parent-child node relationship, wherein the leaf node of the Merkle tree is the parent node of the transaction account to be participated in, and the transaction account to be participated in is the child node of the leaf node, and the account data of each transaction account to be participated in is calculated together once, so that the hash value of the leaf node can be updated; Selecting one uncalculated child node corresponding to each parent node from the node layer to be calculated each time, and calculating the first hash value of each selected child node simultaneously based on the parallel computing mechanism of the central processing unit; When it is determined that the calculation of the first hash value of each child node in the node layer to be calculated is completed, the second hash value of each parent node is updated according to the first hash value of the child node corresponding to each parent node.
2. The method according to claim 1, characterized in that The Merkle tree updating method further includes: Set the count value of the counter corresponding to each parent node; After the parallel calculation of the first hash value of each selected child node is completed each time, the count value of the counter corresponding to each parent node is reduced; When the count value of the counter corresponding to each of the parent nodes is 0, it is determined that the calculation of the first hash value of each child node in the node layer to be calculated is completed.
3. The method according to claim 2, characterized in that When the child nodes in the node layer to be calculated are not leaf nodes of the Merkle tree, the step of setting the count value of the counter corresponding to each parent node includes: Get the number of all child nodes corresponding to one of the parent nodes; The number of nodes is set to the count value of the counter corresponding to each of the parent nodes.
4. The method according to claim 2, characterized in that When the child nodes in the node layer to be calculated are leaf nodes of the Merkle tree, the step of setting the count value of the counter corresponding to each parent node further includes: The count value of the counter corresponding to each parent node is set to 1.
5. The method according to claim 1, characterized in that The step of updating the second hash value of each parent node according to the first hash value of the child node corresponding to each parent node comprises: Combine the first hash values of all child nodes corresponding to each parent node to obtain combined data corresponding to each parent node; Performing a hash operation on the combined data corresponding to each of the parent nodes to obtain a hash value of the combined data; The second hash value of each of the parent nodes is modified using the hash value of the combined data.
6. The method according to claim 1, characterized in that The Merkle tree updating method further includes: When the child nodes in the node layer to be calculated are root nodes of the Merkle tree, first hash values of the child nodes in the node layer to be calculated are calculated in parallel.
7. The method according to claim 1, characterized in that The Merkle tree updating method further includes: When the account to be involved in the transaction does not exist on the Merkle tree, obtaining the account to be involved in the transaction from a database of the blockchain system; Insert the transaction account to be participated in into the Merkle tree, and execute the step of determining the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree.
8. A blockchain system, characterized in that: The blockchain system includes: A node determination module is used to find out whether there is a transaction account to be participated in on the Merkle tree when the blockchain system generates a transaction; when there is a transaction account to be participated in on the Merkle tree, determine the parent node corresponding to each child node in the node layer to be calculated on the Merkle tree, the node layer to be calculated is the upper node layer and the lower node layer in the lower node layer where there is a parent-child node relationship, wherein the leaf node of the Merkle tree is the parent node of the transaction account to be participated in, and the transaction account to be participated in is the child node of the leaf node, and the account data of each transaction account to be participated in is calculated together once, so that the hash value of the leaf node can be updated; A node calculation module, used for selecting an uncalculated child node corresponding to each parent node from the node layer to be calculated each time, and calculating the first hash value of each child node selected each time simultaneously based on the parallel calculation mechanism of the central processing unit; The node updating module is used to update the second hash value of each parent node according to the first hash value of the child node corresponding to each parent node when determining that the calculation of the first hash value of each child node in the node layer to be calculated is completed.
9. A terminal device, characterized in that: include: A memory, a processor, and a Merkle tree update method program stored in the memory and executable on the processor, wherein the Merkle tree update method program implements the steps of the Merkle tree update method as described in any one of claims 1 to 7 when executed by the processor.
10. A storage medium, characterized in that: A Merkle tree updating method program is stored thereon, and when the Merkle tree updating method program is executed by a processor, the steps of the Merkle tree updating method described in any one of claims 1-7 are implemented.
