State tree data caching method and device, electronic equipment and computer program product

By introducing a new state tree cache data structure into the blockchain system, using three cache arrays and index mechanisms, the problems of high read and write pressure and high addressing complexity in the existing technology are solved, and more efficient data reading and cache management are achieved.

CN119759896AActive Publication Date: 2025-04-04HANGZHOU HIGH-TECH ZONE (BINJIANG) INSTITUTE OF BLOCKCHAIN & DATA SECURITY
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202411561672.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-04
Publication Date
2025-04-04
Estimated Expiration
2044-11-04

AI Technical Summary

Technical Problem

When existing blockchain systems read and write state tree data, they cause high disk read and write pressure, and the addressing complexity of the general cache is high, affecting performance.

Method used

A new state tree cache data structure is proposed, including three cache arrays: a first cache array for cache header information of intermediate nodes, a second cache array for cache child information of intermediate nodes, and a third cache array for cache state data information of leaf nodes. This structure reduces addressing complexity through parent node index and child node index.

Benefits of technology

It significantly reduces the number of addressing times for reading state tree data, reduces disk read and write pressure, improves the reading speed of state tree data, and reduces the GC overhead during Golang program run.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119759896A_ABST
    Figure CN119759896A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of block chains, and provides a state tree data caching method and device, electronic equipment and a computer program product. According to the method, after to-be-cached state tree data is obtained, head information of each intermediate node is cached to a first cache array, child information of each child node of each intermediate node is cached to a second cache array, and state data information of each leaf node is cached to a third cache array. Aiming at each intermediate node, utilizing a father node index in the head information of the intermediate node and a first child node index of each child node, and utilizing meta information and a second child node index in child information of each child node of the intermediate node; the information of the father node and each child node connected with the intermediate node can be conveniently found through each cache array, so that the logic relationship among the nodes of the state tree can be accurately constructed. By adopting the method, the addressing complexity can be reduced, and the addressing times of reading the state tree data can be reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of blockchain technology, and in particular to a state tree data caching method, device, electronic device, and computer program product. Background Art

[0002] At present, blockchain systems usually use state trees as data structures for storing account and contract states, such as MPT (Merkle Patricia Tree) state trees and JMT (Jellyfish Merkle Tree) state trees. The state change data obtained by executing block transactions will eventually be applied to the structure of the state tree and stored persistently in the blockchain ledger. This process requires reading a large amount of node data in the state tree corresponding to the previous block from the ledger, which puts a lot of pressure on the disk to read and write. To address this problem, the prior art generally uses a general local cache to cache part of the state tree's node data into memory, so that it is not necessary to read the node data directly from the disk every time, which can not only alleviate the disk's read and write pressure, but also improve the reading speed of the state tree data. However, most general caches are constructed based on the underlying data structure of hash buckets and circular arrays, and their addressing complexity is high. When reading state tree data, it is necessary to perform a large number of addressing operations. Summary of the invention

[0003] In view of this, embodiments of the present application provide a state tree data caching method, apparatus, electronic device, and computer program product. By proposing a new state tree cache data structure, the addressing complexity can be reduced and the number of addressing times for reading state tree data can be reduced.

[0004] A first aspect of an embodiment of the present application provides a state tree data caching method, including:

[0005] Obtaining state tree data to be cached, the state tree data includes several intermediate nodes and several leaf nodes;

[0006] Cache the header information of each intermediate node into a first cache array, where the header information includes a parent node index corresponding to the intermediate node and a first child node index of each child node corresponding to the intermediate node;

[0007] Cache the child information of each child node of each intermediate node into the second cache array, where the child information includes the meta information of the corresponding child node and the second child node index;

[0008] Cache the status data information of each leaf node into the third cache array;

[0009] Among them, the parent node index is used to indicate that the corresponding intermediate node is the root node, or to indicate the storage position of the header information of the parent node of the corresponding intermediate node in the first cache array; the first child node index is used to indicate the storage position of the child information of the corresponding child node of the corresponding intermediate node in the second cache array; the second child node index is used to indicate the storage position of the status data information of the corresponding child node of the leaf node type in the third cache array, or to indicate the storage position of the header information of the corresponding child node of the intermediate node type in the first cache array.

[0010] The embodiment of the present application proposes a new state tree cache data structure, which mainly includes three cache arrays, wherein the first cache array is used to cache the header information of the intermediate node, the second cache array is used to cache the child information of the intermediate node, and the third cache array is used to cache the state data information of the leaf node. After the state tree data to be cached is obtained, the header information of each intermediate node of the state tree data will be cached to the first cache array, the child information of each child node of each intermediate node of the state tree data will be cached to the second cache array, and the state data information of each leaf node of the state tree data will be cached to the third cache array. For each intermediate node, using the parent node index in its header information and the first child node index of each child node, and using the meta information and the second child node index in the child information of each child node, it is convenient to find the information of the parent node and each child node connected to the intermediate node through each cache array, so as to accurately construct the logical relationship between each node of the state tree. For a general cache based on hash bucketing and a circular array, assuming that the number of nodes in the state tree is N and the number of buckets is M, the time complexity of the number of addressing times required to read the data is O(N / M). If the state tree cache data structure provided by the embodiment of the present application is used, its addressing method is consistent with the method of traversing in the state tree, the addressing complexity is greatly reduced, and the time complexity of the number of addressing times required to read data is O(logN), which shows that the number of addressing times for reading state tree data can be significantly reduced compared with general cache. In addition, the state tree cache data structure uses a long array as a cache, which can effectively reduce the GC overhead when the Golang program is running.

[0011] In one implementation of the embodiment of the present application, the method further includes:

[0012] When receiving a request to read status data information of a target leaf node among the plurality of leaf nodes, reading the header information of the root node from the first cache array as the current header information;

[0013] Determine the current addressing position according to the addressing principle of the prefix tree and the addressing path of the target leaf node;

[0014] From the current header information, find the first child node index of the target child node corresponding to the current addressing position;

[0015] If the first child node index of the target child node is found, the child information of the target child node is read from the second cache array according to the first child node index of the target child node;

[0016] If the target child node is an intermediate node, then read the header information of the target child node from the first cache array according to the second child node index of the child information of the target child node, use the header information of the target child node to update the current header information, and return to execute the step of determining the current addressing position according to the addressing principle of the prefix tree and the addressing path of the target leaf node;

[0017] If the target child node is a leaf node, the state data information of the target child node is read from the third cache array according to the second child node index of the child information of the target child node as the state data information of the target leaf node.

[0018] In an implementation of the embodiment of the present application, after searching the current header information for the first child node index of the target child node corresponding to the current addressing position, the method further includes:

[0019] If the first child node index of the target child node cannot be found, the state data information of the target leaf node is read from the disk according to the addressing path of the target leaf node.

[0020] In one implementation of the embodiment of the present application, the method further includes:

[0021] When receiving a pruning request for the state tree data, determining the node to be deleted;

[0022] If the node to be deleted is an intermediate node, the storage location of the header information of the node to be deleted in the first cache array is marked as free; the first child node indexes of all child nodes of the node to be deleted are searched from the header information of the node to be deleted, and the storage locations corresponding to the first child node indexes of all child nodes in the second cache array are marked as free; the header information of the parent node of the node to be deleted is searched from the first cache array, the first child node index of the node to be deleted is searched from the header information of the parent node of the node to be deleted, the storage location corresponding to the first child node index of the node to be deleted in the second cache array is marked as free, and the first child node index of the node to be deleted in the header information of the parent node of the node to be deleted is set to a specified value;

[0023] If the node to be deleted is a leaf node, the storage location of the status data information of the node to be deleted in the third cache array is marked as free; the header information of the parent node of the node to be deleted is searched from the first cache array, the first child node index of the node to be deleted is searched from the header information of the parent node of the node to be deleted, the storage location corresponding to the first child node index of the node to be deleted in the second cache array is marked as free, and the first child node index of the node to be deleted in the header information of the parent node of the node to be deleted is set to a specified value.

[0024] In one implementation of the embodiment of the present application, the method further includes:

[0025] When receiving an update request for the state tree data, determining a node to be updated;

[0026] Find the state tree node with the same addressing path as the node to be updated from the state tree data;

[0027] If the status tree node is found and the node to be updated is an intermediate node, then according to the header information of the node to be updated, the header information of the status tree node cached in the first cache array is updated; according to the child information of all child nodes of the node to be updated, each child information to be updated and each child information to be added is determined; according to each child information to be updated, the child information cached in the second cache array is updated, and each child information to be added is written into an idle storage location of the second cache array respectively; if the node to be updated is not a root node, the header information of the parent node of the node to be updated is searched from the first cache array, and according to the header information of the parent node of the node to be updated, the child information corresponding to the node to be updated is searched and updated from the second cache array;

