Smart contract resource consumption metering method and device, electronic equipment and medium

Through the dynamic resource measurement method, the problem of static resource consumption instrumentation in WASM smart contracts is solved, more accurate resource consumption measurement and management and operation cost reduction are achieved, forcing developers to optimize logic to save computer memory resources.

CN118550688APending Publication Date: 2024-08-27INST OF SOFTWARE - CHINESE ACAD OF SCI
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410507923.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-04-25
Publication Date
2024-08-27

AI Technical Summary

Technical Problem

Existing smart contract resource consumption measurement technology has static resource consumption instrumentation problems in WASM smart contracts, resulting in high resource management and operation costs and inaccurate measurement results. It also lacks resource consumption measurement for external codes and cannot force developers to Optimize logic to save computer memory resources.

Method used

Adopt a dynamic resource measurement method based on external code and memory resource consumption mapping table, calculate the resource consumption of the external and internal code of the smart contract through instruction resource consumption measurement rules and memory resource consumption measurement rules, establish a resource consumption mapping table, and dynamically update The total amount of resource consumption is used to determine the execution status, avoid static value instrumentation, and support external code resource consumption measurement.

Benefits of technology

It achieves more accurate smart contract resource consumption measurement, reduces management and operation costs, forces developers to optimize logic to save computer memory resources, and improves resource usage efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118550688A
    Figure CN118550688A
Patent Text Reader

Abstract

The invention provides a smart contract resource consumption metering method and device, electronic equipment and a medium, and relates to the field of block chain smart contracts. The intelligent contract resource consumption metering method provided by the invention comprises the following steps: on the basis of a first resource consumption metering rule corresponding to an external code, performing instruction resource consumption calculation on all first instructions of the external code to obtain a total resource consumption amount of the first instructions; based on the called memory resource consumption mapping table and a second resource consumption metering rule corresponding to the memory page of the smart contract, performing memory resource consumption calculation on the memory page of the smart contract to obtain a total memory resource consumption amount; and determining an execution state of the smart contract based on at least one of the first instruction resource consumption total amount and the memory resource consumption total amount and a preset total resource consumption upper limit value. The resource consumption of the external code of the smart contract can be calculated, the resource consumption metering result of the smart contract can be obtained more accurately, and computer storage resources are saved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of blockchain smart contracts, and in particular to a smart contract resource consumption metering method, device, electronic device and medium. Background Art

[0002] The computer resources used by smart contracts include CPU computing power, memory storage, and disk storage. For a complete smart contract mechanism, there are problems such as the inability to terminate the smart contract program and the vulnerability to denial of service attacks; therefore, Ethereum uses a smart contract resource consumption metering (Gas Metering) method in its smart contract virtual machine (Ethereum Virtual Machine, EVM) to solve the above problems, that is, to accumulate the computer resources consumed during the life cycle of the smart contract, and terminate the execution of the smart contract when the amount of Gas consumed during the execution of the smart contract exceeds a certain threshold. WebAssembly (abbreviated as WASM), as an emerging open virtual machine instruction set standard, provides an underlying instruction set architecture (Instruction Set Architecture, ISA) with a compact format and performance close to that of the native platform. More and more blockchain platforms choose to build their smart contract execution engines based on the WASM instruction set architecture;

[0003] Nowadays, Ethereum WebAssembly (abbreviated as ewasm) is considered to be a better next-generation Ethereum virtual machine technology. Its smart contract resource consumption metering technology solution has been made public and has been used by other blockchain platforms for reference. However, since the metering value inserted into the WASM smart contract code is a static value, when the insertion is completed, the corresponding smart contract must be accumulated according to the resource consumption written during the insertion when it is called later. Therefore, if the WASM smart contract is to be measured according to the latest instruction unit resource consumption, the already inserted WASM smart contract code must be modified, or the original WASM smart contract code must be re-inserted and the old inserted code must be replaced. In addition, there is no resource consumption metering for external code that has a calling relationship with the smart contract, and there is a lack of mechanisms to further force smart contract developers to optimize the implementation of smart contract logic. Summary of the invention

[0004] In order to avoid inserting static values ​​of resource consumption into smart contract codes, reduce the smart contract management and operation costs caused by subsequent updates of instruction unit resource consumption, and obtain more accurate and reasonable WASM smart contract resource consumption measurement results, while achieving the effect of saving computer memory resources, the present application provides a smart contract resource consumption measurement method, device, electronic device and medium.

[0005] This application provides a smart contract resource consumption measurement method, which adopts the following technical solutions:

[0006] A method for measuring resource consumption of a smart contract, comprising:

[0007] Based on the first resource consumption measurement rule corresponding to the external code, the instruction resource consumption of all first instructions of the external code is calculated to obtain the total first instruction resource consumption; wherein the external code is a program code that has a calling relationship with the internal code of the smart contract and is executed by the external execution environment;

[0008] Based on the called memory resource consumption mapping table and the second resource consumption measurement rule corresponding to the memory page of the smart contract, the memory resource consumption of the memory page of the smart contract is calculated to obtain the total memory resource consumption;

[0009] The execution status of the smart contract is determined based on at least one of the total resource consumption of the first instruction, the total memory resource consumption, and a preset upper limit value of the total resource consumption.

[0010] According to some embodiments, the construction of the memory resource consumption mapping table includes:

[0011] Determine multiple thresholds, and determine the unit memory resource consumption corresponding to the multiple thresholds respectively based on a monotonically increasing relationship between the thresholds and the unit memory resource consumption;

[0012] A memory resource consumption mapping table is constructed by using a plurality of thresholds and unit memory resource consumption amounts corresponding to the plurality of thresholds.

[0013] According to some embodiments, the smart contract resource consumption metering method further includes:

[0014] Based on the encoding information of the internal code of the smart contract, the internal instruction resource consumption measurement rule corresponding to the encoding information is called, and the instruction resource consumption of the second instruction included in the internal code of the smart contract is calculated based on the instruction resource consumption measurement rule and the called instruction resource consumption mapping table to obtain the total amount of the second instruction resource consumption;

[0015] The execution status of the smart contract is determined based on at least one of the total resource consumption of the first instruction, the total resource consumption of the second instruction, the total memory resource consumption, and a preset total resource consumption upper limit.

[0016] According to some embodiments, construction of the instruction resource consumption mapping table includes:

[0017] Determine instruction index values ​​and instruction unit resource consumption corresponding to all second instructions respectively;

[0018] A one-to-one mapping relationship is established between the instruction index value and the instruction unit resource consumption corresponding to each second instruction, and an instruction resource consumption mapping table is constructed using the instruction index values ​​and instruction unit resource consumptions for which the one-to-one mapping relationship has been established.

[0019] According to some embodiments, the external code includes a plurality of external codes; and the construction of the first resource consumption metering rule includes:

[0020] Determine a plurality of first instructions corresponding to the plurality of external codes respectively, and determine the unit resource consumption corresponding to the plurality of first instructions respectively; wherein the plurality of first instructions include the first instructions to be processed and input parameters representing the first instructions to be processed;

[0021] The unit resource consumption corresponding to multiple first instructions corresponding to each external code and the unit resource consumption corresponding to the input parameters are summarized to obtain the total resource consumption of the first sub-instructions corresponding to each external code as the first resource consumption measurement rule corresponding to each external code.

[0022] According to some embodiments, the external code includes a plurality of external codes, each of which includes a plurality of first instructions; and performing instruction resource consumption calculation on all first instructions of the external code to obtain a total first instruction resource consumption includes:

[0023] Calculating resource consumption of multiple first instructions of each external code to obtain the total resource consumption of the first sub-instructions corresponding to each external code;

[0024] The total resource consumption of the first sub-instructions corresponding to all external codes is accumulated to a predefined total cumulative resource consumption to obtain the total resource consumption of the first instruction.

[0025] According to some embodiments, the construction of the mapping relationship includes: based on the called memory resource consumption mapping table and the second resource consumption measurement rule corresponding to the memory page of the smart contract, the memory resource consumption of the memory page of the smart contract is calculated to obtain the total memory resource consumption, including:

[0026] Compare the number of memory pages of the smart contract with the threshold value contained in the memory resource consumption mapping table, determine the threshold value that is the same as the memory page, and retrieve the unit memory resource consumption corresponding to the threshold value that is the same as the memory page;

[0027] The second resource consumption measurement rule is called to multiply the unit memory resource consumption by the number of memory pages to obtain the total memory resource consumption.

[0028] This application provides a smart contract resource consumption metering device, which adopts the following technical solution;

[0029] A smart contract resource consumption metering device includes: an instruction resource consumption total amount determination module, a memory resource consumption total amount determination module and a smart contract execution status determination module, wherein:

[0030] An instruction resource consumption total amount determination module, configured to calculate instruction resource consumption for all first instructions of the external code based on the first resource consumption measurement rule corresponding to the external code, and obtain the first instruction resource consumption total amount; wherein the external code is a program code that has a calling relationship with the internal code of the smart contract and is executed by the external execution environment;

[0031] A total memory resource consumption determination module, used to calculate the memory resource consumption of the memory page of the smart contract based on the called memory resource consumption mapping table and the second resource consumption measurement rule corresponding to the memory page of the smart contract to obtain the total memory resource consumption;

[0032] A smart contract execution status determination module is used to determine the execution status of the smart contract based on at least one of the total resource consumption of the first instruction, the total memory resource consumption, and a preset total resource consumption upper limit.

[0033] According to some embodiments, the smart contract resource consumption metering device further includes: a memory resource consumption mapping table construction module, specifically used to:

[0034] Determine a plurality of thresholds, and determine the unit memory resource consumption corresponding to the plurality of thresholds respectively based on a monotonically increasing relationship between the thresholds and the unit memory resource consumption;

[0035] A memory resource consumption mapping table is constructed by using a plurality of thresholds and unit memory resource consumption amounts corresponding to the plurality of thresholds.

[0036] According to some embodiments, the instruction resource consumption total amount determination module is further configured to:

[0037] Based on the encoding information of the internal code of the smart contract, the internal instruction resource consumption measurement rule corresponding to the encoding information is called, and the instruction resource consumption of the second instruction included in the internal code of the smart contract is calculated based on the instruction resource consumption measurement rule and the called instruction resource consumption mapping table to obtain the total amount of the second instruction resource consumption;

[0038] The smart contract execution status determination module is also used for:

[0039] The execution status of the smart contract is determined based on at least one of the total resource consumption of the first instruction, the total resource consumption of the second instruction, the total memory resource consumption, and a preset total resource consumption upper limit.

[0040] According to some embodiments, the smart contract resource consumption metering device further includes: an instruction resource consumption mapping table construction module, specifically used to:

[0041] Determine instruction index values ​​and instruction unit resource consumption corresponding to all second instructions respectively;

[0042] A one-to-one mapping relationship is established between the instruction index value and the instruction unit resource consumption corresponding to each second instruction, and an instruction resource consumption mapping table is constructed using the instruction index values ​​and the instruction unit resource consumption with which the one-to-one mapping relationship has been established.

[0043] According to some embodiments, the external code includes multiple external codes; the smart contract resource consumption metering device also includes: a resource consumption metering rule construction module, which is specifically used to:

[0044] Determine a plurality of first instructions corresponding to the plurality of external codes respectively, and determine the unit resource consumption corresponding to the plurality of first instructions respectively; wherein the plurality of first instructions include a first instruction to be processed and an input parameter representing the first instruction to be processed;

