Smart contract execution method and device

By using PMU to monitor the number of instructions during the execution of smart contracts, the problem of excessive computing resources consumption in the existing technology is solved, effective restrictions and billing of resources are achieved, and the execution efficiency of blockchain nodes is improved.

CN114327695BActive Publication Date: 2025-08-12ANT BLOCKCHAIN TECHNOLOGY (SHANGHAI) CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111646674.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-29
Publication Date
2025-08-12
Estimated Expiration
2041-12-29

AI Technical Summary

Technical Problem

In the process of executing smart contracts, the existing technology is difficult to effectively limit the consumption of computing resources, resulting in excessive use of computing resources from blockchain nodes, affecting the execution of other transactions, and modifying the compiler/interpreter to increase the number of contract instructions and execution time.

Method used

By obtaining the number of instructions allowed to be executed by the smart contract, and using the performance detection unit PMU in the processor to monitor the number of executed instructions, determine whether the number of instructions allowed to be executed is reached, so as to abort or terminate the contract execution and avoid excessive resource consumption.

Benefits of technology

It realizes effective billing and restrictions on smart contract computing resources, without modifying the compiler/interpreter, reducing the number of contract instructions and execution time, and improving the performance of blockchain nodes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114327695B_ABST
    Figure CN114327695B_ABST
Patent Text Reader

Abstract

A method and apparatus for executing a smart contract. The method includes: obtaining a first instruction number allowed to be executed by a to-be-executed smart contract; and during execution of the smart contract, determining, based on a processor PMU, whether a second instruction number of the executed smart contract is equal to the first instruction number.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of this specification belong to the field of blockchain, and more particularly to a method and device for executing a smart contract. Background Art

[0002] Blockchain is a new application model for computer technologies, including distributed data storage, peer-to-peer transmission, consensus mechanisms, and encryption algorithms. Due to its decentralized, tamper-proof, and autonomous nature, blockchain is gaining increasing attention and application. In the Blockchain 2.0 era, smart contracts have emerged, enabling transactions to be executed by invoking user-defined code within smart contracts.

[0003] During the execution of a smart contract, it may be necessary to charge or limit the computing resources consumed. Summary of the Invention

[0004] The purpose of the present invention is to provide a method and device for executing a smart contract.

[0005] In a first aspect, a method for executing a smart contract is provided, comprising: obtaining a first number of instructions allowed to be executed by the smart contract to be executed; during the execution of the smart contract, determining whether the number of instructions of the smart contract that have been executed is equal to the first number of instructions based on a performance monitoring unit PMU in a processor.

[0006] In a second aspect, a smart contract execution device is provided, including: an acquisition unit, configured to obtain a first number of instructions allowed to be executed by the smart contract to be executed; a determination unit, configured to determine whether the number of instructions of the smart contract that has been executed is equal to the first number of instructions based on a performance monitoring unit PMU in a processor during the execution of the smart contract.

[0007] In a third aspect, a computing device is provided, comprising a memory and a processor, wherein the memory stores a computer program / instruction, and when the processor executes the computer program / instruction, the method described in the first aspect is implemented.

[0008] In a fourth aspect, a computer-readable storage medium is provided, on which a computer program / instruction is stored. When the computer program / instruction is executed in a computing device, the computing device executes the method described in the first aspect.

[0009] In the above embodiment, the number of instructions a smart contract is allowed to execute is used as the computing resources allowed to be consumed by the smart contract. During the execution of the smart contract, the processor's PMU can be used to determine whether the number of instructions executed in the smart contract is equal to the number of instructions allowed to be executed. Therefore, if the number of instructions executed in the smart contract reaches the number of instructions allowed to be executed, the execution of the smart contract can be suspended or terminated. This facilitates billing or limiting the computing resources consumed by smart contracts without modifying the compiler / interpreter and without excessively increasing the number of instructions and execution time of the smart contract. BRIEF DESCRIPTION OF THE DRAWINGS

[0010] In order to more clearly illustrate the technical solutions of the embodiments of this specification, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments recorded in this specification. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0011] Figure 1 A timing diagram of a blockchain node executing a smart contract as an example;

[0012] Figure 2 A flowchart of a method for executing a smart contract provided in an embodiment of this specification;

[0013] Figure 3 A flowchart of another smart contract execution method provided in the embodiments of this specification;

[0014] Figure 4 One of the schematic diagrams for determining whether the number of instructions executed by a smart contract is equal to the number of instructions allowed to be executed;

[0015] Figure 5 Diagram 2 for determining whether the number of executed smart contract instructions is equal to the number of instructions allowed to be executed;

