Intelligent contract charging system and method based on sandbox

By introducing a memory access control unit and a GAS billing unit in the smart contract billing system, the problem of smart contract billing occupies a lot of processor resources in the existing technology is solved, and more efficient smart contract execution and system performance are achieved.

CN119576550APending Publication Date: 2025-03-07BEIJING MICROCHIP EDGE COMPUTING RES INST
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202411637475.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-15
Publication Date
2025-03-07

AI Technical Summary

Technical Problem

The existing technology occupies a lot of processor resources when billing smart contracts, affects contract efficiency and becomes a system bottleneck.

Method used

The sandbox-based smart contract billing system is adopted, and the billing is carried out through the memory access control unit and the GAS billing unit to reduce the use of processor resources. The system includes a memory access control unit, a GAS billing unit and a RISC-V processor core. The memory access control unit collects smart contract instructions and outputs a GAS value to the GAS billing unit. The GAS billing unit superimposes the GAS value of the instruction and outputs an overflow interrupt signal when the total consumption GAS value is greater than the set value.

Benefits of technology

By setting up a memory access control unit and a GAS billing unit outside the processor, the use of processor resources is reduced, the execution efficiency of smart contracts is improved, and the occurrence of system bottlenecks is avoided.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119576550A_ABST
    Figure CN119576550A_ABST
Patent Text Reader

Abstract

The invention discloses a sandbox-based smart contract charging system and method, and the system comprises a memory access control unit which is used for collecting a smart contract instruction executed by an RISC-V processor core and obtaining a corresponding GAS value according to the type of the executed smart contract instruction when monitoring that an RISC-V processor starts to execute a new smart contract again; the RISC-V processor is used for resetting the total consumption GAS value of the GAS charging unit to be 0 by loading a pulse when a new smart contract is started to be executed again; and the GAS charging unit is used for superposing the GAS values of all the instructions corresponding to the obtained smart contract to obtain a total consumption GAS value, and outputting an overflow interrupt signal to the RISC-V processor core if the total consumption GAS value is greater than a preset GAS maximum value. Through the method and the device, occupation of processor resources can be greatly 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 smart contract billing system and method. Background Art

[0002] Specific blockchain applications have the need to charge the computing power and storage resources used by smart contracts. Each operation of a smart contract and each ledger storage will consume certain resources. Common charging methods include charging for each instruction executed by the contract, charging for the instruction storage and stack storage occupied by the contract execution according to the size of the space, and charging for the system time occupied by the contract execution. If the resource consumption exceeds the preset upper limit during the contract execution, the smart contract will be terminated.

[0003] At present, the implementation of smart contract billing usually adopts a software solution: that is, it is implemented by executing the corresponding resource billing management program through the smart contract processor. For example, the publication number CN111738710A discloses "Method and processor for resource deduction for the execution of smart contracts", which introduces a method for resource deduction based on a smart contract-specific processor and custom extended instructions. It adds extended instructions and special registers on the basis of using the basic instruction set and general registers, but the resource billing algorithm is still implemented through multiple extended instructions on the basis of the processor, which requires additional processor resources for contract billing, especially the billing method of billing the instructions executed by the contract one by one. The billing software consumes almost the same number of instructions as the execution of the smart contract itself, which seriously affects the contract efficiency in the process of large-scale contract execution and becomes a system bottleneck. Summary of the invention

[0004] Based on this, in response to the above technical problems, a sandbox-based smart contract billing system and method are provided to solve the problem of excessive processor resources occupied when performing contract billing in the prior art.

[0005] In a first aspect, a sandbox-based smart contract billing system includes: a memory access control unit, a GAS billing unit, and a RISC-V processor core; the memory access control unit is connected to the RISC-V processor core, the GAS billing unit is connected to the memory access control unit, and the GAS billing unit is connected to the GAS register of the RISC-V processor core;

[0006] The memory access control unit is used to collect the smart contract instructions executed by the RISC-V processor core when it is detected that the RISC-V processor starts to re-execute a new smart contract, and obtain the corresponding GAS value according to the type of the executed smart contract instruction and output it to the GAS billing unit;