[0045] The unit resource consumption corresponding to multiple first instructions corresponding to each external code and the unit resource consumption corresponding to the input parameters are summarized to obtain the total resource consumption of the first sub-instructions corresponding to each external code as the first resource consumption measurement rule corresponding to each external code.

[0046] According to some embodiments, the external code includes a plurality of external codes, each of which includes a plurality of first instructions; and the instruction resource consumption total amount determination module is further specifically used for:

[0047] Calculating resource consumption of multiple first instructions of each external code to obtain the total resource consumption of the first sub-instructions corresponding to each external code;

[0048] The total resource consumption of the first sub-instructions corresponding to all external codes is accumulated to a predefined total cumulative resource consumption to obtain the total resource consumption of the first instruction.

[0049] According to some embodiments, the total memory resource consumption determination module is further configured to:

[0050] Compare the number of memory pages of the smart contract with the threshold value contained in the memory resource consumption mapping table, determine the threshold value that is the same as the memory page, and retrieve the unit memory resource consumption corresponding to the threshold value that is the same as the memory page;

[0051] The second resource consumption measurement rule is called to multiply the unit memory resource consumption by the number of memory pages to obtain the total memory resource consumption.

[0052] The present application provides an electronic device, which adopts the following technical solution:

[0053] An electronic device, comprising:

[0054] processor;

[0055] The memory stores a computer program, and when the computer program is executed by the processor, the processor executes the smart contract resource consumption metering method.

[0056] The present application provides a computer-readable medium, which adopts the following technical solution:

[0057] A computer-readable medium stores a computer program, which, when executed in a computer, causes the computer to execute a method for measuring resource consumption of a smart contract.

[0058] This application includes the following beneficial technical effects:

[0059] 1. The resource consumption measurement rule for the first instruction that calls external code calculates the resource consumption of the external code that has a calling relationship with the smart contract, so that the resource consumption of the smart contract obtained is more accurate.

[0060] 2. Define the instruction index values ​​corresponding to all second instructions in the internal code of the smart contract. Through the instruction index values, it is avoided to insert static values ​​of resource consumption into the internal code of the smart contract, thereby reducing the smart contract management and operation costs caused by subsequent updates of the instruction unit resource consumption.

[0061] 3. Utilize the called memory resource consumption mapping table and the second resource consumption metering rule to calculate the memory storage resource consumption of the smart contract, further forcing the smart contract developer to optimize the logic implementation of the smart contract, thereby achieving the effect of saving computer memory storage resources. BRIEF DESCRIPTION OF THE DRAWINGS

[0062] Figure 1 It is a block diagram of a method for measuring resource consumption of a smart contract according to an embodiment of the present application;

[0063] Figure 2 It is a schematic diagram of the input and output of the instruction resource consumption metering rule of the embodiment of the present application;

[0064] Figure 3 It is a schematic diagram of the definition of the instruction index value of the embodiment of the present application;

[0065] Figure 4 It is a schematic diagram of the correspondence between external code and instruction resource consumption measurement rules in the embodiment of the present application;

[0066] Figure 5 It is a schematic diagram of the relationship between the unit memory resource consumption and the memory page change in the embodiment of the present application;

[0067] Figure 6 It is a block diagram of a smart contract resource consumption metering device according to an embodiment of the present application;

[0068] Figure 7 It is a schematic diagram of an electronic device according to an embodiment of the present application.

[0069] Explanation of the reference numerals: 601: instruction resource consumption total amount determination module; 602: memory resource consumption total amount determination module; 603: smart contract execution status determination module; 70: electronic device; 701: processor; 702: bus; 703: memory; 704: transceiver. DETAILED DESCRIPTION

[0070] The following is combined with Figure 1-Figure 7 This application is described in further detail.

[0071] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the drawings in the embodiments of the present application are collected below to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0072] An embodiment of the present application provides a smart contract resource consumption metering method, which can be executed by an electronic device, wherein the electronic device can be a server or a terminal device, wherein the server can be an independent physical server, or a server cluster or distributed device composed of multiple physical servers, or a cloud server that provides cloud computing services; the terminal device can be a computer, etc., but is not limited to this. The terminal device and the server can be directly or indirectly connected via wired or wireless communication, and the embodiment of the present application does not limit this.

[0073] Reference Figure 1 A smart contract resource consumption measurement method includes: step S101, step S102 and step S103, wherein:

[0074] S101, based on a first resource consumption measurement rule corresponding to an external code, calculating instruction resource consumption of all first instructions of the external code to obtain a total first instruction resource consumption.

[0075] In some embodiments, all first instructions include virtual instructions and / or machine instructions; the external code is a program code that has a calling relationship with the internal code of the smart contract and is executed by an external execution environment.

[0076] For the external code called in the smart contract, a first resource consumption metering rule corresponding to each external code to be introduced into the smart contract is defined and implemented in its external execution environment, that is, each external code corresponds to a first resource consumption metering rule; a statement for calling the corresponding first resource consumption metering rule is added at the beginning of the implementation code of the external code to be introduced into the smart contract; during the internal code execution process of the smart contract, the electronic device calls the external code and executes the external code in the external execution environment. At this time, the first resource metering rule corresponding to the external code is called through the statement for calling the corresponding first resource consumption metering rule, and the resource consumption corresponding to each first instruction is calculated through the first resource consumption metering rule, so as to obtain the total first instruction resource consumption consumed during the execution of the external code and the first instruction.

[0077] S102, based on the called memory resource consumption mapping table and the second resource consumption metering rule corresponding to the memory page of the smart contract, calculate the memory resource consumption of the memory page of the smart contract to obtain the total memory resource consumption.

