A method and device for accelerating the execution of smart contracts based on just-in-time compilation
Through just-in-time compilation technology and hot contract identification algorithm, the problem of low interpretation and execution efficiency of the Ethereum virtual machine is solved, the execution efficiency and system throughput of smart contracts are improved, and the application prospects of blockchain technology are expanded.
Patent Information
- Application Number
- CN202410971522.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-19
- Publication Date
- 2024-10-11
- Estimated Expiration
- 2044-07-19
AI Technical Summary
The interpretation and execution mode of the Ethereum Virtual Machine results in low smart contract execution efficiency, resulting in a decrease in system availability and throughput, which limits the application prospects of blockchain technology in finance, law and other fields.
Adopt a smart contract accelerated execution method based on just-in-time compilation, use the hot contract identification algorithm to determine frequently called contracts and convert their bytecodes into LLVM intermediate representations, use the LLVM compiler to generate efficient machine code, and introduce the basic block level during the optimization execution process Runtime status checks and stack operations are eliminated to improve execution efficiency.
It significantly improves the execution efficiency and system throughput of smart contracts, reduces network congestion, optimizes user experience, and expands the application prospects of blockchain technology in finance, law and other fields.
Smart Images

Figure CN118509159B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of blockchain, and in particular to a method and device for accelerating the execution of a smart contract based on just-in-time compilation. Background Art
[0002] Ethereum is one of the most popular public, open-source blockchain platforms. Its support for smart contracts has greatly promoted the development and application of blockchain technology. Smart contracts are executable programs deployed on Ethereum that can execute smart contract logic without any third-party intervention. Users assemble smart contract call requests into transactions, sign the transactions, and then submit them to the Ethereum node to execute smart contracts. These smart contracts are interpreted and executed by the Ethereum Virtual Machine (EVM). The emergence of smart contracts has greatly promoted the development of decentralized applications. Developers can write and deploy specific smart contracts to provide users with various decentralized services, ranging from financial services to identity authentication and voting systems, demonstrating the application potential of blockchain technology.
[0003] In the Ethereum network, smart contracts are usually written in high-level languages such as Solidity or Vyper and compiled into bytecode that can be understood by the EVM. EVM bytecode is a low-level, stack-based instruction set language that is interpreted and executed one by one by the EVM on the Ethereum network nodes. This interpretation and execution mode ensures the correctness of smart contracts and the consistency of the network, but its efficiency is low, mainly due to the following reasons: (1) In the interpretation and execution mode, each bytecode instruction requires the interpreter to perform multiple steps such as syntax parsing, operand extraction and execution. For each instruction, these steps will bring significant additional overhead. (2) EVM uses a stack-based virtual machine architecture rather than a register-based virtual machine. In a stack-based virtual machine, operands and intermediate results are stored in a stack, and all operations require frequent stack push and pop operations. The instructions of the stack-based virtual machine require more stack operations, which further increases the execution time and computational complexity of the instructions; while the register-based virtual machine uses registers to store operands and intermediate results, and the instruction set is more concise and efficient. (3) The interpretation and execution mode makes it difficult to perform deep code optimization. While smart contract compilers are able to perform basic optimizations such as constant folding and simple instruction merging, these optimizations are far less sophisticated and comprehensive than modern compiler optimization techniques.
[0004] Inefficient interpretation and execution can lead to a significant decrease in system availability and throughput. Increased latency and excessive node load will reduce system stability and responsiveness, while inefficient transaction processing and block packaging efficiency will limit network throughput, leading to network congestion and rising user costs, which in turn will affect the performance and user experience of decentralized applications. In addition, the inefficient execution of smart contracts also limits the development of more complex applications. For example, in decentralized finance (DeFi) applications, smart contracts need to respond to market changes in real time and handle complex transactions and automated strategies. Under the current interpretation and execution mode, this demand is often difficult to achieve due to execution delays and processing capacity limitations.
[0005] Correspondingly, the efficient execution of smart contracts is crucial to the development of blockchain technology. It can not only improve the performance of existing applications and reduce operating costs, but also open up new application areas, such as real-time data processing, complex automated decision-making, etc. This will greatly expand the application prospects of blockchain technology in finance, law, supply chain management and other fields, and promote the digital transformation of these industries. To this end, the industry urgently needs new technical solutions to overcome the limitations of existing technologies and achieve a more efficient smart contract execution model. Summary of the invention
[0006] In view of the performance bottleneck problem in the Ethereum virtual machine interpreted execution mode in the prior art, the present invention proposes a smart contract accelerated execution method and device based on just-in-time compilation.
[0007] The specific technical solutions are as follows:
[0008] A method for accelerating the execution of a smart contract based on just-in-time compilation comprises the following steps:
[0009] S1. Calling a smart contract: The Ethereum node receives a call request from a smart contract, performs a legality and validity check on the call request, and parses the call request that passes the check; the call request is initiated by a user or other smart contract and contains the bytecode and input parameters of the smart contract to be executed;
[0010] S2, hot contract judgment: comprehensively consider the number of calls of the smart contract, the most recent call time and the complexity of the smart contract execution to determine whether the smart contract is a hot contract; if not, no mark is made, and the interpretation execution mode is directly entered to let the EVM interpret and execute the smart contract bytecode and output the execution result; if yes, the smart contract is marked as a hot contract and S3 is executed;
[0011] S3, hotspot contract compilation status judgment: judge whether the bytecode of the hotspot contract has been compiled into machine code; if so, load the corresponding machine code from the cache and execute S5; if not, execute S4;
[0012] S4. Compile the hotspot contract: convert the bytecode of the hotspot contract into LLVM intermediate representation, optimize the LLVM intermediate representation using LLVM's optimization tool chain, compile the LLVM intermediate representation into machine code that can be directly run on the hardware environment using the LLVM compiler, and store the machine code in the cache;
[0013] S5: Execute the compiled machine code. After the execution is completed, the execution result is returned to the caller who made the call request, completing the execution process of the smart contract.
[0014] Furthermore, in S2, the hot contract is determined based on a hot contract identification algorithm that decays exponentially over time. The algorithm maintains a statistical table of contract call information, which records the number of calls, computing consumption, and call time of each smart contract. Based on the data in the statistical table, the heat value W of each smart contract is calculated. i ; When the heat value W i When the preset threshold parameter W is exceeded, the smart contract is marked as a hot contract, otherwise it is not marked; the heat value W i The expression is as follows:
[0015] ;
[0016] ;
[0017] Where i represents the time information, i.e., the block height; W i Indicates the heat value of the smart contract at the current moment, L i Indicates the activity of the smart contract at the current moment; α, β, λ, μ, ν are preset parameters, α is used to adjust the sensitivity of the heat value to the change of activity, β and λ are used to adjust the decay rate of the heat value over time, μ and ν are used to adjust the weights of each component of the activity of the smart contract at the current moment; contract_gas_cost indicates the total computing cost of smart contract calls, block_gas_cost indicates the total computing cost of all transactions in the current block, invoke_count indicates the number of times the smart contract is called in the current block, and block_tx_count indicates the total number of transactions in the current block;
[0018] The heat value of each smart contract at each moment will be recorded and optimized to record the heat value W of each smart contract at the current moment i , the expression is as follows:
[0019] ;
[0020] Where k is the block height at which the smart contract was last called. <i。
[0021] Furthermore, the S4 specifically includes the following sub-steps:
[0022] S4.1: Read the bytecode of the smart contract and parse it into an EVM instruction sequence; convert the parsed EVM instruction sequence into an LLVM intermediate representation, that is, map the EVM operation instructions to the LLVM intermediate representation, and process the control flow, data flow, and storage structure at the same time;
[0023] S4.2: Use LLVM's optimization tool chain to optimize the generated LLVM intermediate representation;
[0024] S4.3: Compile the optimized LLVM intermediate representation into the machine code of the target platform through the LLVM backend compiler, and store the machine code in the cache.
[0025] Furthermore, in said S4.1, in the process of converting EVM instructions into LLVM IR, a basic block level optimization method for EVM instructions is introduced, including runtime state check optimization and stack operation elimination;
[0026] (1) Runtime status check optimization: Through static analysis methods, the runtime status check at the instruction level is optimized to the runtime status check at the basic block level. Specifically, during the compilation phase, the static computation consumption, generated and consumed stack elements of all instructions in the same basic block are aggregated as the resource requirements of the basic block. Before the execution of each basic block, a centralized status check is performed. If the resource requirements of the basic block are met, all instructions within it can be executed safely without the need to perform status checks on each instruction separately. Otherwise, the transaction is considered to have failed.
[0027] (2) Stack operation elimination: Static analysis technology is used to identify local stack elements that are only produced and consumed within the same basic block, where the basic block is a straight-line code sequence without branches in the program; the stack operations corresponding to the local stack elements are eliminated, and the local stack elements in the instructions are replaced with corresponding register variables.
[0028] Furthermore, in S4.2, the optimization process includes: loop optimization, inline expansion, constant folding, dead code elimination, and register allocation.
[0029] A smart contract accelerated execution device includes: a hotspot smart contract identification module, a smart contract compilation module, and a smart contract execution module; the hotspot smart contract identification module is used to determine whether the smart contract is a hotspot contract; the smart contract compilation module is used to compile the smart contract bytecode into machine code that can be directly run on a hardware environment; the smart contract execution module is used to execute the smart contract and return the result to the caller who made the call request.
[0030] An electronic device includes a memory and a processor, wherein the memory stores executable code, and when the processor executes the executable code stored in the memory, the smart contract accelerated execution method based on just-in-time compilation is implemented.
[0031] A computer-readable storage medium stores a computer program, which, when executed by a processor, implements the method for accelerating the execution of smart contracts based on just-in-time compilation.
[0032] The beneficial effects of the present invention are:
[0033] (1) The present invention converts the bytecode of hot contracts deployed in the Ethereum network into LLVM IR, and uses the LLVM compiler to compile and optimize the LLVM IR into efficient machine code, thereby significantly improving the execution efficiency of smart contracts, the system throughput and transaction processing capabilities, reducing network congestion, and optimizing the user experience.
[0034] (2) Based on the cross-platform support of LLVM, the present invention has good portability and scalability, can adapt to different types of node devices, and further expand the application scenarios of the Ethereum network, thereby providing solid technical support for the sustainable development and wide application of the Ethereum network. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] Figure 1 It is a flow chart of a method for accelerating the execution of a smart contract based on just-in-time compilation in an embodiment of the present invention.
[0036] Figure 2 It is a flow chart of calculating the total static computing consumption of basic blocks and the upper and lower bounds of the stack in an embodiment of the present invention.
[0037] Figure 3 Schematic diagram of an electronic device in an embodiment of the present invention. DETAILED DESCRIPTION
[0038] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings 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 making creative work are within the scope of protection of this application.
[0039] In the following description, reference is made to “one specific embodiment”, which describes a subset of all possible embodiments, but it can be understood that “one specific embodiment” describes the same subset or different subsets of all possible embodiments and can be combined with each other without conflict.
[0040] Unless otherwise defined, all technical science and technology used herein have the same meaning as commonly understood by those skilled in the art of the present invention. The terms used herein are only for the purpose of describing the embodiments of the present invention and are not intended to limit the present invention.
[0041] Before further describing the embodiments of the present invention in detail, the nouns and terms involved in the embodiments of the present invention are described. The nouns and terms involved in the embodiments of the present invention are subject to the following explanations.
[0042] (1) Transaction is equivalent to the computer term "transaction". Transaction includes operations that need to be submitted to the blockchain network for execution, that is, the call request for the smart contract. It does not refer only to transactions in the business context. Since the term "transaction" is commonly used in blockchain technology, the embodiments of the present invention follow this convention.
[0043] (2) Blockchain is an encrypted, chained transaction ledger formed by blocks.
[0044] (3) Smart Contracts are executable codes deployed in the blockchain network. Blockchain nodes execute smart contracts called in received transactions to update or query key-value data in the state database.
[0045] (4) A basic block is a linear program (instruction), or a straight-line code sequence without branching structure in the program. That is, it can only be entered from the beginning of the program and can only be left from the end of the program. There is no other jump code in the middle of the program. Usually in such a code sequence, the dependency between data is determined.
[0046] (5) Just-in-time compilation (JIT) is a dynamic compilation technology that converts bytecode (or intermediate code) into machine code during program execution, so that the machine code can be directly run in subsequent execution, significantly improving the execution speed. This method can reduce the additional overhead caused by interpreted execution, thereby greatly improving the execution efficiency of the program.
[0047] (6) Hot contracts refer to smart contracts that are frequently called, that is, smart contracts whose calling frequency and computing consumption ratio exceed the set threshold.
[0048] like Figure 1 As shown, a method for accelerating the execution of a smart contract based on just-in-time compilation includes the following steps:
[0049] S1. Calling a smart contract: When an Ethereum node receives a call request from a smart contract, it first checks the legality and validity of the call request. Then, during the transaction packaging and synchronization process, the Ethereum node parses the call request that has passed the test to prepare for subsequent processing. The call request is initiated by the user or other smart contract, and usually contains: the address of the smart contract to be executed, the bytecode, the input parameters, and the relevant information of the caller.
[0050] To ensure the validity and legitimacy of the call request of the smart contract, the call request is tested. The specific operations are as follows: first, verify the signature of the call request to confirm the legitimacy of the request source. Then, confirm the validity of the call request, including: checking the relevant information of the caller to see whether the account balance of the caller is sufficient to pay the transaction fee required for the execution of the contract. If it is sufficient, it is valid, otherwise it is invalid.
[0051] S2. Hot contract judgment: Consider the number of calls, the most recent call time and the complexity of contract execution of a smart contract to determine whether the smart contract is a hot contract. If not, it will not be marked and will directly enter the interpretation and execution mode. The EVM will interpret and execute the bytecode and directly output the execution result. If yes, the smart contract will be marked as a hot contract and continue to execute S3.
[0052] Specifically, a hot contract refers to a frequently called smart contract, and its execution efficiency has an important impact on the overall performance of the system. In this embodiment, the hot contract is determined based on a hot contract identification algorithm that decays exponentially over time. The algorithm maintains a statistical table of contract call information, which records the number of calls, computing consumption, and call time of each smart contract; based on these statistical data in the statistical table, the heat value W of each smart contract is calculated in real time. i , when the heat value W i When the preset threshold parameter W is exceeded, the smart contract is marked as a hot contract, otherwise it is not marked. When Ethereum is running, the dynamic heat value W of each smart contract i The expression is as follows:
[0053]
[0054]
[0055] Where i represents the time information, i.e., the block height; W iIndicates the heat value of the smart contract at the current moment, L i Indicates the activity of the smart contract at the current moment; α, β, λ, μ, ν are preset parameters, α is used to adjust the sensitivity of the heat value to the change of activity, β and λ are used to adjust the decay rate of the heat value over time, μ and ν are used to adjust the weights of each component of the activity of the smart contract at the current moment; contract_gas_cost indicates the total computational cost (gas cost) of smart contract calls, block_gas_cost indicates the sum of the computational cost (gas cost) of all transactions in the current block, invoke_count indicates the number of times the smart contract is called in the current block, and block_tx_count indicates the total number of transactions in the current block.
[0056] In order to facilitate the algorithm's program implementation, it is necessary to record the heat value of each smart contract at each moment, and optimize it to only record the heat value W of each smart contract at the current moment. i , the expression is as follows:
[0057]
[0058] Where k is the block height at which the smart contract was last called. <i。
[0059] The above-mentioned hot contract identification algorithm that decays exponentially over time can not only reflect the usage and importance of each smart contract in real time, but also effectively allocate compilation and execution resources, improving the efficiency and response speed of the entire platform. This dynamic evaluation method based on mathematical models can respond to changes in the activity of smart contracts more flexibly and promptly, and is particularly suitable for blockchain environments with rapid dynamic changes. It can ensure the reasonable allocation and priority management of resources, thereby optimizing the performance and stability of the entire system.
[0060] S3, hotspot contract compilation status judgment: judge whether the bytecode of the hotspot contract has been compiled into machine code; if the bytecode of the hotspot contract has been compiled, directly load the corresponding machine code from the cache and execute S5; if the bytecode of the hotspot contract has not been compiled, execute the compilation process of S4.
[0061] S4, compile hot contracts: compile the smart contract bytecode into machine code that can be directly run on the hardware environment, and put the machine code into the cache. Existing methods that can compile smart contract bytecode into machine code can be applied to the present invention. In this embodiment, S4 specifically includes the following sub-steps:
[0062] S4.1: Read the bytecode of the smart contract and parse it into an EVM instruction sequence; convert the parsed EVM instruction sequence into the LLVM intermediate representation (hereinafter referred to as LLVM IR). This step involves mapping the EVM operation instructions to the LLVM intermediate representation. The instruction mapping process requires careful processing of each EVM instruction to ensure that it is correctly converted into an equivalent LLVM IR instruction. EVM instructions include sequential instructions, termination instructions, unconditional jumps (hereinafter referred to as JUMP) and conditional jump instructions (hereinafter referred to as JUMPI); at the same time, maintain the corresponding control flow, data flow and smart contract runtime storage structure.
[0063] The control flow maintenance operation is as follows: when converting EVM instructions to LLVM IR, first parse the EVM instruction sequence into a series of continuous EVM basic blocks; secondly, translate each EVM basic block into an LLVM IR basic block. This step translates EVM sequential instructions into LLVM IR sequential instructions; sequential instructions are the most basic form of control flow. They are executed in the order in which they appear in the code. In LLVM, the maintenance of sequential instructions is relatively simple because the basic blocks of LLVM are essentially a series of sequentially executed instruction sets until a termination instruction or jump instruction is encountered. The termination instruction is translated into a ret instruction in LLVM. Then translate the jump instruction used for jump operations between different basic blocks. The JUMP instruction does not require any conditions when executing to jump to the specified position in the code. It is implemented in LLVM through the unconditional br instruction; the JUMPI instruction decides whether to execute the jump according to specific conditions. It is implemented in LLVM through the conditional br instruction.
[0064] The specific operations of data flow maintenance are as follows: the execution of smart contracts will access smart contract persistent storage, transaction context (such as caller address, smart contract address, transaction timestamp) and smart contract runtime data (including stack and memory data); in LLVM, in order to ensure the correct execution of transactions, it is necessary to ensure the correct mapping and loading of these data. LLVM passes smart contract persistent storage and transaction context to the execution environment through function parameters, and access to smart contract runtime data is completed by accessing the smart contract runtime storage structure.
[0065] The operations for maintaining storage structures during the runtime of smart contracts are as follows: EVM maintains two storage structures, stack and memory, during the runtime of each smart contract. When corresponding to LLVM IR, these two storage structures need to be simulated to maintain compatibility with EVM behavior; the allocation and release of the stack and memory storage structures in EVM are implemented through the dynamic memory management interface provided by LLVM (i.e., malloc and free functions). For stack operations, a stack pointer variable is maintained to track the current position of the top of the stack, and the PUSH and POP operations of the stack in EVM are simulated by increasing or decreasing the stack pointer; for memory operations, LLVM pointer and array operations are used to read and write simulated memory.
[0066] In the process of converting EVM instructions into LLVM IR, basic block-level optimization methods for EVM instructions are introduced. These optimization methods include runtime state check optimization, stack operation elimination, etc., as follows:
[0067] (1) Runtime status check optimization. During the EVM execution process, each instruction needs to perform a large number of runtime status checks to determine whether the resources required for the current instruction execution can be met. The runtime status checks include balance checks and stack overflow checks. However, these checks will introduce additional computing overhead during the execution of each instruction, affecting the overall performance of the system. In order to reduce the overhead of these runtime status checks, this embodiment optimizes the runtime status checks at the instruction level to the runtime status checks at the basic block level through a static analysis method. The specific implementation plan is as follows:
[0068] Since the static computation consumption, generated and consumed stack elements required by each instruction are determined, and the instructions in the same basic block are executed sequentially (i.e., no jumps are generated), the above resource requirements (i.e., static computation consumption, generated and consumed stack elements) of all instructions in the same basic block can be aggregated during the compilation phase to calculate the resource requirements of the entire basic block, i.e., the total static computation consumption of a basic block and the stack upper and lower bounds that must be met before executing the basic block. The stack upper bound corresponds to the maximum number of stack elements, and the stack lower bound corresponds to the minimum number of stack elements. Figure 2 As shown in the figure, calculating the total static computational cost of the basic block and the upper and lower bounds of the stack specifically includes the following steps:
[0069] (1.1) Initialize the EVM basic block: set the total static computation consumption, current stack offset, minimum stack offset, and maximum stack offset to zero.
[0070] (1.2) Read the first instruction of the EVM basic block in order.
[0071] (1.3) Calculate the total static computational cost of the EVM basic block and the current stack offset up to the current instruction, and use the current stack offset to update the current minimum stack offset and the current maximum stack offset, as follows:
[0072] This step involves two assignment calculations of the current stack offset. To distinguish the current stack offsets obtained by the two calculations, they are recorded as the current first stack offset and the current second stack offset according to the calculation order.
[0073] ① Update the total static computing consumption, and add the static computing consumption of the current instruction to the total static computing consumption.
[0074] ②Current first stack offset = historical stack offset - stack consumption of current instruction.
[0075] ③Current minimum stack offset = min (historical minimum stack offset, current first stack offset).
[0076] That is, if the current first stack offset is less than the historical minimum stack offset, the value of the current minimum stack offset in the record is updated to the current first stack offset; otherwise, no update is performed, and the value of the current minimum stack offset remains the historical minimum stack offset. During the loop, the current minimum stack offset obtained in the previous loop is the historical minimum stack offset for the next loop.
[0077] ④Current second stack offset = current first stack offset + stack production of current instruction.
[0078] During the loop, the current second stack offset obtained in the previous loop is the historical stack offset of the next loop.
[0079] ⑤Current maximum stack offset = max (historical maximum stack offset, current second stack offset).
[0080] That is, if the current second stack offset is greater than the historical maximum stack offset, the value of the current maximum stack offset in the record is updated to the current second stack offset; otherwise, no update is performed, and the value of the current maximum stack offset remains the historical maximum stack offset. During the loop, the current maximum stack offset obtained in the previous loop is the historical maximum stack offset for the next loop.
[0081] (1.4) Determine whether the end of the basic block has been reached. If not, read the next instruction of the EVM basic block in order and repeat (1.3)-(1.4); if so, output the total static computing cost of the basic block, calculate and output the upper and lower bounds of the stack, and the expression is as follows:
[0082] Stack upper limit = 1024-maximum stack offset;
[0083] Stack lower bound = max(0, - minimum stack offset);
[0084] Through the above steps, the resource requirements of the entire basic block are obtained, and a centralized status check is performed before the execution of each basic block. If the resource requirements of the basic block are met, then all instructions within it can be executed safely without the need to perform a status check on each instruction separately; otherwise, the transaction is considered to have failed. The core of this optimization idea is to determine the runtime status as much as possible in advance through static analysis at compile time, thereby reducing unnecessary checks during the execution of each instruction and reducing runtime overhead.
[0085] (2) Stack operation elimination. In the execution model of the Ethereum virtual machine, due to its stack-like design characteristics, the execution of instructions is often accompanied by frequent push and pop operations, which brings significant performance overhead. This embodiment optimizes local stack operations into register operations, reduces the overhead of stack operations, and improves execution efficiency. Specifically:
[0086] First, static analysis techniques are used to identify local stack elements that are only produced and consumed within the same basic block. Then, the stack operations corresponding to these local stack elements are eliminated, and the local stack elements in the instructions are replaced with corresponding register variables. This optimization method greatly reduces the number of stack accesses and significantly improves overall performance.
[0087] S4.2: Use LLVM's optimization toolchain to further optimize the LLVM IR generated by S4.1. The optimization process includes: loop optimization, inlining, constant folding, dead code elimination, register allocation, etc. These optimization steps can significantly improve code execution efficiency and reduce unnecessary calculations and memory accesses. Loop optimization specifically improves loop efficiency by reducing the number of loop executions. Inline expansion specifically reduces the overhead of function calls by replacing function calls with the contents of the function body. Constant folding specifically reduces the computational overhead of the program at runtime by determining the calculation results of one or more LLVM IRs at compile time instead of calculating them at runtime. Dead code elimination specifically speeds up smart contract compilation and reduces the size of the generated machine code by identifying and removing code segments that will not be executed in the program. Register allocation specifically reduces the number of accesses to slow memory by reasonably allocating memory variables to registers.
[0088] S4.3: Compile the LLVM IR optimized by S4.2 into the machine code of the target platform through the LLVM backend; put the machine code into the cache for subsequent smart contract calls. LLVM supports a variety of hardware architectures, including X86, ARM, etc., so that the generated machine code can be efficiently executed on different hardware platforms.
[0089] S5: Execute the compiled smart contract machine code, which runs directly on the hardware, avoiding the extra overhead of interpretation and execution. After the execution is completed, the result is returned to the caller who made the call request, completing the execution process of the smart contract.
[0090] In order to realize the above-mentioned smart contract accelerated execution method based on just-in-time compilation, this embodiment proposes a smart contract accelerated execution device, including: a hot smart contract identification module, a smart contract compilation module, and a smart contract execution module. Among them, the hot smart contract identification module is used to determine whether the smart contract is a hot contract; the smart contract compilation module is used to compile the smart contract bytecode into a machine code that can be directly run on the hardware environment; the smart contract execution module is used to execute the smart contract (including EVM execution of the smart contract bytecode, and the machine code corresponding to the execution of the smart contract), and return the result to the caller who made the call request.
[0091] like Figure 3 As shown, an electronic device provided by an embodiment of the present invention includes a memory and one or more processors, wherein executable code is stored in the memory, and when the one or more processors execute the executable code, it is used to implement the smart contract accelerated execution method based on just-in-time compilation in the above embodiment.
[0092] An electronic device proposed in the present invention can be applied to any device with data processing capability, and the device with data processing capability can be a device or apparatus such as a computer. The electronic device can be implemented by software, or by hardware or a combination of software and hardware. Taking software implementation as an example, as a device in a logical sense, the processor of any device with data processing capability reads the corresponding computer program instructions in the non-volatile memory into the memory and runs them. From the hardware level, if Figure 3 As shown, it is a hardware structure diagram of an electronic device of the present invention, in which any device with data processing capability is located, except Figure 3 In addition to the processor, memory, network interface, and non-volatile memory shown, any device with data processing capabilities in which the device of the present invention is located in the embodiments may also include other hardware, which will not be described in detail.
[0093] For the electronic device embodiment, since it basically corresponds to the method embodiment, the relevant parts can refer to the partial description of the method embodiment. The electronic device embodiment described above is only schematic, and the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the scheme of the present invention. Ordinary technicians in this field can understand and implement it without paying creative work.
[0094] An embodiment of the present invention also provides a computer-readable storage medium on which a program is stored. When the program is executed by a processor, the smart contract accelerated execution method based on just-in-time compilation in the above embodiment is implemented.
[0095] The computer-readable storage medium may be an internal storage unit of any device with data processing capability in any of the aforementioned embodiments, such as a hard disk or a memory. The computer-readable storage medium may also be an external storage device of any device with data processing capability, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), an SD card, a flash card (Flash Card), etc. equipped on the device. Furthermore, the computer-readable storage medium may also include both an internal storage unit and an external storage device of any device with data processing capability. The computer-readable storage medium is used to store computer programs and other programs and data required by any device with data processing capability, and may also be used to temporarily store data that has been output or is to be output.
[0096] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0097] The above description is only the preferred embodiment of the present invention, which enables those skilled in the art to understand or implement the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various changes and variations, and the various modifications to these embodiments will be obvious to those skilled in the art. The general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention shall be included in the protection scope of the present invention.
Claims
1. A method for accelerating the execution of smart contracts based on just-in-time compilation, characterized in that: The following steps are involved: S1. Calling a smart contract: The Ethereum node receives a call request from a smart contract, performs a legality and validity check on the call request, and parses the call request that passes the check; the call request is initiated by a user or other smart contract and contains the bytecode and input parameters of the smart contract to be executed; S2, hot contract judgment: comprehensively consider the number of calls of the smart contract, the most recent call time and the complexity of the smart contract execution to determine whether the smart contract is a hot contract; if not, no mark is made, and the interpretation execution mode is directly entered to let the EVM interpret and execute the smart contract bytecode and output the execution result; if yes, the smart contract is marked as a hot contract and S3 is executed; S3, hotspot contract compilation status judgment: judge whether the bytecode of the hotspot contract has been compiled into machine code; if so, load the corresponding machine code from the cache and execute S5; if not, execute S4; S4. Compile the hotspot contract: convert the bytecode of the hotspot contract into LLVM intermediate representation, optimize the LLVM intermediate representation using LLVM's optimization tool chain, compile the LLVM intermediate representation into machine code that can be directly run on the hardware environment using the LLVM compiler, and store the machine code in the cache; S5: Execute the compiled machine code. After the execution is completed, the execution result is returned to the caller who made the call request, completing the execution process of the smart contract; In S2, the hot contract is determined based on a hot contract identification algorithm that decays exponentially over time. The algorithm maintains a statistical table of contract call information, which records the number of calls, computing consumption, and call time of each smart contract. Based on the data in the statistical table, the heat value W of each smart contract is calculated. i ; When the heat value W i When the preset threshold parameter W is exceeded, the smart contract is marked as a hot contract, otherwise it is not marked; the heat value W i The expression is as follows: ; ; In the formula, i represents the time information, that is, the block height; W i Indicates the heat value of the smart contract at the current moment, L i Indicates the activity of the smart contract at the current moment; α, β, λ, μ, ν are preset parameters, α is used to adjust the sensitivity of the heat value to the change of activity, β and λ are used to adjust the decay rate of the heat value over time, μ and ν are used to adjust the weights of each component of the activity of the smart contract at the current moment; contract_gas_cost indicates the total computing cost of smart contract calls, block_gas_cost indicates the total computing cost of all transactions in the current block, invoke_count indicates the number of times the smart contract is called in the current block, and block_tx_count indicates the total number of transactions in the current block; The heat value of each smart contract at each moment will be recorded and optimized to record the heat value W of each smart contract at the current moment i , the expression is as follows: ; Where k is the block height at which the smart contract was last called. <i。 2. The method for accelerating the execution of smart contracts based on just-in-time compilation according to claim 1, characterized in that: The S4 specifically includes the following sub-steps: S4.1: Read the bytecode of the smart contract and parse it into an EVM instruction sequence; convert the parsed EVM instruction sequence into an LLVM intermediate representation, that is, map the EVM operation instructions to the LLVM intermediate representation, and process the control flow, data flow, and storage structure at the same time; S4.2: Use LLVM's optimization tool chain to optimize the generated LLVM intermediate representation; S4.3: Compile the optimized LLVM intermediate representation into the machine code of the target platform through the LLVM backend compiler, and store the machine code in the cache.
3. The method for accelerating the execution of smart contracts based on just-in-time compilation according to claim 2, characterized in that: In S4.1, in the process of converting EVM instructions into LLVM IR, a basic block level optimization method for EVM instructions is introduced, including runtime state check optimization and stack operation elimination; (1) Runtime status check optimization: Through static analysis methods, the runtime status check at the instruction level is optimized to the runtime status check at the basic block level. Specifically, during the compilation phase, the static computation consumption, generated and consumed stack elements of all instructions in the same basic block are aggregated as the resource requirements of the basic block. Before the execution of each basic block, a centralized status check is performed. If the resource requirements of the basic block are met, all instructions within it can be executed safely without the need to perform status checks on each instruction separately. Otherwise, the transaction is considered to have failed. (2) Stack operation elimination: Static analysis technology is used to identify local stack elements that are only produced and consumed within the same basic block, where the basic block is a straight-line code sequence without branches in the program; the stack operations corresponding to the local stack elements are eliminated, and the local stack elements in the instructions are replaced with corresponding register variables.
4. The method for accelerating the execution of smart contracts based on just-in-time compilation according to claim 2, characterized in that: In S4.2, the optimization process includes: loop optimization, inline expansion, constant folding, dead code elimination, and register allocation.
5. A smart contract accelerated execution device, used to implement the smart contract accelerated execution method based on just-in-time compilation according to any one of claims 1 to 4, characterized in that: include: Hotspot smart contract identification module, smart contract compilation module, smart contract execution module; the hotspot smart contract identification module is used to determine whether the smart contract is a hotspot contract; the smart contract compilation module is used to compile the smart contract bytecode into machine code that can be directly run on the hardware environment; The smart contract execution module is used to execute the smart contract and return the result to the caller who made the call request.
6. An electronic device, characterized in that: It comprises a memory and a processor, wherein the memory stores executable code, and when the processor executes the executable code stored in the memory, the smart contract accelerated execution method based on just-in-time compilation as described in any one of claims 1 to 4 is implemented.
7. A computer-readable storage medium, characterized in that: A computer program is stored thereon, and when the computer program is executed by a processor, the method for accelerating the execution of a smart contract based on just-in-time compilation as described in any one of claims 1 to 4 is implemented.