[0028] If the status tree node is found and the node to be updated is a leaf node, the status data information of the status tree node cached in the third cache array is updated according to the status data information of the node to be updated; the header information of the parent node of the node to be updated is searched from the first cache array, and the child information corresponding to the node to be updated is searched and updated from the second cache array according to the header information of the parent node of the node to be updated.

[0029] In an implementation of the embodiment of the present application, after searching the status tree data for a status tree node having the same addressing path as the node to be updated, the method further includes:

[0030] If the state tree node cannot be found and the node to be updated is an intermediate node, the header information of the node to be updated is written into a free storage position of the first cache array; according to the child information of all child nodes of the node to be updated, each child information to be updated and each child information to be added is determined; according to each child information to be updated, the child information cached in the second cache array is updated, and each child information to be added is written into a free storage position of the second cache array respectively; if the node to be updated is not a root node, the header information of the parent node of the node to be updated is searched from the first cache array, and according to the header information of the parent node of the node to be updated, the child information corresponding to the node to be updated is searched and updated from the second cache array;

[0031] If the status tree node cannot be found and the node to be updated is a leaf node, the status data information of the node to be updated is written into an idle storage location of the third cache array; the header information of the parent node of the node to be updated is searched from the first cache array, and based on the header information of the parent node of the node to be updated, the child information corresponding to the node to be updated is searched and updated from the second cache array.

[0032] In one implementation of the embodiment of the present application, the sizes of the storage locations of the first cache array are the same, and the sizes of the storage locations of the second cache array are the same.

[0033] A second aspect of an embodiment of the present application provides a state tree data cache device, including:

[0034] A state tree acquisition module is used to acquire state tree data to be cached, where the state tree data includes a number of intermediate nodes and a number of leaf nodes;

[0035] A first cache module, used to cache the header information of each intermediate node into a first cache array, wherein the header information includes a parent node index corresponding to the intermediate node and a first child node index of each child node corresponding to the intermediate node;

[0036] A second cache module, used for caching the child information of each child node of each intermediate node into a second cache array, wherein the child information includes the meta information of the corresponding child node and the second child node index;

[0037] A third cache module, used for caching the state data information of each leaf node into a third cache array;

[0038] Among them, the parent node index is used to indicate that the corresponding intermediate node is the root node, or to indicate the storage position of the header information of the parent node of the corresponding intermediate node in the first cache array; the first child node index is used to indicate the storage position of the child information of the corresponding child node of the corresponding intermediate node in the second cache array; the second child node index is used to indicate the storage position of the status data information of the corresponding child node of the leaf node type in the third cache array, or to indicate the storage position of the header information of the corresponding child node of the intermediate node type in the first cache array.

[0039] A third aspect of an embodiment of the present application provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the state tree data caching method provided in the first aspect of the embodiment of the present application is implemented.

[0040] A fourth aspect of the embodiments of the present application provides a computer program product. When the computer program product is run on an electronic device, the electronic device executes the state tree data caching method provided in the first aspect of the embodiments of the present application.

[0041] A fifth aspect of an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the state tree data caching method provided in the first aspect of the embodiment of the present application is implemented.

[0042] It can be understood that the beneficial effects of the second to fifth aspects mentioned above can be found in the relevant description of the first aspect mentioned above, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0044] Figure 1 It is a schematic diagram of a state tree cache data structure provided by an embodiment of the present application;

[0045] Figure 2 It is a flow chart of a state tree data caching method provided by an embodiment of the present application;

[0046] Figure 3 It is a schematic diagram of an operation of caching state tree data using a state tree cache data structure provided by an embodiment of the present application;

[0047] Figure 4 It is a schematic diagram of an operation flow for reading node data cached in a state tree cache data structure provided by an embodiment of the present application;

[0048] Figure 5 It is a schematic diagram of an operation of pruning state tree data cached in a state tree cache data structure provided by an embodiment of the present application;

[0049] Figure 6 It is a schematic diagram of an operation of updating the state tree data cached in the state tree cache data structure provided by an embodiment of the present application;

[0050] Figure 7 It is a structural diagram of a state tree data cache device provided in an embodiment of the present application;

[0051] Figure 8 It is a schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0052] In the following description, for the purpose of illustration rather than limitation, specific details such as specific system structures, technologies, etc. are proposed, so as to thoroughly understand the embodiments of the present application. However, it should be clear to those skilled in the art that the present application can also be implemented in other embodiments without these specific details. In other cases, the detailed description of well-known systems, devices, circuits and methods is omitted to prevent unnecessary details from hindering the description of the present application. In addition, in the description of the present application specification and the attached claims, the terms "first", "second", "third" etc. are only used to distinguish the description, and cannot be interpreted as indicating or suggesting relative importance.

[0053] In the field of blockchain technology, the read and write performance of state tree data is an important factor affecting blockchain performance. The use of general cache can reduce the disk read and write pressure caused by reading state tree data and improve data reading speed. However, the existing general cache is not designed for the state tree structure of blockchain. Especially in the pruning scenario where the state tree is frequently added and deleted, the general cache is based on the lazy deletion mechanism, which has problems such as large GC overhead and low cache space utilization when the Golang program is running. Moreover, most general caches are based on the underlying data structure of hash buckets and circular arrays, and their addressing complexity is high. When reading state tree data, it is necessary to perform more addressing operations.

[0054] In view of the above-mentioned problems of using a general cache, the embodiment of the present application proposes a dedicated cache for the blockchain state tree according to the actual operation scenario of the blockchain. It adopts a new state tree cache data structure, which can reduce the addressing complexity, improve the cache space utilization, and has the advantage of low GC overhead when the Golang program is running. The dedicated cache is very suitable for caching the state tree data of the blockchain, and can effectively improve the overall performance of the blockchain, but this does not mean that the dedicated cache can only be used to cache the state tree data of the blockchain. In fact, any state tree data that meets the requirements of the relevant data structure can be stored through the dedicated cache.

[0055] Based on the dedicated cache, the embodiments of the present application provide a state tree data caching method, device, electronic device and computer program product, which can reduce the addressing complexity and reduce the number of addressing times for reading state tree data. The data structure of the dedicated cache is first described below.

[0056] This dedicated cache is mainly for the JMT state tree, which consists of the intermediate node InternalNode and the leaf node LeafNode. The intermediate node InternalNode contains an array of length 16, each array element corresponds to the meta information of a child node, and the meta information mainly includes the hash value of the child node, the version number (block height) Version, and whether it is a leaf node. The leaf node LeafNode corresponds to a state data information, including the state key Key and the state value Value.

[0057] Based on the data structure of the JMT state tree, the state tree cache data structure of the dedicated cache mainly includes three cache arrays, which are respectively recorded as the first cache array, the second cache array and the third cache array. The first cache array is used to cache the header information of the intermediate node, the second cache array is used to cache the child information of the intermediate node, and the third cache array is used to cache the state data information of the leaf node. The header information includes the parent node index of the corresponding intermediate node and the first child node index of each child node of the corresponding intermediate node, and the child information includes the meta information of the corresponding child node and the second child node index. Among them, the parent node index is used to indicate that the corresponding intermediate node is a root node, or to indicate the storage position of the header information of the parent node of the corresponding intermediate node in the first cache array; the first child node index is used to indicate the storage position of the child information of the corresponding child node of the corresponding intermediate node in the second cache array; the second child node index is used to indicate the storage position of the state data information of the corresponding child node of the type of leaf node in the third cache array, or to indicate the storage position of the header information of the corresponding child node of the type of intermediate node in the first cache array. For each intermediate node, by using the parent node index in its header information and the first child node index of each child node, as well as the meta information and the second child node index in the child information of each child node, it is possible to easily find the information of the parent node and each child node connected to the intermediate node through each cache array, thereby accurately constructing the logical relationship between each node in the state tree.

[0058] As an example, see Figure 1 , showing a schematic diagram of a state tree cache data structure provided in an embodiment of the present application. Figure 1 There are three buffer arrays: HeaderBuffer, ChildBuffer and LeafBuffer.

[0059] HeaderBuffer is the first cache array, which is used to cache the header information CachedHeader of the intermediate node. Its buffer field is a long array. Each array element is fixed to 68B, corresponding to a CachedHeader structure instance, that is, each array element is used to cache the header information CachedHeader of an intermediate node. The bitmap field is a bitmap, which is used to efficiently mark whether each storage location (array element) in the buffer is free. For example, "0" corresponds to a free storage location, and "1" corresponds to an occupied storage location. The current field is used to indicate the location of the last allocated buffer element, which is convenient for quickly querying free storage locations.