[0007] The RISC-V processor is used to reset the total consumed GAS value of the GAS billing unit to 0 by loading a pulse when starting to re-execute a new smart contract;

[0008] The GAS billing unit is used to superimpose the GAS values ​​of all instructions corresponding to a smart contract to obtain a total consumed GAS value. If the total consumed GAS value is greater than a preset GAS maximum value, an overflow interrupt signal is output to the RISC-V processor core.

[0009] In the above scheme, optionally, the GAS register includes a GAS enable register, a GAS record register, and a GAS count register.

[0010] In the above scheme, further optionally, the GAS billing unit is turned on and off under the control of the state of the GAS enable register of the RISC-V processor core.

[0011] In the above solution, further optionally, the GAS counting register is used to obtain the total consumed GAS value stored in the GAS billing unit.

[0012] In the above scheme, further optionally, the RISC-V processor core is used to read the total consumed GAS value through the GAS count register.

[0013] In the above solution, optionally, the system also includes a random access memory, which is used to store an associated Gas Table of different types of smart contract instructions and corresponding GAS values.

[0014] In the above solution, further optionally, the Gas Table also includes table addresses corresponding to different types of smart contract instructions.

[0015] In the above scheme, further optionally, the memory access control unit is used to obtain the corresponding GAS value from the random access memory storing the Gas Table according to the smart contract instructions executed by the RISC-V processor core and output it to the GAS billing unit.

[0016] In a second aspect, a sandbox-based smart contract billing method is provided, which uses the sandbox-based smart contract billing system described in the first aspect and is applied to a memory access control unit, the method comprising:

[0017] Step S1: when it is monitored that the RISC-V processor starts to re-execute a new smart contract, the smart contract instructions executed by the RISC-V processor core are collected, the GAS value corresponding to the executed smart contract instructions is obtained and output to the GAS billing unit; when the RISC-V processor starts to re-execute a new smart contract, the RISC-V processor is used to reset the total consumption GAS value of the GAS billing unit to 0;

[0018] Step S2: Control the GAS billing unit to start billing calculation; the GAS billing unit billing calculation includes: superimposing the GAS values ​​of all instructions corresponding to a smart contract to obtain a total consumed GAS value, and if the total consumed GAS value is greater than a set value, outputting an overflow interrupt signal to the RISC-V processor core;

[0019] Repeat step S1 to step S2.

[0020] This application has at least the following beneficial effects:

[0021] This application sets a memory access control unit and a GAS billing unit outside the processor, collects the smart contract instructions executed by the RISC-V processor core through the memory access control unit, obtains the GAS value corresponding to the executed smart contract instructions and outputs it to the GAS billing unit; the GAS billing unit superimposes the GAS values ​​of all instructions corresponding to a smart contract to obtain the total consumption GAS value, and if the total consumption GAS value is greater than the set value, an overflow interrupt signal is output to the RISC-V processor core. Therefore, when the processor performs contract billing, it is performed through the memory access control unit and the GAS billing unit, which greatly reduces the occupation of processor resources. BRIEF DESCRIPTION OF THE DRAWINGS

[0022] Figure 1 A module architecture block diagram of a sandbox-based smart contract billing system provided for one embodiment of the present application;

[0023] Figure 2 This is a circuit diagram of a Gas billing calculation unit in one embodiment of the present application;

[0024] Figure 3 A structural diagram of a GAS billing unit provided for one embodiment of the present application;

[0025] Figure 4 A timing diagram of an internal bus read operation of a sandbox-based smart contract billing system provided by an embodiment of the present application;

[0026] Figure 5 A flowchart of a sandbox-based smart contract billing method provided for one embodiment of the present application. DETAILED DESCRIPTION