[0016] Figure 6 A flowchart of another smart contract execution method provided in the embodiments of this specification;

[0017] Figure 7 This is a schematic diagram of a smart contract execution device provided in an embodiment of this specification. DETAILED DESCRIPTION

[0018] In order to help those skilled in the art better understand the technical solutions in this specification, the following will clearly and completely describe the technical solutions in the embodiments of this specification in conjunction with the drawings in the embodiments of this specification. Obviously, the embodiments described are only some of the embodiments of this specification and not all of them. Based on the embodiments in this specification, other embodiments obtained by ordinary technicians in this field without making any creative efforts should fall within the scope of protection of this specification.

[0019] Smart contracts deployed to a blockchain system can be in bytecode format or written in machine instructions. Bytecode-based smart contracts require an interpreter for execution and cannot be directly executed by the blockchain node's processor. Smart contracts written in machine instructions are referred to as native contracts. They can be generated by statically compiling smart contracts written in a high-level language or dynamically compiling bytecode-based smart contracts, and can be directly executed by the blockchain node's processor.

[0020] The computer program used to build the blockchain system in the blockchain node can be described as the blockchain platform. When the blockchain node executes the smart contract, the control of its processor may be transferred between the blockchain platform and the smart contract. For example, please refer to Figure 1 , the blockchain node starts executing the transaction for calling smart contract A at time t0, then its processor can continue to execute instructions belonging to the blockchain platform in user mode from time t0 to time t2 to complete certain transactions, and start executing smart contract A from time t2; the various instructions belonging to smart contract A executed by the processor from time t2 may include instructions requesting to call the blockchain platform. For example, the processor executes instructions in smart contract A requesting to call the blockchain platform at time t3, then the processor starts executing instructions belonging to the blockchain platform at time t3 to complete the response to the request from smart contract A; when the blockchain platform completes the response to the request from smart contract A at time t4, it may continue to call back smart contract A, then the processor starts executing instructions belonging to smart contract A from time t4 to complete the response to the callback from the blockchain platform; similar to the aforementioned time t2 to time t4, the control of the processor may also be transferred between the blockchain platform and smart contract A at time t5, time t6, and time t7 until the blockchain node completes the execution process of smart contract A. It should be noted that the aforementioned Figure 1The descriptions thereof are only used to assist in describing the technical solutions provided in the embodiments of this specification and do not limit the technical solutions. For example, the blockchain node may complete the execution process of smart contract A at time t3, or the control of the processor may be transferred several times between the blockchain platform and smart contract A after time t7 until the execution process of smart contract A is completed.

[0021] See also Figure 1 As shown in the execution process of Smart Contract A, the execution of a single smart contract can affect the execution of other transactions by blockchain nodes. For example, if a single smart contract contains too many instructions, it will take a long time to execute, occupying too many computing resources and preventing the blockchain node from quickly starting to execute other transactions. Accordingly, in some scenarios, to prevent smart contracts from excessively occupying computing resources, ensure good blockchain system performance, or achieve other goals, it is necessary to charge or limit the computing resources consumed during smart contract execution.

[0022] In one possible implementation, during the generation of a native contract, the computing resources required to be consumed by each basic block contained in the native contract can be defined; then, based on the computing resources required to be consumed by each basic block, corresponding billing instructions are inserted into each basic block to indicate the corresponding amount of computing resources consumed; during the execution of the native contract, based on the billing instructions that have been inserted into each basic block, it can be determined whether the computing resources actually consumed by the native contract have reached the computing resources allowed to be consumed. If so, the execution of the native contract is terminated or suspended. However, the aforementioned method involves modifying the functions of the compiler / interpreter and requires inserting corresponding billing instructions into each basic block contained in the native contract, which will significantly increase the number of instructions in the native contract, thereby increasing the execution time of the smart contract itself.

[0023] The embodiments of this specification at least provide a method and device for executing a smart contract. The number of instructions that a smart contract is allowed to execute is used as the computing resources allowed to be consumed by the smart contract. During the execution of the smart contract, it can be determined based on the PMU in the processor whether the number of instructions of the executed smart contract is equal to the number of instructions allowed to be executed. Thus, when the number of instructions of the executed smart contract reaches the number of instructions allowed to be executed, the execution of the smart contract can be suspended or terminated. This is conducive to billing or limiting the computing resources consumed by the smart contract without modifying the compiler / interpreter, and will not excessively increase the number of instructions and execution time of the smart contract.

[0024] Figure 2This is a flow chart of a method for executing a smart contract provided in an embodiment of this specification. The method can be executed by a blockchain node, and the method may at least include the following: Figure 2 The following steps 21 and 23 are shown.