[0060] ChildBuffer is the second cache array, which is used to cache the child information CachedChild of the intermediate node. Its buffer field is also a long array. Each array element is fixed to 45B and corresponds to a CachedChild structure instance. That is, each array element is used to cache the child information CachedHeader of a child node of an intermediate node. The descriptions of the bitmap field and the current field are the same as above.

[0061] LeafBuffer is the third cache array, which is used to cache the status data information CachedLeaf of leaf nodes. Its buffer field is also a long array, but each array element is of variable length and corresponds to a CachedLeaf structure instance, that is, each array element is used to cache the status data information CachedLeaf of a leaf node. The descriptions of the bitmap field and the current field are the same as above.

[0062] Since the GC overhead of long arrays without pointer types is negligible when the Golang program is running, we use Figure 1 The state tree cache data structure shown in the figure is used for caching, which can effectively reduce the GC overhead when the Golang program is running.

[0063] exist Figure 1 In the state tree cache data structure shown, the information of the intermediate node InternalNode is divided into two parts: header information CachedHeader and child information CachedChild, which are stored in the first cache array and the second cache array respectively, and the sizes of the storage locations of the first cache array are made the same, that is, the size of each array element of HeaderBuffer is 68B, and the sizes of the storage locations of the second cache array are made the same, that is, the size of each array element of ChildBuffer is 45B. The reason for this design is that the number of child nodes of the intermediate node of the state tree is uncertain. The number of child nodes of some intermediate nodes may reach 16, and the number of child nodes of some intermediate nodes may be only 1. If the information of the intermediate node is not divided, the size of each intermediate node is indefinite. In this way, in the scenario of frequent addition and deletion of node data, the problem of memory space fragmentation will occur, resulting in a decrease in cache space utilization.

[0064] As an example, assume that there are three intermediate nodes, namely intermediate node X, intermediate node Y and intermediate node Z. Intermediate node X has one child node, intermediate node Y has two child nodes, and intermediate node Z has three child nodes. If the information of the intermediate nodes is not segmented, the data sizes obtained after serialization of the three intermediate nodes are not equal, which can be recorded as Dx, Dy and Dz respectively. Then, when allocating memory, if the memory block is allocated according to the actual size of the data in an indefinite length allocation method, external fragmentation problems will occur; if the fixed length allocation method is used, the size of each memory block is equal and must be greater than or equal to the data size of the intermediate node with the maximum number of child nodes. Such a memory block will have internal fragmentation problems when loading the data of the intermediate node with less than the maximum number of child nodes. In short, allocating memory without segmenting the information of the intermediate nodes will inevitably lead to external or internal fragmentation problems, resulting in waste of content space and reduced cache space utilization.

[0065] If adopted Figure 1 The state tree cache data structure shown in the figure, after serialization, the three intermediate nodes obtain a total of three header information CachedHeader, denoted as Hx, Hy and Hz, and six child information CachedChild, namely one child information C_X1 of the intermediate node X, two child information C_Y1 and C_Y2 of the intermediate node Y, and three child information C_Z1, C_Z2 and C_Z3 of the intermediate node Z. Since the size of each header information CachedHeader and each child information CachedChild is fixed, 68B and 45B respectively, these header information CachedHeader and child information CachedChild can be managed by the buffer arrays of HeaderBuffer and ChildBuffer respectively, without internal or external fragmentation, and without the problem of wasting memory space, so the cache space utilization can be effectively improved.

[0066] It should be noted that Figure 1 The data structure shown is only an example and is not used to uniquely define the state tree cache data structure provided by the embodiment of the present application. It can be understood that the embodiment of the present application does not limit the array element size of the three cache arrays in the state tree cache data structure, that is, according to actual needs, the size of each array element of the first cache array may not be 68B, the size of each array element of the second cache array may not be 45B, and it is not mandatory to require that the sizes of the array elements of the first cache array are the same, and it is not mandatory to require that the sizes of the array elements of the second cache array are the same.

[0067] It should be understood that the execution subjects of the various method embodiments proposed in this application can be various types of electronic devices, such as mobile phones, tablet computers, wearable devices, learning machines, augmented reality (AR) / virtual reality (VR) devices, laptops, ultra-mobile personal computers (UMPC), netbooks, personal digital assistants (PDA), large-screen TVs, etc. The embodiments of this application do not impose any restrictions on the specific types of the electronic devices. For example, if applied to the scenario of blockchain state tree data caching, the electronic device can be any blockchain node.

[0068] See also Figure 2 , shows a state tree data caching method provided by an embodiment of the present application, including:

[0069] 201. Obtain state tree data to be cached, where the state tree data includes a number of intermediate nodes and a number of leaf nodes;

[0070] In use Figure 1 When the state tree cache data structure shown in the figure caches the state tree data, the state tree data to be cached is first obtained, and the state tree data includes a plurality of intermediate nodes and a plurality of leaf nodes. The embodiment of the present application does not impose any restrictions on the number of intermediate nodes, the number of leaf nodes, and the node connection relationship of the cached state tree data. In fact, as long as Figure 1 If the number of array elements of each cache array shown is large enough, the state tree data of a complex structure including a large number of intermediate nodes and a large number of leaf nodes can be cached.

[0071] 202. Cache the header information of each intermediate node into a first cache array;

[0072] After obtaining the state tree data to be cached, the header information of each intermediate node of the state tree data is cached to the first cache array, and each array element of the first cache array is used to cache the header information of an intermediate node. For example, assuming that the state tree data has intermediate node 1, intermediate node 2, and intermediate node 3, the header information H1 of intermediate node 1 is cached to array element 1 of the first cache array, the header information H2 of intermediate node 2 is cached to array element 2 of the first cache array, and the header information H3 of intermediate node 3 is cached to array element 3 of the first cache array, and so on. It should be noted that array element 1, array element 2, and array element 3 here only represent 3 different storage locations, and do not represent continuous storage locations.

[0073] The header information includes the parent node index of the corresponding intermediate node and the first child node index of each child node of the corresponding intermediate node, wherein the parent node index is used to indicate that the corresponding intermediate node is a root node, or is used to indicate the storage position of the header information of the parent node of the corresponding intermediate node in the first cache array; the first child node index is used to indicate the storage position of the child information of the corresponding child node of the corresponding intermediate node in the second cache array. For example, the header information H of a certain intermediate node may include a parent field and a Children field, wherein the parent node index is recorded in the parent field. If the parent node index is a special value (such as 0), it indicates that the intermediate node is a root node. If the parent node index is not a special value, such as idx_H1, it indicates that the header information of the parent node of the intermediate node is stored in the array element corresponding to idx_H1 in the first cache array, that is, the storage position of the header information of the parent node of the corresponding intermediate node in the first cache array can be determined according to the parent node index. The Childs field records the first child node index of all child nodes of the corresponding intermediate node. For example, if the Childs field in the header information of an intermediate node is "Childs: idx_C1, idx_C2", it means that the intermediate node has two child nodes, and the first child node index of child node 1 is idx_C1, which means that the child information of child node 1 is stored in the array element corresponding to idx_C1 in the second cache array, and the first child node index of child node 2 is idx_C2, which means that the child information of child node 2 is stored in the array element corresponding to idx_C2 in the second cache array. It can be seen that the storage position of the child information of the corresponding child node of the corresponding intermediate node in the second cache array can be determined according to the first child node index.

[0074] 203. Cache the child information of each child node of each intermediate node into the second cache array;

[0075] The child information of each child node of each intermediate node of the state tree data is stored in the second cache array, and each array element of the second cache array is used to cache the child information of a child node of an intermediate node. For example, assuming that the state tree has intermediate node 1 and intermediate node 2, wherein intermediate node 1 has child node 1 and child node 2, and intermediate node 2 has child node 3 and child node 4, then the child information C1 of child node 1 is cached to array element 1 of the second cache array, the child information C2 of child node 2 is cached to array element 2 of the second cache array, the child information C3 of child node 3 is cached to array element 3 of the second cache array, and the child information C4 of child node 4 is cached to array element 4 of the second cache array. Similarly, array element 1, array element 2, array element 3, and array element 4 here only represent 4 different storage locations, and do not represent continuous storage locations.

