A method, system, device and medium for storing blockchain contract data in a sharded manner
By splitting the blockchain contract into logical contracts, proxy contracts and index contracts, and storing data contracts in shards, the performance degradation caused by excessive data volume in traditional contracts is solved, and efficient data reading and writing and business logic upgrades are achieved.
Patent Information
- Application Number
- CN202311155175.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-07
- Publication Date
- 2025-07-08
- Estimated Expiration
- 2043-09-07
AI Technical Summary
The accumulation of data in traditional blockchain contracts is too large, resulting in a decline in read and write performance, making it difficult to efficiently handle business logic upgrades and data migrations.
The blockchain contract is split into logical contracts, proxy contracts and index contracts. The data contract is stored in multiple instances. The index number is calculated through proxy contracts and index contracts to locate the data contract address, providing a reading and writing method of data.
It improves the read and write performance of the contract, supports no need to worry about data migration and loss when upgrading business logic, reduces the amount of data in a single contract, and improves data processing efficiency.
Smart Images

Figure CN117112568B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of blockchain, and particularly to a method, system, device and medium for storing blockchain contract data in a sharded manner. Background Art
[0002] In the field of blockchain, the separation of traditional logic contracts and data contracts can facilitate the subsequent upgrade of logic contracts. However, as time goes by, the amount of data accumulated in the data contract is getting larger and larger, resulting in a gradual decline in the read and write performance of the data contract. Summary of the Invention
[0003] The object of the present invention is to provide a method, system, device and medium for storing blockchain contract data in a sharded manner to improve the read and write performance of contracts.
[0004] To achieve the above object, the present invention provides the following solutions:
[0005] A method for storing blockchain contract data in a sharded manner includes:
[0006] Deploying an agency contract, an index contract and a logic contract; the agency contract is used to store the initial business time, the time period interval and the address of the index contract; the index contract is used to store the mapping relationship between the index number and the data contract address; the logic contract is used to store the address of the agency contract;
[0007] When a business party needs to use a smart contract for business processing, calling the logic contract; the smart contract includes: a logic contract, an agency contract, an index contract and a data contract; the data contract is used to store contract data in a sharded manner according to the index number and provide read and write methods for the data; the data contract includes a number of data contract instances, and the data contract instances correspond to the index numbers one by one;
[0008] The logic contract calls the agency contract and passes the business number and the operation flag to the agency contract; the business number includes a field carrying business time information;
[0009] The agency contract parses the business number to obtain the business time information, and calculates the index number according to the business time information, the initial business time and the time period interval;
[0010] The agency contract requests the data contract address from the index contract according to the index number and the operation flag and returns it to the logic contract;
[0011] The logic contract reads and writes data through the data contract according to the data contract address.
[0012] Optionally, deploying the agency contract, the index contract and the logic contract specifically includes:
[0013] The business party deploys the proxy contract and the index contract by sending a transaction, and initializes the business initial time, the time period interval, and the index contract address for the proxy contract through the transaction, and initializes the mapping relationship between the index number and the data contract address for the index contract;
[0014] The business party deploys the logic contract by means of a transaction, and initializes the proxy contract address for the logic contract through the transaction.
[0015] Optionally, when the business party needs to use the smart contract for business processing, it calls the logic contract, specifically including:
[0016] When the business party needs to use the smart contract for business processing, it constructs a transaction according to the method parameter rules of the logic contract, signs it, and then sends it to the blockchain to call the logic contract.
[0017] Optionally, the proxy contract parses the business number to obtain business time information, and calculates the index number according to the business time information, the business initial time, and the time period interval, specifically including:
[0018] The proxy contract parses the business number to obtain business time information, calculates the difference between the business time information and the business initial time, and takes the integer after dividing the difference by the time interval to obtain the index number.
[0019] Optionally, the proxy contract requests the data contract address from the index contract according to the index number and the operation flag, and returns it to the logic contract, specifically including:
[0020] The proxy contract requests the index contract according to the index number, so that the index contract queries the data contract according to the mapping relationship;
[0021] When the index contract cannot query the data contract, it judges whether the operation flag is a write operation; if the operation flag is a write operation, it creates a new data contract instance and updates the mapping relationship between the index number and the data contract address; if the operation flag is not a write operation, it returns an empty data contract address; when the index contract queries the data contract, it returns the data contract address;
[0022] The proxy contract returns the data contract address to the logic contract.
[0023] Optionally, the mapping relationship between the index number and the data contract address is stored in the index contract in the form of a hash table.
[0024] A blockchain contract data sharding storage system, comprising:
[0025] A deployment module, configured to deploy a proxy contract, an index contract, and a logic contract; the proxy contract is used to store the business initial time, the time period interval, and the index contract address; the index contract is used to store the mapping relationship between the index number and the data contract address; the logic contract is used to store the proxy contract address;
[0026] A processing module, configured to:
[0027] When a business party needs to use a smart contract for business processing, call the logic contract; the smart contract includes: a logic contract, a proxy contract, an index contract, and a data contract; the data contract is used to shard and store contract data according to the index number, and provide data reading and writing methods; several data contract instances are included in the data contract, and the data contract instances correspond one-to-one with the index number;
[0028] The logic contract calls the proxy contract, and passes the business number and the operation flag to the proxy contract; the business number includes a field carrying business time information;
[0029] The proxy contract parses the business number to obtain business time information, and calculates an index number according to the business time information, the business initial time, and the time period interval;
[0030] The proxy contract requests a data contract address from the index contract according to the index number and the operation flag, and returns it to the logic contract;
[0031] The logic contract reads and writes data through the data contract according to the data contract address.
[0032] An electronic device, comprising a memory and a processor, the memory is used to store a computer program, and the processor runs the computer program to enable the electronic device to execute the above-mentioned blockchain contract data sharding storage method.
[0033] A computer-readable storage medium, which stores a computer program, and when the computer program is executed by a processor, the above-mentioned blockchain contract data sharding storage method is implemented.
[0034] According to the specific embodiments provided by the present invention, the present invention discloses the following technical effects:
[0035] The blockchain contract data sharding storage method provided by the present invention splits the smart contract on the blockchain into a logic contract, an agency contract, an index contract, and a data contract. Among them, the logic contract does not store business data. In this way, when the subsequent business logic changes and the contract needs to be upgraded, a new logic contract can be deployed without worrying about the migration and loss of business data. The agency contract is responsible for returning the actual data contract address or instance according to the request of the logic contract. The agency contract calculates the index number based on the time information passed in by the logic contract, looks up the actual data contract address from the index contract, and returns the data contract address or instance to the logic contract for processing. The index contract is responsible for storing the mapping relationship between the index number and the data contract address, and the agency contract obtains the address of the data contract through the index contract. The data contract is responsible for storing the data that the logic contract needs to save and providing data reading and writing methods. Among them, only one instance of the agency contract and the index contract needs to be deployed. As time goes by, the index contract creates multiple data contract instances according to the index number. By sharding and storing the contract data into multiple different data contract instances, the present invention reduces the size of the data volume within a single contract and can improve the contract reading and writing performance. Description of the Drawings
[0036] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the embodiments. Obviously, the drawings in the following description are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings.
[0037] Figure 1 It is a flowchart of the blockchain contract data sharding storage method provided by the present invention;
[0038] Figure 2 It is a process diagram of the business party using the smart contract for business processing provided by the present invention. Detailed Embodiments
[0039] The following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts belong to the scope of protection of the present invention.
[0040] The purpose of the present invention is to provide a blockchain contract data sharding storage method, system, device and medium, which reduces the size of the data volume within a single contract and improves the contract reading and writing performance by sharding and storing the contract data into multiple different data contracts.
[0041] To make the above objects, features, and advantages of the present invention more apparent and understandable, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.
[0042] The present invention provides a method for storing blockchain contract data in slices. Figure 1 It is a flowchart of the method for storing blockchain contract data in slices provided by the present invention. As Figure 1 shown, the method includes:
[0043] Step S1: Deploy a proxy contract, an index contract, and a logic contract; the proxy contract is used to store the business initial time, the time period interval, and the index contract address; the index contract is used to store the mapping relationship between the index number and the data contract address; the logic contract is used to store the proxy contract address.
[0044] Step S1 specifically includes:
[0045] Step S1.1: The business party deploys the proxy contract and the index contract by sending a transaction, and initializes the business initial time, the time period interval, and the index contract address for the proxy contract through the transaction, and initializes the mapping relationship between the index number and the data contract address for the index contract.
[0046] Specifically, the business party deploys the business proxy contract and the index contract by sending a transaction. After the above contracts are successfully deployed, the configurations in the contracts are initialized. The business party initializes the business initial time, the time period interval, and the index contract address for the proxy contract through the transaction. The business initial time is equal to the start time of the business, and the business initial time is stored in the contract in numerical format, and the time interval is the interval duration of the time slice.
[0047] Step S1.2: The business party deploys the logic contract by sending a transaction, and initializes the proxy contract address for the logic contract through the transaction.
[0048] Specifically, the business party deploys the business logic contract by sending a transaction. The business party initializes the proxy contract address information stored in the logic contract through the transaction. The business logic contract only stores the proxy contract address information and does not store the actual business data. Multiple business logic contracts can share a proxy contract and an index contract.
[0049] Step S2: When the business party needs to use the smart contract for business processing, call the logic contract; the smart contract includes: a logic contract, a proxy contract, an index contract, and a data contract; the data contract is used to store the contract data in slices according to the index number and provide methods for reading and writing data; there are several data contract instances in the data contract, and the data contract instances correspond to the index numbers one by one.
[0050] Preferably, when the business party needs to use a smart contract for business processing, a transaction is constructed according to the method parameter rules of the logic contract and signed, and then sent to the blockchain to invoke the logic contract. Among them, the contract parameters include a business number and an operation flag; the business number needs to contain a Key (field) carrying business time information.
[0051] Step S3: The logic contract invokes the proxy contract and passes the business number and the operation flag to the proxy contract; the business number includes a field carrying business time information.
[0052] Preferably, the business logic contract processes the transaction. When accessing or updating data in the contract, the business logic contract passes the Key with business time information and the write operation flag to the proxy contract according to the proxy contract address stored in the contract.
[0053] Step S4: The proxy contract parses the business number to obtain business time information, and calculates an index number according to the business time information, the business initial time, and the time period interval.
[0054] Preferably, the proxy contract parses the business number to obtain business time information, calculates the difference between the business time information and the business initial time, and takes the integer after dividing the difference by the time interval to obtain the index number.
[0055] Specifically, the proxy contract parses according to the Key passed in by the logic contract, obtains the time information in the Key, calculates the difference between the time information Key and the business initial time, and then divides the difference by the time interval and takes the integer to obtain the time index number.
[0056] Step S5: The proxy contract requests the data contract address from the index contract according to the index number and the operation flag, and returns it to the logic contract.
[0057] Step S5 specifically includes:
[0058] Step S5.1: The proxy contract requests the index contract according to the index number, so that the index contract queries the data contract according to the mapping relationship.
[0059] Step S5.2: When the index contract cannot query the data contract, it is judged whether the operation flag is a write operation; if the operation flag is a write operation, a new data contract instance is created, and the mapping relationship between the index number and the data contract address is updated; if the operation flag is not a write operation, an empty data contract address is returned; when the index contract queries the data contract, the data contract address is returned.
[0060] Step S5.3: The proxy contract returns the data contract address to the logic contract.
[0061] Specifically, the proxy contract calls the index contract by passing in two parameters, namely the index number and the write operation flag, to obtain or create the corresponding data contract address or instance and return it to the business logic contract. If there is no data contract in the corresponding index number in the index contract and the current request is a data write request, the index contract dynamically creates a new data contract and then returns the address or instance of the data contract to the proxy contract, and then the proxy contract returns it to the logic contract.
[0062] Step S6: The logic contract reads and writes data through the data contract according to the data contract address.
[0063] Specifically, the business logic contract performs data read and write operations through the instance of the data contract, and the data contract provides relevant read and write methods for data reading and writing. The data contract provides multiple types of hash tables to store data. In this way, the data contract can be regarded as a key-value database, where the key is the hash of the relevant data, and the value selects different types of hash tables according to the type of the stored data. For example, for storing the amount of an order, hash(order number, "amount") can be used as the key. Since the amount is a numeric type, a hash table with a numeric type value is selected for storage; for the order product name, which is a string type, a hash table with a string type value can be selected for storage, and the key can use hash(business number, "product name").
[0064] Taking the order processing business as an example, the business number is specifically the order number. To handle the order business, usually a single order contract can be used. However, if it is found that there are bugs in the contract or the order logic changes and needs to be upgraded, redeploying the contract will cause the data already stored in the original contract to be unable to be migrated. Therefore, the order contract can be split into a logic contract and a data contract. The logic contract is used to handle the order logic, and the data contract is used to store the contract data, which is equivalent to the concept of a database. In this way, when the business needs to be upgraded, only the logic contract needs to be redeployed, and then the address of the original data contract is stored in the new logic contract, and the historical data can be read. However, as the amount of data in the data contract increases, the read and write performance of the data contract becomes slower and slower. Therefore, the method of the present invention is designed to split the order contract into a logic contract, a data contract, a proxy contract, and an index contract.
[0065] 1) First, the business party deploys the logic contract and the proxy contract and initializes them. Among them, the business initial time is the earliest start time of the business. For example, the earliest order of the order business is September 1, 2001, so the business initial time is 20010901, which can be converted into an integer calculated by day based on January 1, 1970. According to the current business volume, the order business is sliced every 30 days, that is, the time interval is 30. That is, the order data between September 1, 2001 and September 30, 2001 is in data contract 1, the order data between October 1, 2001 and October 30, 2001 is in data contract 2, and the order data between October 31, 2001 and November 29, 2001 is in data contract 3.
[0066] 2) The business party deploys the business logic contract and initializes the logic contract.
[0067] 3) Call the logic contract. The method parameters of the logic contract need to include the order number and other specific business data in the order. Among them, the order number needs to include the date information of the order. For example, F20010903-X23-B, and its coding rule includes the date information 20010903 of the order. The order number is the Key containing the business time information. Note that the date in the order number is not the current actual time information, but a kind of business number, referring to the time information when the business occurs.
[0068] 4) When the logic contract needs to read and write contract data, pass the order number and the flag indicating whether it is a write operation to the proxy contract.
[0069] 5) The proxy contract needs to be able to decode the order number, obtain the date information 20010903 in the number, and calculate the index number of the order according to the business initial time and the time interval. The index number in the current example is 0.
[0070] 6) The proxy contract queries the actual address of the data contract from the index contract according to the index number and the write operation flag. In the index contract, the mapping relationship between the index number and the data contract address is stored in the form of a hash table. When the index contract cannot query the data contract, if it is a write operation, a new data contract is created and the mapping relationship between the index number and the contract address is stored; when it is not a write operation, a contract address of all 0s is returned, indicating that there is no such data contract. The proxy contract then returns the result to the business logic contract.
[0071] 7) The business logic contract performs data read and write operations through the instance of the data contract.
[0072] Figure 2This is a process diagram for a service provider to use a smart contract for business processing. Here, the service provider is the user of the system and is responsible for calling business contracts. The proxy contract is responsible for parsing the Key passed in by the business contract and obtaining the index number through parsing. The index contract is responsible for storing the mapping relationship between the index number and the data contract. The data contract is responsible for storing the actual data in the business contract.
[0073] As Figure 2 shown, the process of the service provider calling the logic contract for processing is as follows:
[0074] 1. The service provider calls the logic contract with a key containing time information in the parameters for business processing.
[0075] 2. The logic contract calls the proxy contract, and the passed-in parameters include the key, requesting to obtain the data contract.
[0076] 3. The proxy contract parses the key and calculates the index number.
[0077] 4. The proxy contract requests the index contract based on the index number to obtain the data contract address or instance.
[0078] 5. The proxy contract returns the data contract address or instance to the logic contract.
[0079] 6. The logic contract reads and writes data through the data contract.
[0080] In summary, the present invention keeps the amount of data stored in each data contract from being too large through a sharding method, which can improve the read and write efficiency of the data contract.
[0081] To execute the above method to achieve the corresponding functions and technical effects, a blockchain contract data sharding storage system is provided below. The system includes:
[0082] A deployment module for deploying the proxy contract, the index contract, and the logic contract; the proxy contract is used to store the business start time, the time period interval, and the index contract address; the index contract is used to store the mapping relationship between the index number and the data contract address; the logic contract is used to store the proxy contract address.
[0083] A processing module is used to call the logic contract when a business party needs to use a smart contract for business processing. The smart contract includes: a logic contract, an agency contract, an index contract, and a data contract. The data contract is used to store contract data in slices according to an index number and provide read and write methods for the data. The data contract includes a number of data contract instances, and the data contract instances correspond to the index numbers one by one. The logic contract calls the agency contract and passes a business number and an operation flag to the agency contract. The business number includes a field carrying business time information. The agency contract parses the business number to obtain the business time information, and calculates the index number according to the business time information, the business initial time, and the time period interval. The agency contract requests a data contract address from the index contract according to the index number and the operation flag, and returns it to the logic contract. The logic contract reads and writes data through the data contract according to the data contract address.
[0084] The present invention also provides an electronic device, including a memory and a processor. The memory is used to store a computer program, and the processor is used to run the computer program to enable the electronic device to execute the above-mentioned blockchain contract data sharding storage method. The electronic device may be a server.
[0085] In addition, the present invention also provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, it implements the above-mentioned blockchain contract data sharding storage method.
[0086] In this specification, the various embodiments are described in a progressive manner. Each embodiment focuses on the differences from other embodiments. The same or similar parts among the various embodiments can be referred to each other. For the system disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple. For the relevant parts, reference can be made to the description in the method part.
[0087] In this article, specific examples are used to elaborate on the principle and implementation manner of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention. At the same time, for those of ordinary skill in the art, according to the idea of the present invention, there will be changes in the specific implementation manner and application scope. In summary, the content of this specification should not be construed as a limitation to the present invention.
Claims
1. A method for storing blockchain contract data in a sharded manner, characterized in that, Including: A deployment proxy contract, an index contract, and a logic contract; the proxy contract is used to store the business initial time, the time period interval, and the index contract address; the index contract is used to store the mapping relationship between the index number and the data contract address; the logic contract is used to store the proxy contract address. When a business party needs to use a smart contract for business processing, it calls the logic contract. The smart contract includes: a logic contract, a proxy contract, an index contract, and a data contract; the data contract is used to store contract data in slices according to the index number and provide read and write methods for the data; there are several data contract instances in the data contract, and the data contract instances correspond to the index numbers one by one. The logic contract calls the proxy contract and passes the business number and the operation flag to the proxy contract; the business number includes a field carrying business time information. The proxy contract parses the business number to obtain the business time information, and calculates the index number according to the business time information, the business initial time, and the time period interval. The proxy contract requests the data contract address from the index contract according to the index number and the operation flag, and returns it to the logic contract. The logic contract reads and writes data through the data contract according to the data contract address.
2. The blockchain contract data sharding storage method according to claim 1, wherein Deploying the proxy contract, the index contract, and the logic contract specifically includes: The business party deploys the proxy contract and the index contract by sending a transaction, and initializes the business initial time, the time period interval, and the index contract address for the proxy contract through the transaction, and initializes the mapping relationship between the index number and the data contract address for the index contract. The business party deploys the logic contract by means of a transaction and initializes the proxy contract address for the logic contract through the transaction.
3. The blockchain contract data sharding storage method according to claim 1, wherein When a business party needs to use a smart contract for business processing, calling the logic contract specifically includes: When a business party needs to use a smart contract for business processing, it constructs a transaction according to the method parameter rules of the logic contract and signs it, and then sends it to the blockchain to call the logic contract.
4. The blockchain contract data sharding storage method according to claim 1, wherein The proxy contract parses the business number to obtain the business time information, and calculates the index number according to the business time information, the business initial time, and the time period interval, specifically including: The proxy contract parses the business number to obtain the business time information, calculates the difference between the business time information and the business initial time, and takes the integer after dividing the difference by the time period interval to obtain the index number.
5. The blockchain contract data sharding storage method according to claim 1, wherein The proxy contract requests the data contract address from the index contract according to the index number and the operation flag, and returns it to the logic contract, specifically including: The proxy contract requests the index contract according to the index number, so that the index contract queries the data contract according to the mapping relationship. When the index contract fails to query the data contract, determine whether the operation flag is a write operation; if the operation flag is a write operation, create a new data contract instance and update the mapping relationship between the index number and the data contract address; if the operation flag is not a write operation, return an empty data contract address; when the index contract queries the data contract, return the data contract address; The proxy contract returns the data contract address to the logic contract.
6. The blockchain contract data sharding storage method according to claim 1, wherein, In the index contract, the mapping relationship between the index number and the data contract address is stored in the form of a hash table.
7. A blockchain contract data sharding storage system, characterized in that, It includes: A deployment module for deploying the proxy contract, the index contract, and the logic contract; the proxy contract is used to store the business initial time, the time period interval, and the index contract address; the index contract is used to store the mapping relationship between the index number and the data contract address; the logic contract is used to store the proxy contract address; A processing module for: When a business party needs to use a smart contract for business processing, call the logic contract; the smart contract includes: a logic contract, a proxy contract, an index contract, and a data contract; the data contract is used to store contract data in slices according to the index number and provide data reading and writing methods; there are several data contract instances in the data contract, and the data contract instances correspond one by one to the index number; The logic contract calls the proxy contract and passes the business number and the operation flag to the proxy contract; the business number includes a field carrying business time information; The proxy contract parses the business number to obtain the business time information, and calculates the index number according to the business time information, the business initial time, and the time period interval; The proxy contract requests the data contract address from the index contract according to the index number and the operation flag and returns it to the logic contract; The logic contract reads and writes data through the data contract according to the data contract address.
8. An electronic device, characterized in that, It includes a memory and a processor, the memory is used to store a computer program, and the processor runs the computer program to enable the electronic device to execute the blockchain contract data sharding storage method according to any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that, It stores a computer program, and when the computer program is executed by the processor, it implements the blockchain contract data sharding storage method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Block chain query external index development framework
CN112035466A
Data processing method and device based on smart contract
CN113326525A