[0078] In some embodiments, the electronic device defines and implements a second resource consumption metering rule in the external execution environment of the smart contract, introduces the second resource consumption metering rule into the internal code of the smart contract, and uses a statement that calls the second resource consumption metering rule to replace the memory application instruction in the internal code of the smart contract; the memory page of the smart contract can be one or more; when the electronic device receives a parameter variable representing the number of memory pages that need to be applied this time, it calls the memory resource consumption mapping table constructed in the external execution environment to determine the resource consumption of each memory page, and calls the second resource consumption rule constructed in the external execution environment based on the statement inserted in the internal code of the smart contract to calculate the resource consumption of all memory pages to obtain the total memory resource consumption.

[0079] S103, determining the execution status of the smart contract based on at least one of the total resource consumption of the first instruction, the total memory resource consumption, and a preset upper limit value of the total resource consumption.

[0080] In some embodiments, the electronic device executes the calculation of the total resource consumption of the first instruction and the calculation of the total memory resource consumption one by one according to the instruction sequence order of the internal code of the smart contract; in addition, the electronic device presets the upper limit value of the total resource consumption and the cumulative total resource consumption, and the initial cumulative total resource consumption value is 0.

[0081] When the electronic device preferentially calculates the total resource consumption of the first instruction, the total resource consumption of the first instruction is updated to the cumulative total resource consumption, and the updated cumulative total resource consumption is compared with the preset upper limit of the total resource consumption; whether the cumulative total resource consumption exceeds the upper limit of the total resource consumption is determined, and when the cumulative total resource consumption exceeds the upper limit of the total resource consumption, the execution state of the current smart contract is determined to be the stopped execution state, and the execution of the smart contract is stopped immediately; when the cumulative total resource consumption does not exceed the upper limit of the total resource consumption, the execution state of the current smart contract is determined to be the running state, that is, the execution of the smart contract is not stopped.

[0082] When the electronic device preferentially calculates the total amount of memory resource consumption, it updates the total amount of memory resource consumption to the cumulative total amount of resource consumption, and compares the updated cumulative total amount of resource consumption with the preset upper limit of total resource consumption; it compares the cumulative total amount of resource consumption with the preset upper limit of total resource consumption to determine whether the cumulative total amount of resource consumption exceeds the upper limit of total resource consumption; when the cumulative total amount of resource consumption exceeds the upper limit of total resource consumption, it determines that the execution state of the current smart contract is the stopped execution state, and then stops the execution of the smart contract; when the cumulative total amount of resource consumption does not exceed the upper limit of total resource consumption, it determines that the execution state of the current smart contract is the running state, that is, the execution of the smart contract is not stopped.

[0083] When the electronic device calculates the total resource consumption of the first instruction and the total memory resource consumption one by one based on the instruction sequence of the internal code of the smart contract, the calculation result that is calculated first among the above two calculation results is accumulated and updated to the cumulative total resource consumption, and the updated cumulative total resource consumption is compared with the upper limit of the total resource consumption. If the cumulative total resource consumption does not exceed the upper limit of the total resource consumption, the other calculation result is also accumulated and updated to the cumulative total resource consumption, and the updated cumulative total resource consumption is compared with the upper limit of the total resource consumption; if the cumulative total resource consumption exceeds the upper limit of the total resource consumption , determine that the current execution state of the smart contract is a stopped execution state, and then stop the execution of the smart contract; when the total accumulated resource consumption does not exceed the upper limit of the total resource consumption, determine that the current execution state of the smart contract is a running state, that is, the execution of the smart contract is not stopped; thereby, the resource consumption of the external code of the smart contract is measured by the first resource consumption measurement rule directly corresponding to the external code called by the smart contract, thereby improving the accuracy of the resource consumption measurement of the external code; and use the constructed memory resource consumption mapping table and the second resource consumption measurement rule to calculate the memory resource consumption of the smart contract, thereby saving computer memory storage resources.

[0084] The embodiment of the present application provides a smart contract resource consumption metering method. During the process of executing the internal code of the smart contract by the electronic device, the instruction resource consumption of the external code is calculated based on the first resource consumption metering rule corresponding to the called external code, and the memory resource consumption of the memory page of the smart contract is measured based on the memory resource consumption mapping table constructed by the called external execution environment and the second resource consumption metering rule corresponding to the memory page of the smart contract; then the first instruction resource consumption total amount and the memory resource consumption total amount are updated to the preset cumulative resource consumption total amount, and the updated cumulative resource consumption total amount is compared with the preset total resource consumption upper limit value, and the execution status of the smart contract is determined based on the judgment result; thereby, the resource consumption of the external code of the smart contract is measured by the first resource consumption metering rule directly corresponding to the external code called by the smart contract, so as to improve the accuracy of the resource consumption metering of the external code; and the constructed memory resource consumption mapping table and the second resource consumption metering rule are used to calculate the memory resource consumption of the smart contract, thereby saving computer memory storage resources.

[0085] In some embodiments, while the electronic device measures the resource consumption of external codes that have a calling relationship with the smart contract, it also needs to measure the resource consumption of the internal code of the smart contract; that is, based on the coding information of the internal code of the smart contract, the internal instruction resource consumption metering rules corresponding to the coding information are called, and the instruction resource consumption of the second instruction contained in the internal code of the smart contract is calculated based on the instruction resource consumption metering rules and the called instruction resource consumption mapping table to obtain the total second instruction resource consumption; the execution status of the smart contract is determined based on the first instruction resource consumption total, the second instruction resource consumption total, at least one of the memory resource consumption total, and a preset total resource consumption upper limit.

[0086] In some embodiments, the internal code instruction sequence of the smart contract, that is, the second instruction is divided into multiple logical segments, and each logical segment contains one or more second instructions; all the second instructions of each divided logical segment are taken as a whole, and the index values ​​corresponding to all the second instructions appearing in each logical segment and the number of times each second instruction appears are encoded to obtain corresponding encoding information; the obtained encoding information is used as a parameter at the beginning of the corresponding logical segment, and a statement code representing the call to the instruction resource consumption metering rule is inserted; wherein, each logical segment corresponds to an instruction resource consumption metering rule.