[0076] The child information includes the meta information of the corresponding child node and the second child node index, wherein the meta information mainly includes the hash value of the child node, the version number (block height) Version and the identifier Leaf of whether it is a leaf node, and the second child node index is used to indicate the storage position of the state data information of the corresponding child node of the leaf node type in the third cache array, or to indicate the storage position of the header information of the corresponding child node of the intermediate node type in the first cache array. For example, the child information C of a child node may include a Version field and a Child field, wherein the Version field records the version number information in the meta information, and the Child field records the second child node index of the child node. If the child node is a leaf node, the second child node index is used to indicate the storage position of the state data information of the child node in the third cache array, and if the child node is an intermediate node, the second child node index is used to indicate the storage position of the header information of the child node in the first cache array. Assuming that the child information C1 of the child node 1 of the intermediate node type includes "Version: 2023, Child: idx_H2", it means that the version number is 2023, and the header information of the child node 1 is stored in the array element corresponding to idx_H2 in the first cache array, that is, the storage position of the header information of the corresponding child node (intermediate node) in the first cache array can be determined according to the second child node index. Assuming that the child information C2 of the child node 2 of the leaf node type includes "Version: 2024, Child: idx_L1", it means that the version number is 2024, and the status data information of the child node 2 is stored in the array element corresponding to idx_L1 in the third cache array, that is, the storage position of the status data information of the corresponding child node (leaf node) in the third cache array can be determined according to the second child node index.

[0077] 204. Cache the status data information of each leaf node into a third cache array.

[0078] The status data information of each leaf node of the status tree data is cached to the third cache array, and each array element of the third cache array is used to cache the status data information of a leaf node. For example, assuming that the status tree has leaf node 1, leaf node 2, leaf node 3 and leaf node 4, the status data information L1 of leaf node 1 is cached to array element 1 of the third cache array, the status data information L2 of leaf node 2 is cached to array element 2 of the third cache array, the status data information L3 of leaf node 3 is cached to array element 3 of the third cache array, and the status data information L4 of leaf node 4 is cached to array element 4 of the third cache array. Similarly, array element 1, array element 2, array element 3 and array element 4 here only represent 4 different storage locations, and do not represent continuous storage locations.

[0079] As an example, Figure 3 It is a schematic diagram of an operation of caching state tree data using a state tree cache data structure provided by an embodiment of the present application. Figure 3 The upper left corner is a logical JMT state tree, which includes two intermediate nodes: Internal_1 and Internal_2, and three leaf nodes: Leaf_1, Leaf_2, and Leaf_3. When caching, these five nodes will be converted into two header information H1 and H2 (corresponding to the two intermediate nodes respectively), four child information C1, C2, C3, and C4 (corresponding to the four child nodes of the two intermediate nodes respectively), and three state data information L1, L2, and L3 (corresponding to the three leaf nodes respectively). Figure 3 As shown in the lower left corner, the two header information H1 and H2 are cached in different array elements of the first cache array HeaderBuffer, the four child information C1, C2, C3 and C4 are cached in different array elements of the second cache array ChildBuffer, and the three status data information L1, L2 and L3 are cached in different array elements of the third cache array LeafBuffer. Figure 3The right side reveals the logical relationship between the nodes of the status tree in the cache. For example, the parent node index of the header information H1 is 0, indicating that its corresponding intermediate node Internal_1 is the root node, and the Children field of the header information H1 is "Childs: idx_C1, idx_C2", which means that the intermediate node Internal_1 has 2 child nodes, and the child information of one child node is stored in the array element corresponding to idx_C1 in the second cache array ChildBuffer, that is, C1, and the child information of the other child node is stored in the array element corresponding to idx_C2 in the second cache array ChildBuffer, that is, C2. For another example, the parent node index of the header information H2 is idx_H1, which means that the header information of the parent node (i.e. Internal_1) of the corresponding intermediate node Internal_2 is stored in the array element corresponding to idx_H1 in the first cache array HeaderBuffer, that is, H1; the Children field of the header information H2 is "Childs: idx_C3, idx_C4", which means that the intermediate node Internal_2 has 2 child nodes, the child information of one child node is stored in the array element corresponding to idx_C3 in the second cache array ChildBuffer, that is, C3, and the child information of the other child node is stored in the array element corresponding to idx_C4 in the second cache array ChildBuffer, that is, C4. For another example, the child information C1 includes "Version: 2023, Child: idx_H2", which means that the version number is 2023, and the header information of the corresponding child node (i.e., the child node Internal_2 of Internal_1) is stored in the array element corresponding to idx_H2 in the first cache array HeaderBuffer, that is, H2. For another example, the child information C2 includes "Version: 2024, Child: idx_L1", which means that the version number is 2024, and the state data information of the corresponding child node (i.e., the child node Leaf_1 of Internal_1) is stored in the array element corresponding to idx_L1 in the third cache array LeafBuffer, that is, L1. By analogy, it can be seen that the logical relationship between the nodes of the state tree can be accurately constructed based on the state tree data in the cache, thereby correctly restoring the state tree. Figure 3 The JMT state tree on the upper left.

[0080] After the state tree data is cached based on the above data structure, operations such as reading, pruning, and updating may be performed on the cached state tree data, which are described in detail below.

[0081] In one implementation of the embodiment of the present application, the method further includes:

[0082] (1) when receiving a request to read status data information of a target leaf node among a plurality of leaf nodes, reading the header information of the root node from the first cache array as the current header information;

[0083] (2) Determine the current addressing position based on the addressing principle of the prefix tree and the addressing path of the target leaf node;

[0084] (3) Find the first child node index of the target child node corresponding to the current addressing position from the current header information;

[0085] (4) If the first child node index of the target child node is found, the child information of the target child node is read from the second cache array according to the first child node index of the target child node;

[0086] (5) If the target child node is an intermediate node, read the header information of the target child node from the first cache array according to the second child node index of the child information of the target child node, use the header information of the target child node to update the current header information, and return to execute the step of determining the current addressing position according to the addressing principle of the prefix tree and the addressing path of the target leaf node;

[0087] (6) If the target child node is a leaf node, the state data information of the target child node is read from the third cache array according to the second child node index of the child information of the target child node as the state data information of the target leaf node.

[0088] First, the basic operation process of reading the state tree data from the cache is introduced. Assuming that the state data information of a target leaf node in the state tree data needs to be read, the header information of the root node is first read from the first cache array HeaderBuffer as the current header information currentHeader. Then, according to the addressing principle of the prefix tree and the addressing path of the target leaf node, the current addressing position is determined. The JMT state tree belongs to the prefix tree. Since the addressing path of the target leaf node is known, the next addressing position to be addressed can be inferred from the addressing path according to the addressing principle of the prefix tree, that is, the current addressing position. Next, the first child node index of the target child node corresponding to the current addressing position is searched from the current header information currentHeader. The current header information currentHeader includes the first child node indexes of all child nodes of the root node. Here, the target child node corresponding to the current addressing position needs to be searched. For example, assuming that the current addressing position is "b (hexadecimal)", then the 12th element of the Children array is searched from the current header information currentHeader (hexadecimal b represents 11, and the 12th element is found from position 0 of the array). If the value in the 12th element of the Children array is not the special value 0, it means that the first child node index of the target child node is successfully found. According to the first child node index of the target child node, the child information of the target child node can be read from the second buffer array ChildBuffer. After that, the type of the next child node, that is, the target child node, is determined. Since the structure of the child information contains the Leaf field to indicate whether it is a leaf node, the type of the target child node can be determined according to the Leaf field contained in the child information of the target child node. After determining the type of the target child node, if the target child node is an intermediate node, the header information of the target child node is read from the first cache array HeaderBuffer according to the second child node index of the child information of the target child node, and the current header information is updated using the header information of the target child node, that is, the header information of the target child node is used as the new currentHeader, and then the step of determining the current addressing position according to the addressing principle of the prefix tree and the addressing path of the target leaf node is returned to execute. Here, the next addressing position will be determined according to the addressing path, and then the same process is repeated, that is, starting from the root node, the correct child node is continuously searched according to the addressing path until the final target leaf node is found. If the target child node is a leaf node, it means that the target child node is the target leaf node that needs to read data. At this time, according to the second child node index of the child information of the target child node, the state data information of the target child node is read from the third cache array LeafBuffer as the state data information of the target leaf node. At this point, the whole process of reading the state data information of the target leaf node from the cache is completed.It can be seen that the above addressing method is consistent with the method of traversing in the state tree, and the addressing complexity is greatly reduced. Assuming that the average depth of the state tree is H, the time complexity of reading data once is only O(H).

[0089] In an implementation of the embodiment of the present application, after searching the current header information for the first child node index of the target child node corresponding to the current addressing position, the method further includes:

[0090] If the first child node index of the target child node cannot be found, the state data information of the target leaf node is read from the disk according to the addressing path of the target leaf node.

