Ethereum intelligent contract byte code optimization method and device, medium and equipment

By performing basic block segmentation, control flow graph analysis and binary decision tree construction on the Ethereum smart contract bytecode, and optimizing conditional jumps and repeated calculations, the problem of existing tools being unable to effectively reduce gas consumption has been solved, and resource consumption has been achieved. Improvement of efficiency.

CN118567653AActive Publication Date: 2024-08-30NANJING UNIV
View PDF 7 Cites 0 Cited by

Patent Information

Application Number
CN202410651915.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-05-24
Publication Date
2024-08-30
Estimated Expiration
2044-05-24

AI Technical Summary

Technical Problem

Existing Ethereum smart contract optimization tools cannot effectively reduce gas consumption, especially in terms of high-level language pattern detection and local optimization of underlying bytecode. They cannot handle the optimization of larger scenarios, and super optimization relies on exhaustive search. , the time complexity is high.

Method used

By obtaining the smart contract bytecode instruction set, perform basic block segmentation and jump analysis, build a control flow graph, symbolic execution analysis, build a binary decision tree, and perform tailoring processing to optimize conditional jumps and repeated calculations, reducing Unnecessary jumps and calculations.

Benefits of technology

It achieves the overall optimization of the Ethereum smart contract bytecode, reduces gas consumption, improves resource utilization efficiency, and reduces transaction fees.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118567653A
    Figure CN118567653A
Patent Text Reader

Abstract

The invention discloses an Ethereum intelligent contract byte code optimization method and device, a medium and equipment. The method comprises the following steps: firstly, finding out a function and basic blocks contained in the function from a byte code instruction set, and then finding out a basic block representing continuity condition jump of a source code non-cyclic continuity condition judgment statement and a jump target basic block in a function body; the method comprises the following steps: acquiring a constraint relationship between condition variable judgment items and a jump target basic block through symbolic execution, then sorting the condition variable judgment items based on the constraint relationship, then constructing and optimizing a binary decision tree, and finally reconstructing a byte code instruction according to the optimized binary decision tree. According to the optimization method, resource consumption is greatly reduced in Ethereum.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to program optimization technology, in particular to Ethereum smart contract bytecode optimization technology. Background Art

[0002] The decentralized and distributed ledger characteristics of blockchain technology ensure the security and credibility of transactions. The Ethereum platform enhances the programmability of blockchain through smart contracts, enabling the deployment of programs on the blockchain, providing greater flexibility and development potential. Smart contracts are automatically executed codes that are not subject to external interference, ensuring transparency and security. On Ethereum, there are already smart contracts in various fields such as cryptocurrency, finance, and games.

[0003] Smart contracts are written in Solidity, compiled into bytecode and deployed on the blockchain. They run on the Ethereum Virtual Machine (EVM), a stack machine model that supports Turing-complete programs. However, the Turing-completeness of smart contracts also brings risks, which may lead to waste of resources or infinite program loops. Ethereum introduced a fuel (gas) system to limit program execution and prevent waste of computing resources. Users need to pay a fuel fee when executing smart contracts. The fee is denominated in ETH, and the specific rate depends on the degree of network congestion. As the price of ETH rises and the Ethereum ecosystem gradually flourishes, the fees for running smart contracts are getting higher and higher. Therefore, optimizing the overhead of smart contracts has become a key demand in the Ethereum blockchain field. Even a small optimization can save users a lot of transaction fees.

[0004] Existing smart contract analysis tools focus on detecting the security of smart contracts to prevent hackers from exploiting security vulnerabilities in smart contracts and stealing assets in smart contracts. There are relatively limited tools for optimizing smart contracts. From an official perspective, the compilation tools for the smart contract development language Solidity are community-developed and cannot provide the most mature compilation optimization during the compilation process. From the perspective of externally provided optimization tools, such tools mainly focus on high-overhead pattern detection in high-level languages ​​and local optimization of underlying bytecodes. For pattern detection in high-level languages, its limitation is that most tools only provide detection of related patterns, cannot automatically complete optimization, and are limited to detecting a few predefined patterns, with limited scalability. For local optimization of underlying bytecodes, they mainly focus on peephole optimization of preset patterns and super optimization based on synthesis. The former also has the problem that predefined patterns cannot be expanded, while the latter is limited to the exhaustive search characteristics of super optimization and cannot handle the optimization of larger scenarios. It is necessary to divide the program into fine-grained code windows to complete the optimization within an acceptable time. Therefore, there is still a lot of room for optimization of current smart contracts. Summary of the invention

[0005] The problem to be solved by the present invention is to optimize the bytecode of Ethereum smart contracts and reduce gas consumption.

[0006] To solve the above problems, the solution adopted by the present invention is as follows: According to an Ethereum smart contract bytecode optimization method of the present invention, the method comprises the following steps: Step S1: Obtain the smart contract bytecode instruction set; Step S2: Perform basic block segmentation and basic block jump analysis on the smart contract bytecode instruction set to determine the jump relationship between basic blocks and the scheduler basic block; then determine the function and the basic blocks contained in the function according to the function entry jump from the scheduler basic block and the exit jump to the scheduler basic block; Step S3: According to the control flow graph and path determined by the basic block jump relationship, the control flow graph path analysis is performed on the basic blocks included in the function, and the sub-control flow graph consisting of the basic block representing the continuity conditional jump of the non-loop continuity condition judgment statement of the source code and the jump target basic block is found to obtain a first sub-graph set; Step S4: performing symbolic execution analysis on the bytecode instructions corresponding to the sub-control flow graphs in the first sub-graph set, and obtaining the bytecode instructions for conditional variable calculation in the sub-control flow graphs, and the constraint relationship between the conditional variable determination item and the jump target basic block through the symbolic execution analysis; the conditional variable determination item is either a Boolean conditional variable or a conditional variable comparison expression about a single comparison operator; Step S5: construct a binary decision tree according to the constraint relationship between the conditional variable determination item and the jump target basic block in the sub-control flow graph; the binary decision tree is composed of nodes and edges representing the parent-child relationship of the nodes to form a tree data structure; wherein the nodes include branch nodes with two child nodes and leaf nodes without child nodes; the branch nodes at the same level of the constructed binary decision tree are the same conditional variable determination item in the sub-control flow graph; the two child nodes of the branch node respectively represent two subsequent processings of the conditional variable determination item corresponding to the branch node being true or false; the leaf node is the jump target basic block in the sub-control flow graph; Step S6: Loop through the branch nodes whose child nodes include leaf nodes in the binary decision tree, and perform the following pruning on the traversed branch nodes: If the two child nodes of the branch node point to the same jump target basic block, the edge from the parent node of the branch node to the branch node points to the jump target basic block; If two branch nodes are isomorphic and are two child nodes of the parent node, then the edge from the parent node of the branch node's parent node to the parent node of the branch node points to one of the two branch nodes; The two branch nodes are isomorphic, which means that when the condition variable judgment item is true, the child nodes of the two branch nodes are the same, and when the condition variable judgment item is false, the child nodes of the two branch nodes are the same; Step S7: According to the bytecode instructions and jump target basic blocks calculated by the conditional variables, corresponding jump basic blocks are constructed for the branch nodes in the pruned binary decision tree, and the positions of the jump target basic blocks are readjusted to replace the basic blocks in the atomic control flow graph.