[0087] In some embodiments, during the process of executing the internal code of a smart contract, the electronic device obtains the second instruction corresponding to the logic segment and its corresponding instruction index value based on the encoding information at the beginning of the logic segment each time a logic segment is executed; then the electronic device calls the second instruction resource consumption metering rule corresponding to the logic segment and the instruction resource consumption mapping table; substitutes the instruction index value of the second instruction contained in the logic segment into the instruction resource consumption mapping table to determine the instruction unit resource consumption corresponding to the second instruction; then calls the second instruction resource consumption metering rule corresponding to the logic segment to measure the resource consumption of the second instruction of the logic segment to obtain the total resource consumption of the second sub-instruction of the logic segment; in the case of multiple logic segments contained in the smart contract, the electronic device executes the resource consumption calculation process of multiple logic segments one by one based on the instruction sequence order of the internal code of the smart contract, and each time the total resource consumption of the second sub-instruction of a logic segment is calculated, the calculated total resource consumption of the second sub-instruction corresponding to the logic segment is accumulated and updated to the cumulative resource consumption, and the updated cumulative resource consumption is compared with the upper limit of the total resource consumption.

[0088] In some embodiments, in the process of calculating the total resource consumption of the second sub-instructions of each logic segment, the instruction unit resource consumption of the second instruction in the logic segment is multiplied by the number of times the second instruction appears to obtain the instruction resource consumption corresponding to the second instruction; then, the instruction resource consumption corresponding to all second instructions is accumulated to obtain the total resource consumption of the second sub-instructions corresponding to the logic segment.

[0089] In some embodiments, the calculation of the total amount of resource consumption of the first instruction, the calculation of the total amount of memory resource consumption, and the calculation of the total amount of resource consumption of the second instruction are executed one by one based on the instruction sequence of the internal code of the smart contract, and after obtaining the calculation results of any of the above three, the calculation results are accumulated and updated to the cumulative total amount of resource consumption, and the updated cumulative total amount of resource consumption is compared with the upper limit of the total resource consumption; the execution status of the current smart contract is determined, and whether the smart contract stops running is controlled based on the execution status.

[0090] Reference Figure 2 , discloses a schematic diagram of the input and output of an instruction resource consumption metering rule; the instruction index value, the instruction unit resource consumption and the number of instruction occurrences are input, and the instruction resource consumption metering rule is used to calculate and output the resource consumption metering value.

[0091] In step S101, the construction of the instruction resource consumption mapping table includes: determining the instruction index values ​​and instruction unit resource consumption corresponding to all second instructions respectively; establishing a one-to-one mapping relationship between the instruction index value and the instruction unit resource consumption corresponding to each second instruction, and constructing an instruction resource consumption mapping table based on the instruction index values ​​and instruction unit resource consumption with which the one-to-one mapping relationship has been established.

[0092] In some embodiments, reference Figure 3 , discloses a schematic diagram of an instruction index value definition, wherein a smart contract may include one or more second instructions; an electronic device defines an instruction index value for each second instruction in the smart contract that requires resource consumption measurement and determines the corresponding instruction unit resource consumption; then, based on the second instruction, a one-to-one mapping relationship between the instruction index value and the instruction unit resource consumption of each second instruction is established, and an instruction resource consumption mapping table is established with a one-to-one correspondence between all second instructions that require resource consumption measurement, all instruction index values, and all instruction unit resource consumption.

[0093] In step S101, construction of the first resource consumption metering rule includes: determining multiple first instructions corresponding to multiple external codes respectively, and determining unit resource consumption corresponding to the multiple first instructions respectively; wherein the multiple first instructions include first instructions to be processed and input parameters representing the first instructions to be processed; aggregating the unit resource consumption corresponding to the multiple first instructions corresponding to each external code and the unit resource consumption corresponding to the input parameters, and obtaining the total resource consumption of the first sub-instructions corresponding to each external code as the first resource consumption metering rule corresponding to each external code.

[0094] In some embodiments, reference Figure 4 , Figure 4 The external method includes an external code, and each external method corresponds to an external code; when there are multiple external methods, there are multiple external codes, and each external code corresponds to a method resource consumption metering method, that is, the first resource consumption metering rule; the external code can be a data encryption code, a contract status data storage code, etc.; since there are certain differences between the external codes, such as the type and quantity of the first instruction included; different types of instructions correspond to different instruction unit resource consumption; before the electronic device performs instruction resource consumption metering on the external code, it is necessary to construct the first resource consumption metering rule corresponding to each external code.

[0095] First, the user enters relevant instructions through external entry, and the first instructions corresponding to each external code and the unit resource consumption corresponding to each first instruction are determined based on the relevant instructions, wherein the multiple first instructions include the first instructions to be processed and the input parameters representing the first instructions to be processed; then, the unit resource consumption corresponding to the multiple first instructions corresponding to each external code and the unit resource consumption corresponding to the input parameters are summarized to obtain the total resource consumption of the first sub-instructions corresponding to each external code as the first resource consumption measurement rule corresponding to each external code; thereafter, the electronic device packages the first resource consumption measurement rule and the corresponding external code into a new external code, replaces the original external code and establishes a calling relationship with the smart contract.

[0096] In some embodiments, taking the data encryption code as an example, the external execution environment implements the data encryption code encrypt, and adds a call statement to its corresponding first instruction resource consumption metering rule encrypt_meter_gas at the beginning of its code implementation. The data encryption code encrypt implemented by the external execution environment and its corresponding first instruction resource consumption metering rule encrypt_meter_gas are encapsulated to form a new encrypt_wrap method to replace the original encrypt method as the external code to be introduced into the smart contract.