[0091] In the above basic operation process of reading the status tree data, if the first child node index of the target child node corresponding to the current addressing position cannot be found in the current header information currentHeader, for example, the value read from the 12th element of the Children array of the current header information currentHeader is the special value 0, it means that the information of the target child node is not in the cache, and then the status data information of the target leaf node to be read is not in the cache. In this case, you can exit the process of reading the status tree data from the cache, and directly read the status data information of the target leaf node through the disk. The specific principles and operating methods of searching the JMT status tree data from the disk according to the known addressing path can refer to the prior art and will not be repeated here.

[0092] As an example, Figure 4 This is a schematic diagram of an operation flow for reading node data cached in a state tree cache data structure provided by an embodiment of the present application. Figure 4 In the process, first, the header information of the root node is read from the first cache array HeaderBuffer as currentHeader according to the target key of the root node; then, it is determined whether the information of the next child node in the addressing path is in the cache. If not, the data is read from the disk and returned. If it is in the cache, the child information of the next child node is read from the second cache array ChildBuffer, and it is determined whether the next child node is a leaf node according to the child information; if the next child node is not a leaf node, the header information of the next child node is searched from the first cache array HeaderBuffer, and the currentHeader is updated with the found header information, and the next loop process is entered; if the next child node is a leaf node, it means that the next child node is the target node to be read. At this time, the status data information of the next child node is read from the third cache array LeafBuffer and returned, thus ending the data reading process.

[0093] For ease of understanding, Figure 3 The example shown illustrates the data reading process. Assuming that you want to query the status value of the account with address "d2a8" at block height 2024 in the blockchain, the main operation process includes:

[0094] (1) Read the header information H1 of the root node from the idx_H1 position of the first buffer array HeaderBuffer as currentHeader;

[0095] (2) The current addressing position is "d", read the 14th position of the Children array of currentHeader, read idx_C1, and then read the child information C1 at the idx_C1 position from the second buffer array ChildBuffer;

[0096] (3) According to the child information C1, it can be known that the next node is an intermediate node, and its header information is cached at the idx_H2 position of the first cache array HeaderBuffer. Therefore, the header information H2 at the idx_H2 position is read from the first cache array HeaderBuffer as the new currentHeader;

[0097] (4) The next addressing position is "2", so the third position of the Children array of currentHeader is read, idx_C3 is read, and then the child information C3 at the idx_C3 position is read from the second buffer array ChildBuffer;

[0098] (5) According to the child information C3, it can be known that the next node is a leaf node, and its state data information is cached at the idx_L2 position of the third cache array LeafBuffer. Therefore, the state data information L2 at the idx_L2 position is read from the third cache array LeafBuffer. Figure 3 From the data structure of the JMT status tree, we can see that L2 corresponds to the leaf node Leaf_2, and L2 contains the status value information of the target account "d2a8", thus completing the data reading process.

[0099] The above is the operation flow of reading the cached state tree data. Next, the operation flow of pruning the cached state tree data is described.

[0100] In one implementation of the embodiment of the present application, the method further includes:

[0101] (1) When receiving a pruning request for state tree data, determining the node to be deleted;

[0102] (2) If the node to be deleted is an intermediate node, the storage location of the header information of the node to be deleted in the first cache array is marked as free; the first child node indexes of all child nodes of the node to be deleted are searched from the header information of the node to be deleted, and the storage locations corresponding to the first child node indexes of all child nodes in the second cache array are marked as free; the header information of the parent node of the node to be deleted is searched from the first cache array, the first child node index of the node to be deleted is searched from the header information of the parent node of the node to be deleted, the storage location corresponding to the first child node index of the node to be deleted in the second cache array is marked as free, and the first child node index of the node to be deleted in the header information of the parent node of the node to be deleted is set to a specified value;

[0103] (3) If the node to be deleted is a leaf node, the storage location of the status data information of the node to be deleted in the third cache array is marked as free; the header information of the parent node of the node to be deleted is searched from the first cache array, the first child node index of the node to be deleted is searched from the header information of the parent node of the node to be deleted, the storage location corresponding to the first child node index of the node to be deleted in the second cache array is marked as free, and the first child node index of the node to be deleted in the header information of the parent node of the node to be deleted is set to a specified value.

[0104] In order to optimize the performance of reading the state tree, some blockchain systems have designed a pruning mechanism to prune the historical branches of the state tree that exceed a certain block range. In particular, when the block range is 1, only the latest state data will be retained in the state tree, thereby reducing the read and write pressure of the disk. Based on the pruning mechanism, after each block executes the update of the ledger, a write set WriteSet and a pruning set PruneSet will be obtained. According to the pruning set PruneSet, the nodes to be deleted in the cached state tree data can be determined. The number of nodes to be deleted can be 1 or more. When deleting the nodes to be deleted in the cached state tree data, delete them from the bottom up according to the depth of the nodes in the state tree. The specific operation process of deletion is related to the type of nodes to be deleted.

[0105] Specifically, if the node to be deleted is an intermediate node, the storage location of the header information of the node to be deleted in the first cache array HeaderBuffer is first marked as free. Here, the bit corresponding to the header information of the node to be deleted in the bitmap field of HeaderBuffer can be found, and the bit is changed from "1" to "0", indicating that the corresponding storage location is free. This operation can be called adding the subscript of the node to be deleted to the free list. It should be noted that after adding the subscript of the node to be deleted to the free list, there is no need to clear the header information of the node to be deleted cached in HeaderBuffer. You only need to mark the corresponding storage location as free. Later, when the header information of other intermediate nodes needs to be cached, you can write to the storage location to overwrite the original header information of the node to be deleted. By doing this, the GC overhead of the Golang program can be effectively reduced when it is running. Then, the first child node index of all child nodes of the node to be deleted is searched from the header information of the node to be deleted, and the storage locations corresponding to the first child node index of all child nodes in the second cache array ChildBuffer are all marked as free. Similarly, the bits corresponding to the first child node index of all child nodes in the bitmap field of ChildBuffer can be found here, and these bits are changed from "1" to "0", indicating that the corresponding storage locations are free. This operation can be called adding the subscripts of all child nodes of the node to be deleted to the free list. Similarly, when adding to the free list, it is not necessary to clear the child information of the corresponding position cached in ChildBuffer. Next, the header information of the parent node of the node to be deleted is searched from the first cache array HeaderBuffer, and the first child node index of the node to be deleted is searched from the Childs array of the header information, and the storage location corresponding to the first child node index of the node to be deleted in the second cache array ChildBuffer is marked as free, and the first child node index of the node to be deleted in the header information is set to a specified value, such as 0, indicating that the child node at the corresponding position (i.e., the node to be deleted) has been deleted in the cache.

[0106] If the node to be deleted is a leaf node, the storage location of the status data information of the node to be deleted in the third cache array LeafBuffer is marked as free. Here, the bit corresponding to the status data information of the node to be deleted in the bitmap field of LeafBuffer can be found, and the bit is changed from "1" to "0", indicating that the corresponding storage location is free. This operation can also be called adding the subscript of the node to be deleted to the free list.

[0107] Similarly, when adding to the free list, there is no need to clear the status data information of the corresponding position cached in LeafBuffer. Then, similar to the pruning process of the intermediate node, the header information of the parent node of the node to be deleted is searched from the first cache array HeaderBuffer, the first child node index of the node to be deleted is searched from the header information, the storage position corresponding to the first child node index of the node to be deleted in the second cache array ChildBuffer is marked as free, and the first child node index of the node to be deleted in the header information is set to a specified value, such as 0.

[0108] As an example, Figure 5 It is a schematic diagram of an operation of pruning state tree data cached in a state tree cache data structure provided by an embodiment of the present application. Figure 5 Shown is for Figure 3 The example of pruning the state tree data shown in the figure requires deleting a total of three nodes, Internal_2, Leaf_2, and Leaf_3. The main operation process includes:

[0109] (1) Delete the leaf node Leaf_2, add the subscript idx_L2 of Leaf_2 to the free list in the third cache array LeafBuffer; then, find the header information H2 of the parent node of Leaf_2 in the first cache array HeaderBuffer, find the element idx_C3 at the third position of the Childs array of H2 (the last bit of the addressing path of Leaf_2 is "2", corresponding to the third position of the array), add idx_C3 to the free list in the second cache array ChildBuffer, and finally set the element at the third position of the Childs array of H2 to 0, indicating that the child node Leaf_2 at the corresponding position has been deleted in the cache;

[0110] (2) Delete the leaf node Leaf_3. This process is similar to deleting the leaf node Leaf_2 and will not be repeated here.