[0007] Further, according to the Ethereum smart contract bytecode optimization method of the present invention, in the step S5, when constructing the binary decision tree, the condition variable determination items are first sorted, and the condition variable determination items at different levels of the constructed binary decision tree are configured according to the sorted condition variable determination items; the sorting of the condition variable determination items includes the following steps: Step S51: constructing a truth table between true and false values ​​of the conditional variable determination item and the jump target basic block according to the constraint relationship between the conditional variable determination item and the jump target basic block in the sub-control flow graph; Step S52: Calculate the evaluation value of each condition variable determination item according to the truth table. The evaluation value of each condition variable determination item is calculated using the following formula: eV(i)=abs(max(TCount(i,j)-FCount(i,j)); where, eV(i) represents the evaluation value of the i-th conditional variable judgment item; abs means absolute value; TCount(i,j) represents the number of basic blocks of the jth jump target corresponding to the true value of the i-th conditional variable judgment item in the truth table; FCount(i,j) represents the number of basic blocks of the j-th jump target corresponding to the false value of the i-th condition variable judgment item in the truth table; max means taking the maximum value among all jump target basic blocks; Step S53: Sort the condition variable determination items in descending order according to their evaluation values.

[0008] Further, according to the Ethereum smart contract bytecode optimization method of the present invention, step S2 includes the following steps: Step S21: traverse the scheduler basic block, if the last instruction of the scheduler basic block is a JUMP instruction, and the basic block of the jump target does not belong to the scheduler basic block, then add the basic block of the jump target to the first basic block set; Step S22: Determine whether there are at least two other basic blocks that use the basic block in the first basic block set as a jump target, and whether these basic blocks that use the other basic blocks in the first basic block set as a jump target are all immediately jumped using a JUMP instruction; if there are at least two other basic blocks that use the basic block in the first basic block set as a jump target, and whether these basic blocks that use the other basic blocks in the first basic block set as a jump target are all immediately jumped using a JUMP instruction, then these basic blocks in the first basic block set are combined into a second basic block set; Step S23: according to the jump relationship between basic blocks, starting from the basic block in the second basic block set as the basic block at the function entry, a deep search of the jump target basic block is performed until the jump target of the last JUMP immediate jump instruction of the basic block is found to be the scheduler basic block; then the basic block whose immediate jump target is the scheduler basic block is added to the candidate function exit basic block set; the basic block at the function entry and the candidate function exit basic block set constitute the candidate function entry and exit information, thereby obtaining the candidate function entry and exit information set; Step S24: determining whether the basic blocks in the candidate function exit basic block set of the candidate function exit information have at least two jump targets and whether they are all jumped immediately through the JUMP instruction; if the basic blocks have at least two jump targets and whether they are all jumped immediately through the JUMP instruction, then using them as the basic blocks at the function exit and the basic blocks at the function entry to form the determined function exit information, thereby obtaining the determined function exit information set; Step S25: basic blocks that can be traversed through jump relationships between the basic blocks at the function entry and the basic blocks at the function exit are combined into a function, and these basic blocks are used as the basic blocks included in the function.

[0009] Further, according to the Ethereum smart contract bytecode optimization method of the present invention, step S3 includes the following steps: Step S31: First, traverse the basic blocks included in the function to find out the basic blocks whose last instruction is a conditional jump JUMPI and are not loop bodies, and add them to the jump basic block set; Step S32: extracting basic blocks from the jump basic block set and adding them to the continuous conditional jump sequence; Step S33: extracting the last basic block from the continuous conditional jump sequence, and if the first instruction of the subsequent basic block is JUMPDEST and the last instruction is JUMPI, adding the subsequent basic block to the continuous conditional jump sequence; Step S34: repeat step S33 until the subsequent basic block of the last basic block in the continuous conditional jump sequence does not satisfy the first instruction being JUMPDEST and the last instruction being JUMPI; Step S35: traverse the basic blocks in the continuous conditional jump sequence in order, add the traversed basic blocks to the continuous conditional jump set, and determine whether there is a basic block subsequent to the basic block that satisfies the following conditions: the first instruction of the subsequent basic block is a POP instruction, the other instructions are conditional variable calculation instructions, and the subsequent basic block of the subsequent basic block is in the continuous conditional jump sequence, then add the subsequent basic block to the continuous conditional jump set; Step S36: traverse the basic blocks in the continuity conditional jump set, and if its subsequent basic block is not in the traversed continuity conditional jump set, add it as a jump target basic block to the jump target basic block set; Step S37: basic blocks in the continuity conditional jump set and basic blocks in the jump target basic block set are combined into a sub-control flow graph, and added to the first sub-graph set; Step S38: After removing the basic blocks in the continuous conditional jump set from the jump basic block set, repeat steps S32 to S38 until the jump basic block set is empty.

[0010] According to an Ethereum smart contract bytecode optimization device of the present invention, the device includes the following modules: Module M1 is used to: obtain the smart contract bytecode instruction set; Module M2 is used to: perform basic block segmentation and basic block jump analysis on the smart contract bytecode instruction set to determine the jump relationship between basic blocks and the scheduler basic block; then determine the function and the basic blocks contained in the function according to the function entry jump from the scheduler basic block and the exit jump to the scheduler basic block; Module M3 is used to: perform path analysis of the control flow graph on the basic blocks included in the function according to the control flow graph and path determined by the basic block jump relationship, find out the sub-control flow graph composed of the basic block representing the continuity conditional jump of the non-loop continuity conditional judgment statement of the source code and its jump target basic block, and obtain a first sub-graph set; Module M4 is used to: perform symbolic execution analysis on the bytecode instructions corresponding to the sub-control flow graph in the first sub-graph set, and obtain the bytecode instructions for conditional variable calculation in the sub-control flow graph, and the constraint relationship between the conditional variable determination item and the jump target basic block through the symbolic execution analysis; the conditional variable determination item is either a Boolean conditional variable or a conditional variable comparison expression about a single comparison operator; Module M5 is used to: construct a binary decision tree according to the constraint relationship between the conditional variable judgment item and the jump target basic block in the sub-control flow graph; the binary decision tree is composed of nodes and edges representing the parent-child relationship of the nodes to form a tree data structure; wherein the nodes include branch nodes with two child nodes and leaf nodes without child nodes; the branch nodes at the same level of the constructed binary decision tree are the same conditional variable judgment item in the sub-control flow graph; the two child nodes of the branch node respectively represent two subsequent processings of the conditional variable judgment item corresponding to the branch node being true or false; the leaf node is the jump target basic block in the sub-control flow graph; Module M6 is used to loop through the branch nodes including leaf nodes in the binary decision tree, and perform the following pruning on the traversed branch nodes: If the two child nodes of the branch node point to the same jump target basic block, the edge from the parent node of the branch node to the branch node points to the jump target basic block; If two branch nodes are isomorphic and are two child nodes of the parent node, then the edge from the parent node of the branch node's parent node to the parent node of the branch node points to one of the two branch nodes; The two branch nodes are isomorphic, which means that when the condition variable judgment item is true, the child nodes of the two branch nodes are the same, and when the condition variable judgment item is false, the child nodes of the two branch nodes are the same; Module M7 is used to: construct corresponding jump basic blocks for branch nodes in the pruned binary decision tree according to the bytecode instructions and jump target basic blocks calculated by the conditional variables, and readjust the position of the jump target basic blocks to replace the basic blocks in the atomic control flow graph.

[0011] Further, according to the Ethereum smart contract bytecode optimization device of the present invention, in the module M5, when constructing a binary decision tree, the condition variable determination items are first sorted, and the condition variable determination items at different levels of the constructed binary decision tree are configured according to the sorted condition variable determination items; the sorting of the condition variable determination items includes the following modules: Module M51, used to: construct a truth table between true and false values ​​of conditional variable determination items and jump target basic blocks according to the constraint relationship between conditional variable determination items and jump target basic blocks in the sub-control flow graph; Module M52 is used to calculate the evaluation value of each condition variable determination item according to the truth table, and the evaluation value of each condition variable determination item is calculated using the following formula: eV(i)=abs(max(TCount(i,j)-FCount(i,j)); where, eV(i) represents the evaluation value of the i-th conditional variable judgment item; abs means absolute value; TCount(i,j) represents the number of basic blocks of the jth jump target corresponding to the true value of the i-th conditional variable judgment item in the truth table; FCount(i,j) represents the number of basic blocks of the j-th jump target corresponding to the false value of the i-th condition variable judgment item in the truth table; max means taking the maximum value among all jump target basic blocks; Module M53 is used to sort the condition variable determination items in descending order according to their evaluation values.

[0012] Further, according to the Ethereum smart contract bytecode optimization device of the present invention, the module M2 includes the following modules: Module M21 is used to: traverse the scheduler basic block, and if the last instruction of the scheduler basic block is a JUMP instruction, and the basic block of the jump target does not belong to the scheduler basic block, add the basic block of the jump target to the first basic block set; Module M22 is used to: determine whether there are at least two other basic blocks that use the basic block in the first basic block set as a jump target, and whether these basic blocks that use the other basic blocks in the first basic block set as a jump target are all immediately jumped using a JUMP instruction; if there are at least two other basic blocks that use the basic block in the first basic block set as a jump target and whether these basic blocks that use the other basic blocks in the first basic block set as a jump target are all immediately jumped using a JUMP instruction, then these basic blocks in the first basic block set are combined into a second basic block set; Module M23 is used to: perform a deep search of the jump target basic block according to the jump relationship between the basic blocks, starting from the basic block in the second basic block set as the basic block at the function entry, until the jump target of the last JUMP immediate jump instruction of the basic block is found to be the scheduler basic block; then add the basic block whose immediate jump target is the scheduler basic block to the candidate function exit basic block set; the basic block at the function entry and the candidate function exit basic block set constitute the candidate function entry and exit information, thereby obtaining the candidate function entry and exit information set; Module M24 is used to: determine whether there are at least two jump targets of the basic blocks in the candidate function exit basic block set of the candidate function exit information and whether they are all jumped immediately through the JUMP instruction; if there are at least two jump targets of the basic block and whether they are all jumped immediately through the JUMP instruction, use them as the basic block at the function exit and the basic block at the function entry to form the determined function exit information, thereby obtaining the determined function exit information set; The module M25 is used to: compose a function from the basic blocks that can be traversed through the jump relationship between the basic blocks at the function entry and the basic blocks at the function exit, and use these basic blocks as the basic blocks included in the function.

[0013] Further, according to the Ethereum smart contract bytecode optimization device of the present invention, the module M3 includes the following modules: Module M31 is used to: first traverse the basic blocks included in the function to find out the basic blocks whose last instruction is a conditional jump JUMPI and are not loop bodies, and add them to the jump basic block set; Module M32, used for: extracting basic blocks from the jump basic block set and adding them to the continuous conditional jump sequence; Module M33 is used to: extract the last basic block from the continuous conditional jump sequence, and if the first instruction of the subsequent basic block is JUMPDEST and the last instruction is JUMPI, add the subsequent basic block to the continuous conditional jump sequence; Module M34 is used to: repeatedly execute the function of module M33 until the subsequent basic block of the last basic block in the continuous conditional jump sequence does not satisfy the first instruction is JUMPDEST and the last instruction is JUMPI; Module M35 is used to: sequentially traverse the basic blocks in the continuous conditional jump sequence, add the traversed basic blocks to the continuous conditional jump set, and determine whether there is a basic block subsequent to the basic block that satisfies the following conditions: the first instruction of the subsequent basic block is a POP instruction, the other instructions are conditional variable calculation instructions, and the subsequent basic block of the subsequent basic block is in the continuous conditional jump sequence, then add the subsequent basic block to the continuous conditional jump set; Module M36, used to: traverse the basic blocks in the continuity conditional jump set, and if its subsequent basic block is not in the traversed continuity conditional jump set, add it as a jump target basic block to the jump target basic block set; Module M37, used to: combine the basic blocks in the continuity conditional jump set and the basic blocks in the jump target basic block set into a sub-control flow graph, and add the sub-control flow graph to the first sub-graph set; Module M38 is used to: after removing the basic blocks in the continuous conditional jump set from the jump basic block set, repeatedly execute the functions of modules M32 to M38 until the jump basic block set is empty.

[0014] According to a medium of the present invention, a program instruction set that can be read by a machine is stored in the medium. When the program instruction set stored in the medium is read and executed by a machine, the machine can implement the above-mentioned Ethereum smart contract bytecode optimization method.

[0015] According to a device of the present invention, the device includes a connected processor and a memory; a program instruction set is stored in the memory; when the program instruction set stored in the memory is read and executed by the processor, the device can implement the above-mentioned Ethereum smart contract bytecode optimization method.

[0016] The technical effects of the present invention are as follows: the present invention optimizes the conditional jump instructions in the Ethereum smart contract bytecode instruction set, deletes unnecessary intermediate jumps and repeated calculations to achieve overall optimization. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] Figure 1 It is a control flow graph obtained by the example bytecode of the present invention.

[0018] Figure 2 It is a schematic diagram of the structure of an electronic device according to an embodiment of the present invention.

[0019] Figure 3 It is a truth table obtained by the example bytecode of the present invention and used to represent the constraint relationship between the condition variable judgment item and the jump target basic block.

[0020] Figure 4 and Figure 6 They are binary decision trees constructed by two different sequences of conditional variable determination items in the example bytecode.

[0021] Figure 5 Yes Figure 4 The result of optimizing the binary decision tree in .

[0022] Figure 7 and Figure 8 Respectively Figure 6 The optimization results of the first and second rounds of traversal of the binary decision tree in . DETAILED DESCRIPTION

[0023] The present invention is further described in detail below with reference to the accompanying drawings.

[0024] The Ethereum smart contract bytecode optimization method of the present invention is implemented by a computer program running on an electronic device. That is, the Ethereum smart contract bytecode optimization method of the present invention is a method implemented by an electronic device by executing a computer program instruction set. The electronic device here, referred to as the device, is a general-purpose computer device of the von Neumann type, specifically a server that can connect to the network and communicate data through the network connection, refer to Figure 2Example, including processor 201, memory 202 and communication unit 203. The processor 201 is connected to the memory 202 and the communication unit 203. The memory 202 is used to store computer program instruction sets and data. The processor 201 implements the Ethereum smart contract bytecode optimization method referred to in the present invention by loading and executing the computer program instruction set stored in the memory 202. The memory 202 is the machine-readable medium referred to in the present invention, which is usually a sustainable storage device, including but not limited to disks, tapes, solid-state hard drives, etc. The communication unit 203 is used for interconnection between electronic devices.

[0025] The Ethereum smart contract bytecode optimization method of the present invention is to optimize the conditional jumps in the Ethereum smart contract bytecode instruction set, and mainly includes the following steps: function basic block decomposition step, condition judgment basic block extraction step, symbolic execution step, binary decision tree construction step, binary decision tree optimization step, jump optimization step. In the aforementioned step S1, the acquisition in the smart contract bytecode instruction set is represented as the input of the smart contract bytecode instruction set of the present invention. Smart contracts and smart contract bytecode instruction sets are Ethereum-specific concepts. The smart contract bytecode instruction set is an intermediate program instruction compiled by a smart contract developed according to the Solidity language through the corresponding solc compiler, which can be loaded and executed by the Ethereum virtual machine.

[0026] The following sample code defines a simple Ethereum smart contract example.

[0027] contract Copium is Context, IERC20, Ownable { ... address public dexPair; ... function _tTransfer(address s, address r, uint256 a, bool tf) private { if (dexPair == s&&tf) { ... / / BlockA } else if (dexPair == r&&tf) { ... / / BlockB else { ... / / BlockC } } } After compiling the smart contract in the above example, the following smart contract bytecode instruction set can be obtained: ... 10774:JUMPDEST 10775:[dexPair==s] 10776:DUP1 10777:ISZERO 10778:PUSH 10782 10779:JUMPI 10780:POP 10781:[tf] 10782:JUMPDEST 10783:ISZERO 10784:PUSH 11230 10785:JUMPI 10786: / / BLOCKA ... 11230:JUMPDEST 11231:[dexPair==r] 11232:DUP1 11233:ISZERO 11234:PUSH 11238 11235:JUMPI 11236:POP 11237:[tf] 11238:JUMPDEST 11239:ISZERO 11240:PUSH 11878 11241:JUMPI 11242: / / BLOCKB ... 11878: / / BLOCKC ... For ease of explanation, each step of the Ethereum smart contract bytecode optimization method of this embodiment is illustrated using the smart contract bytecode instruction set of the above example.

[0028] The function basic block decomposition step, that is, the aforementioned step S2, performs basic block segmentation and basic block jump analysis on the smart contract bytecode instruction set to determine the jump relationship between the basic blocks and the basic blocks, and determine the scheduler basic block; then determine the function and the basic blocks contained in the function according to the function entry jump from the scheduler basic block and the exit jump to the scheduler basic block. The output of this step is the function and the basic blocks contained in the function. The basic block is an instruction segment in the program instruction that is always executed in the coding order. The jump instruction makes it impossible for the program instruction to be executed in the coding order, so the jump instruction or the jump destination can be used as the dividing boundary of the basic block. For example, in the smart contract bytecode instruction set of the above example, the instruction at address 10774 corresponds to the entry of the Ethereum smart contract function _tTransfer, the JUMPI jump instruction at address 10779 is the split point, and the destination address 10782 of the jump instruction is the split point. By analogy, the following basic blocks can be obtained: Basic Block 1: 10774:JUMPDEST 10775:[dexPair==s] 10776:DUP1 10777:ISZERO 10778:PUSH 10782 10779:JUMPI Basic Block 2: 10780:POP 10781:[tf] Basic Block 3: 10782:JUMPDEST 10783:ISZERO 10784:PUSH 11230 10785:JUMPI Basic Block 4: 10786: / / BLOCKA, 10786~11229 ... Basic Block 5: 11230:JUMPDEST 11231:[dexPair==r] 11232:DUP1 11233:ISZERO 11234:PUSH 11238 11235:JUMPI Basic Block 6: 11236:POP 11237:[tf] Basic Block 7: 11238:JUMPDEST 11239:ISZERO 11240:PUSH 11878 11241:JUMPI Basic Block 8: 11242: / / BLOCKB, 11242~11877 ... Basic Block 9: 11878: / / BLOCKC, 11878~12235 ... It should be noted that in the above steps, the determination of the scheduler basic block is used to determine the function entry. In Ethereum smart contracts, when calling a contract, a scheduler program will be entered first. The program will compare the function signature hash of the calling parameter and jump to the corresponding function body for execution. If the basic block belongs to the scheduler program, it is called a scheduler basic block.

[0029] In order to determine the scheduler basic block, this embodiment uses a third-party tool to determine which bytecode instructions in the smart contract bytecode instruction set belong to the scheduler program. Therefore, when dividing the basic blocks, it is only necessary to determine whether the bytecode instructions in the basic block belong to the scheduler program.

[0030] In addition, the jump relationship between basic blocks can be regarded as a directed graph with basic blocks as nodes and jump relationships as edges. This directed graph is called a control flow graph. In other words, this step implies the step of building a control flow graph based on the smart contract bytecode instruction set. Building a control flow graph is essentially the same as determining the jump relationship between basic blocks. Taking the smart contract bytecode instruction set in the above example as an example, we can get the following: Figure 1 The control flow graph shown.

[0031] Since the bytecode instructions do not contain function definition instructions, nor do they have proprietary function call related instructions such as CALL, RET, etc. When the smart contract is compiled into bytecode, the function call in the smart contract is compiled into a JUMP instruction by the compiler. When the function returns, it also returns through the JUMP instruction. The JUMP instruction uses the top value of the stack as the jump destination address.

[0032] The function and the basic blocks it contains must first determine the function's entry and exit. According to the characteristics of the smart contract bytecode instruction set, the basic blocks at the function's entry and exit must meet the following conditions: The basic block at the function entry has at least two incoming edges, and all incoming edges are immediate jumps; The basic block at the function entry is not a scheduler basic block, but at least one of the predecessor basic blocks that jump to the basic block is a scheduler basic block.

[0033] The basic block at the function exit has at least two outgoing edges, and all outgoing edges are immediate jumps; The basic block at the function exit is not a scheduler basic block itself, but there is at least one outgoing edge jumping to the scheduler basic block.

[0034] According to the conditions that the basic blocks at the function entry and the basic blocks at the function exit need to meet, this embodiment determines the function and the basic blocks contained in the function according to the following steps: Step S21: traverse the scheduler basic block, if the last instruction of the scheduler basic block is a JUMP instruction, and the basic block of the jump target does not belong to the scheduler basic block, then add the basic block of the jump target to the first basic block set; Step S22: Determine whether there are at least two other basic blocks that use the basic block in the first basic block set as a jump target, and whether these basic blocks that use the other basic blocks in the first basic block set as a jump target are all immediately jumped using a JUMP instruction; if there are at least two other basic blocks that use the basic block in the first basic block set as a jump target, and whether these basic blocks that use the other basic blocks in the first basic block set as a jump target are all immediately jumped using a JUMP instruction, then these basic blocks in the first basic block set are combined into a second basic block set; Step S23: according to the jump relationship between basic blocks, starting from the basic block in the second basic block set as the basic block at the function entry, a deep search of the jump target basic block is performed until the jump target of the last JUMP immediate jump instruction of the basic block is found to be the scheduler basic block; then the basic block whose immediate jump target is the scheduler basic block is added to the candidate function exit basic block set; the basic block at the function entry and the candidate function exit basic block set constitute the candidate function entry and exit information, thereby obtaining the candidate function entry and exit information set; Step S24: determining whether the basic blocks in the candidate function exit basic block set of the candidate function exit information have at least two jump targets and whether they are all jumped immediately through the JUMP instruction; if the basic blocks have at least two jump targets and whether they are all jumped immediately through the JUMP instruction, then using them as the basic blocks at the function exit and the basic blocks at the function entry to form the determined function exit information, thereby obtaining the determined function exit information set; Step S25: basic blocks that can be traversed through jump relationships between the basic blocks at the function entry and the basic blocks at the function exit are combined into a function, and these basic blocks are used as the basic blocks included in the function.

[0035] In the above steps, steps S21, S22, S23 and S24 respectively correspond to the relevant processing of the second, first, fourth and third conditions in the conditions that the basic blocks at the entrance of the above function and the basic blocks at the exit of the function need to satisfy.

[0036] It should be pointed out that the JUMP instruction uses the top value of the stack as the jump target address. The jump target address of the JUMP instruction when the function returns needs to be pushed into the stack before the function is called. Therefore, when determining the jump target of the function exit in the above step S24, it is necessary to analyze the top value of the stack of the JUMP instruction.

[0037] The conditional judgment basic block extraction step, namely the aforementioned step S3, performs a control flow graph path analysis on the basic blocks contained in the function according to the control flow graph and path determined by the basic block jump relationship, finds out the sub-control flow graph composed of the basic block representing the continuous conditional jump of the non-loop continuity conditional judgment statement of the source code and its jump target basic block, and obtains the first sub-graph set.

[0038] Conditional jumps in smart contract bytecode refer to JUMPI instructions, and the corresponding basic block of conditional jumps is the basic block with the last instruction of JUMPI instruction. The basic blocks of continuous conditional jumps representing non-loop continuous conditional judgment statements in the source code refer to the basic blocks of these conditional jumps compiled from the continuous conditional judgment statements in the smart contract source code. For example, in the aforementioned examples of smart contracts and smart contract bytecode instruction sets, the conditional judgment statement if(dexPair == s&&tf) is compiled into basic blocks 1, 2, and 3, and the conditional judgment statement if (dexPair == r&&tf) is compiled into basic blocks 5, 6, and 7. The continuous conditional statements correspond to the above example, which means that when the condition (dexPair == s&&tf) in the conditional statement if(dexPair == s&&tf) evaluates to false, the conditional statement if(dexPair == r&&tf) is executed. At this time, the execution of the conditional statements if (dexPair == s&&tf) and if (dexPair == r&&tf) is continuous. Of course, in the above sample code, the codes of BLOCKA, BLOCKB and BLOCKC are omitted. If the first statement in the codes of BLOCKA, BLOCKB and BLOCKC is a conditional statement, then the conditional statement is continuous with the above conditional statements if (dexPair == s&&tf) and if (dexPair == r&&tf). Finding out the basic blocks of continuous conditional jumps representing non-cyclic continuity conditional judgment statements in the source code refers to the continuous basic blocks 1, 2, 3, 5, 6, and 7 corresponding to the continuous conditional judgment statements if (dexPair == s&&tf) and if (dexPair == r&&tf) compiled into bytecode instructions. These basic blocks of conditional judgment representing non-cyclic continuity conditional judgment statements in the source code are also continuous. The jump target basic block refers to the basic block corresponding to the different code bodies executed under different conditions of the above conditional judgment statements. For example, the conditional judgment statements if (dexPair == s&&tf) and if (dexPair == r&&tf) in the above example respectively execute the code bodies of BLOCKA, BLOCKB, and BLOCKC under different conditional values. Corresponding to the smart contract bytecode instruction set, these parts of the code bodies correspond to basic blocks 4, 8, and 9 respectively. In the actual code, the code body of the BLOCKA, BLOCKB and BLOCKC parts may include multiple basic blocks. When used here to represent the jump target basic block, it is only necessary to take the first basic block in the bytecode instruction set corresponding to the code body of the BLOCKA, BLOCKB and BLOCKC parts.

[0039] In order to find out the basic block representing the continuous conditional jump of the non-loop continuity conditional judgment statement of the source code and its jump target basic block, this embodiment specifically adopts the following steps: Step S31: First, traverse the basic blocks included in the function to find out the basic blocks whose last instruction is a conditional jump JUMPI and are not loop bodies, and add them to the jump basic block set; Step S32: extracting basic blocks from the jump basic block set and adding them to the continuous conditional jump sequence; Step S33: extracting the last basic block from the continuous conditional jump sequence, and if the first instruction of the subsequent basic block is JUMPDEST and the last instruction is JUMPI, adding the subsequent basic block to the continuous conditional jump sequence; Step S34: repeat step S33 until the subsequent basic block of the last basic block in the continuous conditional jump sequence does not satisfy the first instruction being JUMPDEST and the last instruction being JUMPI; Step S35: traverse the basic blocks in the continuous conditional jump sequence in order, add the traversed basic blocks to the continuous conditional jump set, and determine whether there is a basic block subsequent to the basic block that satisfies the following conditions: the first instruction of the subsequent basic block is a POP instruction, the other instructions are conditional variable calculation instructions, and the subsequent basic block of the subsequent basic block is in the continuous conditional jump sequence, then add the subsequent basic block to the continuous conditional jump set; Step S36: traverse the basic blocks in the continuity conditional jump set, and if its subsequent basic block is not in the traversed continuity conditional jump set, add it as a jump target basic block to the jump target basic block set; Step S37: basic blocks in the continuity conditional jump set and basic blocks in the jump target basic block set are combined into a sub-control flow graph, and added to the first sub-graph set; Step S38: After removing the basic blocks in the continuous conditional jump set from the jump basic block set, repeat steps S32 to S38 until the jump basic block set is empty.

[0040] It should be pointed out that the basic blocks in the jump target basic block set obtained after the above processing must be executed one by one.

[0041] It should be noted that the basic block of the first entry node of the sub-control flow graph obtained after the above processing may contain other instructions not related to the jump. In this case, in another optional implementation, the first entry node jump of the sub-control flow graph in step S37 can be cut off according to the JUMPDEST instruction as the cutoff point. For example, the basic block 1 of the smart contract bytecode instruction set in the above example also includes other instructions before address 10774, which can be truncated to 10774, so that the basic block 1 in the sub-control flow graph is the truncated basic block 1.

[0042] The symbolic execution step, i.e., the aforementioned step S4, performs symbolic execution analysis on the bytecode instructions corresponding to the sub-control flow graph in the first sub-graph set, and obtains the bytecode instructions for conditional variable calculation in the sub-control flow graph through symbolic execution analysis, as well as the constraint relationship between the conditional variable judgment item and the jump target basic block. The conditional variable judgment item is divided into two cases: one is a Boolean conditional variable, such as tf in the bytecode of the aforementioned example; the other is a conditional variable comparison expression about a single comparison operator, such as dexPair==s or dexPair==r in the bytecode of the aforementioned example. The value of the conditional variable judgment item is always a Boolean value, which can be expressed as true and false or as 0 and 1. As mentioned above, the jump target basic block needs to be executed one by one. The constraint relationship between the conditional variable judgment item and the jump target basic block is the conditional variable judgment item value that needs to be satisfied for the jump target basic block to be executed one by one, which can be represented by a truth table about the jump target basic block being executed one by one. For example, the aforementioned example smart contract and smart contract bytecode instruction set can be obtained as follows Figure 3 The constraint relationship between the condition variable judgment item and the jump target basic block is shown.

[0043] It should be pointed out that if the condition variable judgment item involves mathematical calculations, a Boolean condition variable can be introduced as the condition variable judgment item. When the condition variable is introduced, the bytecode instructions related to the condition variable need to be recorded. For example, the above dexPair==s or dexPair==r can introduce condition variables s0 and s1 to represent dexPair==s and dexPair==r respectively. In more complex cases, such as when the condition variable judgment item is in the form of a+b>32, the corresponding sequence in the bytecode instruction may be as follows: JUMPDEST [a] [b] ADD PUSH 32 LT PUSH addr JUMPI In the above instruction sequence, a and b in the conditional variable determination item are pushed onto the top of the stack through the [a] and [b] instructions. The ADD instruction is used to add the a and b on the top of the stack and then put the result on the top of the stack. The LT instruction is used to compare the 32 on the top of the stack with the value of a + b. If 32 < a + b, then 1 is pushed onto the top of the stack. In such a complex situation, a boolean conditional variable can be introduced for a + b > 32, and the relevant bytecode instructions of this boolean conditional variable are extracted, including: [a] [b] ADD PUSH 32 LT Of course, another possible implementation method is to directly convert the above bytecode instructions into a determination formula of a + b > 32 as the conditional variable determination item.

[0044] The steps for constructing a binary decision tree are the aforementioned step S5. According to the constraint relationship between the conditional variable determination item in the sub-control flow graph and the jump target basic block, a binary decision tree is constructed. The binary decision tree consists of nodes and edges representing the parent-child relationship of the nodes, forming a tree-shaped data structure. Among them, the nodes include branch nodes with two child nodes and leaf nodes without child nodes. The branch nodes at the same level of the constructed binary decision tree are the same conditional variable determination item in the sub-control flow graph. The two child nodes of a branch node respectively represent two subsequent processes when the conditional variable determination item corresponding to the branch node is true or false; the leaf nodes are the jump target basic blocks in the sub-control flow graph. For example, in the aforementioned example of the smart contract bytecode instruction set, a binary decision tree as shown in Figure 4 or Figure 6 can be constructed. The dotted line and solid line in the figure respectively represent the situations where the conditional variable determination item is false and true. The node dexPair = s in the figure corresponds to the conditional variable determination item dexPair == s, and the node dexPair = r corresponds to the conditional determination item dexPair == r.

[0045] From Figure 4 and Figure 6 The comparison of the constructed binary decision trees shows that obviously, different conditional variable determination items have different binary decision tree structures as different parent-child node orders. Therefore, in this embodiment, the conditional variable determination items are first sorted, and the conditional variable determination items at different levels of the constructed binary decision tree are configured according to the sorted conditional variable determination items. The specific steps for sorting the conditional variable determination items are as follows: Step S51: According to the constraint relationship between the conditional variable determination item in the sub-control flow graph and the jump target basic block, construct a truth table between the true and false values of the conditional variable determination item and the jump target basic block; Step S52: Calculate the evaluation value of each condition variable determination item according to the truth table. The evaluation value of each condition variable determination item is calculated using the following formula: eV(i)=abs(max(TCount(i,j)-FCount(i,j)); where, eV(i) represents the evaluation value of the i-th conditional variable judgment item; abs means absolute value; TCount(i,j) represents the number of basic blocks of the jth jump target corresponding to the true value of the i-th conditional variable judgment item in the truth table; FCount(i,j) represents the number of basic blocks of the j-th jump target corresponding to the false value of the i-th condition variable judgment item in the truth table; max means taking the maximum value among all jump target basic blocks; Step S53: Sort the condition variable determination items in descending order according to their evaluation values.

[0046] by Figure 3 The truth table in is an example, TCCount(dexPair==s, basic block 4)=2; TFCount(dexPair==s, basic block 4)=0; TCCount(dexPair==s, basic block 8)=0; TFCount(dexPair==s, basic block 8)=1; TCCount(dexPair==s, basic block 9)=2; TFCount(dexPair==s, basic block 9)=3; Thus we get eV(dexPair==s)=2; TCCount(dexPair==r, basic block 4)=1;TFCount(dexPair==r, basic block 4)=1; TCCount(dexPair==r, basic block 8)=1; TFCount(dexPair==r, basic block 8)=0; TCCount(dexPair==r, basic block 9)=2; TFCount(dexPair==r, basic block 9)=3; Thus we get eV(dexPair==r)=1; TCCount(tf, basic block 4) = 2; TFCount(tf, basic block 4) = 0; TCCount(tf, basic block 8) = 1; TFCount(tf, basic block 8) = 0; TCCount(tf, basic block 9) = 1; TFCount(tf, basic block 9) = 4; Thus we get eV(tf)=3; According to the size of eV(dexPair==s)=2, eV(dexPair==r)=1 and eV(tf)=3, the conditional variable judgment items are sorted as tf, dexPair==s, dexPair==r. Figure 6 The binary decision tree shown.

[0047] The binary decision tree optimization step is the aforementioned step S6, which loops through the branch nodes of the binary decision tree, including the branch nodes with leaf nodes, and performs the following pruning processing on the traversed branch nodes: If the two child nodes of the branch node point to the same jump target basic block, the edge from the parent node of the branch node to the branch node points to the jump target basic block; If two branch nodes are isomorphic and are two child nodes of the parent node, then the edge from the parent node of the branch node’s parent node to the parent node of the branch node points to one of the two branch nodes; The two branch nodes are isomorphic in that when the condition variable judgment item is true, the child nodes of the two branch nodes are the same, and when the condition variable judgment item is false, the child nodes of the two branch nodes are the same.

[0048] by Figure 4 Take the binary decision tree in as an example. In the first round of traversal, there are 8 tf nodes including branch nodes with leaf nodes. The two child nodes of the leftmost tf node point to the same jump target basic block, that is, both point to basic block 9. Then the edge of the tf node's parent node dexpair=r pointing to the tf node points to basic block 9. The two rightmost tf nodes are isomorphic, that is, both point to basic block 4 when the condition variable judgment item is true, and both point to basic block 9 when it is false. These two tf nodes are two child nodes of the parent node dexpair=r. Then the parent node dexpair=s of the branch node tf parent node dexpair=r is pointed to one of the tf nodes. Finally, we get Figure 5 The pruned binary decision tree of the example.

[0049] by Figure 6Take the binary decision tree in as an example. In the first round of traversal, there are 8 dexpair=r nodes including leaf nodes. The two child nodes of the two leftmost dexpair=r nodes both point to basic block 9, so the edge of the parent node dexpair=s of the dexpair=r node pointing to the dexpair=r node points to basic block 9; the two child nodes of the rightmost dexpair=r node both point to basic block 4, so the edge of the parent node dexpair=s of the dexpair=r node pointing to the dexpair=r node points to basic block 4. After the first round of traversal, we get Figure 7 The binary decision tree of the example. In the second round of traversal, the branch nodes including leaf nodes are dexpair=s node and dexpair=r node. The two child nodes of the leftmost dexpair=s node both point to basic block 9, so the edge from the parent node tf of the dexpair=s node to the dexpair=s node points to basic block 9. Finally, we get Figure 8 Binary decision tree for example.

[0050] The jump optimization step, i.e., the aforementioned step S7, constructs corresponding jump basic blocks for the branch nodes in the pruned binary decision tree according to the bytecode instructions and jump target basic blocks calculated by the conditional variables, and readjusts the position of the jump target basic blocks to replace the basic blocks in the atomic control flow graph. The specific processing of this step is as follows: Step S71, count the maximum depth of each node in the binary decision tree after pruning, sort the nodes in order from small to large maximum depth, with leaf nodes taking precedence over branch nodes at the same depth, and obtain a node sequence. The nodes in the node sequence are divided into conditional variable determination item nodes and jump target basic block nodes. The conditional variable determination item nodes correspond to the branch nodes of the binary decision tree, and the jump target basic block nodes correspond to the leaf nodes of the binary decision tree.

[0051] Step S72, traversing the node sequence in order, for the traversed condition variable judgment item node, according to the bytecode instruction calculated by the corresponding condition variable, whether the next node of the node sequence is a subsequent node and whether the condition variable judgment item is required to be true or false when it is a subsequent node, pre-generate the corresponding bytecode instruction to obtain a jump basic block; in the pre-generated jump basic block, the jump address is to be filled; Step S73, traversing the node sequence in order, and calculating the start address of each basic block for the traversed nodes through the bytecode instructions of the corresponding basic blocks; Step S74, filling in the jump address into the pre-generated jump basic block according to the calculated start address of each basic block.

[0052] by Figure 8Taking the example of the pruned binary decision tree as an example, the node sequence obtained after sorting in step S71 is: tf node, dexpair=s node, basic block 4 node, dexpair=r node, basic block 9 node and basic block 8 node. Then, according to step S72, the jump basic blocks are pre-generated for the tf node, dexpair=s node and dexpair=r node as follows: tf node [tf] ISZERO PUSH {address of basic block 9 to be filled in} JUMPI dexpair=s node [dexpair==s] ISZERO PUSH {dexpair=r node basic block address to be filled in} JUMPI dexpair=r node [dexpair==r] PUSH {address of basic block 8 to be filled in} JUMPI Then, starting from the original starting address 10774, the addresses of each instruction are arranged as follows through step S73: 10774:JUMPDEST 10775:[tf] 10776:ISZERO 10777:PUSH {address of basic block 9 to be filled in} 10778:JUMPI 10780:JUMPDEST 10781:[dexpair==s] 10782:ISZERO 10783:PUSH {dexpair=r node basic block address to be filled in} 10784:JUMPI Original basic block 4: 10785: / / BLOCKA, address range 10786~11229 adjusted to: 10785~11228 ... 11229:JUMPDEST 11230:[dexpair==r] 11231:PUSH {address of basic block 8 to be filled in} 11232:JUMPI Original basic block 9: 11233: / / BLOCKC, the address range 11878~12235 is adjusted to: 11233~11590 ... Original basic block 8: 11591: / / BLOCKB, address range 11242~11877 adjusted to: 11591~12226 ... After filling in the jump address to be filled in step S74, the following optimized bytecode is finally obtained: 10774:JUMPDEST 10775:[tf] 10776:ISZERO 10777:PUSH 11233 10778:JUMPI 10780:JUMPDEST 10781:[dexpair==s] 10782:ISZERO 10783:PUSH 11229 10784:JUMPI Original basic block 4: 10785: / / BLOCKA, address range 10786~11229 adjusted to: 10785~11228 ... 11229:JUMPDEST 11230:[dexpair==r] 11231:PUSH 11591 11232:JUMPI Original basic block 9: 11233: / / BLOCKC, the address range 11878~12235 is adjusted to: 11233~11590 ... Original basic block 8: 11591: / / BLOCKB, address range 11242~11877 adjusted to: 11591~12226 ... From the comparison between the above bytecode instruction set and the original bytecode instruction set, it can be seen that unnecessary jumps are reduced and repeated calculations of conditional variables are reduced, thereby improving efficiency and reducing resource consumption.

[0053] In addition, the Ethereum smart contract bytecode optimization device referred to in the present invention is a virtual device implemented by a machine executing a computer program. The modules it contains correspond one-to-one to the steps in the Ethereum smart contract bytecode optimization method, and this specification will not go into details.

Claims

1. A method for optimizing Ethereum smart contract bytecode, characterized in that: The method comprises the following steps: Step S1: Obtain the smart contract bytecode instruction set; Step S2: Perform basic block segmentation and basic block jump analysis on the smart contract bytecode instruction set to determine the jump relationship between basic blocks and the scheduler basic block; then determine the function and the basic blocks contained in the function according to the function entry jump from the scheduler basic block and the exit jump to the scheduler basic block; Step S3: According to the control flow graph and path determined by the basic block jump relationship, the control flow graph path analysis is performed on the basic blocks included in the function, and the sub-control flow graph consisting of the basic block representing the continuity conditional jump of the non-loop continuity condition judgment statement of the source code and the jump target basic block is found to obtain a first sub-graph set; Step S4: performing symbolic execution analysis on the bytecode instructions corresponding to the sub-control flow graphs in the first sub-graph set, and obtaining the bytecode instructions for conditional variable calculation in the sub-control flow graphs, and the constraint relationship between the conditional variable determination items and the jump target basic blocks through the symbolic execution analysis; The condition variable determination item is either a Boolean condition variable or a condition variable comparison expression related to a single comparison operator; Step S5: construct a binary decision tree according to the constraint relationship between the conditional variable determination item and the jump target basic block in the sub-control flow graph; the binary decision tree is composed of nodes and edges representing the parent-child relationship of the nodes to form a tree data structure; wherein the nodes include branch nodes with two child nodes and leaf nodes without child nodes; the branch nodes at the same level of the constructed binary decision tree are the same conditional variable determination item in the sub-control flow graph; the two child nodes of the branch node respectively represent two subsequent processings of the conditional variable determination item corresponding to the branch node being true or false; the leaf node is the jump target basic block in the sub-control flow graph; Step S6: Loop through the branch nodes whose child nodes include leaf nodes in the binary decision tree, and perform the following pruning on the traversed branch nodes: If the two child nodes of the branch node point to the same jump target basic block, the edge from the parent node of the branch node to the branch node points to the jump target basic block; If two branch nodes are isomorphic and are two child nodes of the parent node, then the edge from the parent node of the branch node's parent node to the parent node of the branch node points to one of the two branch nodes; The two branch nodes are isomorphic, which means that when the condition variable judgment item is true, the child nodes of the two branch nodes are the same, and when the condition variable judgment item is false, the child nodes of the two branch nodes are the same; Step S7: According to the bytecode instructions and jump target basic blocks calculated by the conditional variables, corresponding jump basic blocks are constructed for the branch nodes in the pruned binary decision tree, and the positions of the jump target basic blocks are readjusted to replace the basic blocks in the atomic control flow graph.

2. The Ethereum smart contract bytecode optimization method according to claim 1, characterized in that: In step S5, when constructing a binary decision tree, the condition variable determination items are first sorted, and the condition variable determination items at different levels of the constructed binary decision tree are configured according to the sorted condition variable determination items; the sorting of the condition variable determination items includes the following steps: Step S51: constructing a truth table between true and false values ​​of the conditional variable determination item and the jump target basic block according to the constraint relationship between the conditional variable determination item and the jump target basic block in the sub-control flow graph; Step S52: Calculate the evaluation value of each condition variable determination item according to the truth table. The evaluation value of each condition variable determination item is calculated using the following formula: eV(i)=abs(max(TCount(i,j)-FCount(i,j)); in, eV(i) represents the evaluation value of the i-th conditional variable judgment item; abs means absolute value; TCount(i,j) represents the number of basic blocks of the jth jump target corresponding to the true value of the i-th conditional variable judgment item in the truth table; FCount(i,j) represents the number of basic blocks of the j-th jump target corresponding to the false value of the i-th condition variable judgment item in the truth table; max means taking the maximum value among all jump target basic blocks; Step S53: Sort the condition variable determination items in descending order according to their evaluation values.

3. The Ethereum smart contract bytecode optimization method according to claim 1 or 2, characterized in that: The step S2 comprises the following steps: Step S21: traverse the scheduler basic block, if the last instruction of the scheduler basic block is a JUMP instruction, and the basic block of the jump target does not belong to the scheduler basic block, then add the basic block of the jump target to the first basic block set; Step S22: Determine whether there are at least two other basic blocks that use the basic block in the first basic block set as a jump target, and whether these basic blocks that use the other basic blocks in the first basic block set as a jump target are all immediately jumped using a JUMP instruction; if there are at least two other basic blocks that use the basic block in the first basic block set as a jump target, and whether these basic blocks that use the other basic blocks in the first basic block set as a jump target are all immediately jumped using a JUMP instruction, then these basic blocks in the first basic block set are combined into a second basic block set; Step S23: according to the jump relationship between basic blocks, starting from the basic block in the second basic block set as the basic block at the function entry, a deep search of the jump target basic block is performed until the jump target of the last JUMP immediate jump instruction of the basic block is found to be the scheduler basic block; then the basic block whose immediate jump target is the scheduler basic block is added to the candidate function exit basic block set; the basic block at the function entry and the candidate function exit basic block set constitute the candidate function entry and exit information, thereby obtaining the candidate function entry and exit information set; Step S24: determining whether the basic blocks in the candidate function exit basic block set of the candidate function exit information have at least two jump targets and whether they are all jumped immediately through the JUMP instruction; if the basic blocks have at least two jump targets and whether they are all jumped immediately through the JUMP instruction, then using them as the basic blocks at the function exit and the basic blocks at the function entry to form the determined function exit information, thereby obtaining the determined function exit information set; Step S25: basic blocks that can be traversed through jump relationships between the basic blocks at the function entry and the basic blocks at the function exit are combined into a function, and these basic blocks are used as the basic blocks included in the function.

4. The Ethereum smart contract bytecode optimization method according to claim 1 or 2, characterized in that: The step S3 comprises the following steps: Step S31: First, traverse the basic blocks included in the function to find out the basic blocks whose last instruction is a conditional jump JUMPI and are not loop bodies, and add them to the jump basic block set; Step S32: extracting basic blocks from the jump basic block set and adding them to the continuous conditional jump sequence; Step S33: extracting the last basic block from the continuous conditional jump sequence, and if the first instruction of the subsequent basic block is JUMPDEST and the last instruction is JUMPI, adding the subsequent basic block to the continuous conditional jump sequence; Step S34: repeat step S33 until the subsequent basic block of the last basic block in the continuous conditional jump sequence does not satisfy the first instruction being JUMPDEST and the last instruction being JUMPI; Step S35: traverse the basic blocks in the continuous conditional jump sequence in order, add the traversed basic blocks to the continuous conditional jump set, and determine whether there is a basic block subsequent to the basic block that satisfies the following conditions: the first instruction of the subsequent basic block is a POP instruction, the other instructions are conditional variable calculation instructions, and the subsequent basic block of the subsequent basic block is in the continuous conditional jump sequence, then add the subsequent basic block to the continuous conditional jump set; Step S36: traverse the basic blocks in the continuity conditional jump set, and if its subsequent basic block is not in the traversed continuity conditional jump set, add it as a jump target basic block to the jump target basic block set; Step S37: basic blocks in the continuity conditional jump set and basic blocks in the jump target basic block set are combined into a sub-control flow graph, and added to the first sub-graph set; Step S38: After removing the basic blocks in the continuous conditional jump set from the jump basic block set, repeat steps S32 to S38 until the jump basic block set is empty.

5. An Ethereum smart contract bytecode optimization device, characterized in that: The device includes the following modules: Module M1 is used to: obtain the smart contract bytecode instruction set; Module M2 is used to: perform basic block segmentation and basic block jump analysis on the smart contract bytecode instruction set to determine the jump relationship between basic blocks and the scheduler basic block; then determine the function and the basic blocks contained in the function according to the function entry jump from the scheduler basic block and the exit jump to the scheduler basic block; Module M3 is used to: perform path analysis of the control flow graph on the basic blocks included in the function according to the control flow graph and path determined by the basic block jump relationship, find out the sub-control flow graph composed of the basic block representing the continuity conditional jump of the non-loop continuity conditional judgment statement of the source code and its jump target basic block, and obtain a first sub-graph set; Module M4 is used to: perform symbolic execution analysis on the bytecode instructions corresponding to the sub-control flow graph in the first sub-graph set, and obtain the bytecode instructions for conditional variable calculation in the sub-control flow graph, and the constraint relationship between the conditional variable determination item and the jump target basic block through the symbolic execution analysis; The condition variable determination item is either a Boolean condition variable or a condition variable comparison expression related to a single comparison operator; Module M5 is used to: construct a binary decision tree according to the constraint relationship between the conditional variable judgment item and the jump target basic block in the sub-control flow graph; the binary decision tree is composed of nodes and edges representing the parent-child relationship of the nodes to form a tree data structure; wherein the nodes include branch nodes with two child nodes and leaf nodes without child nodes; the branch nodes at the same level of the constructed binary decision tree are the same conditional variable judgment item in the sub-control flow graph; the two child nodes of the branch node respectively represent two subsequent processings of the conditional variable judgment item corresponding to the branch node being true or false; the leaf node is the jump target basic block in the sub-control flow graph; Module M6 is used to loop through the branch nodes including leaf nodes in the binary decision tree, and perform the following pruning on the traversed branch nodes: If the two child nodes of the branch node point to the same jump target basic block, the edge from the parent node of the branch node to the branch node points to the jump target basic block; If two branch nodes are isomorphic and are two child nodes of the parent node, then the edge from the parent node of the branch node's parent node to the parent node of the branch node points to one of the two branch nodes; The two branch nodes are isomorphic, which means that when the condition variable judgment item is true, the child nodes of the two branch nodes are the same, and when the condition variable judgment item is false, the child nodes of the two branch nodes are the same; Module M7 is used to: construct corresponding jump basic blocks for branch nodes in the pruned binary decision tree according to the bytecode instructions and jump target basic blocks calculated by the conditional variables, and readjust the position of the jump target basic blocks to replace the basic blocks in the atomic control flow graph.

6. The Ethereum smart contract bytecode optimization device according to claim 5, characterized in that: In the module M5, when constructing a binary decision tree, the condition variable determination items are first sorted, and the condition variable determination items at different levels of the constructed binary decision tree are configured according to the sorted condition variable determination items; the sorting of the condition variable determination items includes the following modules: Module M51, used to: construct a truth table between true and false values ​​of conditional variable determination items and jump target basic blocks according to the constraint relationship between conditional variable determination items and jump target basic blocks in the sub-control flow graph; Module M52 is used to calculate the evaluation value of each condition variable determination item according to the truth table, and the evaluation value of each condition variable determination item is calculated using the following formula: eV(i)=abs(max(TCount(i,j)-FCount(i,j)); in, eV(i) represents the evaluation value of the i-th conditional variable judgment item; abs means absolute value; TCount(i,j) represents the number of basic blocks of the jth jump target corresponding to the true value of the i-th conditional variable judgment item in the truth table; FCount(i,j) represents the number of basic blocks of the j-th jump target corresponding to the false value of the i-th condition variable judgment item in the truth table; max means taking the maximum value among all jump target basic blocks; Module M53 is used to sort the condition variable determination items in descending order according to their evaluation values.

7. The Ethereum smart contract bytecode optimization device according to claim 5 or 6, characterized in that: The module M2 includes the following modules: Module M21 is used to: traverse the scheduler basic block, and if the last instruction of the scheduler basic block is a JUMP instruction, and the basic block of the jump target does not belong to the scheduler basic block, add the basic block of the jump target to the first basic block set; Module M22 is used to: determine whether there are at least two other basic blocks that use the basic block in the first basic block set as a jump target, and whether these basic blocks that use the other basic blocks in the first basic block set as a jump target are all immediately jumped using a JUMP instruction; if there are at least two other basic blocks that use the basic block in the first basic block set as a jump target and whether these basic blocks that use the other basic blocks in the first basic block set as a jump target are all immediately jumped using a JUMP instruction, then these basic blocks in the first basic block set are combined into a second basic block set; Module M23 is used to: perform a deep search of the jump target basic block according to the jump relationship between the basic blocks, starting from the basic block in the second basic block set as the basic block at the function entry, until the jump target of the last JUMP immediate jump instruction of the basic block is found to be the scheduler basic block; then add the basic block whose immediate jump target is the scheduler basic block to the candidate function exit basic block set; the basic block at the function entry and the candidate function exit basic block set constitute the candidate function entry and exit information, thereby obtaining the candidate function entry and exit information set; Module M24 is used to: determine whether there are at least two jump targets of the basic blocks in the candidate function exit basic block set of the candidate function exit information and whether they are all jumped immediately through the JUMP instruction; if there are at least two jump targets of the basic block and whether they are all jumped immediately through the JUMP instruction, use them as the basic block at the function exit and the basic block at the function entry to form the determined function exit information, thereby obtaining the determined function exit information set; The module M25 is used to: compose a function from the basic blocks that can be traversed through the jump relationship between the basic blocks at the function entry and the basic blocks at the function exit, and use these basic blocks as the basic blocks included in the function.

8. The Ethereum smart contract bytecode optimization device according to claim 5 or 6, characterized in that: The module M3 includes the following modules: Module M31 is used to: first traverse the basic blocks included in the function to find out the basic blocks whose last instruction is a conditional jump JUMPI and are not loop bodies, and add them to the jump basic block set; Module M32, used for: extracting basic blocks from the jump basic block set and adding them to the continuous conditional jump sequence; Module M33 is used to: extract the last basic block from the continuous conditional jump sequence, and if the first instruction of the subsequent basic block is JUMPDEST and the last instruction is JUMPI, add the subsequent basic block to the continuous conditional jump sequence; Module M34 is used to: repeatedly execute the function of module M33 until the subsequent basic block of the last basic block in the continuous conditional jump sequence does not satisfy the first instruction is JUMPDEST and the last instruction is JUMPI; Module M35 is used to: sequentially traverse the basic blocks in the continuous conditional jump sequence, add the traversed basic blocks to the continuous conditional jump set, and determine whether there is a basic block subsequent to the basic block that satisfies the following conditions: the first instruction of the subsequent basic block is a POP instruction, the other instructions are conditional variable calculation instructions, and the subsequent basic block of the subsequent basic block is in the continuous conditional jump sequence, then add the subsequent basic block to the continuous conditional jump set; Module M36, used to: traverse the basic blocks in the continuity conditional jump set, and if its subsequent basic block is not in the traversed continuity conditional jump set, add it as a jump target basic block to the jump target basic block set; Module M37, used to: combine the basic blocks in the continuity conditional jump set and the basic blocks in the jump target basic block set into a sub-control flow graph, and add the sub-control flow graph to the first sub-graph set; Module M38 is used to: after removing the basic blocks in the continuous conditional jump set from the jump basic block set, repeatedly execute the functions of modules M32 to M38 until the jump basic block set is empty.

9. A medium, characterized in that The medium stores a program instruction set that can be read by a machine. When the program instruction set stored in the medium is read and executed by a machine, the machine can implement the Ethereum smart contract bytecode optimization method according to any one of claims 1 to 4.

10. A device, characterized in that: The device includes a connected processor and a memory; a program instruction set is stored in the memory; when the program instruction set stored in the memory is read and executed by the processor, the device can implement the Ethereum smart contract bytecode optimization method according to any one of claims 1 to 4.

Citation Information

Patent Citations

  • Intelligent contract formal verification system and method based on state space

    CN111062038A

  • Smart contract processing method and device, medium and electronic equipment

    CN112104709A

  • Intelligent contract optimization method and device based on source codes

    CN112631656A

  • Method and system for executing smart contract, block chain node and storage medium

    CN112866392A

  • Vulnerability detection method for smart contract based on low-dimensional byte code features

    CN115758388A