[0097] In step S101, the external code includes multiple external codes, each external code includes multiple first instructions; instruction resource consumption is calculated for all first instructions of the external code to obtain a total first instruction resource consumption, including: resource consumption is calculated for multiple first instructions of each external code to obtain a total first sub-instruction resource consumption corresponding to each external code; the total first sub-instruction resource consumption corresponding to all external codes is added to a pre-defined cumulative resource consumption total to obtain a total first instruction resource consumption.

[0098] In some embodiments, taking the data encryption code as an example, the data encryption rule definition and the first instruction resource consumption metering rule encrypt_meter_gas are implemented using the following C++ code:

[0099] void encrypt_meter_gas(uint32_t plain_data_size){

[0100] const gas_per_plain_data_byte=42;

[0101] uint64_t gas_amount_call=gas_per_plain_data_byte*

[0102] plain_data_size;

[0103] gas_accumulation+=gas_amount_call;

[0104] if(gas_accumulation>gas_limit){

[0105] throw std::runtime_error("Out Of Gas!");

[0106] }

[0107] }

[0108] In the above implementation code, the instruction unit resource consumption gas_per_plain_data_byte of a single first instruction processed for the data encryption operation is 42, and the total first instruction resource consumption is calculated together with the input parameter plain_data_size representing the size of the data bytes to be processed.

[0109] After that, the defined global variable gas_accumulation representing the total accumulated resource consumption is updated, and finally a check is made to see whether the current total accumulated resource consumption exceeds the upper limit of the total resource consumption. If so, an exception is thrown to terminate the execution of the smart contract.

[0110] In step S102, the construction of the memory resource consumption mapping table includes: determining multiple thresholds, and determining the unit memory resource consumption corresponding to the multiple thresholds based on the monotonically increasing relationship between the thresholds and the unit memory resource consumption; and constructing a memory resource consumption mapping table from the multiple thresholds and the unit memory resource consumption corresponding to the multiple thresholds.

[0111] In some embodiments, the user enters the relevant instructions in an external input manner, and the electronic device sets multiple thresholds based on the relevant instructions, wherein the threshold is an integer, and defines the corresponding unit memory resource consumption for each threshold. Since there is a monotonically increasing relationship between the threshold and the unit memory resource consumption, therefore, referring to Figure 5 As the threshold increases, the corresponding unit memory resource consumption also increases; then a one-to-one mapping relationship is established between multiple thresholds and the corresponding unit memory resource consumption; a memory resource consumption mapping table is constructed by multiple thresholds and the unit memory resource consumption corresponding to the multiple thresholds.

[0112] For example, define the memory page threshold and unit resource consumption mapping table mem_page_threshold_gas_map of the smart contract, set 5 thresholds and their corresponding memory page unit resource consumption values, and define a cumulative number of memory pages. The following C++ code is defined and implemented:

[0113] const int N = 5;

[0114] int thresholds[N]={5,10,20,50,100};

[0115] int gas_per_mem_page[N]={100,150,250,500,1000};

[0116] std::map<int,int> mem_page_threshold_gas_map={

[0117] {thresholds[0],gas_per_mem_page[0]},

[0118] {thresholds[1],gas_per_mem_page[1]},

[0119] {thresholds[2],gas_per_mem_page[2]},

[0120] {thresholds[3],gas_per_mem_page[3]},

[0121] {thresholds[4],gas_per_mem_page[4]},

[0122] };

[0123] int mem_page_accumulation=0;

[0124] The value gas_per_mem_page[i] representing the unit resource consumption of the memory page increases monotonically with the threshold thresholds[i], and the variable mem_page_accumulation represents the cumulative number of memory application pages and is initialized to 0;

[0125] In step S103, based on the called memory resource consumption mapping table and the second resource consumption metering rule corresponding to the memory page of the smart contract, the memory resource consumption of the memory page of the smart contract is calculated to obtain the total memory resource consumption, including: comparing the number of memory pages of the smart contract with the threshold contained in the memory resource consumption mapping table, determining the threshold that is the same as the memory page and retrieving the unit memory resource consumption corresponding to the threshold that is the same as the memory page; calling the second resource consumption metering rule, multiplying the unit memory resource consumption by the number of memory pages to obtain the total memory resource consumption.

[0126] In some embodiments, a second resource consumption metering rule is introduced into the internal code of a smart contract as an externally run code program, and a statement insert that calls the second resource consumption metering rule replaces a memory request instruction in the internal code of the smart contract; during the execution of the internal code of the smart contract, the electronic device calls the second resource consumption metering rule based on the statement inserted by the insert, compares the number of memory pages of the smart contract with the threshold contained in the memory resource consumption mapping table, determines the threshold that is the same as the memory page, and retrieves the unit memory resource consumption corresponding to the threshold; based on the second resource consumption metering rule, multiplies the unit memory resource consumption by the number of memory pages to obtain the total memory resource consumption.

[0127] This application provides a smart contract resource consumption metering device, which adopts the following technical solution;

[0128] Reference Figure 6 , a smart contract resource consumption metering device 60, comprising: an instruction resource consumption total amount determination module 601, a memory resource consumption total amount determination module 602 and a smart contract execution state determination module 603, wherein,

[0129] The instruction resource consumption total amount determination module 601 is used to calculate the instruction resource consumption of all first instructions of the external code based on the first resource consumption measurement rule corresponding to the external code to obtain the first instruction resource consumption total amount; wherein the external code is a program code that has a calling relationship with the internal code of the smart contract and is executed by the external execution environment;

[0130] A total memory resource consumption determination module 602 is used to calculate the memory resource consumption of the memory page of the smart contract based on the called memory resource consumption mapping table and the second resource consumption measurement rule corresponding to the memory page of the smart contract to obtain the total memory resource consumption;

