A pruning method and device applied to a blockchain
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CHINA CONSTRUCTION BANK
- Filing Date
- 2023-08-21
- Publication Date
- 2026-06-09
AI Technical Summary
In existing blockchain technologies, Merkle Patricia trees have high computational overhead during state updates and are difficult to prune, making them unsuitable for different scenarios and optimization needs.
By defining the state space and state transition function, pruning strategies for invalid and duplicate states are designed. Dynamic programming algorithm is used to optimize the storage space of the blockchain state tree and select target pruning nodes for pruning processing.
It effectively reduces the storage space requirements of blockchain state data, lowers system resource consumption, improves computing speed and system performance, and adapts to various scenario requirements.
Smart Images

Figure CN117076563B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of blockchain technology, and in particular to a pruning method and apparatus for blockchain. Background Technology
[0002] Blockchain, as a distributed database technology, features decentralization, data immutability, and high security, and is currently widely used in finance, supply chain, and the Internet of Things (IoT). However, with the development of blockchain networks and the growth of data scale, how to effectively manage and optimize blockchain data storage has become a key issue. Existing blockchain data storage and state management face challenges in scalability, computational efficiency, and applicability.
[0003] Currently, the primary approach used for state storage optimization is based on Merkle Patricia trees. Merkle Patricia trees are a data structure combining Merkle and Patricia trees, widely applied in blockchain systems such as Ethereum. By using Merkle Patricia trees, efficient storage and retrieval of blockchain state can be achieved while ensuring data integrity and consistency.
[0004] However, Merkle Patricia trees require recalculating the Merkle root every time the state is updated, which may introduce additional computational overhead and reduce computational efficiency. Furthermore, the structure of Merkle Patricia trees makes pruning useless states relatively difficult, making them unsuitable for different scenarios and optimization needs. Summary of the Invention
[0005] In view of this, embodiments of the present invention provide a pruning method and apparatus for blockchain, which can at least solve the problems of high computational overhead and difficulty in state pruning in the prior art.
[0006] To achieve the above objectives, according to one aspect of the present invention, a pruning method for blockchain is provided, comprising:
[0007] In response to the detected update operation on the blockchain state tree, the storage space of each node in the state tree is traversed to determine the set of candidate pruning nodes whose state meets the preset requirements.
[0008] For each candidate pruning node, obtain the storage space saving value before pruning and calculate the storage space saving value after pruning. Update the state space of each candidate pruning node based on the smaller storage space saving value; where the state space represents the space occupied by the node.
[0009] Obtain the preset boundary conditions, call the main function to process the boundary conditions and the state space of all candidate pruning nodes, so as to select the target pruning node from the set of candidate pruning nodes and perform pruning processing. In response to the detection that pruning is completed, perform the update operation on the state tree.
[0010] Optionally, the invocation of the main function processes the boundary conditions and the state space of all candidate pruning nodes to filter the target pruning node from the set of candidate pruning nodes and perform pruning processing, including:
[0011] Based on the state space of each candidate pruning node, an array is generated, and the main function is called to process the boundary conditions and the array in order to select the target pruning node from the set of candidate pruning nodes and perform pruning processing.
[0012] Optionally, the invocation of the main function processes the boundary conditions and the state space of all candidate pruning nodes to filter the target pruning node from the set of candidate pruning nodes and perform pruning processing, including:
[0013] Sort the state space of each candidate pruning node in descending order of storage space savings;
[0014] The main function processes the boundary conditions and the state space of all candidate pruning nodes to select target pruning nodes from the candidate pruning node set according to the sorting and perform pruning processing.
[0015] Optionally, the boundary condition is at least one of a preset threshold for the number of pruning operations and the target total storage space saving value.
[0016] Optionally, the candidate pruning nodes are at least one of invalid nodes and duplicate nodes, and the step of selecting the target pruning node from the set of candidate pruning nodes and performing pruning processing includes one or more of the following:
[0017] Invoke the invalid state pruning strategy, obtain the key value of the invalid node, process the key value using a hash function to obtain a hash pointer, find the position corresponding to the hash pointer in the storage space, and perform pruning on the data at the position;
[0018] Invoke the duplicate state pruning strategy, query the hash table for the target node that is the same as the duplicate node and has been traversed and processed, and point the hash pointer of the duplicate node to the target node.
[0019] To achieve the above objectives, according to another aspect of the present invention, a pruning device for blockchain is provided, comprising:
[0020] The alternative module is used to respond to the listening operation of updating the blockchain state tree, traverse the storage space of each node in the state tree, and determine the set of alternative pruning nodes whose state meets the preset requirements.
[0021] The calculation module is used to obtain the storage space saving value before pruning for each candidate pruning node, and to calculate the storage space saving value after pruning. The state space of each candidate pruning node is updated based on the smaller storage space saving value. The state space represents the space occupied by the node.
[0022] The pruning module is used to obtain preset boundary conditions, call the main function to process the boundary conditions and the state space of all candidate pruning nodes, so as to select target pruning nodes from the set of candidate pruning nodes and perform pruning processing. In response to the detection that pruning is completed, it performs an update operation on the state tree.
[0023] Optionally, the pruning module is used for:
[0024] Based on the state space of each candidate pruning node, an array is generated, and the main function is called to process the boundary conditions and the array in order to select the target pruning node from the set of candidate pruning nodes and perform pruning processing.
[0025] Optionally, the pruning module is used for:
[0026] Sort the state space of each candidate pruning node in descending order of storage space savings;
[0027] The main function processes the boundary conditions and the state space of all candidate pruning nodes to select target pruning nodes from the candidate pruning node set according to the sorting and perform pruning processing.
[0028] Optionally, the boundary condition is at least one of a preset threshold for the number of pruning operations and the target total storage space saving value.
[0029] Optionally, the candidate pruning nodes are at least one of invalid nodes and duplicate nodes, and the pruning module includes one or more of the following:
[0030] Invoke the invalid state pruning strategy, obtain the key value of the invalid node, process the key value using a hash function to obtain a hash pointer, find the position corresponding to the hash pointer in the storage space, and perform pruning on the data at the position;
[0031] Invoke the duplicate state pruning strategy, query the hash table for the target node that is the same as the duplicate node and has been traversed and processed, and point the hash pointer of the duplicate node to the target node.
[0032] To achieve the above objectives, according to another aspect of the present invention, a pruning electronic device applied to blockchain is provided.
[0033] The electronic device of this invention includes: one or more processors; and a storage device for storing one or more programs, which, when executed by the one or more processors, cause the one or more processors to implement any of the above-described pruning methods applied to blockchain.
[0034] To achieve the above objectives, according to another aspect of the present invention, a computer-readable medium is provided having a computer program stored thereon, which, when executed by a processor, implements any of the above-described pruning methods applied to blockchain.
[0035] To achieve the above objectives, according to another aspect of the present invention, a computing program product is provided. One such computing program product includes a computer program that, when executed by a processor, implements the pruning method for blockchain provided in the present invention.
[0036] According to the solution provided by the present invention, one embodiment of the above invention has the following advantages or beneficial effects: by defining the state space, the state transition function, and designing corresponding pruning strategies for invalid and repetitive states, the storage space pruning optimization operation of the blockchain state tree is realized based on the dynamic programming algorithm, thereby effectively reducing the storage space requirement of blockchain state data, reducing the consumption of system resources and storage costs, and improving computing speed, system performance and scalability.
[0037] The further effects of the aforementioned unconventional alternative methods will be explained below in conjunction with specific implementation methods. Attached Figure Description
[0038] The accompanying drawings are provided to better understand the invention and are not intended to unduly limit the scope of the invention. Wherein:
[0039] Figure 1 This is a schematic diagram of the main process of a pruning method applied to blockchain according to an embodiment of the present invention;
[0040] Figure 2 This is a flowchart illustrating an optional pruning method for blockchain according to an embodiment of the present invention;
[0041] Figure 3 This is a flowchart illustrating another optional pruning method applied to blockchain according to an embodiment of the present invention;
[0042] Figure 4 This is a flowchart illustrating another optional pruning method applied to blockchain according to an embodiment of the present invention;
[0043] Figure 5 This is a flowchart illustrating another optional pruning method applied to blockchain according to an embodiment of the present invention;
[0044] Figure 6 This is a flowchart illustrating another optional pruning method applied to blockchain according to an embodiment of the present invention;
[0045] Figure 7 This is a schematic diagram of the main modules of a pruning device applied to blockchain according to an embodiment of the present invention;
[0046] Figure 8 This is an exemplary system architecture diagram in which embodiments of the present invention can be applied;
[0047] Figure 9 This is a schematic diagram of the structure of a computer system suitable for implementing the embodiments of the present invention, such as a mobile device or server. Detailed Implementation
[0048] The following description, in conjunction with the accompanying drawings, illustrates exemplary embodiments of the present invention, including various details to aid understanding. These details should be considered merely exemplary. Therefore, those skilled in the art will recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the invention. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description.
[0049] It should be noted that, unless otherwise specified, the embodiments and features described in the present invention can be combined with each other. The collection, analysis, use, transmission, and storage of user personal information involved in the technical solutions of the present invention all comply with relevant laws and regulations, are used for legal and reasonable purposes, and are not shared, disclosed, or sold outside of these legal uses, and are subject to supervision and management by regulatory authorities. Necessary measures should be taken to prevent unauthorized access to such personal information data, ensure that personnel authorized to access personal information data comply with relevant laws and regulations, and ensure the security of user personal information.
[0050] Once this user's personal information data is no longer needed, the risk should be minimized by restricting or even prohibiting data collection and / or deleting the data. Where applicable, including in certain relevant applications, user privacy should be protected by de-identifying the data, such as by removing specific identifiers (e.g., date of birth), controlling the amount or specificity of the stored data (e.g., collecting location data at the city level rather than the specific address level), controlling how the data is stored, and / or other de-identification methods.
[0051] The drawbacks of the Merkle Patricia tree data structure are explained in detail here:
[0052] 1. High computational overhead: Although Merkle Patricia trees can reduce storage space requirements and lower storage costs, they require recalculating the Merkle root with each state update, resulting in additional computational overhead and impacting computational efficiency. This computational overhead can become very significant when state updates and transactions are frequent in a blockchain system.
[0053] 2. Pruning Difficulty: The structure of the Merkle Patricia tree makes pruning useless state nodes relatively difficult. In practical applications, more complex pruning strategies are needed to effectively remove useless states, while ensuring data integrity and consistency during the pruning process.
[0054] See Figure 1 The diagram shows the main flowchart of a pruning method for blockchain provided by an embodiment of the present invention, which includes the following steps:
[0055] S101: In response to the detected update operation on the blockchain state tree, traverse the storage space of each node in the state tree to determine the set of candidate pruning nodes whose state meets the preset requirements.
[0056] S102: For each candidate pruning node, obtain the storage space saving value before pruning and calculate the storage space saving value after pruning. Update the state space of each candidate pruning node based on the smaller storage space saving value; where the state space represents the space occupied by the node.
[0057] S103: Obtain the preset boundary conditions, call the main function to process the boundary conditions and the state space of all candidate pruning nodes, so as to select the target pruning node from the set of candidate pruning nodes and perform pruning processing, and in response to the detection that pruning is completed, perform the update operation on the state tree.
[0058] In the above implementation, for step S101, the so-called Merkle Tree state tree refers to a data structure used to organize and store user state data, which can be a user's assets (such as digital resources, virtual electronic resources, etc.). As the blockchain network runs longer, the state data stored in the blockchain nodes also expands continuously. The large amount of state data puts pressure on the storage space of the blockchain nodes, so it is necessary to alleviate the storage pressure on the blockchain nodes.
[0059] A state tree is a binary tree composed of a set of nodes, including: numerous leaf nodes at the bottom containing the underlying data; a set of intermediate nodes, each of which is a hash of its two child nodes; and a single root node, also formed by the hashes of its two child nodes, representing the top of the tree. This scheme preferably computes from the bottom up, starting from the leaf nodes and traversing upwards. It iterates through the storage space of each state node in the state tree and analyzes it to determine state nodes that meet preset requirements. These requirements are considered invalid or duplicate states, therefore the resulting nodes are invalid and / or duplicate nodes.
[0060] When the blockchain state tree is initially formed, it relies on staff to set up an update and optimization mechanism. Therefore, the updates in this solution can be automatic or manually selected, with automatic updates being preferred to achieve automatic storage optimization. When this solution detects an update operation on the blockchain state tree, it intercepts the operation and prioritizes traversing the state nodes in the state tree. After pruning the state tree and reducing the storage pressure on the blockchain nodes, it updates the state tree. Assuming that five state nodes meet the requirements, these five state nodes are selected as candidate pruning state nodes to generate a candidate pruning state node set.
[0061] For step S102, when recording data in the blockchain, each state needs to occupy storage space. In order to save storage space, some state nodes can be pruned.
[0062] This scheme first defines the state space dp[i][j] of the candidate pruned state nodes. dp[i][j] is a two-dimensional array representing the space occupied by the candidate pruned state nodes, where i represents the number of states retained after pruning, and j represents the number of pruning operations. Therefore, dp[i][j] represents the minimum storage space requirement to retain i states when j pruning operations are performed. The size of the state space depends on the size of the state tree to be pruned and the maximum allowed number of pruning operations.
[0063] A pre-defined auxiliary function, `costOfPruning`, calculates the storage space savings resulting from pruning operations based on a specific state. This value is typically pre-set; for example, the storage space savings for a candidate pruning state node might be 10. The implementation of this function can vary depending on the application scenario.
[0064] In the blockchain state pruning problem, a state transition function describes the transition relationship from one state to another in the state space. For example, it describes the pruning process from one state tree node to another, such as pruning node i to obtain a new state. The state transition function can be described as follows:
[0065] dp[i][j]=min(dp[i][j],dp[i-1][j-1]+cost_of_pruning(states[i-1]))
[0066] Here, `cost_of_pruning(states[i-1])` represents the storage space saving achieved by pruning state `i`. The state transition function indicates that, given `j` pruning operations, the minimum storage space requirement for retaining `i` states can be achieved by pruning state `i` after `j-1` pruning operations have been performed.
[0067] The above uses the auxiliary function `costOfPruning` and the state transition function to compare two values: the storage space saving value before pruning (represented by `dp[i-1][j]`) and the storage space saving value after pruning the current state (represented by `cost_of_pruning(states[i-1])`). Then, the smaller storage space saving value is selected to update the candidate pruning state node `dp[i][j]`.
[0068] By defining the state space and state transition function, the blockchain state pruning problem can be modeled as a dynamic programming problem. Therefore, the pruning problem can be solved using a dynamic programming algorithm, and the blockchain state can be pruned based on the solution. It should be noted that this algorithm is applied to the underlying blockchain and implemented through code.
[0069] For step S103, assuming there are 5 candidate pruning state nodes, through the above operations, 5 state spaces are obtained, and the final result is stored in dp[n][maxPruningOperations], where n is 5. Assume that the storage space savings obtained by pruning these 5 candidate pruning state nodes are [10, 20, 30, 40, 50].
[0070] When a blockchain state tree is initially formed, it relies on workers to set up update and optimization mechanisms and define boundary conditions. Boundary conditions refer to the limitations in the state space, such as restrictions on the allowed number of pruning operations and the target storage space savings. A main function named `blockchainStatePruning` is set up. `blockchainStatePruning` solves the problem through dynamic programming. This solution takes the state spaces and boundary conditions of the five candidate pruning state nodes as input to select the target pruning state node from these five candidates. `blockchainStatePruning` returns the calculated minimum storage space savings, representing the minimum storage space savings achievable through the optimal pruning strategy.
[0071] In this scheme, pruning does not necessarily process only one state node at a time; the number is not fixed. Similarly, using the example above, these five candidate pruning state nodes might be processed with a single pruning operation, while three pruning operations might only process four state nodes. To optimize the pruning result, this scheme preferably arranges the space-saving values of each state space in ascending order. Following this order, and under the premise of satisfying the constraints, the target pruning state nodes are selected to obtain the optimal pruning strategy. While pruning the selected target pruning state nodes, the processing progress is monitored. When the progress reaches 100%, an update operation is performed on the state tree.
[0072] The method provided in the above embodiments, by modeling the blockchain state pruning problem as a dynamic programming problem and designing an effective state pruning strategy, can reduce computation time, improve computation efficiency, and thus effectively reduce the blockchain data storage requirements. Furthermore, it can be applied to various demand scenarios based on boundary conditions, thereby improving the scalability of the entire network.
[0073] See Figure 2 The diagram illustrates an optional pruning method for blockchain according to an embodiment of the present invention, including the following steps:
[0074] S201: In response to the detected update operation on the blockchain state tree, traverse the storage space of each node in the state tree to determine a set of candidate pruning nodes whose states meet the preset requirements; wherein, the candidate pruning nodes are invalid nodes or duplicate nodes.
[0075] S202: For each candidate pruning node, obtain the storage space saving value before pruning and calculate the storage space saving value after pruning. Update the state space of each candidate pruning node based on the smaller storage space saving value; where the state space represents the space occupied by the node.
[0076] S203: Obtain the preset boundary conditions, call the main function to process the boundary conditions and the state space of all candidate pruning nodes, so as to filter the target pruning node from the set of candidate pruning nodes;
[0077] S204: Invoke the invalid state pruning strategy, obtain the key value of the invalid node, process the key value using a hash function to obtain a hash pointer, find the position corresponding to the hash pointer in the storage space, and perform pruning processing on the data at the position;
[0078] S205: Invoke the duplicate state pruning strategy, query the hash table for the target node that is the same as the duplicate node and has been traversed and processed, and point the hash pointer of the duplicate node to the target node.
[0079] S206: In response to the detection that pruning is complete, perform an update operation on the state tree.
[0080] In the above embodiments, steps S202-S203 and S206 can be found in [reference needed]. Figure 1 The description shown will not be repeated here.
[0081] For step S201, the preset requirement of this scheme is one of invalid state and repeated state. Therefore, the candidate pruning nodes are invalid state nodes and / or repeated state nodes. In actual operation, only invalid state nodes can be considered, or only repeated state nodes can be considered, or both invalid state nodes and repeated state nodes can be considered. This scheme preferably considers both modes.
[0082] 1. Invalid state nodes, mainly those at invalid transaction locations, can also include other abandoned states and empty accounts. 1) Abandoned states: Some states may become useless or outdated over time. These states may originate from completed or invalid transactions, such as spent UTXOs (Unspent Transaction Outputs) or intermediate states of completed smart contract execution. 2) Empty accounts: Some accounts may no longer have any balance in the blockchain system, or have been inactive for a long time. The states of these accounts can be considered invalid because they have no real impact on the operation of the entire system.
[0083] 2. Duplicate State Nodes: State trees may reuse some tree nodes, or some state nodes may reuse the same information. These duplicate state nodes can be saved, thus saving storage space in the blockchain. This mainly refers to state nodes with shared state or duplicate smart contract code. 1) Shared State: In a blockchain network, multiple states may have the same data structure and values. These states may originate from similar transactions or smart contract execution results. For example, multiple accounts may have the same balance and transaction history; these states can be considered duplicate states. 2) Duplicate Smart Contract Code: Smart contract code may be reused in different contract instances. In this case, the smart contract code may appear multiple times in the state tree, leading to wasted storage space.
[0084] For steps S204 and S205, the state pruning strategy can be formulated based on the type of state node, and is divided into invalid state pruning strategy and duplicate state pruning strategy. These two strategies can be performed simultaneously.
[0085] 1. For invalid state nodes, the hash pointer property of the blockchain can be used to ensure the correct removal of invalid state nodes while maintaining the integrity of the data structure.
[0086] During program execution, data is required. However, when the data is large and requires significant space, it can cause considerable inconvenience. Blockchain utilizes hash pointers; when data needs to be retrieved, it is simply read from the corresponding location at the address provided by the pointer, thus greatly saving memory space.
[0087] A hash table is a data structure that allows direct access based on a key value. It speeds up the search by mapping the key value to a location within the table. Elements in a hash table are determined by a hash function, which maps the key value of an element to its storage location. The key of the data element is used as the argument, and the value calculated by the hash function is the storage address of that element.
[0088] The purpose of hash pointers is to map invalid states to corresponding storage locations using a hash function, rather than performing a sequential search. Each invalid state node's hash pointer uniquely identifies that state and its location. Hash pointers allow for fast access to the storage location of a specific invalid state node without needing to sequentially traverse the entire data structure.
[0089] When pruning invalid state nodes, the nodes following the invalid node are not affected. In effect, the corresponding state is marked as invalid, which does not affect the position or links of other nodes. Other nodes maintain their original order and pointer relationships, thus preventing data structure breaks or errors.
[0090] 2. For duplicate state nodes, the hash table stores the identification information of the state node, which is the hash value or other unique identifier of the state node. For duplicate state nodes, a hash table can be used to store the state nodes that have been processed in the state tree, and during the pruning process, it is checked whether the current state node already exists in the hash table. If it exists, the duplicate state node is merged, and the pointer pointing to the current state node is reassigned to the existing target state.
[0091] It should be noted that the approach of this scheme is as follows: define the actual state of the problem, treat the leaf state nodes as the smallest subproblems, and start from the smallest subproblem to progressively calculate the solutions to larger subproblems upwards. The most basic and smallest subproblem in the problem, which has no smaller subproblems to which it can be decomposed, is the smallest subproblem. This scheme processes the problem progressively upwards from the leaf state nodes and stores the pruning results in a hash table. Therefore, the hash table stores only the processed state nodes, thus avoiding redundant calculations.
[0092] The methods provided in the above embodiments propose various pruning strategies for the blockchain state pruning problem, such as invalid state pruning and duplicate state pruning. These strategies can effectively remove irrelevant state nodes and reduce storage space requirements. Compared with existing state storage optimization based on Merkle Patricia trees, this solution has better performance in state pruning and can better adapt to different scenarios and optimization needs.
[0093] See Figure 3 The diagram illustrates another optional pruning method for blockchain according to an embodiment of the present invention, including the following steps:
[0094] S301: In response to the detected update operation on the blockchain state tree, traverse the storage space of each node in the state tree to determine the set of candidate pruning nodes whose state meets the preset requirements.
[0095] S302: For each candidate pruning node, obtain the storage space saving value before pruning and calculate the storage space saving value after pruning. Update the state space of each candidate pruning node based on the smaller storage space saving value; where the state space represents the space occupied by the node.
[0096] S303: Sort the state space of each candidate pruning node in descending order of storage space saving value; generate an array based on the state space of each candidate pruning node;
[0097] S304: Obtain the preset boundary conditions, call the main function to process the boundary conditions and the array, and select the target pruning node from the candidate pruning node set according to the sorting and perform pruning processing;
[0098] S305: In response to the detection that pruning is complete, perform an update operation on the state tree.
[0099] In the above embodiments, steps S301, S302, and S305 can be found in [reference needed]. Figure 1 The description shown will not be repeated here.
[0100] For steps S303 and S304, when creating the state space dp[i][j] of each candidate pruning node, the auxiliary function costOfPruning and the state transition function compare two values: the storage space saving value before pruning (represented by dp[i-1][j]) and the storage space saving value after pruning the current state (represented by cost_of_pruning(states[i-1])). The smaller storage space saving value is selected to update dp[i][j]. The code uses two nested loops to fill the dp array. The outer loop iterates through each state, and the inner loop iterates through the number of pruning operations, thus filling the dp array.
[0101] Assuming there are 5 candidate pruned state nodes, the above operations yield 5 state spaces. Assuming the storage space savings from pruning each state node are [10, 20, 30, 40, 50], an array is constructed based on these 5 state spaces. The final result is stored in dp[n][maxPruningOperations], where n is 5. Thus, the array is State{{Cost:10},{Cost:20},{Cost:30},{Cost:40},{Cost:50}}. The main function blockchainStatePruning takes the above array State and preset boundary conditions as input to select the target pruned state node from the 5 candidate pruned state nodes.
[0102] In this scheme, pruning does not necessarily process only one state node at a time; the number is not fixed. Similarly, in the example above, these five candidate pruning state nodes might be processed with a single pruning operation, while three pruning operations might only process four state nodes. To optimize the pruning result, this scheme preferably arranges the space-saving values of each state space in ascending order. The target pruning state nodes are then selected according to this order to obtain the best pruning strategy. After pruning the selected target pruning state nodes, the processing progress is monitored. When the progress reaches 100%, an update operation is performed on the state tree.
[0103] The method provided in the above embodiments uses a dynamic programming algorithm to find the optimal pruning strategy in descending order of storage space savings, so as to effectively reduce the storage space requirements of blockchain state data, thereby reducing the consumption of system resources and improving system performance.
[0104] See Figure 4 The diagram illustrates another optional pruning method for blockchain according to an embodiment of the present invention, including the following steps:
[0105] S401: In response to the detected update operation on the blockchain state tree, traverse the storage space of each node in the state tree to determine the set of candidate pruning nodes whose state meets the preset requirements.
[0106] S402: For each candidate pruning node, obtain the storage space saving value before pruning and calculate the storage space saving value after pruning. Update the state space of each candidate pruning node based on the smaller storage space saving value; where the state space represents the space occupied by the node.
[0107] S403: Obtain the preset pruning operation count threshold, call the main function to process the preset pruning operation count threshold and the state space of all candidate pruning nodes, so as to select the target pruning node from the candidate pruning node set and perform pruning processing, and in response to the pruning completion being detected, perform an update operation on the state tree.
[0108] In the above embodiments, steps S401 and S402 can be found in [reference needed]. Figure 1 The description shown will not be repeated here.
[0109] For step S403, when the blockchain state tree is first formed, it needs to rely on workers to set up an update and optimization mechanism for it, and at the same time set the maximum number of allowed pruning operations, that is, a preset pruning operation threshold, such as 3 times, maxPruningOperations:=3. Therefore, before resetting this number, no matter when the state tree is updated, the maximum number of pruning operations is 3.
[0110] This scheme also defines the blockchainStatePruning function to implement the dynamic programming algorithm, which takes maxPruningOperations and the state space of all candidate pruning nodes as input and returns the minimum storage space requirement under the maximum number of pruning operations.
[0111] Assuming there are 5 candidate pruning state nodes, and the above operations result in 5 state spaces, and assuming that the storage space savings obtained by pruning each state node are [10, 20, 30, 40, 50], only 4 state nodes can be processed when maxPruningOperations:=3. Therefore, in descending order of storage space savings, the candidate pruning state nodes corresponding to [50, 40, 30, 20] are selected as the target pruning state nodes.
[0112] The method provided in the above embodiments allows the dynamic programming algorithm to be adjusted according to different scenarios and needs, such as adjusting the maximum allowed number of pruning operations and finding the optimal state transition path in the state space, so as to minimize the storage space requirement given the maximum number of pruning operations, thereby realizing a flexible pruning strategy to adapt to various practical application scenarios.
[0113] See Figure 5 The diagram illustrates another optional pruning method for blockchain according to an embodiment of the present invention, including the following steps:
[0114] S501: In response to the detected update operation on the blockchain state tree, traverse the storage space of each node in the state tree to determine the set of candidate pruning nodes whose state meets the preset requirements.
[0115] S502: For each candidate pruning node, obtain the storage space saving value before pruning and calculate the storage space saving value after pruning. Update the state space of each candidate pruning node based on the smaller storage space saving value; where the state space represents the space occupied by the node.
[0116] S503: Obtain the total value of the target storage space saving, call the main function to process the total value of the target storage space saving and the state space of all candidate pruning nodes, so as to filter the target pruning node from the set of candidate pruning nodes and perform pruning processing, and in response to the detection that pruning is completed, perform an update operation on the state tree.
[0117] In the above embodiments, steps S501 and S502 can be found in [reference needed]. Figure 1 The description shown will not be repeated here.
[0118] For step S503, at the initial stage of blockchain state tree formation, workers need to set up an update and optimization mechanism for it, and simultaneously set a target total storage space saving value, such as 120. This scheme also defines the blockchainStatePruning function to implement a dynamic programming algorithm, taking the state space of all candidate pruning nodes and the target total storage space saving value as input, and returning the minimum storage space requirement under the constraint of the target total storage space saving value.
[0119] Assuming there are 5 candidate pruning state nodes, the above operations result in 5 state spaces. Assuming the storage space savings obtained by pruning each state node are [10, 20, 30, 40, 50], and with a target total storage space savings of 120, two schemes are obtained: [10, 20, 40, 50] and [30, 40, 50]. Either one can be chosen, or the one with fewer state nodes can be selected. This scheme does not impose any restrictions on this.
[0120] The method provided in the above embodiments allows the dynamic programming algorithm to be adjusted according to different scenarios and needs, such as adjusting the target storage space to save total value, thereby realizing a flexible pruning strategy to adapt to various practical application scenarios.
[0121] See Figure 6 The diagram illustrates another optional pruning method for blockchain according to an embodiment of the present invention, including the following steps:
[0122] S601: In response to the detection of an update operation on the blockchain state tree, traverse the storage space of each node in the state tree to determine a set of candidate pruning nodes whose states meet preset requirements.
[0123] S602: For each candidate pruning node, obtain the storage space saving value before pruning and calculate the storage space saving value after pruning. Update the state space of each candidate pruning node based on the smaller storage space saving value; where the state space represents the space occupied by the node.
[0124] S603: Obtain the preset pruning operation count threshold and the target storage space saving value, call the main function to process the preset pruning operation count threshold, the target storage space saving value and the state space of all candidate pruning nodes, so as to filter the target pruning node from the candidate pruning node set and perform pruning processing;
[0125] S604: In response to the detection that pruning is complete, perform an update operation on the state tree.
[0126] The method provided in the above embodiments allows the dynamic programming algorithm to be adjusted according to different scenarios and needs, such as adjusting the total value of target storage space savings and the maximum number of allowed pruning operations, thereby realizing a flexible pruning strategy to adapt to various practical application scenarios.
[0127] The method provided in this embodiment of the invention has at least the following advantages over the prior art:
[0128] 1. By defining the state space and state transition functions, and designing corresponding pruning strategies for invalid and repetitive states, a dynamic programming algorithm is used to optimize the storage space of the blockchain state tree. This effectively reduces the storage space requirements of blockchain state data, lowers system resource consumption and storage costs, and improves system performance and scalability. For large-scale blockchain systems and commercial applications, reducing storage costs has a significant impact on the overall system operating costs.
[0129] 2. Existing state pruning techniques may not achieve ideal optimization results in certain scenarios. The dynamic programming algorithm, pruning strategy, and maximum number of pruning operations provided in this solution can better adapt to different scenarios and optimization needs, and are more flexible.
[0130] 3. The blockchain state pruning problem is modeled as a dynamic programming problem. The characteristic of dynamic programming algorithm is that it decomposes the problem into subproblems and avoids repeated calculations by looking up tables. In addition, an effective pruning strategy is designed to effectively remove irrelevant states, reduce computation time, improve computation efficiency, and thus reduce computational overhead.
[0131] See Figure 7 The diagram shows a schematic of the main modules of a pruning device 700 for blockchain application provided in an embodiment of the present invention, including:
[0132] Alternate module 701 is used to respond to the listening operation of updating the blockchain state tree, traverse the storage space of each node in the state tree, and determine the set of alternative pruning nodes whose state meets the preset requirements.
[0133] The calculation module 702 is used to obtain the storage space saving value before pruning for each candidate pruning node, and to calculate the storage space saving value after pruning. The state space of each candidate pruning node is updated based on the smaller storage space saving value. The state space represents the space occupied by the node.
[0134] The pruning module 703 is used to obtain preset boundary conditions, call the main function to process the boundary conditions and the state space of all candidate pruning nodes, so as to select target pruning nodes from the set of candidate pruning nodes and perform pruning processing. In response to the detection that pruning is completed, it performs an update operation on the state tree.
[0135] In the apparatus of this invention, the pruning module 703 is used for:
[0136] Based on the state space of each candidate pruning node, an array is generated, and the main function is called to process the boundary conditions and the array in order to select the target pruning node from the set of candidate pruning nodes and perform pruning processing.
[0137] In the apparatus of this invention, the pruning module 703 is used for:
[0138] Sort the state space of each candidate pruning node in descending order of storage space savings;
[0139] The main function processes the boundary conditions and the state space of all candidate pruning nodes to select target pruning nodes from the candidate pruning node set according to the sorting and perform pruning processing.
[0140] In the implementation device of the present invention, the boundary condition is at least one of a preset threshold for the number of pruning operations and a target total storage space saving value.
[0141] In the apparatus of this invention, the candidate pruning nodes are at least one of invalid nodes and duplicate nodes, and the pruning module 703 includes one or more of the following:
[0142] Invoke the invalid state pruning strategy, obtain the key value of the invalid node, process the key value using a hash function to obtain a hash pointer, find the position corresponding to the hash pointer in the storage space, and perform pruning on the data at the position;
[0143] Invoke the duplicate state pruning strategy, query the hash table for the target node that is the same as the duplicate node and has been traversed and processed, and point the hash pointer of the duplicate node to the target node.
[0144] Furthermore, the specific implementation details of the device described in the embodiments of the present invention have been described in detail in the above-described method, so the details will not be repeated here.
[0145] Figure 8 An exemplary system architecture 800 to which embodiments of the present invention can be applied is shown, including terminal devices 801, 802, 803, network 804, and server 805 (only an example).
[0146] Terminal devices 801, 802, and 803 can be various electronic devices with displays and web browsing capabilities, and can be equipped with various communication client applications. Users can use terminal devices 801, 802, and 803 to interact with server 805 via network 804 to receive or send messages, etc.
[0147] Network 804 is a medium used to provide a communication link between terminal devices 801, 802, 803 and server 805. Network 804 can include various connection types, such as wired or wireless communication links or fiber optic cables, etc.
[0148] Server 805 can be a server that provides various services. It should be noted that the methods provided in the embodiments of the present invention are generally executed by server 805, and correspondingly, the devices are generally set in server 805.
[0149] It should be understood that Figure 8 The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.
[0150] The following is for reference. Figure 9It shows a schematic diagram of the structure of a computer system 900 suitable for implementing a terminal device of the present invention. Figure 9 The terminal device shown is merely an example and should not impose any limitations on the functionality and scope of use of the embodiments of the present invention.
[0151] like Figure 9 As shown, the computer system 900 includes a central processing unit (CPU) 901, which can perform various appropriate actions and processes based on programs stored in read-only memory (ROM) 902 or programs loaded from storage section 908 into random access memory (RAM) 903. The RAM 903 also stores various programs and data required for the operation of the system 900. The CPU 901, ROM 902, and RAM 903 are interconnected via a bus 904. An input / output (I / O) interface 905 is also connected to the bus 904.
[0152] The following components are connected to I / O interface 905: an input section 906 including a keyboard, mouse, etc.; an output section 907 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and speakers, etc.; a storage section 908 including a hard disk, etc.; and a communication section 909 including a network interface card such as a LAN card, modem, etc. The communication section 909 performs communication processing via a network such as the Internet. A drive 910 is also connected to I / O interface 905 as needed. A removable medium 911, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on drive 910 as needed so that computer programs read from it can be installed into storage section 908 as needed.
[0153] In particular, according to the embodiments disclosed in this invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this invention include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via communication section 909, and / or installed from removable medium 911. When the computer program is executed by central processing unit (CPU) 901, it performs the functions defined above in the system of this invention.
[0154] It should be noted that the computer-readable medium shown in this invention can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this invention, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In this invention, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. Computer-readable signal media can also be any computer-readable medium other than computer-readable storage media, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wireless, wire, optical fiber, RF, etc., or any suitable combination thereof.
[0155] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0156] The modules described in the embodiments of the present invention can be implemented in software or hardware. The described modules can also be housed in a processor; for example, a processor can be described as including alternative modules, a computation module, and a pruning module. The names of these modules do not necessarily limit the module itself; for example, the computation module can also be described as a "state space module."
[0157] In another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiments; or it may exist independently and not assembled into the device. The computer-readable medium carries one or more programs that, when executed by the device, cause the device to perform any of the aforementioned pruning methods applied to blockchain.
[0158] The computer program product of the present invention includes a computer program that, when executed by a processor, implements the pruning method applied to blockchain in the embodiments of the present invention.
[0159] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.
Claims
1. A pruning method applied to blockchain, characterized in that, include: In response to the detected update operation on the blockchain state tree, the storage space of each node in the state tree is traversed to determine the set of candidate pruning nodes whose state meets the preset requirements. For each candidate pruning node, obtain the storage space saving value before pruning and calculate the storage space saving value after pruning. Update the state space of each candidate pruning node based on the smaller storage space saving value. Here, the state space represents the space occupied by the node. The storage space saving value before pruning is represented by dp[i-1][j], and the storage space saving value after pruning the current state is represented by cost_of_pruning(states[i-1]). dp[i][j] represents the minimum storage space requirement of retaining i states when pruning is performed j times. cost_of_pruning(states[i-1]) represents the storage space saving value brought by pruning state i. Obtain the preset boundary conditions, call the main function to process the boundary conditions and the state space of all candidate pruning nodes, so as to select the target pruning node from the set of candidate pruning nodes and perform pruning processing. In response to the detection that pruning is completed, perform the update operation on the state tree.
2. The method according to claim 1, characterized in that, The invocation of the main function processes the boundary conditions and the state space of all candidate pruning nodes to select target pruning nodes from the set of candidate pruning nodes and perform pruning processing, including: Based on the state space of each candidate pruning node, an array is generated, and the main function is called to process the boundary conditions and the array in order to select the target pruning node from the set of candidate pruning nodes and perform pruning processing.
3. The method according to claim 1 or 2, characterized in that, The invocation of the main function processes the boundary conditions and the state space of all candidate pruning nodes to select target pruning nodes from the set of candidate pruning nodes and perform pruning processing, including: Sort the state space of each candidate pruning node in descending order of storage space savings; The main function processes the boundary conditions and the state space of all candidate pruning nodes to select target pruning nodes from the candidate pruning node set according to the sorting and perform pruning processing.
4. The method according to claim 1 or 2, characterized in that, The boundary condition is at least one of a preset threshold for the number of pruning operations and the target total storage space saving value.
5. The method according to claim 1, characterized in that, The candidate pruning nodes are at least one of invalid nodes and duplicate nodes. The step of selecting target pruning nodes from the set of candidate pruning nodes and performing pruning processing includes one or more of the following: Invoke the invalid state pruning strategy, obtain the key value of the invalid node, process the key value using a hash function to obtain a hash pointer, find the position corresponding to the hash pointer in the storage space, and perform pruning on the data at the position; Invoke the duplicate state pruning strategy, query the hash table for the target node that is the same as the duplicate node and has been traversed and processed, and point the hash pointer of the duplicate node to the target node.
6. A pruning device for blockchain applications, characterized in that, include: The alternative module is used to respond to the listening operation of updating the blockchain state tree, traverse the storage space of each node in the state tree, and determine the set of alternative pruning nodes whose state meets the preset requirements. The calculation module is used to obtain the storage space saving value before pruning for each candidate pruning node, and to calculate the storage space saving value after pruning. It updates the state space of each candidate pruning node based on the smaller storage space saving value. Here, the state space represents the space occupied by the node; the storage space saving value before pruning is represented by dp[i-1][j], and the storage space saving value after pruning the current state is represented by cost_of_pruning(states[i-1]); dp[i][j] represents the minimum storage space requirement of retaining i states when pruning is performed j times; cost_of_pruning(states[i-1]) represents the storage space saving value brought by pruning state i. The pruning module is used to obtain preset boundary conditions, call the main function to process the boundary conditions and the state space of all candidate pruning nodes, so as to select target pruning nodes from the set of candidate pruning nodes and perform pruning processing. In response to the detection that pruning is completed, it performs an update operation on the state tree.
7. The apparatus according to claim 6, characterized in that, The pruning module is used for: Based on the state space of each candidate pruning node, an array is generated, and the main function is called to process the boundary conditions and the array in order to select the target pruning node from the set of candidate pruning nodes and perform pruning processing.
8. The apparatus according to claim 6 or 7, characterized in that, The pruning module is used for: Sort the state space of each candidate pruning node in descending order of storage space savings; The main function processes the boundary conditions and the state space of all candidate pruning nodes to select target pruning nodes from the candidate pruning node set according to the sorting and perform pruning processing.
9. The apparatus according to claim 6 or 7, characterized in that, The boundary condition is at least one of a preset threshold for the number of pruning operations and the target total storage space saving value.
10. The apparatus according to claim 6, characterized in that, The candidate pruning nodes are at least one of invalid nodes and duplicate nodes, and the pruning module includes one or more of the following: Invoke the invalid state pruning strategy, obtain the key value of the invalid node, process the key value using a hash function to obtain a hash pointer, find the position corresponding to the hash pointer in the storage space, and perform pruning on the data at the position; Invoke the duplicate state pruning strategy, query the hash table for the target node that is the same as the duplicate node and has been traversed and processed, and point the hash pointer of the duplicate node to the target node.
11. An electronic device, characterized in that, include: One or more processors; Storage device for storing one or more programs. When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described in any one of claims 1-5.
12. A computer-readable medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-5.
13. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the method as described in any one of claims 1-5.