[0111] (3) Delete the intermediate node Internal_2, add the index idx_H2 of Internal_2 to the free list in the first cache array HeaderBuffer; then, add the indexes of all child nodes contained in the Childs array of the header information H2 of Internal_2 to the free list in the second cache array ChildBuffer; then, find the header information H1 of the parent node of Internal_2 in the first cache array HeaderBuffer, find the element idx_C1 at the 14th position of the Childs array of H1 (the last bit of the addressing path of Internal_2 is "d", corresponding to the 14th position of the array), add idx_C1 to the free list in the second cache array ChildBuffer, and finally set the element at the 14th position of the Childs array of H1 to 0, indicating that the child node Internal_2 at the corresponding position has been deleted in the cache.

[0112] After the above pruning operation, Figure 5 It can be seen that the state tree data in the cache has not changed. This is because the relevant information of Internal_2, Leaf_2 and Leaf_3 has not been cleared, but the corresponding storage locations in the cache array are marked as free. However, the logical relationship between the nodes of the state tree in the cache has changed, such as Figure 5 The bold boxed areas indicate the invalid data after Internal_2, Leaf_2, and Leaf_3 are deleted.

[0113] Finally, the operation flow of updating the cached state tree data is described.

[0114] In one implementation of the embodiment of the present application, the method further includes:

[0115] (1) When receiving an update request for status tree data, determining the node to be updated;

[0116] (2) searching the status tree data for a status tree node that has the same addressing path as the node to be updated;

[0117] (3) If the status tree node is found and the node to be updated is an intermediate node, then according to the header information of the node to be updated, the header information of the status tree node cached in the first cache array is updated; according to the child information of all child nodes of the node to be updated, each child information to be updated and each child information to be added is determined; according to each child information to be updated, the child information cached in the second cache array is updated, and each child information to be added is written into an idle storage location of the second cache array; if the node to be updated is not a root node, the header information of the parent node of the node to be updated is searched from the first cache array, and according to the header information of the parent node of the node to be updated, the child information corresponding to the node to be updated is searched and updated from the second cache array;

[0118] (4) If the status tree node is found and the node to be updated is a leaf node, the status data information of the status tree node cached in the third cache array is updated according to the status data information of the node to be updated; the header information of the parent node of the node to be updated is searched from the first cache array, and the child information corresponding to the node to be updated is searched and updated from the second cache array according to the header information of the parent node of the node to be updated.

[0119] After each block completes the ledger update, it will get a write set WriteSet and a prune set PruneSet. According to the write set WriteSet, the nodes to be updated in the cached state tree data can be determined. The number of nodes to be updated can be 1 or more. When updating the cached state tree data, it is updated from top to bottom according to the depth of the node to be updated in the state tree. Based on the design principle of the state tree, if a node is in the update set, its parent node must also be in the update set. Similar to the process of node deletion, the specific operation process of node update is also related to the type of node to be updated.

[0120] The addressing path of the node to be updated is known. First, the status tree node with the same addressing path as the node to be updated is searched from the cached status tree data. If the status tree node can be found, it indicates a node update operation. If the status tree node cannot be found, it indicates a node addition operation.

[0121] Specifically, for the case where the state tree node with the same addressing path can be found, if the node to be updated is an intermediate node, the header information of the state tree node cached in the first cache array HeaderBuffer is updated according to the header information of the node to be updated; then, the child information to be updated and the child information to be added are determined according to the child information of all child nodes of the node to be updated; according to the child information to be updated, the child information of the corresponding storage position cached in the second cache array ChildBuffer is updated, and multiple free storage positions of the second cache array ChildBuffer are applied according to the number of child information to be added, and each child information to be added is written into a free storage position of the second cache array. On the basis of updating the node to be updated, its parent node also needs to be updated. At this time, it is first determined whether the node to be updated is a root node. If it is a root node, there is no update of the parent node. If it is not a root node, the header information of the parent node of the node to be updated is searched from the first cache array HeaderBuffer, and the child information corresponding to the node to be updated is searched and updated from the second cache array ChildBuffer according to the header information of the parent node of the node to be updated. The updated child information includes a hash value, a version number Version and a Child field. If the node to be updated is a leaf node, the status data information of the status tree node cached in the third cache array LeafBuffer is updated according to the status data information of the node to be updated; the subsequent processing process is similar to that of the intermediate node, and the header information of the parent node of the node to be updated is searched from the first cache array HeaderBuffer, and according to the header information of the parent node of the node to be updated, the child information corresponding to the node to be updated is searched and updated from the second cache array ChildBuffer, and the updated child information includes a hash value, a version number Version and a Child field, etc.

[0122] In an implementation of the embodiment of the present application, after searching the status tree data for a status tree node having the same addressing path as the node to be updated, the method further includes:

[0123] (1) If the state tree node cannot be found and the node to be updated is an intermediate node, the header information of the node to be updated is written into a free storage location of the first cache array; based on the child information of all child nodes of the node to be updated, each child information to be updated and each child information to be added is determined; based on each child information to be updated, the child information cached in the second cache array is updated, and each child information to be added is written into a free storage location of the second cache array; if the node to be updated is not a root node, the header information of the parent node of the node to be updated is searched from the first cache array, and based on the header information of the parent node of the node to be updated, the child information corresponding to the node to be updated is searched and updated from the second cache array;

[0124] (2) If the status tree node cannot be found and the node to be updated is a leaf node, the status data information of the node to be updated is written into an idle storage location of the third cache array; the header information of the parent node of the node to be updated is searched from the first cache array, and based on the header information of the parent node of the node to be updated, the child information corresponding to the node to be updated is searched and updated from the second cache array.

[0125] In the case where the state tree node with the same addressing path cannot be found, it indicates that the node to be updated is a newly added node. Specifically, if the node to be updated is an intermediate node, apply for an idle storage position in the first cache array HeaderBuffer, and write the header information of the node to be updated into the idle storage position; then, determine each child information to be updated and each child information to be added based on the child information of all child nodes of the node to be updated; update the child information of the corresponding storage position cached in the second cache array ChildBuffer based on each child information to be updated, and apply for multiple idle storage positions of the second cache array ChildBuffer according to the number of child information to be added, and write each child information to be added into an idle storage position of the second cache array. On the basis of updating the node to be updated, its parent node also needs to be updated. At this time, it is first determined whether the node to be updated is a root node. If it is a root node, there is no update of the parent node. If it is not a root node, the header information of the parent node of the node to be updated is searched from the first cache array HeaderBuffer, and according to the header information of the parent node of the node to be updated, the child information corresponding to the node to be updated is searched and updated from the second cache array ChildBuffer. The updated child information includes a hash value, a version number Version, and a Child field. If the node to be updated is a leaf node, an idle storage location is applied in the third cache array LeafBuffer, and the status data information of the node to be updated is written into the idle storage location; the subsequent processing process is similar to that of the intermediate node, the header information of the parent node of the node to be updated is searched from the first cache array HeaderBuffer, and according to the header information of the parent node of the node to be updated, the child information corresponding to the node to be updated is searched and updated from the second cache array ChildBuffer. The updated child information includes a hash value, a version number Version, and a Child field.

[0126] As an example, Figure 6 It is a schematic diagram of an operation of updating the state tree data cached in the state tree cache data structure provided by an embodiment of the present application. Figure 6 Shown is for Figure 3The example of updating the state tree data shown in the figure requires adding a leaf node Leaf_4, which will cause the corresponding parent nodes Internal_1 and Internal_2 to be updated. The main operation process includes:

[0127] (1) Update the intermediate node Internal_1. Internal_1 is the root node. Since the corresponding header information H1 can be found in the first cache array HeaderBuffer, it is a node update. At this time, the child information corresponding to all updated child nodes of H1 in the second cache array ChildBuffer is updated, which is C1 in this example. Since Internal_1 is a root node and has no parent node, its parent node does not need to be updated.

[0128] (2) Update the intermediate node Internal_2. Since the corresponding header information H2 can be found in the first cache array HeaderBuffer, it is a node update. At this time, all the newly added child nodes of H2 are added to the second cache array ChildBuffer, which is C5 in this example. Then, the header information H1 of the parent node of H2 is found. According to H1, the corresponding child information of Internal_2 can be found and updated in the second cache array ChildBuffer, which is C1.

[0129] (3) Update the leaf node Leaf_4. Since the status data information corresponding to Leaf_4 cannot be found in the third cache array LeafBuffer, it is a new node. At this time, apply for an idle position idx_L4 in the third cache array LeafBuffer to store the status data information L4 of Leaf_4; then, find the header information H2 of the parent node of Leaf_4, and further find the child information corresponding to Leaf_4 through H2, that is, C5, and update the information of C5.