[0025] Step 21: Obtain the number of instructions M1 that the smart contract to be executed is allowed to execute.

[0026] The number of instructions M1 that the smart contract is allowed to execute can be pre-declared in the smart contract; accordingly, in step 201, the number of instructions M1 that is allowed to execute can be obtained from the smart contract to be executed.

[0027] Step 23: During the execution of the smart contract, determine based on the PMU in the processor whether the number of instructions of the executed smart contract is equal to the number of instructions M1 allowed to be executed.

[0028] The PMU is a hardware module in the processor that can monitor events when the processor executes certain instructions and can trigger the processor to generate an interrupt under specific conditions. The following describes in detail how to use the above two functions of the PMU to implement the above step 23.

[0029] In the first case, the PMU is set to monitor events when the processor executes instructions in user mode. In this case, please refer to Figure 3 Before the aforementioned step 23, the method may further include the following steps 221 and 223.

[0030] Step 221: Before executing the smart contract, set a first number in a first register corresponding to the PMU, so that after the processor executes the first number of instructions in user mode, an interrupt is generated under the triggering of the PMU.

[0031] The first number is determined based on the number of smart contract instructions M1 and the preset number delta, for example, the first number is greater than 0 and not greater than the sum of the number of instructions M1 and the preset number delta; wherein the preset number delta is the minimum number of instructions of the blockchain platform executed by the processor after setting the first number. For example, please refer to Figure 1 , the blockchain node sets the first quantity at time t1 between time t0 and time t2, and the preset quantity delta is the number of instructions of the blockchain platform executed by the blockchain node between time t1 and time t2.

[0032] For the PMU, when a non-zero value is written into its corresponding first register, for example, a first number is set in the first register, each time the PMU detects that the processor executes an instruction in user mode, it can perform a subtraction operation on the value stored in the first register until the value stored in the first register is 0, triggering the processor to generate an interrupt.

[0033] Step 223: When control of the processor is transferred between the smart contract and the blockchain platform, a first cumulative number of instructions executed by the processor in user mode is obtained, and a statistical number of first-category instructions executed by the processor after the first number is set or the execution of the smart contract is started is determined based on the first cumulative number.

[0034] The PMU can record the cumulative number of instructions executed by the processor in user mode based on the event of the processor executing instructions in user mode that it monitors, for example, by storing the cumulative number in its corresponding second register. For example, after the processor is restarted or the processor generates an interrupt triggered by the PMU, an initial value of 0 can be set in the second register; each time the PMU monitors that the processor executes an instruction in user mode, the PMU can perform an increment operation on the value stored in the second register, so that the value stored in the second register can be used to represent the cumulative number of instructions executed by the processor in user mode. Accordingly, in step 224, the first cumulative number of instructions executed by the processor in user mode can be read from the second register corresponding to the PMU.

[0035] The first category of instructions may include instructions pertaining to smart contracts or instructions pertaining to blockchain platforms. Regarding the timing for determining the aforementioned statistical quantities, if the first category of instructions includes instructions pertaining to blockchain platforms, the aforementioned statistical quantities may be determined specifically when the blockchain platform transfers control of the processor to the smart contract; if the first category of instructions includes instructions pertaining to smart contracts, the aforementioned statistical quantities may be determined specifically when the blockchain platform obtains control of the processor from the smart contract.

[0036] In step 223, the statistical number of first-category instructions executed by the processor after the first quantity is set or the smart contract begins executing can be accumulated based on the first cumulative quantities obtained twice in succession. For example, assuming that the first cumulative quantities counter2 to counter7 are obtained at times t2 to t7 in the aforementioned example, if the first-category instructions include instructions belonging to the blockchain platform, the statistical number of first-category instructions executed by the processor after the smart contract begins executing can be determined at time t4 to be counter4 - counter3, and at time t6, the statistical number can be updated to counter4 - counter3 + counter6 - counter5. Alternatively, the number of instructions belonging to the blockchain platform executed between times t0 and t2 can be recorded as delta, and the statistical number of first-category instructions executed by the processor after the first quantity is set can be determined at time t4 to be delta + counter4 - counter3, and at time t6, the statistical number can be updated to delta + counter4 - counter3 + counter6 - counter5. If the first category of instructions includes instructions belonging to a smart contract, the statistical number of the first category of instructions executed by the processor after setting the first number or starting to execute the smart contract can be determined through a process similar to the above, which is not repeated here.

[0037] The aforementioned steps 21, 221, and 223 can all be executed through the blockchain platform. When the processor generates an interrupt under the triggering of the PMU, other method steps including step 23 can be executed through an interrupt handler independent of the blockchain platform.