[0131] The smart contract execution status determination module 603 is used to determine the execution status of the smart contract based on at least one of the total resource consumption of the first instruction, the total memory resource consumption, and a preset total resource consumption upper limit.

[0132] According to some embodiments, the smart contract resource consumption metering device 60 further includes: a memory resource consumption mapping table construction module, specifically used to:

[0133] Determine multiple thresholds, and determine the unit memory resource consumption corresponding to the multiple thresholds respectively based on a monotonically increasing relationship between the thresholds and the unit memory resource consumption;

[0134] A memory resource consumption mapping table is constructed by using a plurality of thresholds and unit memory resource consumption amounts corresponding to the plurality of thresholds.

[0135] According to some embodiments, the instruction resource consumption total amount determination module 601 is further configured to:

[0136] Based on the encoding information of the internal code of the smart contract, the internal instruction resource consumption measurement rule corresponding to the encoding information is called, and the instruction resource consumption of the second instruction included in the internal code of the smart contract is calculated based on the instruction resource consumption measurement rule and the called instruction resource consumption mapping table to obtain the total amount of the second instruction resource consumption;

[0137] The smart contract execution status determination module 603 is further specifically used for:

[0138] The execution status of the smart contract is determined based on at least one of the total resource consumption of the first instruction, the total resource consumption of the second instruction, the total memory resource consumption, and a preset total resource consumption upper limit.

[0139] According to some embodiments, the smart contract resource consumption metering device 60 further includes: an instruction resource consumption mapping table construction module, specifically used to:

[0140] Determine instruction index values ​​and instruction unit resource consumption corresponding to all second instructions respectively;

[0141] A one-to-one mapping relationship is established between the instruction index value and the instruction unit resource consumption corresponding to each second instruction, and an instruction resource consumption mapping table is constructed using the instruction index values ​​and the instruction unit resource consumption with which the one-to-one mapping relationship has been established.

[0142] According to some embodiments, the external code includes multiple external codes; the smart contract resource consumption metering device 60 also includes: a resource consumption metering rule construction module, specifically used to:

[0143] Determine a plurality of first instructions corresponding to the plurality of external codes respectively, and determine the unit resource consumption corresponding to the plurality of first instructions respectively; wherein the plurality of first instructions include a first instruction to be processed and an input parameter representing the first instruction to be processed;

[0144] The unit resource consumption corresponding to multiple first instructions corresponding to each external code and the unit resource consumption corresponding to the input parameters are summarized to obtain the total resource consumption of the first sub-instructions corresponding to each external code as the first resource consumption measurement rule corresponding to each external code.

[0145] According to some embodiments, the external code includes multiple external codes, each of which includes multiple first instructions; the instruction resource consumption total amount determination module 601 is further specifically used for:

[0146] Calculating resource consumption of multiple first instructions of each external code to obtain the total resource consumption of the first sub-instructions corresponding to each external code;

[0147] The total resource consumption of the first sub-instructions corresponding to all external codes is accumulated to a predefined total cumulative resource consumption to obtain the total resource consumption of the first instruction.

[0148] According to some embodiments, the total memory resource consumption determination module 602 is further configured to:

[0149] Compare the number of memory pages of the smart contract with the threshold value contained in the memory resource consumption mapping table, determine the threshold value that is the same as the memory page, and retrieve the unit memory resource consumption corresponding to the threshold value that is the same as the memory page;

[0150] The second resource consumption measurement rule is called to multiply the unit memory resource consumption by the number of memory pages to obtain the total memory resource consumption.

[0151] In some embodiments, the instruction resource consumption total amount determination module 601 may include a logic circuit, or may be implemented by a central processing unit, a digital signal processor, or a field programmable gate array included in an electronic device;

[0152] The total memory resource consumption determination module 602 may include a logic circuit, or may be implemented by a central processing unit, a digital signal processor, or a field programmable gate array included in the electronic device;

[0153] The smart contract execution status determination module 603 may include a logic circuit, or may be implemented by a central processing unit, a digital signal processor, or a field programmable gate array included in an electronic device;

[0154] The instruction resource consumption mapping table building module may include a logic circuit, or may be implemented by a central processing unit, a digital signal processor, or a field programmable gate array included in the electronic device;

[0155] The memory resource consumption mapping table building module may include a logic circuit, or may be implemented by a central processing unit, a digital signal processor, or a field programmable gate array included in the electronic device;

[0156] The resource consumption metering rule building module may include a logic circuit, or may be implemented by a central processing unit, a digital signal processor, or a field programmable gate array included in the electronic device.

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

[0158] The embodiment of the present invention also introduces an electronic device from the perspective of a physical device, such as Figure 7 As shown, Figure 7 The electronic device 70 shown includes: a processor 701 and a memory 703. The processor 701 and the memory 703 are connected, such as through a bus 702. Optionally, the electronic device 70 may also include a transceiver 704. It should be noted that in actual applications, the transceiver 704 is not limited to one, and the structure of the electronic device 70 does not constitute a limitation on the embodiments of the present invention.

[0159] Processor 701 may be a CPU (Central Processing Unit), a general purpose processor, a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Gate Array) or other programmable logic devices, transistor logic devices, hardware components or any combination thereof. It may implement or execute various exemplary logic blocks, modules and circuits described in the disclosure of the present invention. Processor 701 may also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, etc.

[0160] The bus 702 may include a path to transmit information between the above components. The bus 702 may be a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus. The bus 702 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 7 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0161] The memory 703 can be a ROM (Read Only Memory) or other types of static storage devices that can store static information and instructions, a RAM (Random Access Memory) or other types of dynamic storage devices that can store information and instructions, or an EEPROM (Electrically Erasable Programmable Read Only Memory), a CD-ROM (Compact Disc Read Only Memory) or other optical disk storage, optical disk storage (including compressed optical disk, laser disk, optical disk, digital versatile disk, Blu-ray disk, etc.), a magnetic disk medium or other magnetic storage device, or any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited to these.