[0027] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0028] With the widespread use of blockchain technology in all walks of life, smart contracts are playing an increasingly important role at the application level. This is because blockchain smart contracts have the advantages of transparency, trustworthiness, immutability, and automatic execution, which helps to enhance the control and autonomy of network users in front of algorithms. As an automated script running on the blockchain system, the execution environment of smart contracts needs to have certain security measures. Therefore, people have proposed the concept of smart contract sandbox, which is to provide an execution environment and container for smart contracts, isolate them from other system resources, and manage the loading, execution, resource access, and termination of smart contracts in the container, so as to ensure that the execution of smart contracts can be completed in a controllable and orderly manner under the premise of consuming reasonable system resources. Since smart contracts are general programs written by users, there is a certain degree of uncertainty in their program behavior. If unreasonable or malicious contracts are not subject to any control, once deployed and executed, they may lead to a large consumption or malicious occupation of system resources, reducing the efficiency of the entire blockchain system or even causing a complete collapse. Therefore, the most basic function of the smart contract management sandbox is to provide a container that is effectively isolated from system resources to monitor and manage the loading, execution, and termination of contracts, as well as to restrict the access of contracts to system resources outside the container, so as to ensure the good operation of smart contracts throughout the life cycle.

[0029] In one embodiment, Figure 1 As shown, a sandbox-based smart contract billing system is provided, the system comprising: a memory access control unit, a GAS billing unit, and a RISC-V processor core; the memory access control unit is connected to the RISC-V processor core, the GAS billing unit is connected to the memory access control unit, and the GAS billing unit is connected to the GAS register of the RISC-V processor core;

[0030] The memory access control unit is used to collect the smart contract instructions executed by the RISC-V processor core when it is detected that the RISC-V processor starts to re-execute a new smart contract, and obtain the corresponding GAS value according to the type of the executed smart contract instruction and output it to the GAS billing unit;

[0031] The RISC-V processor is used to reset the total consumed GAS value of the GAS billing unit to 0 by loading a pulse when starting to re-execute a new smart contract;

[0032] The GAS billing unit is used to superimpose the GAS values ​​of all instructions corresponding to a smart contract to obtain a total consumed GAS value. If the total consumed GAS value is greater than a preset GAS maximum value, an overflow interrupt signal is output to the RISC-V processor core.

[0033] Specifically, the RISC-V processor core is mainly responsible for the execution of smart contracts. This application uses a 32-bit lightweight RISC-V processor core, whose memory design samples the Harvard architecture with separate instructions and data. Smart contract transaction data is distributed by the system in advance to the data memory DTCM (Data Tightly Coupled Memory) of the processor core, and the contract code is distributed to the instruction memory ITCM (Instruction Tightly Coupled Memory). Another part of the memory space is used to allocate Gas control and status registers.

[0034] There are two main registers in the Gas register that need to be explained, as shown in Table 1:

[0035] Table 1

[0036] register Access Rights describe Gas Enable Register RW (Readable and writable) Enable / disable Gas billing function Gas Count RO (read only) Store the current Gas value

[0037] In the design, the RISC-V processor core accesses the ITCM, DTCM and GAS registers, as well as the system configuration Gas Table, using the following internal bus interface, and its protocol signals are shown in Table 2:

[0038] Table 2

[0039]

[0040]

[0041] Specifically, in the Gas billing calculation unit, such as Figure 2 As shown in the figure, the system first configures the maximum value of Gas, that is, the maximum value of Gas that can be consumed by a smart contract. After the Gas billing function is turned on, Gas consumption will be performed according to the Gas value read from the Gas Table. The current Gas value is stored in the Gas billing register and can be queried by the RISC-V core. When the Gas is consumed, an interrupt will be sent to the RISC-V core. After receiving the interrupt, RISC-V will record the current contract Gas arrears, stop the execution of the current contract, and start the next contract. Before each contract is executed, the RISC-V core will reset the Gas value in the Gas billing register to the Gas initial value (0) by configuring Gas loading.

[0042] In the above-mentioned sandbox-based smart contract billing system, a memory access control unit and a GAS billing unit are set outside the processor, and the smart contract instructions executed by the RISC-V processor core are collected through the memory access control unit, and the GAS value corresponding to the executed smart contract instructions is obtained and output to the GAS billing unit; the GAS billing unit superimposes the GAS values ​​of all instructions corresponding to a smart contract to obtain the total consumption GAS value, and if the total consumption GAS value is greater than the set value, an overflow interrupt signal is output to the RISC-V processor core. Therefore, when the processor performs contract billing, it is performed through the memory access control unit and the GAS billing unit, which greatly reduces the occupation of processor resources.

[0043] In one embodiment, the GAS registers include a GAS enable register, a GAS record register, and a GAS count register.

