Storage device and method for smart contract
By using the document database in the blockchain system to replace the MPT tree storage method, the performance problems caused by smart contract data storage are solved, and more efficient read and write performance and data management are realized.
Patent Information
- Application Number
- CN202010989054.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-09-18
- Publication Date
- 2025-01-28
- Estimated Expiration
- 2040-09-18
AI Technical Summary
In blockchain systems, with the increase of historical data, the storage method of MPT trees leads to performance problems, especially in the read and write operations of smart contracts.
The document database is used as a smart contract storage method. Through the smart contract data table and system structure table through the document database, efficient storage and management of smart contract data.
Improve the performance during the use of smart contracts, provide stronger read and write performance, and meet the requirements of the blockchain system's irreversible and traceability of data.
Smart Images

Figure CN114201487B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of blockchain technology, and in particular to a storage device and method for smart contracts. Background Art
[0002] The Merkle Patricia Trie (MPT) tree is a classic data storage method in blockchain technology. The MPT tree can organize the data of the smart contracts involved in the block, and realize the search and traceability of data.
[0003] At present, blockchain technology has been applied to some scenarios represented by clearing and settlement systems. In these scenarios, data such as billing flow lists and billing rules need to be stored on the chain, with many fields and large amounts of data. In some cases, more than 50k of data will be stored in the blockchain smart contract at a time. Summary of the invention
[0004] After analysis, the inventors found that when the amount of historical data gradually increases, performance problems will occur due to the storage method and disk I / O (Input / Output).
[0005] A technical problem to be solved by the embodiments of the present invention is: how to improve the performance of smart contracts during use.
[0006] According to a first aspect of some embodiments of the present invention, a storage device for a smart contract is provided, comprising: a document database, wherein the document database comprises: one or more smart contract data tables, each smart contract data table corresponding to a smart contract, storing data of the corresponding smart contract; and a system structure table, storing the correspondence between the identifier and the hash value of each smart contract data table, and the hash value of the transaction record involved in the status change of the smart contract.
[0007] In some embodiments, the storage device also includes: a processor, configured to, after receiving an execution request for a smart contract, read the status and code of the executed smart contract from the data table of the executed smart contract in the document database; execute the smart contract to obtain the changed status of the executed smart contract; and update the corresponding smart contract data table according to the changed status.
[0008] In some embodiments, the processor is further configured to: obtain a request to create a smart contract, wherein the creation request includes a storage type; when the storage type is a document type, establish a smart contract data table corresponding to the smart contract requested to be created in the document database; and add an entry corresponding to the established smart contract data table in the system structure table.
[0009] In some embodiments, the processor is further configured to: create a transaction record for recording the creation of a smart contract, wherein the transaction record includes the storage type of the created smart contract.
[0010] In some embodiments, each record in the smart contract data table includes a document, and the document adopts a BSON data structure.
[0011] In some embodiments, in the smart contract data table, the key of each record is a serial number recorded in the smart contract, and the value of each record is a serialized value of a member attribute of the corresponding smart contract.
[0012] In some embodiments, the document database also includes at least one of a permission table and a partition table, the permission table is used to record account information with write permissions, and the partition table is used to record the partition where each smart contract data table is located.
[0013] In some embodiments, the storage device also includes: a key-value database configured to store the correspondence between nodes in the MPT tree corresponding to the smart contract.
[0014] According to a second aspect of some embodiments of the present invention, a method for storing a smart contract is provided, comprising: after receiving an execution request for a smart contract, determining a data table of the executed smart contract from a document database, and reading a status and code of the executed smart contract from the data table, wherein the document database comprises one or more smart contract data tables, each smart contract data table corresponds to a smart contract, and stores data of the corresponding smart contract; executing the smart contract to obtain a changed status of the executed smart contract; and updating the corresponding smart contract data table according to the changed status.
[0015] In some embodiments, the document database also includes a system structure table that stores the correspondence between the identifier and the verification value of each smart contract data table, and the hash value of the transaction involved in the state change of the smart contract.
[0016] In some embodiments, the storage method also includes: obtaining a creation request for a smart contract, wherein the creation request includes a storage type; when the storage type is a document type, establishing a smart contract data table corresponding to the smart contract requested to be created in the document database; and adding an entry corresponding to the established smart contract data table to the system structure table.
[0017] In some embodiments, the storage method further includes: creating a transaction record for recording the creation of a smart contract, wherein the transaction record includes the storage type of the created smart contract.
[0018] Some embodiments of the above invention have the following advantages or beneficial effects: while meeting the characteristics and requirements of blockchain, the embodiments of the present invention replace the loose storage method based on MPT tree with document storage, thereby providing a storage solution with stronger read and write performance for various smart contract applications, thereby improving the performance of smart contracts during use.
[0019] Further features and advantages of the present invention will become apparent from the following detailed description of exemplary embodiments of the present invention with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0021] Figure 1 A schematic diagram of the structure of a storage device for a smart contract according to some embodiments of the present invention is shown.
[0022] Figure 2 A schematic diagram of the structure of a storage device for a smart contract according to some other embodiments of the present invention is shown.
[0023] Figure 3 A schematic diagram of the structure of a storage device for a smart contract according to some further embodiments of the present invention is shown.
[0024] Figure 4 A flow chart of a method for storing a smart contract according to some embodiments of the present invention is shown.
[0025] Figure 5 A schematic diagram of a process for creating a smart contract according to some embodiments of the present invention is shown.
[0026] Figure 6 A schematic diagram of the structure of a storage device for a smart contract according to some further embodiments of the present invention is shown.
[0027] Figure 7 A schematic diagram of the structure of a storage device for a smart contract according to some further embodiments of the present invention is shown. DETAILED DESCRIPTION
[0028] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments. The following description of at least one exemplary embodiment is actually only illustrative and is by no means intended to limit the present invention and its application or use. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0029] The relative arrangement of components and steps, the numerical expressions and numerical values set forth in these embodiments do not limit the scope of the present invention unless specifically stated otherwise.
[0030] At the same time, it should be understood that for the convenience of description, the sizes of the various parts shown in the drawings are not drawn according to the actual proportional relationship.
[0031] Technologies, methods, and apparatus known to ordinary technicians in the relevant field may not be discussed in detail, but where appropriate, such technologies, methods, and apparatus should be considered part of the authorization specification.
[0032] In all examples shown and discussed herein, any specific values should be interpreted as merely exemplary and not as limiting. Therefore, other examples of the exemplary embodiments may have different values.
[0033] It should be noted that like reference numerals and letters refer to similar items in the following figures, and therefore, once an item is defined in one figure, it need not be further discussed in subsequent figures.
[0034] After analyzing the relevant technologies, the inventors found that in some blockchain systems, transactions in blocks operate on accounts, thereby changing the data in the accounts, that is, the status of the accounts. Smart contract data is also stored under accounts. Each account stores the code, parameters, current balance (nonce) and other data of the "user" of the smart contract. Changes in smart contract parameters mean that the contract account has changed. The underlying database of this type of blockchain is a key-value (KV) database.
[0035] In the related art, the data in the block is organized through the MPT tree. The index of a node on the MPT tree is the hash value of the node. In the KV database, when a key is used to search for a value, the hash value of the next node to be jumped to is obtained based on the key representing a certain node. After obtaining the hash value of the next node, the data of the next node can be retrieved from the underlying database, and so on, until the leaf node where the value is located is reached. When new data is to be indexed, the new root node hash of the MPT is used to find the new root node from the underlying database, and then traverse layer by layer to finally find the new data. If you want to query historical data, you can use the historical tree root hash to retrieve the historical root node from the underlying database, and then traverse downward to query the historical data.
[0036] When the data of a leaf node on the MPT tree is updated, the hash of this leaf node will also be updated. As a result, the hashes of all the intermediate nodes involved when this leaf node traces back to the root node will also be updated. Finally, the hash of the MPT root node will also be updated. Although, by taking out the state root of a block from a certain block and querying the root node of the MPT tree, the data history of the account at that time of this block can be indexed. However, this structure of the MPT tree brings a lot of hash calculations and breaks up the continuity of the storage of the underlying data, greatly reducing performance. But in some scenarios, the performance requirement is greater than the traceability requirement.
[0037] The underlying KV database usually uses the Leveldb database. In this database, a file can only store 40 key-value pairs, so the access and reading efficiency is very low, especially for values greater than 50K. Therefore, the inventor proposed a non-MPT tree storage structure to improve the performance of the overall system. Figure 1 An embodiment of the smart contract storage device of the present invention is described.
[0038] Figure 1 FIG. 1 is a schematic diagram showing a structure of a storage device for a smart contract according to some embodiments of the present invention. Figure 1 As shown, the storage device 10 of the smart contract of this embodiment includes a document database 110. The document database 110 includes one or more smart contract data tables 1101 and a system structure table 1102. The document database 110 processes data in units of documents. Since documents can support longer, more complex, and unstructured data, the document database 110 can efficiently store data of various types of smart contracts, such as smart contracts with large data volumes or multiple nested structures. A document is equivalent to a record in a relational database.
[0039] In some embodiments, the document database 110 is a Mongo database. The data structure supported by Mongo is very loose and can store relatively complex data types. Therefore, the Mongo database can provide better support for various types of smart contracts.
[0040] Each smart contract data table 1101 corresponds to a smart contract and stores data of the corresponding smart contract. The smart contract is named, for example, according to the address of the smart contract account to distinguish different smart contract data tables.
[0041] In some embodiments, each record of the smart contract data table 1101 includes a document, and the document adopts a BSON data structure. BSON has the characteristics of lightness, traversability, and high efficiency. The BSON data structure makes it more direct to process document-type data and supports complex data structures, such as supporting the definition of complex nested type mappings in smart contracts.
[0042] In some embodiments, the key of each record in the smart contract data table 1101 is the serial number of the record in the smart contract, and the value of each record is the serialized value of the member attribute of the corresponding smart contract. For example, the definition of an entry is added to the smart contract data table, and the entry corresponds to a row in the table. All stored members, that is, member attributes in the smart contract, are indexed from 0 according to the position of the member defined in the smart contract, and 1 is accumulated each time to be stored as their corresponding key value. Value is the serialized value of the corresponding member attribute. Thus, the smart contract data table 1101 can sort each member according to the definition position of the member in the smart contract, so that the data table can accurately reflect the content of the smart contract.
[0043] The system structure table 1102 stores the correspondence between the identifier and the hash value of each smart contract data table, as well as the hash value of the transaction record involved in the state change of the smart contract.
[0044] When a smart contract needs to be verified, by comparing the verification value stored in the system structure table 1102 and the verification value of the calculated smart contract data table, it can be determined whether the smart contract data table has been tampered with. Therefore, even if the MPT tree storage method is not used, the requirement of data immutability in the blockchain can be met.
[0045] When it is necessary to trace the historical status of a smart contract, the hash value of the transaction involved in the state change of the smart contract can be read from the system structure table 1102 to locate the state change value of the smart contract in the historical transaction. Combined with taking out the latest state and state change value from the smart contract data table 1101, the historical status of the smart contract can be determined. Therefore, the requirement for data traceability in the blockchain can also be met.
[0046] In some embodiments, the document database 110 further includes a permission table 1103 for recording account information having write permission, such as an account address.
[0047] In some embodiments, the document database also includes a partition table 1104 for recording the partition where each smart contract data table is located. For application scenarios such as "Hyperledger", some block data is only stored in some nodes. Through the partition table, the node storing relevant information can be quickly located, which improves the reading efficiency.
[0048] While meeting the characteristics and requirements of blockchain, the above embodiments replace the loose storage method based on MPT tree with document storage, thereby providing a storage solution with stronger read and write performance for various smart contract applications, thereby improving the performance of smart contracts during use.
[0049] In blockchain value-added service settlement or blockchain international roaming settlement, it is usually necessary to upload billing data, consumption flow and other data to the blockchain for traceability and evidence storage. Usually, this type of C-end (consumer-end) user data has a large amount of data and a relatively complex structure. Frequent large-scale data upload will drag down the performance of the entire blockchain system. The efficient smart contract storage method provided by the embodiment of the present invention can bring a qualitative leap in system performance.
[0050] Figure 2 FIG. 2 shows a schematic diagram of a storage device for a smart contract according to some other embodiments of the present invention. Figure 2 As shown, the storage device 20 of this embodiment includes a document database 210 and a processor 220. For a specific implementation of the document database 210, see Figure 1 The embodiments are not described in detail here.
[0051] The processor 220 is configured to, after receiving a request to execute a smart contract, read the status and code of the executed smart contract from the data table of the executed smart contract in the document database 210; execute the smart contract to obtain the changed status of the executed smart contract; and update the corresponding smart contract data table according to the changed status.
[0052] The execution request of a smart contract includes the information of the smart contract, such as the address of the smart contract. The processor can determine the corresponding smart contract data table through this information, so that all the information of the smart contract can be obtained without multiple indexing processes. Therefore, the reading and writing efficiency and the performance of the smart contract in use are improved.
[0053] In some embodiments, the processor 220 is further configured to: obtain a smart contract creation request, wherein the creation request includes a storage type; when the storage type is a document type, establish a smart contract data table corresponding to the smart contract requested to be created in the document database; and add an entry corresponding to the established smart contract data table in the system structure table. Thus, the creator can choose whether to use a storage method based on a document database when creating a smart contract.
[0054] In some embodiments, the document database and the KV database used by the MPT tree can exist in the system in parallel. When creating a smart contract, the creator can choose which storage method to use as needed. Figure 3 FIG. 2 shows a schematic diagram of a storage device for a smart contract according to some other embodiments of the present invention. Figure 3 As shown, the storage device 30 of the smart contract of this embodiment includes a document database 310, a processor 320 and a key value (KV) database 330. The specific implementation of the document database 310 and the processor 320 refers to the above-mentioned embodiment and will not be repeated here.
[0055] The key-value database 330 is configured to store the corresponding relationship between nodes in the MPT tree corresponding to the smart contract.
[0056] In some embodiments, the processor 320 is further configured to: obtain a request to create a smart contract; when the storage type is a document type, establish a smart contract data table corresponding to the smart contract requested to be created in the document database, and add an entry corresponding to the established smart contract data table in the system structure table; when the storage type is an MPT tree type, construct an MPT tree corresponding to the smart contract, and store the index relationship between each node of the MPT tree in the key-value database 330.
[0057] In some embodiments, the created smart contract and its type are recorded by creating a transaction: a transaction record is created to record the creation of the smart contract, wherein the transaction record includes the storage type of the created smart contract. Thus, by querying the ledger of the blockchain, the creation information of the smart contract can be obtained.
[0058] Reference below Figure 4 An embodiment of the storage method of the smart contract of the present invention is described.
[0059] Figure 4 FIG. 1 is a flow chart showing a method for storing a smart contract according to some embodiments of the present invention. Figure 4 As shown, the storage method of the smart contract of this embodiment includes steps S402 to S406.
[0060] In step S402, after receiving the execution request of the smart contract, the data table of the executed smart contract is determined from the document database, and the status and code of the executed smart contract are read from the data table, wherein the document database includes one or more smart contract data tables, each smart contract data table corresponds to a smart contract, and stores the data of the corresponding smart contract.
[0061] In step S404, the smart contract is executed to obtain the changed state of the executed smart contract.
[0062] In step S406, the corresponding smart contract data table is updated according to the changed status.
[0063] While meeting the characteristics and requirements of blockchain, the above embodiments replace the loose storage method based on MPT tree with document storage, thereby providing a storage solution with stronger read and write performance for various smart contract applications, thereby improving the performance of smart contracts during use.
[0064] In some embodiments, the document database also includes a system structure table that stores the corresponding relationship between the identifier and the verification value of each smart contract data table, and the hash value of the transaction involved in the state change of the smart contract. Thus, the smart contract data table can be verified and traced using the system structure table.
[0065] In some embodiments, when a smart contract is created, a storage method for the smart contract is selected. Figure 5 An embodiment of the smart contract creation method of the present invention is described.
[0066] Figure 5 A schematic diagram of a process for creating a smart contract according to some embodiments of the present invention is shown. Figure 5 As shown, the smart contract creation method of this embodiment includes steps S502 to S506.
[0067] In step S502, a creation request for a smart contract is obtained, wherein the creation request includes a storage type.
[0068] In step S504, when the storage type is a document type, a smart contract data table corresponding to the smart contract requested to be created is established in the document database.
[0069] In step S506, an entry corresponding to the established smart contract data table is added to the system structure table.
[0070] When creating a smart contract, the creator can choose a storage method that is more suitable for the smart contract being created. For example, for smart contracts with complex structures and large amounts of data, a storage method based on a document database can be used, while other smart contracts can choose a storage method based on an MPT tree.
[0071] In some embodiments, the smart contract creation method also includes step S508.
[0072] In step S508, a transaction record is created to record the creation of the smart contract, wherein the transaction record includes the storage type of the created smart contract. Thus, by querying the ledger of the blockchain, the creation information of the smart contract can be obtained.
[0073] Figure 6 FIG. 2 shows a schematic diagram of a storage device for a smart contract according to some further embodiments of the present invention. Figure 6 As shown, the storage device 60 of the smart contract of this embodiment includes: a memory 610 and a processor 620 coupled to the memory 610, and the processor 620 is configured to execute the storage method of the smart contract in any one of the aforementioned embodiments based on the instructions stored in the memory 610.
[0074] The memory 610 may include, for example, a system memory, a fixed non-volatile storage medium, etc. The system memory may store, for example, an operating system, an application program, a boot loader, and other programs.
[0075] Figure 7 FIG. 2 shows a schematic diagram of a storage device for a smart contract according to some further embodiments of the present invention. Figure 7 As shown, the storage device 70 of the smart contract of this embodiment includes: a memory 710 and a processor 720, and may also include an input / output interface 730, a network interface 740, a storage interface 750, etc. These interfaces 730, 740, 750 and the memory 710 and the processor 720 may be connected, for example, via a bus 760. Among them, the input / output interface 730 provides a connection interface for input / output devices such as a display, a mouse, a keyboard, and a touch screen. The network interface 740 provides a connection interface for various networked devices. The storage interface 750 provides a connection interface for external storage devices such as SD cards and USB flash drives.
[0076] An embodiment of the present invention further provides a computer-readable storage medium having a computer program stored thereon, wherein the program, when executed by a processor, implements any one of the aforementioned smart contract storage methods.
[0077] It should be understood by those skilled in the art that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable non-transient storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0078] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0079] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0080] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0081] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A storage device for a smart contract, comprising: A document database, wherein the document database processes data in units of documents, and the document database includes: One or more smart contract data tables, each of which corresponds to a smart contract and stores data of the corresponding smart contract; and A system structure table stores the correspondence between the identifier and the hash value of each smart contract data table, the correspondence between the identifier and the check value of each smart contract data table, and the hash value of the transaction record involved in the state change of the smart contract, wherein by comparing the check value stored in the system structure table with the calculated check value of the smart contract data table, it is determined whether the smart contract data table has been tampered with.
2. The storage device according to claim 1, further comprising: A processor configured to read the status and code of the executed smart contract from a data table of the executed smart contract in the document database after receiving the execution request of the smart contract; Execute the smart contract to obtain a changed state of the executed smart contract; and update a corresponding smart contract data table according to the changed state.
3. The storage device according to claim 2, wherein: The processor is further configured to: obtain a request to create a smart contract, wherein the creation request includes a storage type; when the storage type is a document type, establish a smart contract data table corresponding to the smart contract requested to be created in the document database; and add an entry corresponding to the established smart contract data table in the system structure table.
4. The storage device according to claim 3, wherein: The processor is further configured to: create a transaction record for recording the creation of the smart contract, wherein the transaction record includes a storage type of the created smart contract.
5. The storage device according to claim 1, wherein: Each record in the smart contract data table includes a document, and the document adopts a binary data exchange format BSON data structure.
6. The storage device according to claim 1 or 5, wherein: In the smart contract data table, the key of each record is the serial number of the record in the smart contract, and the value of each record is the serialized value of the member attribute of the corresponding smart contract.
7. The storage device according to claim 1, wherein: The document database also includes at least one of a permission table and a partition table, wherein the permission table is used to record account information with write permission, and the partition table is used to record the partition where each smart contract data table is located.
8. The storage device according to claim 1, further comprising: The key-value database is configured to store the corresponding relationship between nodes in the Merkle Patricia tree MPT tree corresponding to the smart contract.
9. A method for storing a smart contract, comprising: After receiving the execution request of the smart contract, determine the data table of the executed smart contract from the document database, and read the status and code of the executed smart contract from the data table, wherein the document database includes one or more smart contract data tables, each smart contract data table corresponds to a smart contract, and stores the data of the corresponding smart contract, and the document database also includes a system structure table for storing the corresponding relationship between the identifier and the check value of each smart contract data table, the corresponding relationship between the identifier and the check value of each smart contract data table, and the hash value of the transaction involved in the state change of the smart contract; Execute the smart contract to obtain the changed state of the executed smart contract; Update the corresponding smart contract data table according to the changed status; By comparing the checksum stored in the system structure table with the checksum calculated for the smart contract data table, it is determined whether the smart contract data table has been tampered with.
10. The storage method according to claim 9, further comprising: Obtaining a creation request for a smart contract, wherein the creation request includes a storage type; In the case where the storage type is a document type, establishing a smart contract data table corresponding to the smart contract requested to be created in the document database; and Add an entry corresponding to the established smart contract data table in the system structure table.
11. The storage method according to claim 10, further comprising: A transaction record is created for recording the creation of the smart contract, wherein the transaction record includes a storage type of the created smart contract.
Citation Information
Patent Citations
Blockchain state storage method based on SQL database
CN106874440A
Intelligent contract calling method, device and system, medium and electronic equipment
CN111273970A