[0162] The memory 703 is used to store application code for executing the solution of the present invention, and the execution is controlled by the processor 701. The processor 701 is used to execute the application code stored in the memory 703 to implement the content shown in the above method embodiment.

[0163] Figure 7 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.

[0164] It should be understood that, although the steps in the flowchart of the accompanying drawings are displayed in sequence as indicated by the arrows, these steps are not necessarily executed in sequence in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least a part of the steps in the flowchart of the accompanying drawings may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and their execution order is not necessarily sequential, but can be executed in turn or alternately with other steps or at least a part of the sub-steps or stages of other steps.

[0165] The above are only some implementation methods of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.

Claims

1. A method for measuring resource consumption of a smart contract, characterized in that: include: Based on the first resource consumption measurement rule corresponding to the external code, the instruction resource consumption of all first instructions of the external code is calculated to obtain the total first instruction resource consumption; wherein the external code is a program code that has a calling relationship with the internal code of the smart contract and is executed by the external execution environment; Based on the called memory resource consumption mapping table and the second resource consumption metering rule corresponding to the memory page of the smart contract, the memory resource consumption of the memory page of the smart contract is calculated to obtain the total memory resource consumption; The execution status of the smart contract is determined based on the total resource consumption of the first instruction, at least one of the total memory resource consumption, and a preset total resource consumption upper limit.

2. The method according to claim 1, characterized in that The construction of the memory resource consumption mapping table includes: Determine a plurality of thresholds, and determine the unit memory resource consumptions corresponding to the plurality of thresholds respectively based on a monotonically increasing relationship between the thresholds and the unit memory resource consumption; The memory resource consumption mapping table is constructed by the multiple thresholds and the unit memory resource consumptions respectively corresponding to the multiple thresholds.

3. The method according to claim 1, characterized in that Also includes: Based on the encoding information of the internal code of the smart contract, call the internal instruction resource consumption measurement rule corresponding to the encoding information, and calculate the instruction resource consumption of the second instruction included in the internal code of the smart contract based on the instruction resource consumption measurement rule and the called instruction resource consumption mapping table to obtain the total amount of second instruction resource consumption; The execution status of the smart contract is determined based on the total resource consumption of the first instruction, the total resource consumption of the second instruction, at least one of the total memory resource consumption, and the preset total resource consumption upper limit.

4. The method according to claim 3, characterized in that The construction of the instruction resource consumption mapping table includes: Determine instruction index values ​​and instruction unit resource consumption corresponding to all second instructions respectively; A one-to-one mapping relationship is established between the instruction index value and the instruction unit resource consumption corresponding to each second instruction, and the instruction resource consumption mapping table is constructed by the instruction index values ​​and the instruction unit resource consumption with which the one-to-one mapping relationship is established.

5. The method according to claim 1, characterized in that The external code comprises a plurality of external codes; The construction of the first resource consumption metering rule includes: Determine a plurality of first instructions corresponding to the plurality of external codes respectively, and determine the unit resource consumption corresponding to the plurality of first instructions respectively; wherein the plurality of first instructions include first instructions to be processed and input parameters representing the first instructions to be processed; The unit resource consumption corresponding to the multiple first instructions corresponding to each external code and the unit resource consumption corresponding to the input parameters are summarized to obtain the total resource consumption of the first sub-instructions corresponding to each external code as the first resource consumption measurement rule corresponding to each external code.

6. The method according to claim 1, characterized in that The external code includes a plurality of external codes, each of which includes a plurality of first instructions; the instruction resource consumption calculation is performed on all the first instructions of the external code to obtain the total first instruction resource consumption, including: Calculating resource consumption of multiple first instructions of each external code to obtain the total resource consumption of the first sub-instructions corresponding to each external code; The total resource consumption of the first sub-instructions corresponding to all external codes is accumulated to a predefined total cumulative resource consumption to obtain the total resource consumption of the first instruction.

7. The method according to any one of claims 1 to 6, characterized in that: The memory resource consumption mapping table based on the call and the second resource consumption metering rule corresponding to the memory page of the smart contract is used to calculate the memory resource consumption of the memory page of the smart contract to obtain the total memory resource consumption, including: Compare the number of memory pages of the smart contract with the threshold value included in the memory resource consumption mapping table, determine the threshold value that is the same as the memory page, and retrieve the unit memory resource consumption corresponding to the threshold value that is the same as the memory page; The second resource consumption measurement rule is called to multiply the unit memory resource consumption by the number of memory pages to obtain the total memory resource consumption.

8. A smart contract resource consumption metering device, characterized in that: include: An instruction resource consumption total amount determination module, configured to calculate instruction resource consumption for all first instructions of the external code based on a first resource consumption measurement rule corresponding to the external code, to obtain a first instruction resource consumption total amount; wherein the external code is a program code that has a calling relationship with the internal code of the smart contract and is executed by an external execution environment; A total memory resource consumption determination module, configured to calculate the memory resource consumption of the memory page of the smart contract based on the called memory resource consumption mapping table and the second resource consumption measurement rule corresponding to the memory page of the smart contract to obtain the total memory resource consumption; A smart contract execution status determination module is used to determine the execution status of the smart contract based on the total resource consumption of the first instruction, at least one of the total memory resource consumption, and a preset total resource consumption upper limit.

9. An electronic device, characterized in that: include: processor; A memory storing a computer program, which, when executed by the processor, enables the processor to execute the smart contract resource consumption metering method as described in any one of claims 1 to 7.

10. A computer readable medium having a computer program stored thereon, characterized in that: When the computer program is executed in a computer, the computer is caused to execute the smart contract resource consumption metering method described in any one of claims 1 to 7.