[0044] In one embodiment, the GAS billing unit is turned on and off under the control of the state of the GAS enable register of the RISC-V processor core.

[0045] In one embodiment, the GAS counting register is used to obtain the total consumed GAS value stored in the GAS billing unit.

[0046] In one embodiment, the RISC-V processor core is used to read the total consumed GAS value through a GAS count register.

[0047] In one embodiment, the system further includes a random access memory, which is used to store an associated Gas Table of different types of smart contract instructions and corresponding GAS values.

[0048] In one embodiment, the Gas Table also includes table addresses corresponding to different types of smart contract instructions.

[0049] In one embodiment, the memory access control unit is used to obtain the corresponding GAS value from the random access memory storing the Gas Table according to the smart contract instructions executed by the RISC-V processor core and output it to the GAS billing unit.

[0050] In this embodiment, the Gas Table is composed of a single-port SRAM, and the system bus and the Gas request bus access the SRAM after arbitration. The system bus first configures the Gas Table by writing to the SRAM, and writes a Gas value corresponding to a specific instruction in each address. The Gas request reads the corresponding Gas value according to the Gas Table address mapped by the sampled instruction.

[0051] The following Table 3 (Table 3 only describes the functions, the actual situation varies according to different businesses) makes a simple explanation:

[0052] Table 3

[0053] SRAM Address instruction Gas Value 0 ADD 32’h2 1 SUB 32’h3

[0054] The main implementation is to map the RISC-V instruction to a Gas Table address, which stores the Gas value corresponding to the instruction. The reason for this conversion is that the RISC-V instruction is 32-bit binary data, and if it is directly mapped, a 4GB Gas Table is required; and in the instruction, there are immediate values ​​imm, source operand addresses rs1, rs2, and destination operand address rd that are irrelevant to the instruction type; therefore, we make an address mapping to remove redundant information in the instruction, greatly reducing the size of the Gas Table.

[0055] The following Table 4 (Table 4 only uses RISC-V R-type ADD and SUB instructions for functional description, the actual situation varies according to different businesses) provides a simple description:

[0056] Table 4

[0057]

[0058]

[0059] The binary code represents the actual instruction value, and the compressed code represents the instruction type. To calculate the instruction gas, you only need to specify the instruction type, so the compressed code is used to represent the instruction to reduce the size of the table.

[0060] In this protocol, the host (RISC-V processor core) initiates a memory access request to a memory address, and the memory control unit responds by setting the gnt signal. The memory control unit can respond when the host initiates the request, or after several cycles. When the memory control unit responds, it returns the read data and read data valid signals. Figure 4 Timing diagram of an internal bus read operation.

[0061] In one embodiment, a sandbox-based smart contract billing method adopts the above-mentioned sandbox-based smart contract billing system and is applied to a memory access control unit. The method includes:

[0062] Step S1: when it is monitored that the RISC-V processor starts to re-execute a new smart contract, the smart contract instructions executed by the RISC-V processor core are collected, the GAS value corresponding to the executed smart contract instructions is obtained and output to the GAS billing unit; when the RISC-V processor starts to re-execute a new smart contract, the RISC-V processor is used to reset the total consumption GAS value of the GAS billing unit to 0;

[0063] Step S2: Control the GAS billing unit to start billing calculation; the GAS billing unit billing calculation includes: superimposing the GAS values ​​of all instructions corresponding to a smart contract to obtain a total consumed GAS value, and if the total consumed GAS value is greater than a set value, outputting an overflow interrupt signal to the RISC-V processor core;

[0064] Repeat step S1 to step S2.

[0065] In this embodiment, if Figure 5 , the workflow of this application is as follows: first, the top-level system configures the Gas Table through the internal bus, which defines the Gas billing standard; then the top-level system configures the Gas maximum value register, which defines the maximum Gas value that a smart contract can consume; then the RISC-V processor core is started, which first configures the Gas register to implement basic control of the Gas billing function, such as turning on the Gas billing enable, etc.; then the RISC-V processor core starts to execute the smart contract code stored in its instruction memory; the sandbox samples the currently executing smart contract instruction; then the 32-bit instruction is mapped to the 10-bit Gas Table address; the Gas value corresponding to the instruction is read through the address; finally, Gas billing is performed through the Gas billing unit. When the smart contract is executed, the RISC-V processor core reads the current Gas value through the Gas count register; if the smart contract has not been completed but the Gas value has been consumed, the sandbox will generate a Gas overflow interrupt and send it to the RISC-V processor core, and the processor core will immediately stop the execution of the current contract and record the arrears of the contract.