[0038] To facilitate the subsequent implementation of other method steps, including step 23, by the interrupt handler, if the first category of instructions includes instructions belonging to the blockchain platform, each time the blockchain platform obtains control of the processor, it may pass its newly acquired first cumulative quantity to the interrupt handler, and each time the blockchain platform transfers control of the processor to the smart contract, it may pass its newly determined statistical quantity to the interrupt handler. Similarly, if the first category of instructions includes instructions belonging to the smart contract, each time the blockchain platform obtains control of the processor, it may pass its newly determined statistical quantity to the interrupt handler, and each time the blockchain platform transfers control of the processor to the smart contract, it may pass its newly acquired first cumulative quantity to the interrupt handler. For example, if the first category of instructions includes instructions belonging to the blockchain platform, when the blockchain platform obtains control of the processor at time t3, it may update counter3 to the variable start_counter shared with the interrupt handler; and when the blockchain platform transfers control of the processor to the smart contract at time t4, it may update the statistical quantity delta + counter4 - counter3 or counter4 - counter3 to the variable acc_counter shared with the interrupt handler. Similarly, if the first category of instructions includes instructions belonging to a smart contract, the variables start_counter and acc_counter can be assigned values through a similar process, which is not repeated here.

[0039] Based on the aforementioned steps 221 and 223, step 23 may include step 231: when the processor generates an interrupt under the triggering of the PMU, it is determined based on the statistical quantity whether the number of instructions of the executed smart contract is equal to the number of instructions M1 allowed to be executed.

[0040] If the first category of instructions includes instructions belonging to the blockchain platform, please refer to Figure 4 , the aforementioned step 231 may specifically include the following steps 2311 to 2315.

[0041] In step 2311, when the processor generates an interrupt triggered by the PMU, the processor determines whether the instruction currently executed by the processor belongs to the blockchain platform. For example, the processor can determine whether the currently executed instruction belongs to the blockchain platform based on the interrupt scene.

[0042] If so, execute the following steps 2312, 2313 and 2315; otherwise, execute steps 2314 and 2315.

[0043] Step 2312: Obtain a second cumulative number of instructions executed by the processor in user mode, and calculate the number of instructions M2 executed on the blockchain platform based on the first cumulative number, the second cumulative number, and the statistical number. The method for obtaining the first cumulative number is the same as that for obtaining the first cumulative number. Specifically, the second cumulative number of instructions executed by the processor in user mode can be read from the second register corresponding to the PMU. Furthermore, the first cumulative number described in step 2312 refers to the most recently obtained first cumulative number, such as the value of the aforementioned variable start_counter, and the statistical number refers to the most recently determined statistical number, such as the value of the aforementioned variable acc_counter. The number of instructions M2 executed on the blockchain platform can be obtained by subtracting the second cumulative number from the value of start_counter and summing the obtained difference with the value of acc_counter.

[0044] Step 2313 determines the number of instructions of the executed smart contract based on the number of instructions M2 of the executed blockchain platform and the number of instructions M3 executed by the processor in user mode after setting the first number or starting execution of the smart contract. Specifically, when setting the first number or starting execution of the smart contract, the cumulative number of instructions executed by the processor in user mode can be obtained and recorded, and the number of instructions M3 can be obtained by subtracting the second cumulative number from the cumulative number. Alternatively, during the execution of the smart contract, if the processor generates an interrupt for the first time triggered by the PMU, the number of instructions M1 allowed to execute the smart contract can be used as the number of instructions M3. If the processor does not generate an interrupt for the first time triggered by the PMU, the number of instructions M1 allowed to execute the smart contract and other values set to the first register during the execution of the smart contract (e.g., the second number described below) can be summed to obtain the number of instructions M3.

[0045] In step 2314, the number of instructions of the executed smart contract is determined based on the statistical number and the number of instructions M3 executed by the processor in user mode after setting the first number or starting smart contract execution. The statistical number mentioned in step 2314 is the most recently determined statistical number, such as the value of the variable acc_counter. The number of instructions of the executed smart contract can be obtained by subtracting the value of M3 from the value of acc_counter.

[0046] Step 2315: Determine whether the number of instructions of the executed smart contract is equal to the number of instructions M1 allowed to be executed.

[0047] If the first category of instructions includes instructions belonging to smart contracts, please refer to Figure 5 , step 231 may specifically include the following steps 2316 to 2318.

[0048] In step 2316, when the processor generates an interrupt triggered by the PMU, the processor determines whether the instruction currently executed by the processor belongs to the smart contract. For example, the processor can determine whether the currently executed instruction belongs to the smart contract based on the interrupt scene.