[0130] After the above update operation, through Figure 6 It can be seen that the state tree data in the cache has added child information C5 and state data information L4, and the header information H1, header information H2 and child information C1 related to the update operation have also been updated. Figure 6 The bold box in the figure indicates the location where the data changes.

[0131] The above is a specific description of the operations of reading, pruning and updating the cached state tree data.

[0132] The embodiment of the present application proposes a new state tree cache data structure, which mainly includes three cache arrays, wherein the first cache array is used to cache the header information of the intermediate node, the second cache array is used to cache the child information of the intermediate node, and the third cache array is used to cache the state data information of the leaf node. After the state tree data to be cached is obtained, the header information of each intermediate node of the state tree data will be cached to the first cache array, the child information of each child node of each intermediate node of the state tree data will be cached to the second cache array, and the state data information of each leaf node of the state tree data will be cached to the third cache array. For each intermediate node, using the parent node index in its header information and the first child node index of each child node, and using the meta information and the second child node index in the child information of each child node, it is convenient to find the information of the parent node and each child node connected to the intermediate node through each cache array, so as to accurately construct the logical relationship between each node of the state tree. For a general cache based on hash bucketing and a circular array, assuming that the number of nodes in the state tree is N and the number of buckets is M, the time complexity of the number of addressing times required to read the data is O(N / M). If the state tree cache data structure provided by the embodiment of the present application is used, its addressing method is consistent with the method of traversing in the state tree, the addressing complexity is greatly reduced, and the time complexity of the number of addressing times required to read data is O(logN), which shows that the number of addressing times for reading state tree data can be significantly reduced compared with general cache. In addition, the state tree cache data structure uses a long array as a cache, which can effectively reduce the GC overhead when the Golang program is running.

[0133] In summary, the embodiment of the present application proposes a dedicated cache component for the blockchain state tree, which adopts a new state tree cache data structure and has the advantages of high addressing efficiency, high cache space utilization and low GC overhead.

[0134] It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.

[0135] A state tree data caching method is mainly described above, and a state tree data caching device will be described below.

[0136] See also Figure 7 , shows a state tree data cache device provided by an embodiment of the present application, including:

[0137] The state tree acquisition module 701 is used to acquire the state tree data to be cached, the state tree data includes a number of intermediate nodes and a number of leaf nodes;

[0138] A first cache module 702, configured to cache header information of each intermediate node into a first cache array, wherein the header information includes a parent node index corresponding to the intermediate node and a first child node index of each child node corresponding to the intermediate node;

[0139] The second cache module 703 is used to cache the child information of each child node of each intermediate node into the second cache array, where the child information includes the meta information of the corresponding child node and the second child node index;

[0140] The third cache module 704 is used to cache the state data information of each leaf node into a third cache array;

[0141] Among them, the parent node index is used to indicate that the corresponding intermediate node is the root node, or to indicate the storage position of the header information of the parent node of the corresponding intermediate node in the first cache array; the first child node index is used to indicate the storage position of the child information of the corresponding child node of the corresponding intermediate node in the second cache array; the second child node index is used to indicate the storage position of the status data information of the corresponding child node of the leaf node type in the third cache array, or to indicate the storage position of the header information of the corresponding child node of the intermediate node type in the first cache array.

[0142] In one implementation of the embodiment of the present application, the state tree data cache device further includes:

[0143] A header information reading module, configured to read the header information of the root node from the first cache array as the current header information when receiving a request to read the status data information of a target leaf node among the plurality of leaf nodes;

[0144] An addressing position determination module, used to determine the current addressing position according to the addressing principle of the prefix tree and the addressing path of the target leaf node;

[0145] A node index search module, used to search the first child node index of the target child node corresponding to the current addressing position from the current header information;

[0146] A child information reading module, configured to read the child information of the target child node from the second cache array according to the first child node index of the target child node if the first child node index of the target child node is found;

[0147] A header information update module, for, if the target child node is an intermediate node, reading the header information of the target child node from the first cache array according to the second child node index of the child information of the target child node, using the header information of the target child node to update the current header information, and returning to execute the step of determining the current addressing position according to the addressing principle of the prefix tree and the addressing path of the target leaf node;

[0148] The node data reading module is used to read the state data information of the target child node from the third cache array as the state data information of the target leaf node according to the second child node index of the child information of the target child node if the target child node is a leaf node.

[0149] In one implementation of the embodiment of the present application, the state tree data cache device further includes:

[0150] The second node data reading module is used to read the status data information of the target leaf node from the disk according to the addressing path of the target leaf node if the first child node index of the target child node cannot be found.

[0151] In one implementation of the embodiment of the present application, the state tree data cache device further includes:

[0152] A pruning determination module, configured to determine a node to be deleted when a pruning request for state tree data is received;

[0153] A first pruning operation module is used for marking the storage location of the header information of the node to be deleted in the first cache array as free if the node to be deleted is an intermediate node; searching the first child node indexes of all child nodes of the node to be deleted from the header information of the node to be deleted, and marking the storage locations corresponding to the first child node indexes of all child nodes in the second cache array as free; searching the header information of the parent node of the node to be deleted from the first cache array, searching the first child node index of the node to be deleted from the header information of the parent node of the node to be deleted, marking the storage location corresponding to the first child node index of the node to be deleted in the second cache array as free, and setting the first child node index of the node to be deleted in the header information of the parent node of the node to be deleted to a specified value;

[0154] The second pruning operation module is used to mark the storage location of the status data information of the node to be deleted in the third cache array as free if the node to be deleted is a leaf node; search the header information of the parent node of the node to be deleted from the first cache array, search the first child node index of the node to be deleted from the header information of the parent node of the node to be deleted, mark the storage location corresponding to the first child node index of the node to be deleted in the second cache array as free, and set the first child node index of the node to be deleted in the header information of the parent node of the node to be deleted to a specified value.

[0155] In one implementation of the embodiment of the present application, the state tree data cache device further includes:

[0156] An update determination module, configured to determine a node to be updated when receiving an update request for the state tree data;

[0157] A node search module, used to search the state tree node with the same addressing path as the node to be updated from the state tree data;

[0158] The first update operation module is used to update the header information of the state tree node cached in the first cache array according to the header information of the node to be updated if the state tree node is found and the node to be updated is an intermediate node; determine each child information to be updated and each child information to be added according to the child information of all child nodes of the node to be updated; update the child information cached in the second cache array according to each child information to be updated, and write each child information to be added into an idle storage location of the second cache array respectively; if the node to be updated is not a root node, search the header information of the parent node of the node to be updated from the first cache array, and search and update the child information corresponding to the node to be updated from the second cache array according to the header information of the parent node of the node to be updated;

[0159] The second update operation module is used to update the status data information of the status tree node cached in the third cache array according to the status data information of the node to be updated if the status tree node is found and the node to be updated is a leaf node; search for the header information of the parent node of the node to be updated from the first cache array, and search and update the child information corresponding to the node to be updated from the second cache array according to the header information of the parent node of the node to be updated.

[0160] In one implementation of the embodiment of the present application, the state tree data cache device further includes:

[0161] The third update operation module is used for, if the state tree node cannot be found and the node to be updated is an intermediate node, writing the header information of the node to be updated into a free storage position of the first cache array; determining each child information to be updated and each child information to be added according to the child information of all child nodes of the node to be updated; updating the child information cached in the second cache array according to each child information to be updated, and writing each child information to be added into a free storage position of the second cache array respectively; if the node to be updated is not a root node, searching the header information of the parent node of the node to be updated from the first cache array, and searching and updating the child information corresponding to the node to be updated from the second cache array according to the header information of the parent node of the node to be updated;

[0162] The fourth update operation module is used to write the status data information of the node to be updated into an idle storage location of the third cache array if the status tree node cannot be found and the node to be updated is a leaf node; search for the header information of the parent node of the node to be updated from the first cache array, and search and update the child information corresponding to the node to be updated from the second cache array based on the header information of the parent node of the node to be updated.

[0163] In one implementation of the embodiment of the present application, the sizes of the storage locations of the first cache array are the same, and the sizes of the storage locations of the second cache array are the same.

[0164] An embodiment of the present application further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the state tree data caching method described in any of the above embodiments is implemented.

[0165] An embodiment of the present application further provides a computer program product. When the computer program product is executed on an electronic device, the electronic device executes the state tree data caching method described in any of the above embodiments.