[0066] The advantages of this application are:

[0067] 1. Designed a simple and easy-to-implement smart contract gas billing hardware sandbox implementation solution.

[0068] 2. Through the system configuration Gas Table, users can customize Gas billing rules.

[0069] 3. By establishing a mapping relationship between a set of contract instructions and the Gas Table address, redundant information in the instructions is removed, greatly reducing the hardware resources of the Gas Table.

[0070] The technical features of the above embodiments may be arbitrarily combined. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0071] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.

Claims

1. A sandbox-based smart contract billing system, characterized in that: The system comprises: a memory access control unit, a GAS billing unit, and a RISC-V processor core; the memory access control unit is connected to the RISC-V processor core, the GAS billing unit is connected to the memory access control unit, and the GAS billing unit is connected to the GAS register of the RISC-V processor core; The memory access control unit is used to collect the smart contract instructions executed by the RISC-V processor core when it is detected that the RISC-V processor starts to re-execute a new smart contract, and obtain the corresponding GAS value according to the type of the executed smart contract instruction and output it to the GAS billing unit; The RISC-V processor is used to reset the total consumed GAS value of the GAS billing unit to 0 by loading a pulse when starting to re-execute a new smart contract; The GAS billing unit is used to superimpose the GAS values ​​of all instructions corresponding to a smart contract to obtain a total consumed GAS value. If the total consumed GAS value is greater than a preset GAS maximum value, an overflow interrupt signal is output to the RISC-V processor core.

2. The sandbox-based smart contract billing system according to claim 1, characterized in that: The GAS registers include a GAS enable register, a GAS record register, and a GAS count register.

3. The sandbox-based smart contract billing system according to claim 2, characterized in that: The GAS billing unit is turned on and off and is controlled by the state of the GAS enable register of the RISC-V processor core.

4. The sandbox-based smart contract billing system according to claim 3, characterized in that: The GAS counting register is used to obtain the total consumed GAS value stored in the GAS billing unit.

5. The sandbox-based smart contract billing system according to claim 3, characterized in that: The RISC-V processor core is used to read the total consumed GAS value through the GAS count register.

6. The sandbox-based smart contract billing system according to claim 1, characterized in that: The system also includes a random access memory, which is used to store an associated Gas Table of different types of smart contract instructions and corresponding GAS values.

7. The sandbox-based smart contract billing system according to claim 6, characterized in that: The Gas Table also includes table addresses corresponding to different types of smart contract instructions.

8. The sandbox-based smart contract billing system according to claim 7, characterized in that: The memory access control unit is used to obtain the corresponding GAS value from the random access memory storing the Gas Table according to the smart contract instructions executed by the RISC-V processor core and output it to the GAS billing unit.

9. A sandbox-based smart contract billing method, characterized in that: The sandbox-based smart contract billing system according to any one of claims 1 to 8 is applied to a memory access control unit, and the method comprises: Step S1: when it is monitored that the RISC-V processor starts to re-execute a new smart contract, the smart contract instructions executed by the RISC-V processor core are collected, the GAS value corresponding to the executed smart contract instructions is obtained and output to the GAS billing unit; when the RISC-V processor starts to re-execute a new smart contract, the RISC-V processor is used to reset the total consumption GAS value of the GAS billing unit to 0; Step S2: Control the GAS billing unit to start billing calculation; the GAS billing unit billing calculation includes: superimposing the GAS values ​​of all instructions corresponding to a smart contract to obtain a total consumed GAS value, and if the total consumed GAS value is greater than a set value, outputting an overflow interrupt signal to the RISC-V processor core; Repeat step S1 to step S2.

Citation Information

Patent Citations

  • Method for performing resource deduction on execution of smart contract and processor

    CN111738710A