[0049] If so, execute steps 2317 and 2319 below; otherwise, execute steps 2318 and 2319.

[0050] Step 2317: Obtain a third cumulative number of instructions executed by the processor in user mode, and determine the number of instructions of the executed smart contract based on the third cumulative number, the first cumulative number, and the statistical number. The first cumulative number in step 2317 refers to the most recently obtained first cumulative number, such as the value of the aforementioned variable start_counter, and the statistical number refers to the most recently determined statistical number, such as the value of the aforementioned variable acc_counter. The number of instructions of the executed smart contract can be obtained by subtracting the third cumulative number from the value of start_counter and summing the obtained difference with the value of acc_counter.

[0051] In step 2318, the statistical quantity is determined to be the number of instructions of the executed smart contract. The statistical quantity in step 2318 refers to the most recently determined statistical quantity, such as the value of the aforementioned variable acc_counter.

[0052] Step 2319: Determine whether the number of instructions of the executed smart contract is equal to the number of instructions M1 allowed to be executed.

[0053] When the number of executed smart contract instructions is equal to the instruction number M1, please refer to Figure 3 After step 231, step 251 may be executed to terminate the execution of the smart contract. It should be noted that after the execution of the smart contract is terminated, certain predetermined transactions may continue to be executed, such as generating an execution result indicating that the transaction requesting the invocation of the smart contract has failed.

[0054] When the number of executed smart contract instructions is not equal to the instruction number M1, please refer to Figure 3 After step 231, the following steps 253 and 255 may be performed.

[0055] Step 253: Set a second number in a first register corresponding to the PMU, so that after the processor executes the second number of instructions in user mode, an interrupt is generated upon triggering by the PMU. The second number is calculated based on the number of instructions in the executed smart contract and the number of instructions allowed to execute, M1. For example, the second number is the difference between the number of instructions M1 and the number of instructions in the executed smart contract.

[0056] Step 255: Continuing to execute the smart contract based on the processor's interruption context. As previously mentioned, when the processor is interrupted by the PMU, the instruction currently being executed by the processor may belong to the blockchain platform or the smart contract. Therefore, continuing to execute the smart contract based on the interrupt context essentially means continuing to execute the instruction following the instruction that was being executed when the processor was interrupted. This instruction may belong to the smart contract itself or to the blockchain platform.

[0057] In the second case, the PMU is set up to monitor events where the processor executes instructions belonging to a smart contract. In this case, please refer to Figure 6 Before step 23, the process may further include step 225, where, before executing the smart contract, the number of instructions allowed to execute, M1, is set in the first register corresponding to the PMU, so that when the number of instructions in the executed smart contract equals the number of instructions allowed to execute, M1, the processor generates an interrupt triggered by the PMU. Accordingly, step 23 may include step 233, where, after the processor generates an interrupt triggered by the PMU, the processor determines that the number of instructions in the executed smart contract equals the number of instructions allowed to execute, M1.

[0058] In a more specific example, a processor has a contract execution mode distinct from user mode and kernel mode, and executes instructions belonging to a smart contract in contract execution mode. The PMU is configured to monitor events related to the processor executing instructions while in contract execution mode, specifically, to monitor the number of executed smart contract instructions based on the processor's execution of the instructions. Accordingly, the method may further include: triggering the processor to enter contract execution mode when control of the processor is transferred from the blockchain platform to the smart contract; and triggering the processor to exit contract execution mode when control of the processor is transferred from the smart contract to the blockchain platform. The blockchain platform may trigger the processor to enter or exit contract execution mode.

[0059] In a more specific example, the processor supports pausing and resuming the PMU mechanism in user mode, meaning the PMU has multiple operating states, including a running state. The PMU is configured to monitor events related to the processor executing instructions while in the running state, thereby enabling monitoring of the number of instructions executed in a smart contract based on the processor's execution of instructions. Monitoring of events related to the processor executing instructions is not required when the processor is not in the running state (e.g., in a paused state corresponding to the running state). Accordingly, the method may further include: triggering the PMU to enter the running state when control of the processor is transferred from the blockchain platform to the smart contract; and triggering the PMU to exit the running state when control of the processor is transferred from the smart contract to the blockchain platform. Triggering the PMU to enter or exit the running state can be implemented by the blockchain platform.

[0060] Based on the same concept as the above method embodiment, this specification also provides an execution device for a smart contract. Figure 7 As shown, the device includes: an acquisition unit 71, configured to obtain a first number of instructions that the smart contract to be executed is allowed to execute; a determination unit 73, configured to determine whether the number of instructions of the smart contract that has been executed is equal to the first number of instructions based on a performance monitoring unit PMU in a processor during the execution of the smart contract.