[0166] Figure 8 is a schematic diagram of an electronic device provided by an embodiment of the present application. Figure 8 As shown, the electronic device 8 of this embodiment includes: a processor 80, a memory 81, and a computer program 82 stored in the memory 81 and executable on the processor 80. When the processor 80 executes the computer program 82, the steps in the above-mentioned embodiments of the state tree data caching method are implemented, for example Figure 2 Alternatively, when the processor 80 executes the computer program 82, the functions of each module / unit in the above-mentioned device embodiments are realized, for example, Figure 7 Functions of modules 701 to 704 of the illustrated apparatus.

[0167] The computer program 82 may be divided into one or more modules / units, which are stored in the memory 81 and executed by the processor 80 to complete the present application. The one or more modules / units may be a series of computer program instruction segments capable of completing specific functions, which are used to describe the execution process of the computer program 82 in the electronic device 8.

[0168] The processor 80 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.

[0169] The memory 81 may be an internal storage unit of the electronic device 8, such as a hard disk or memory of the electronic device 8. The memory 81 may also be an external storage device of the electronic device 8, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the electronic device 8. Further, the memory 81 may also include both an internal storage unit and an external storage device of the electronic device 8. The memory 81 is used to store the computer program and other programs and data required by the electronic device. The memory 81 may also be used to temporarily store data that has been output or is to be output.

[0170] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.

[0171] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0172] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0173] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0174] In the embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the system embodiments described above are only schematic. For example, the division of the modules or units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.

[0175] The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the embodiments of the present application.

[0176] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0177] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electric carrier signal, telecommunication signal and software distribution medium. It should be noted that the content contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.

[0178] The embodiments described above are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, a person skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.

Claims

1. A state tree data caching method, characterized in that: include: Obtaining state tree data to be cached, wherein the state tree data includes a plurality of intermediate nodes and a plurality of leaf nodes; Cache the header information of each intermediate node into a first cache array, wherein the header information includes a parent node index corresponding to the intermediate node and a first child node index of each child node corresponding to the intermediate node; Cache the child information of each child node of each intermediate node into a second cache array, wherein the child information includes the meta information of the corresponding child node and the second child node index; Cache the status data information of each leaf node into a third cache array; Among them, the parent node index is used to indicate that the corresponding intermediate node is a root node, or is used to indicate the storage position of the header information of the parent node of the corresponding intermediate node in the first cache array; the first child node index is used to indicate the storage position of the child information of the corresponding child node of the corresponding intermediate node in the second cache array; the second child node index is used to indicate the storage position of the status data information of the corresponding child node of the leaf node type in the third cache array, or is used to indicate the storage position of the header information of the corresponding child node of the intermediate node type in the first cache array.

2. The method according to claim 1, characterized in that Also includes: When receiving a request to read the status data information of a target leaf node among the plurality of leaf nodes, reading the header information of the root node from the first cache array as the current header information; Determine the current addressing position according to the addressing principle of the prefix tree and the addressing path of the target leaf node; From the current header information, searching for the first child node index of the target child node corresponding to the current addressing position; If the first child node index of the target child node is found, then according to the first child node index of the target child node, the child information of the target child node is read from the second cache array; If the target child node is an intermediate node, then according to the second child node index of the child information of the target child node, read the header information of the target child node from the first cache array, use the header information of the target child node to update the current header information, and return to execute the step of determining the current addressing position according to the addressing principle of the prefix tree and the addressing path of the target leaf node; If the target child node is a leaf node, the state data information of the target child node is read from the third cache array according to the second child node index of the child information of the target child node as the state data information of the target leaf node.

3. The method according to claim 2, characterized in that After searching the current header information for the first subnode index of the target subnode corresponding to the current addressing position, the method further includes: If the first child node index of the target child node cannot be found, the status data information of the target leaf node is read from the disk according to the addressing path of the target leaf node.

4. The method according to claim 1, characterized in that Also includes: When receiving a pruning request for the status tree data, determining a node to be deleted; If the node to be deleted is an intermediate node, marking the storage location of the first cache array that caches the header information of the node to be deleted as free; Searching for the first child node indexes of all child nodes of the node to be deleted from the header information of the node to be deleted, and marking all storage locations corresponding to the first child node indexes of all child nodes in the second cache array as free; searching for the header information of the parent node of the node to be deleted from the first cache array, searching for the first child node index of the node to be deleted from the header information of the parent node of the node to be deleted, marking the storage location corresponding to the first child node index of the node to be deleted in the second cache array as free, and setting the first child node index of the node to be deleted in the header information of the parent node of the node to be deleted to a specified value; If the node to be deleted is a leaf node, the storage location of the third cache array for caching the status data information of the node to be deleted is marked as free; Search the header information of the parent node of the node to be deleted from the first cache array, search the first child node index of the node to be deleted from the header information of the parent node of the node to be deleted, mark the storage location corresponding to the first child node index of the node to be deleted in the second cache array as free, and set the first child node index of the node to be deleted in the header information of the parent node of the node to be deleted to a specified value.

5. The method according to claim 1, characterized in that Also includes: When receiving an update request for the status tree data, determining a node to be updated; Searching the state tree data for a state tree node having the same addressing path as the node to be updated; If the status tree node is found and the node to be updated is an intermediate node, the header information of the status tree node cached in the first cache array is updated according to the header information of the node to be updated; each child information to be updated and each child information to be added are determined according to the child information of all child nodes of the node to be updated; the child information cached in the second cache array is updated according to each child information to be updated, and each child information to be added is written into an idle storage location of the second cache array respectively; if the node to be updated is not a root node, the header information of the parent node of the node to be updated is searched from the first cache array, and the child information corresponding to the node to be updated is searched and updated from the second cache array according to the header information of the parent node of the node to be updated; If the status tree node is found and the node to be updated is a leaf node, the status data information of the status tree node cached in the third cache array is updated according to the status data information of the node to be updated; the header information of the parent node of the node to be updated is searched from the first cache array, and the child information corresponding to the node to be updated is searched and updated from the second cache array according to the header information of the parent node of the node to be updated.

6. The method according to claim 5, characterized in that After searching the status tree data for a status tree node having the same addressing path as the node to be updated, the method further includes: If the state tree node cannot be found and the node to be updated is an intermediate node, the header information of the node to be updated is written into a free storage location of the first cache array; according to the child information of all child nodes of the node to be updated, each child information to be updated and each child information to be added is determined; according to each child information to be updated, the child information cached in the second cache array is updated, and each child information to be added is written into a free storage location of the second cache array respectively; if the node to be updated is not a root node, the header information of the parent node of the node to be updated is searched from the first cache array, and according to the header information of the parent node of the node to be updated, the child information corresponding to the node to be updated is searched and updated from the second cache array; If the status tree node cannot be found and the node to be updated is a leaf node, the status data information of the node to be updated is written into a free storage location of the third cache array; the header information of the parent node of the node to be updated is searched from the first cache array, and based on the header information of the parent node of the node to be updated, the child information corresponding to the node to be updated is searched and updated from the second cache array.

7. The method according to any one of claims 1 to 6, characterized in that: The storage locations of the first cache array have the same size, and the storage locations of the second cache array have the same size.

8. A state tree data cache device, characterized in that: include: A state tree acquisition module, used to acquire state tree data to be cached, wherein the state tree data includes a plurality of intermediate nodes and a plurality of leaf nodes; A first cache module, used for caching the header information of each intermediate node into a first cache array, wherein the header information includes a parent node index corresponding to the intermediate node and a first child node index of each child node corresponding to the intermediate node; A second cache module, used for caching the child information of each child node of each intermediate node into a second cache array, wherein the child information includes the meta information of the corresponding child node and the second child node index; A third cache module, used for caching the state data information of each leaf node into a third cache array; Among them, the parent node index is used to indicate that the corresponding intermediate node is a root node, or is used to indicate the storage position of the header information of the parent node of the corresponding intermediate node in the first cache array; the first child node index is used to indicate the storage position of the child information of the corresponding child node of the corresponding intermediate node in the second cache array; the second child node index is used to indicate the storage position of the status data information of the corresponding child node of the leaf node type in the third cache array, or is used to indicate the storage position of the header information of the corresponding child node of the intermediate node type in the first cache array.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the state tree data caching method according to any one of claims 1 to 7 is implemented.

10. A computer program product, characterized in that When the computer program product is run on an electronic device, the electronic device executes the status tree data caching method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Block chain state data caching and querying method and device, and storage medium

    CN110175188A

  • State data storage method, state data query method, data management method, equipment and storage medium

    CN110442577A

  • Block chain data storage, updating and reading methods and devices, and electronic equipment

    CN114706848A

  • Blockchain-based data processing method, device, medium, and program product

    US20240305490A1