[0061] In one possible embodiment, the device further includes: a setting unit, configured to set a first number in a first register corresponding to the PMU before executing the smart contract, so that the processor generates an interrupt under the trigger of the PMU after executing the first number of instructions in the user state, wherein the first number is determined based on the first number of instructions and a preset number, and the preset number is the minimum number of instructions of the blockchain platform executed by the processor after setting the first number; a statistical unit, configured to obtain a first cumulative number of instructions executed by the processor in the user state when the control of the processor is transferred between the smart contract and the blockchain platform, and determine the statistical number of first-category instructions executed by the processor after setting the first number or starting to execute the smart contract based on the first cumulative number, wherein the first-category instructions include instructions belonging to the smart contract or instructions belonging to the blockchain platform; the determining unit 73 is configured to determine whether the second number of instructions of the executed smart contract is equal to the first number of instructions based on the statistical number after the processor generates an interrupt under the trigger of the PMU.

[0062] In one possible implementation, the first category of instructions includes instructions belonging to the blockchain platform; the determination unit is specifically configured to determine the number of instructions of the executed smart contract based on the statistical number and the second number of instructions executed by the processor in user mode after setting the first number or starting to execute the smart contract when the instruction currently executed by the processor does not belong to the blockchain platform.

[0063] In one possible implementation, the determination unit 73 is further configured to, when the instruction currently executed by the processor belongs to the blockchain platform, obtain a second cumulative number of instructions executed by the processor in user mode, and calculate a third number of instructions of the blockchain platform that have been executed based on the first cumulative number, the second cumulative number, and the statistical number; and determine the number of instructions of the smart contract that have been executed based on the second number of instructions and the third number of instructions.

[0064] In one possible implementation, the first category of instructions includes instructions belonging to the smart contract; the determination unit 73 is specifically configured to determine the statistical number as the second number of instructions of the smart contract that have been executed when the instruction currently executed by the processor does not belong to the smart contract.

[0065] In one possible implementation, the determination unit 73 is further configured to obtain a third cumulative number of instructions executed by the processor in user mode when the instruction currently executed by the processor belongs to the smart contract, and determine the second number of instructions of the executed smart contract based on the third cumulative number, the first cumulative number and the statistical number.

[0066] In a possible implementation, the device further includes: a control unit configured to terminate execution of the smart contract when the number of executed instructions of the smart contract is equal to the first number of instructions.

[0067] In one possible implementation, the device further includes: a control unit, configured to, when the second number of instructions of the executed smart contract is not equal to the first number of instructions, set a second number in a first register corresponding to the PMU, so that after the processor executes the second number of instructions in user mode, an interrupt is generated under the triggering of the PMU, wherein the second number is calculated based on the number of instructions of the executed smart contract and the first number of instructions; and continue to execute the smart contract according to the interrupt scene of the processor.

[0068] In one possible implementation, the device further includes: a setting unit configured to set the first instruction number in a first register corresponding to the PMU before executing the smart contract, so that the processor generates an interrupt under the triggering of the PMU after the second instruction number of the executed smart contract is equal to the first instruction number; the determining unit 73 is configured to determine that the second instruction number of the executed smart contract is equal to the first instruction number when the processor generates an interrupt under the triggering of the PMU.

[0069] In one possible implementation, the PMU is configured to monitor the number of instructions of the smart contract that have been executed based on the execution of instructions by the processor when the processor is in the contract execution mode; the device also includes: a mode switching unit, configured to trigger the processor to enter the contract execution mode when the control of the processor is transferred from the blockchain platform to the smart contract; and to trigger the processor to exit the contract execution mode when the control of the processor is transferred from the smart contract to the blockchain platform.

[0070] In one possible implementation, the PMU is configured to monitor the number of instructions of the smart contract that have been executed based on the execution of instructions by the processor when it is in a running state; the device also includes: a state switching unit, configured to trigger the PMU to enter a running state when control of the processor is transferred from the blockchain platform to the smart contract; and to trigger the PMU to exit the running state when control of the processor is transferred from the smart contract to the blockchain platform.

[0071] Although one or more embodiments of this specification provide method operation steps as described in the embodiments or flow charts, more or fewer operation steps may be included based on conventional or non-creative means. The order of steps listed in the embodiments is only one way of executing the order of many steps and does not represent the only execution order. When the device or terminal product in practice is executed, it can be executed in sequence or in parallel according to the method shown in the embodiments or the drawings (for example, a parallel processor or a multi-threaded processing environment, or even a distributed data processing environment). The term "comprise", "include" or any other variant thereof is intended to cover non-exclusive inclusion, so that the process, method, product or equipment including a series of elements includes not only those elements, but also includes other elements that are not clearly listed, or also includes elements inherent to such process, method, product or equipment. In the absence of more restrictions, it is not excluded that there are other identical or equivalent elements in the process, method, product or equipment including the elements. For example, if the words first, second, etc. are used to represent the name, they do not represent any particular order.

[0072] The present invention is described with reference to flowcharts and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowcharts and / or block diagrams, as well as combinations of processes and / or blocks in the flowcharts and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowcharts and / or block diagrams. Figure 1 a process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0073] These computer program instructions may also be stored in a computer readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 a process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.

[0074] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing the instructions executed on the computer or other programmable device for implementing the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.

[0075] In a typical configuration, a computing device includes one or more processors (CPUs), input / output interfaces, network interfaces, and memory.

[0076] Memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. Memory is an example of a computer-readable medium.

[0077] Computer-readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, magnetic tape, magnetic disk storage, graphene storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include transitory media such as modulated data signals and carrier waves.

[0078] Those skilled in the art will appreciate that one or more embodiments of this specification may be provided as a method, system, or computer program product. Thus, one or more embodiments of this specification may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware. Furthermore, one or more embodiments of this specification may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0079] One or more embodiments of this specification may be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, and the like that perform specific tasks or implement specific abstract data types. One or more embodiments of this specification may also be practiced in distributed computing environments where tasks are performed by remote processing devices connected via a communications network. In distributed computing environments, program modules may be located in local and remote computer storage media, including storage devices.

[0080] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between the various embodiments can be referenced across them. Each embodiment focuses on the differences from the other embodiments. In particular, since the system embodiments are generally similar to the method embodiments, their description is relatively simple. For relevant parts, reference can be made to the description of the method embodiments. Throughout this specification, reference to the terms "one embodiment," "some embodiments," "examples," "specific examples," or "some examples" means that the specific features, structures, materials, or characteristics described in conjunction with that embodiment or example are included in at least one embodiment or example of this specification. In this specification, the schematic representations of these terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in any one or more embodiments or examples. Furthermore, those skilled in the art may combine and integrate the different embodiments or examples, and features of different embodiments or examples, described in this specification, without conflict.

[0081] The foregoing is merely an example of one or more embodiments of this specification and is not intended to limit the one or more embodiments of this specification. It will be apparent to those skilled in the art that various modifications and variations may be made to one or more embodiments of this specification. Any modifications, equivalent substitutions, or improvements made within the spirit and principles of this specification shall be included within the scope of the claims.

Claims

1. A method for executing a smart contract, comprising: Get the first instruction number that the smart contract to be executed is allowed to execute; Before executing the smart contract, a first number is set in a first register corresponding to a performance monitoring unit (PMU) in the processor, so that after the processor executes the first number of instructions in user mode, an interrupt is generated under the triggering of the PMU, wherein the first number is determined based on the first number of instructions and a preset number, and the preset number is the minimum number of instructions of the blockchain platform executed by the processor after the first number is set; When control of the processor is transferred between the smart contract and the blockchain platform, obtaining a first cumulative number of instructions executed by the processor in user mode, and determining, based on the first cumulative number, a statistical number of first-category instructions executed by the processor after setting a first number or starting to execute the smart contract, wherein the first-category instructions include instructions belonging to the smart contract or instructions belonging to the blockchain platform; During the execution of the smart contract, when the processor generates an interrupt under the triggering of the PMU, it is determined based on the statistical quantity whether the number of second instructions of the executed smart contract is equal to the first number of instructions.

2. The method according to claim 1, wherein The first category of instructions includes instructions belonging to the blockchain platform; The determining, based on the statistical number, whether the second number of instructions of the executed smart contract is equal to the first number of instructions includes: when the instruction currently executed by the processor does not belong to the blockchain platform, determining the number of instructions of the executed smart contract based on the statistical number and the second number of instructions executed by the processor in user mode after setting the first number or starting to execute the smart contract.

3. The method according to claim 2, wherein: The determining, based on the statistical number, whether the number of instructions of the smart contract that have been executed is equal to the first number of instructions also includes: when the instruction currently executed by the processor belongs to the blockchain platform, obtaining a second cumulative number of instructions executed by the processor in user mode, and calculating a third number of instructions of the blockchain platform that have been executed based on the first cumulative number, the second cumulative number and the statistical number; and determining the number of instructions of the smart contract that have been executed based on the second number of instructions and the third number of instructions.

4. The method according to claim 1, wherein The first category of instructions includes instructions belonging to the smart contract; Determining whether the number of instructions of the smart contract that have been executed is equal to the first number of instructions based on the statistical number includes: when the instruction currently executed by the processor does not belong to the smart contract, determining the statistical number as the second number of instructions of the smart contract that have been executed.

5. The method according to claim 4, wherein The determining whether the number of instructions of the smart contract that have been executed is equal to the first number of instructions based on the statistical number also includes: when the instruction currently executed by the processor belongs to the smart contract, obtaining a third cumulative number of instructions executed by the processor in user mode, and determining the second number of instructions of the smart contract that have been executed based on the third cumulative number, the first cumulative number and the statistical number.

6. The method according to any one of claims 1 to 5, further comprising: When the number of executed instructions of the smart contract is equal to the first number of instructions, the execution of the smart contract is terminated.

7. The method according to any one of claims 1 to 5, further comprising: When the number of second instructions of the executed smart contract is not equal to the first number of instructions, a second number is set in a first register corresponding to the PMU, so that after the processor executes the second number of instructions in user mode, an interrupt is generated under the triggering of the PMU, wherein the second number is calculated based on the number of instructions of the executed smart contract and the first number of instructions; Continuing to execute the smart contract according to the interruption scene of the processor.

8. A smart contract execution device, comprising: an acquiring unit, configured to acquire a first number of instructions that the smart contract to be executed is allowed to execute; A setting unit is configured to, before executing the smart contract, set a first number in a first register corresponding to a performance monitoring unit (PMU) in the processor, so that after the processor executes the first number of instructions in user mode, an interrupt is generated under the triggering of the PMU, wherein the first number is determined based on the first number of instructions and a preset number, and the preset number is the minimum number of instructions of the blockchain platform executed by the processor after the first number is set; a counting unit configured to, when control of the processor is transferred between the smart contract and the blockchain platform, obtain a first cumulative number of instructions executed by the processor in user mode, and determine, based on the first cumulative number, a statistical number of first-category instructions executed by the processor after setting a first number or starting execution of the smart contract, wherein the first-category instructions include instructions belonging to the smart contract or instructions belonging to the blockchain platform; The determination unit is configured to determine, during the execution of the smart contract, when the processor generates an interrupt triggered by the PMU, whether the number of second instructions of the executed smart contract is equal to the first number of instructions based on the statistical number.

9. The device according to claim 8, wherein The first category of instructions includes instructions belonging to the blockchain platform; the determination unit is specifically configured to determine the number of instructions of the executed smart contract based on the statistical number and the second number of instructions executed by the processor in user mode after setting the first number or starting to execute the smart contract when the instruction currently executed by the processor does not belong to the blockchain platform.

10. The device according to claim 9, wherein The determining unit is further configured to, when the instruction currently executed by the processor belongs to the blockchain platform, obtain a second cumulative number of instructions executed by the processor in the user state, and calculate a third number of instructions of the blockchain platform that have been executed based on the first cumulative number, the second cumulative number, and the statistical number; Determine the number of instructions of the smart contract that have been executed based on the second instruction number and the third instruction number.

11. The device according to claim 8, wherein The first category of instructions includes instructions belonging to the smart contract; the determination unit is specifically configured to determine the statistical number as the second number of instructions of the smart contract that have been executed when the instruction currently executed by the processor does not belong to the smart contract.

12. The device according to claim 11, wherein The determination unit is further configured to obtain a third cumulative number of instructions executed by the processor in user mode when the instruction currently executed by the processor belongs to the smart contract, and determine the second number of instructions of the executed smart contract based on the third cumulative number, the first cumulative number and the statistical number.

13. The apparatus according to any one of claims 8 to 12, further comprising: The control unit is configured to terminate the execution of the smart contract when the number of instructions of the smart contract that have been executed is equal to the first number of instructions.

14. The apparatus according to any one of claims 8 to 12, further comprising: A control unit is configured to, when the second number of instructions of the executed smart contract is not equal to the first number of instructions, set a second number in a first register corresponding to the PMU, so that after the processor executes the second number of instructions in user mode, an interrupt is generated under the triggering of the PMU, wherein the second number is calculated based on the number of instructions of the executed smart contract and the first number of instructions; and continue to execute the smart contract according to the interrupt scene of the processor.

15. A computing device comprising a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the method according to any one of claims 1 to 7 is implemented.

16. A computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed in a computing device, the computing device executes the method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • An intelligent contract complexity limiting method based on a Java virtual machine

    CN109710385A

  • Smart contract deployment method and device, smart contract execution method and device and terminal equipment

    CN112631729A