Smart contract code translation and detection method, apparatus, device, and medium
Patent Information
- Application Number
- CN202210320803.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-21
- Publication Date
- 2026-10-09
- Estimated Expiration
- 2042-03-21
AI Technical Summary
Petri网有坚实的数学理论支撑,成熟的图形分析技术,强大的仿真工具,可模拟实际系统,分析实际系统的具有网系统的一些性质如可达性、有界性、安全性、活性、虹吸与陷阱等,但现有针对智能合约代码自动生成petri网模型的转化算法仍旧缺乏,基于形式化技术的智能合约建模方法仍旧由手工来进行,且缺乏一体化的自动化建模与分析系统框架
[0023]This application presents an automated modeling and analysis system for smart contract solidity code models adapted to the PIPE analysis tool, based on Petri nets. Before the smart contract code is written and deployed on the blockchain, the system automatically models the smart contract code, describes its structural features through the model, and then inputs the obtained structural feature model into the PIPE analysis tool to perform boundedness, security, and deadlock analysis on the structural features of the smart contract.
Smart Images

Figure CN116820468B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of smart contract conversion and analysis technology. In particular, it relates to a method, apparatus, device, and medium for converting and detecting smart contract code. Background Technology
[0002] Blockchain is a chain-like data structure that combines data blocks sequentially in chronological order. It is a distributed ledger that is cryptographically guaranteed to be immutable and unforgeable, and features decentralization, tamper-proofness, and traceability.
[0003] Smart contracts in a blockchain network are pieces of executable code deployed on the blockchain. Anyone can audit contracts stored on the chain. The immutability of the blockchain means that once a smart contract is published on the real-time network, any vulnerability or error becomes permanent and could lead to significant economic losses. Therefore, analyzing the correctness and other security features of smart contracts during the design phase has received increasing attention from academia and industry. Scholars both domestically and internationally have conducted extensive research on formal methods for smart contracts, including theorem proving, model checking, formal modeling, and finite automata.
[0004] Petri nets, proposed by German computer scientist Carl Adam Petri in the 1960s, are formal modeling tools for describing discrete-event concurrent systems. They possess rigorous mathematical definitions and graphical representation capabilities. A Petri net consists of three basic elements: places, transitions, and directed arcs, with places and transitions connected by directed arcs. Petri nets have solid mathematical theoretical support, mature graphical analysis techniques, and powerful simulation tools, enabling them to simulate real-world systems and analyze properties of such systems, including reachability, boundedness, security, liveness, siphoning, and traps. However, existing algorithms for automatically generating Petri net models from smart contract code are still lacking. Formal-based smart contract modeling methods are still performed manually, and an integrated, automated modeling and analysis system framework is lacking.
[0005] Therefore, this application proposes an automated method for generating Petri net models for solidity code of smart contracts and a matching analysis method based on this problem, thus forming an automatic modeling and detection system for smart contract code. Summary of the Invention
[0006] In view of the shortcomings of the prior art described above, the purpose of this application is to provide a method, apparatus, device and medium for converting and detecting smart contract code, so as to solve the deficiencies of the Petri net model for smart contracts in the prior art.
[0007] To achieve the above and other related objectives, this application provides a method for converting and detecting smart contract code. The method includes: preprocessing the smart contract code; formulating algorithmic rules for converting the smart contract code into Petri nets, thereby converting the sequential structure, selection structure, loop structure, and call structure in the smart contract code into Petri net structures respectively; and inputting the obtained Petri net structures into the PIPE tool for defect detection.
[0008] In one embodiment of this application, the preprocessing of the smart contract code includes: deleting redundant code from the smart contract code; normalizing and splitting the processed smart contract code; using the functions in the smart contract code as Petri net subnets to be transformed, and pre-storing the content of the functions in the corresponding data structure.
[0009] In one embodiment of this application, the method for converting the sequential structure into a Petri net structure includes: representing an executed statement as a transition in a Petri net; and connecting a place representing the execution completion state after the execution of the executed statement is completed.
[0010] In one embodiment of this application, the method for converting the selection structure into a Petri net structure includes: when an if statement of the selection structure appears, calling dealIf() to create a transition place and corresponding arc representing the if branch; calling getEndIf() to obtain the line number endIf where the if statement ends, looping from line if-1 to line endIf to process the statements within the if branch; processing sequential statements to create corresponding transition places and corresponding arcs, and ending the processing of the if branch.
[0011] In one embodiment of this application, the step of calling getEndIf() to obtain the line number endIf of the if statement, and looping from line if-1 to line endIf to process the statements within the if branch, includes: determining whether the statements within the if branch are nested if structures; if so, recursively calling dealIf() to return to the previous step; otherwise, continuing to the next step; determining whether the statements within the if branch are loop statements; if so, calling dealLoop() of the MyLoop class to process the loop statement; otherwise, continuing to the next step; determining whether the statements within the if branch are function call statements; if so, calling dealFunCall() of the MyFunction class to process the function call; otherwise, continuing to the subsequent steps.
[0012] In one embodiment of this application, the method includes: determining in the endIf line whether the selection structure contains else; if not, the selection structure is a single if conditional statement, creating a transition library representing the non-execution of the statements within the if and ending the else branch processing; if yes, determining whether it is a cascading if-else structure; if yes, recursively calling dealIf() for processing; otherwise, looping through all statements from else-1 line to endElse line, and calling the corresponding function to process nested if selection statements, nested loop statements, function call statements, and ordinary execution statements.
[0013] In one embodiment of this application, the method includes: after the if branch processing is completed, recording the last library created by the if branch, endIfPlace; after the else branch processing is completed, recording the last library created by the else branch, endElsePlace; finding all transitions connected to the endElsePlace library, deleting endElsePlace and all arcs connected to it, and reconnecting arcs to endIfPlace starting from all found transitions, completing the branch subnet of the selection structure or loop structure cyclic judgment and outputting it.
[0014] In one embodiment of this application, the method for converting the loop structure into a Petri net structure includes: when a line begins with a keyword statement of the loop structure, recording the loop start line loopStart, calling dealLoop(); creating the first transition place and corresponding arc that satisfy the loop condition branch, calling getEndLoop() to obtain the line number endLoop where the loop ends, and looping all loop processing code from loopStart - 1 to endLoop.
[0015] In one embodiment of this application, the loop processing of all loop statements from line loopStart - 1 to line endLoop includes: determining whether the current loop processing statement is a nested loop structure; if so, recursively calling dealLoop(); otherwise, continuing to the next step; determining whether the current loop processing statement is an if statement; if so, calling dealIf(); otherwise, continuing to the next step; determining whether the current loop processing statement is a function call; if so, calling dealFunCall(); otherwise, processing ordinary statements within the loop; after all statements within the loop have been processed, connecting the last transition library created within the loop to the loop's initial library; creating a second transition library that does not satisfy the loop condition, starting from the loop's initial library and ending with transitions that do not satisfy the loop condition, to complete the transformation of the loop structure.
[0016] In one embodiment of this application, the method for converting the call structure into a Petri net structure includes: obtaining all the code of the called function; traversing all the code from the beginning line of the function to the end line of the function until the end.
[0017] In one embodiment of this application, the method includes: determining whether the current function line is an if statement; if so, calling dealIf(); otherwise, continuing to the next step; determining whether the current function line is a loop structure; if so, calling dealLoop(); otherwise, continuing to the next step; determining whether the current function line is a function call; if so, processing the ordinary statements within the loop and creating its transition library arc; if so, continuing to the next step; determining whether the current function line is a recursive function call; if so, recursively calling dealLoop(); if so, directly connecting the transition link arc created by the recursive call line to the function initial library.
[0018] To achieve the above and other related objectives, this application provides a smart contract code conversion and detection device, the device comprising: a preprocessing module for preprocessing smart contract code; a conversion module for formulating algorithmic rules for converting the smart contract code into Petri nets, thereby converting sequential structures, selection structures, loop structures, and call structures in the smart contract code into Petri net structures respectively; and a detection module for inputting the obtained Petri net structures into a PIPE tool for defect detection.
[0019] To achieve the above and other related objectives, this application provides a computer device, the device comprising: a memory and a processor; the memory storing a computer program, and the processor executing the computer program to implement the method described above.
[0020] To achieve the above and other related objectives, this application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method described above.
[0021] As described above, the present application discloses a method, apparatus, device, and medium for converting and detecting smart contract code. This involves preprocessing the smart contract code; formulating algorithmic rules for converting the smart contract code into Petri nets, thereby converting sequential structures, selection structures, loop structures, and call structures in the smart contract code into Petri net structures; and inputting the obtained Petri net structures into the PIPE tool for defect detection.
[0022] It has the following beneficial effects:
[0023] This application presents an automated modeling and analysis system for smart contract solidity code models adapted to the PIPE analysis tool, based on Petri nets. Before the smart contract code is written and deployed on the blockchain, the system automatically models the smart contract code, describes its structural features through the model, and then inputs the obtained structural feature model into the PIPE analysis tool to perform boundedness, security, and deadlock analysis on the structural features of the smart contract. Attached Figure Description
[0024] Figure 1 The diagram shows a flowchart of a smart contract code conversion and detection method according to one embodiment of this application.
[0025] Figure 2 The diagram shown is a flowchart illustrating the processing sequence structure in one embodiment of this application.
[0026] Figure 3 The diagram shows a flowchart of the sequential structure to Petri net conversion in one embodiment of this application.
[0027] Figure 4 The diagram shown is a flowchart illustrating the selection structure in one embodiment of this application.
[0028] Figure 5 The diagram shows a process for selecting a structure to transform a Petri net in one embodiment of this application.
[0029] Figure 6A This is a schematic diagram illustrating the process of selecting a structure that does not return to the same repository in one embodiment of this application.
[0030] Figure 6B This is a schematic diagram illustrating the process of selecting a structure to return to the same storage location in one embodiment of this application.
[0031] Figure 7 The diagram shown is a flowchart illustrating the processing of a loop structure in one embodiment of this application.
[0032] Figure 8 The diagram shows a flowchart of the transformation of a loop structure into a Petri net in one embodiment of this application.
[0033] Figure 9 The diagram shown is a flowchart illustrating the processing of a call structure in one embodiment of this application.
[0034] Figure 10 The diagram shows a flowchart of an analyzable Petri net model generated automatically in one embodiment of this application.
[0035] Figure 11 The diagram shown is a schematic representation of a smart contract code conversion and detection device in one embodiment of this application.
[0036] Figure 12 The diagram shown is a flowchart of a smart contract code conversion and detection device according to one embodiment of this application.
[0037] Figure 13 The diagram shown is a structural schematic of a computer device according to one embodiment of this application. Detailed Implementation
[0038] The following specific examples illustrate the implementation of this application. Those skilled in the art can easily understand other advantages and effects of this application from the content disclosed in this specification. This application can also be implemented or applied through other different specific embodiments, and various details in this specification can also be modified or changed based on different viewpoints and applications without departing from the spirit of this application. It should be noted that, unless otherwise specified, the following embodiments and features in the embodiments can be combined with each other.
[0039] It should be noted that the illustrations provided in the following embodiments are only schematic representations of the basic concept of this application. Therefore, the drawings only show the components related to this application and are not drawn according to the actual number, shape and size of the components in the actual implementation. In the actual implementation, the form, quantity and proportion of each component can be arbitrarily changed, and the layout of the components may also be more complex.
[0040] To address the aforementioned issues, this application presents an automated modeling, transformation, and analysis system for smart contract solidity code models adapted to the PIPE analysis tool, based on Petri nets. This system automatically models the smart contract code before it is written and deployed on the blockchain, describing its structural characteristics. The system then inputs the transformed structural characteristic model into the PIPE analysis tool to analyze the smart contract's structural characteristics for properties such as boundedness, security, and deadlock.
[0041] Petri nets are a graphical modeling method that consists of a set of places (circles), transitions (squares), directed arcs (directed arrows), and tokens (current state identifiers). Places represent conditions or resource fulfillment. Transitions represent the execution of events, actions, or statements. There cannot be directed arcs between transitions or places. A transition has a certain number of input and output places, representing the preconditions and postconditions of an event, respectively. Tokens in a place represent the amount of available resources or the current state of the system. Petri nets can be used to simulate the specific details of program execution; transitions represent the execution process of a statement, precondition places represent the state before execution, postcondition places represent the state after execution, and directed arcs represent the specific direction of the program flow.
[0042] like Figure 1The diagram illustrates a flowchart of a smart contract code conversion and detection method according to an embodiment of this application. As shown, the method includes:
[0043] Step S101: Preprocess the smart contract code.
[0044] Specifically, the preprocessing of smart contract code includes:
[0045] Redundant code preprocessing: Remove redundant code from the smart contract code, such as removing some comments and blank lines in the smart contract code.
[0046] Function transformation preprocessing: First, the processed smart contract code is normalized and split; for the functions, the functions in the smart contract code are used as Petri net subnets to be transformed, and the contents of the functions are pre-stored in the corresponding data structures.
[0047] Step S102: Formulate algorithm rules for converting the smart contract code into a Petri net, so as to convert the sequential structure, selection structure, loop structure, and call structure (function call) in the smart contract code into a Petri net structure respectively;
[0048] In one embodiment of this application, the method for converting the sequential structure into a Petri net structure includes:
[0049] A. Represent an executed statement as a transition in a Petri net.
[0050] B. After the execution of the statement is completed, a repository indicating the completion status is connected after the transition.
[0051] In simple terms, declaration statements, assignment statements, and even function call statements can be considered a type of sequential structure. This application specifies that in a sequential structure, an executed statement is represented as a transition in a Petri net. After the statement is executed, a placeholder representing the completed state needs to be connected after the transition. This is the most basic Petri net representation method for sequential structures. Therefore, in this case, in the program structure of the Petri net input for a smart contract, no matter how complex the intermediate structure is, it must be a process of entering one placeholder and exiting another. Under certain conditions, the process for handling sequential structures is as follows: Figure 2 As shown.
[0052] For example, the code: uint256 amount = 1, its program can be converted into a Petri net representation as follows: Figure 3 As shown, Figure 3In this model, P0 represents the state before executing the statement, T0 represents the execution process of the statement, and P1 represents the state after executing the statement. Here, we ignore nesting with other program flow structures. However, in reality, sequential structures may nest function call statements. In such cases, we need to consider the Petri representation of function call statements and the representation of the called functions. At this point, the Petri net has formed a basic Petri net subnet, and any other structure within this model can be extended from this structure.
[0053] In one embodiment of this application, the method for converting the selection structure into a Petri net structure includes:
[0054] A. When an if statement with a selection structure appears, call dealIf() to create a transition library and corresponding arc representing the if branch; call getEndIf() to get the line number endIf where the if statement ends, and loop from line if-1 to line endIf to process the statements within the if branch.
[0055] Specifically, processing the statements within the if branch includes:
[0056] First, determine if the statement within the if branch is a nested if structure. If it is, recursively call dealIf() to return to the previous step; otherwise, continue to the next step.
[0057] Then, it checks whether the statement in the if branch is a loop statement. If it is, it calls the dealLoop() method of the MyLoop class to handle the loop statement; otherwise, it continues to the next step.
[0058] Finally, determine whether the statement within the if branch is a function call statement. If it is, call the dealFunCall() method of the MyFunction class to handle the function call; otherwise, continue with the subsequent steps.
[0059] B. Processing sequential statements creates the corresponding transition library and the corresponding arc, and the if branch processing ends.
[0060] Furthermore, in the `endIf` line, it is determined whether the selection structure contains `else`; if not, the selection structure is a single `if` statement, and a transition library representing the non-execution of the statements within the `if` statement and the `else` branch processing ends; if so, it is determined whether it is a cascading `if-else` structure; if so, `dealIf()` is recursively called for processing; otherwise, all statements from `else-1` line to `endElse` line are looped, and the corresponding functions are called to process nested `if` selection statements, nested loop statements, function call statements, and ordinary execution statements.
[0061] In simple terms, the process of handling selection structures is as follows: Figure 4 As shown:
[0062] 1) When an if statement is encountered, the dealIf() method is called; a transition library and corresponding arc representing the "if" branch are created, and getEndIf() is called to get the line number endIf where the if ends. The loop from line if-1 to line endIf processes the statements within the "if" branch.
[0063] 2) If the statement in the branch starts with "if", then the dealIf() method is called recursively and returned; otherwise, it continues.
[0064] 2.1) Determine if this statement is a loop statement. If it is, call the dealLoop() method of the MyLoop class to handle the loop statement; otherwise, continue.
[0065] 2.2) Determine if it is a function call statement. If it is, call the dealFunCall() method of the MyFunction class to handle the function call. Otherwise, continue.
[0066] 2.3) Process sequential statements, create their corresponding transition places and arcs, at which point the "if" branch processing ends.
[0067] 3) In the endIf line, check if this selection structure contains else. If not, this structure is a single if statement, and you only need to create a transition library and arc representing "do not execute the statement inside if". At this time, the "else" branch processing ends, that is, the processing of this selection structure ends. If the endIf line contains else, check if it is an if-else cascade structure. If it is, directly recursively call the dealIf() method for processing. If not, loop through all statements from else-1 line to endElse line and call the corresponding function to process nested if selection statements, nested loop statements, function call statements and ordinary execution statements.
[0068] Generally, selection structures appear frequently in smart contract code, typically used to indicate whether a condition is met, as shown in the following code:
[0069] if (isReceiveLocked == 1) {
[0070] until = lockedWalletInfo[_addr].timeLockUpEnd;
[0071] }
[0072] The above selection structure code program is converted into a Petri net representation as follows: Figure 5As shown: At this point, there is only an `if` branch, no `else` branch. Therefore, there is no state within the `else` branch, only a transition T1 indicating "do not execute the statements within the `if` branch," which connects to the ending place P2 of the `if` branch. In actual smart contract code, the selection structure can be more complex, including nested selections within `if` statements, nested selections within `else` statements, `if-else` cascades, etc. When entering the initial place of this selection structure, it determines whether the condition is met and enters different paths, such as from P0 to T0 or from P0 to T1. However, the end of this selection structure will always return to a final place, P2.
[0073] The above are all standard if statements. Smart contracts also contain selection statements that end in a single line or two lines. See the code below:
[0074] if (!mayHaveLockedTokens[_account]) return 0;
[0075] if (!mayHaveLockedTokens[_account])
[0076] uint locked = pNumberOfLockedTokens(_account)
[0077] If the if statement ends with a single or two lines, the line number returned in the endIf statement is modified so that the dealIf() function directly creates one or two transitions within the if branch, along with the corresponding place and arc, and then ends the selection structure. This completes the processing of special if selection statements, effectively improving the success rate and fault tolerance of model transformation.
[0078] In one embodiment of this application, a normalization processing method is further provided for branches in the selection structure processing:
[0079] A. After the if branch finishes processing, record the last library created by that if branch: endIfPlace. After the else branch finishes processing, record the last library created by that else branch: endElsePlace.
[0080] B. Locate all transitions connected to the `endElsePlace` library, delete `endElsePlace` and all arcs connected to it, and reconnect arcs to `endIfPlace` starting from all found transitions. This completes the branch subnet for the selection structure or loop structure's loop judgment and outputs it.
[0081] When converting smart contracts, regardless of the complexity of the internal program flow structure, the input program must always enter one repository and exit through another. However, in selection or loop structures, the branch generated after all code in the "if" selection branch has been processed and the branch generated after all code in the "else" branch has been processed are two different repositories. This means that after the entire selection structure is executed, the system state will not return to the same repository, which clearly does not meet the requirements. Figure 6A This indicates that the selection structure does not return to the same repository. Connect all transitions linked to the last repository of the "else" branch to the last repository of the "if" branch, such as... Figure 6B The display shows the selected structure returning to the same repository.
[0082] In one embodiment of this application, the method for converting the loop structure into a Petri net structure includes:
[0083] A. When a line begins with a keyword statement of a loop structure, record the loop start line loopStart and call dealLoop();
[0084] B. Create the first transition library and corresponding arc that satisfy the loop condition branch, call getEndLoop() to get the line number endLoop where the loop ends, and loop all the loop processing code from loopStart - 1 to endLoop.
[0085] Specifically, 1) Determine if the statement currently being processed is a nested loop structure. If so, recursively call dealLoop(); otherwise, continue to the next step; 2) Determine if the statement currently being processed is an if statement. If so, call dealIf(); otherwise, continue to the next step; 3) Determine if the statement currently being processed is a function call. If so, call dealFunCall(); otherwise, process the ordinary statements within the loop; 4) After all statements within the loop have been processed, connect the last transition library created within the loop to the loop's initial library; 5) Create a second transition library that does not satisfy the loop condition. Starting from the loop's initial library, create arcs based on the transitions that do not satisfy the loop condition to complete the transformation of the loop structure.
[0086] In simple terms, the process for handling loop structures is as follows: Figure 7 As shown:
[0087] 1) When encountering a loop keyword, i.e. a line starting with "for" or "while", record the loop start line loopStart and call the dealLoop() method.
[0088] 2) First, create transition libraries and corresponding arcs that satisfy the loop condition branches, then call getEndLoop() to get the line number endLoop where the loop ends. The loop contains all the code that handles the loop from line loopStart - 1 to line endLoop.
[0089] 2.1) Determine if the current loop is a nested loop structure. If it is, recursively call the dealLoop() method. Otherwise, continue.
[0090] 2.2) Determine if the current statement is an if statement. If it is, call the dealIf() method. If it is not an if statement, continue.
[0091] 2.3) Determine if the current operation is a function call. If so, call the dealFunCall() method. Otherwise, process the normal statements within the loop.
[0092] 2.4) After all the ordinary statements in the loop have been processed, connect the last transition created in the loop to the initial library at the beginning of the loop.
[0093] 3) Create transitions that do not meet the loop condition. Starting from the initial location of the loop, create arcs that end at the transitions that do not meet the loop condition to complete such a loop structure.
[0094] Generally, loop structures typically include for loops and while loops, but they are essentially the same: they both first check if the loop condition is met. If the condition is met, the loop body is entered, and after the loop body finishes executing, the loop returns to the condition check. If the condition is not met, the loop proceeds directly to the next statement without entering the loop body. For example:
[0095] for(uint index = 0;backerIndex < backers.length;backerIndex++){
[0096] setWhiteBacker(_backers[backerIndex], true);
[0097] }
[0098] The above loop structure code can be converted into a Petri net representation as follows: Figure 8As shown, the code above represents only a basic loop structure and does not include nested structures such as selection structures or function calls. As shown in the diagram, after entering the loop, the loop condition is first checked. If the condition is met, the loop body is entered, i.e., the path represented by the arc from P0 to T0. After the loop body is executed (i.e., after T2 is executed), the loop returns directly to the initial location, P0, and then the next iteration begins. This continues until the loop condition is no longer met, at which point the loop body enters the path represented by the arc from P0 to T1, finally returning to the loop termination location, P2.
[0099] In one embodiment of this application, the method for converting a call structure into a Petri net structure includes:
[0100] A. Obtain all the code of the called function.
[0101] B. Traverse all code within the function from the beginning line to the end line until the end.
[0102] Specifically, 1) Determine if the current function line is an if statement; if so, call dealIf(); otherwise, proceed to the next step; 2) Determine if the current function line is a loop structure; if so, call dealLoop(); otherwise, proceed to the next step; 3) Determine if the current function line is a function call; if not, process the ordinary statements within the loop and create its transition library arc; if so, proceed to the next step; 4) Determine if the current function line is a recursive function call; if not, recursively call dealLoop(); if it is a self-recursive function call, directly connect the transition link arc created by the recursive call line to the function's initial library, indicating that the function is directly returned to its initial state and re-executed. The process for handling call structures is as follows: Figure 9 As shown.
[0103] For example, the smart contract code is:
[0104] contract transfer(){
[0105] for(uint index = 0;backerIndex < backers.length;backerIndex++){
[0106] setWhiteBacker(_backers[backerIndex], true);
[0107] }
[0108] }
[0109] Through the above implementation scheme, the code is converted into text stored in XML format, recording the structural features of the Petri net model.
[0110] Step S103: Input the obtained Petri net structure into the PIPE tool for defect detection.
[0111] In short, A) The PIPE tool is automatically launched via the interface, opens the model XML file, and then the PIPE tool opens it as follows: Figure 10 As shown, it is presented as an analyzable Petri net model generated after automatic conversion.
[0112] B. Based on the embedded analysis module in PIPE, the basic properties of the model are analyzed, and the detectable safety properties include boundedness, safety, ring structure, siphon and trap.
[0113] Typically, siphons and traps can cause three types of problems: if they appear in the middle, the program may get stuck in a loop and unable to exit, or it may exit but be unable to return to the current state; if they appear at the beginning, they may loop indefinitely without entering the next state; and if they appear at the end, they may be impossible to terminate. For smart contracts, such defects can put the account of the contract owner at risk.
[0114] Satisfying boundedness means that the number of tokens in a repository will not accumulate to positive infinity indefinitely. If boundedness is not satisfied, it means that some repositories may be in a state of indefinite accumulation, which may lead to long-term occupation of certain resources during contract execution or prevent the release of resource space.
[0115] Meeting security requirements means that a single repository will contain at most one token. Failure to meet these requirements indicates that tokens may be accumulated multiple times in some repositories. This may not necessarily be a code logic error, but it at least carries the risk of security issues or wasted resources.
[0116] The occurrence of a ring structure means that there is a subnet where tokens are always circulating, and this may continue indefinitely. This can lead to boundedness and security issues, resulting in certain resources being occupied and unable to be released, or thread occupancy problems.
[0117] PIPE (Plaform Independent Petri Net Editor) was developed in 2002 by the Department of Computer Science at Imperial College London. After four revisions, it significantly improved the GUI, added new modeling features, and introduced new analysis modules, thus becoming a fairly complete, mature, and efficient Petri net analysis tool. PIPE is an open-source, platform-independent tool based on Java. It boasts an elegant and easy-to-use graphical user interface for creating, saving, loading, and analyzing Petri nets. It incorporates the latest XML Petri net storage format standard and provides a complete set of analysis modules for examining behavioral attributes, generating performance statistics, and some less common characteristics.
[0118] like Figure 11 The figure shows a schematic diagram of a smart contract code conversion and detection device according to an embodiment of this application. As shown, the smart contract code conversion and detection device 1100 includes:
[0119] The preprocessing module 1101 is used for preprocessing smart contract code.
[0120] The conversion module 1102 is used to formulate algorithm rules for converting the smart contract code into a Petri net, thereby converting the sequential structure, selection structure, loop structure, and call structure in the smart contract code into a Petri net structure.
[0121] The detection module 1103 inputs the obtained Petri net structure into the PIPE tool for defect detection.
[0122] It is understandable that the device 1100, through the operation of its various modules, can achieve the following: Figure 1 The aforementioned method for converting and detecting smart contract code. For example... Figure 12 The diagram shown illustrates the process of a smart contract code conversion and testing device.
[0123] It should be noted that the division of the various modules in the above device is merely a logical functional division. In actual implementation, they can be fully or partially integrated into a single physical entity, or they can be physically separated. Furthermore, these modules can be implemented entirely in software via processing element calls; they can be fully implemented in hardware; or some modules can be implemented by processing element calls to software, while others are implemented in hardware. For example, the detection module 1103 can be a separate processing element, or it can be integrated into a chip in the above device. Alternatively, it can be stored as program code in the memory of the above device, and called and executed by a processing element of the above device. The implementation of other modules is similar. Moreover, these modules can be fully or partially integrated together, or they can be implemented independently. The processing element mentioned here can be an integrated circuit with signal processing capabilities. In the implementation process, each step of the above method or each of the above modules can be completed through the integrated logic circuits in the hardware of the processor element or through software instructions.
[0124] For example, these modules can be one or more integrated circuits configured to implement the above methods, such as one or more Application Specific Integrated Circuits (ASICs), one or more digital signal processors (DSPs), or one or more Field Programmable Gate Arrays (FPGAs). As another example, when a module is implemented using processing element scheduler code, the processing element can be a general-purpose processor, such as a Central Processing Unit (CPU) or other processor capable of calling program code. Furthermore, these modules can be integrated together to form a system-on-a-chip (SOC).
[0125] like Figure 13 The diagram illustrates the structure of a computer device according to one embodiment of this application. As shown, the computer device 1300 includes a memory 1301 and a processor 1302. The memory 1301 stores a computer program, and the processor 1302 executes the computer program to implement the following... Figure 1 The method for converting and detecting smart contract code.
[0126] The memory 1301 may include random access memory (RAM) and may also include non-volatile memory, such as at least one disk storage device.
[0127] The processor 1302 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0128] In one embodiment of this application, a computer-readable storage medium stores a data transfer program thereon, which, when executed by a processor, implements as follows: Figure 1 The method for converting and detecting smart contract code.
[0129] As will be understood by those skilled in the art, all or part of the steps of the above-described method embodiments can be implemented using computer program-related hardware. The aforementioned image processing program can be stored in a computer-readable storage medium. When executed, the program performs the steps of the above-described method embodiments; and the aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disks, or optical disks.
[0130] These computer programs can also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0131] Computer-readable media includes both permanent and non-permanent, removable and non-removable media that can store information using any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.
[0132] In summary, this application provides a method, apparatus, device, and medium for converting and detecting smart contract code, which effectively overcomes the various shortcomings of the prior art and has high industrial application value.
[0133] The above embodiments are merely illustrative of the principles and effects of this application and are not intended to limit this application. Any person skilled in the art can modify or alter the above embodiments without departing from the spirit and scope of this application. Therefore, all equivalent modifications or alterations made by those skilled in the art without departing from the spirit and technical concept disclosed in this application should still be covered by the claims of this application.
Claims
1. A method for converting and detecting smart contract code, characterized in that, The method includes: The preprocessing of smart contract code includes: deleting redundant code from the smart contract code; normalizing and splitting the processed smart contract code; using functions in the smart contract code as Petri net subnets to be transformed, and pre-storing the content of the functions into the corresponding data structures in advance; An algorithmic rule is formulated for converting the smart contract code into a Petri net, thereby converting the sequential structure, selection structure, loop structure, and call structure in the smart contract code into a Petri net structure. The method for converting the selection structure into a Petri net structure includes: when an if statement in a selection structure appears, call `dealIf()` to create a transition library and corresponding arc representing the if branch; call `getEndIf()` to obtain the line number `endIf` where the if statement ends, and loop from line `if-1` to line `endIf` to process the statements within the if branch; process sequential statements to create corresponding transition libraries and corresponding arcs, and end the if branch processing; in the line `endIf`, determine whether the selection structure contains `else`; if not, the selection structure is a single if statement, create a transition library and arc representing the non-execution of the statements within the if, and end the else branch processing; if yes, determine whether it is a cascaded if-else structure; if yes, recursively call `dealIf()` for processing; otherwise, loop through `else-` Process all statements from line 1 to line 'endElse', and call the corresponding functions to handle nested if selection statements, nested loop statements, function call statements, and ordinary execution statements; after the if branch is processed, record the last library created by that if branch, 'endIfPlace'; after the else branch is processed, record the last library created by that else branch, 'endElsePlace'; find all transitions connected to the 'endElsePlace' library, delete 'endElsePlace' and all arcs connected to it, and reconnect arcs to 'endIfPlace' starting from all found transitions, completing the branch subnet of the selection structure or loop structure's loop judgment and outputting it; The method for converting the loop structure into a Petri net structure includes: when a line begins with a keyword statement of the loop structure, recording the loop start line `loopStart`, and calling `dealLoop()`; creating the first transition place and corresponding arc that satisfy the loop condition branch, calling `getEndLoop()` to get the loop end line number `endLoop`, and looping through all loop-processing code from line `loopStart - 1` to line `endLoop`. The code processes all loop statements from line 1 to line endLoop, including: checking if the current loop statement is a nested loop structure; if so, recursively calling dealLoop(); otherwise, proceeding to the next step; checking if the current loop statement is an if statement; if so, calling dealIf(); otherwise, proceeding to the next step; checking if the current loop statement is a function call; if so, calling dealFunCall(); otherwise, processing ordinary statements within the loop; after all statements within the loop have been processed, connecting the last transition string created within the loop to the loop's initial string; creating a second transition string that does not satisfy the loop condition, starting from the loop's initial string and ending with the transition that does not satisfy the loop condition, to complete the transformation of the loop structure; The method for converting the call structure into a Petri net structure includes: obtaining all the code of the called function; traversing all the code from the beginning line to the end line of the function until the end; the method includes: determining whether the current function line is an if statement, if so, calling dealIf(); otherwise, continuing to the next step; determining whether the current function line is a loop structure, if so, calling dealLoop(); otherwise, continuing to the next step; determining whether the current function line is a function call, if so, processing the ordinary statements within the loop and creating its transition library arc; if so, continuing to the next step; determining whether the current function line is a recursive function call, if so, recursively calling dealLoop(); if so, directly connecting the transitions created by the recursive call line to the function's initial library. The obtained Petri net structure is input into the PIPE tool for defect detection.
2. The smart contract code conversion and detection method according to claim 1, characterized in that, The method for converting the sequential structure into a Petri net structure includes: Represent an executed statement as a transition in a Petri net; After the execution of the statement is completed, a repository indicating the completion status is connected after the transition.
3. The smart contract code conversion and detection method according to claim 1, characterized in that, The call to getEndIf() retrieves the line number endIf of the if statement, and the loop from line if-1 to line endIf processes the statements within the if branch, including: Determine if the statements within the if branch are nested if structures. If they are, recursively call dealIf() to return to the previous step; otherwise, continue to the next step. Determine if the statement within the if branch is a loop statement. If it is, call the dealLoop() method of the MyLoop class to handle the loop statement; otherwise, proceed to the next step. Determine if the statement within the if branch is a function call statement. If it is, call the dealFunCall() method of the MyFunction class to handle the function call; otherwise, continue with the subsequent steps.
4. A smart contract code conversion and detection device, characterized in that, The device includes: The preprocessing module is used to preprocess the smart contract code, including: deleting redundant code from the smart contract code; normalizing and splitting the processed smart contract code; using the functions in the smart contract code as Petri net subnets to be transformed, and pre-storing the contents of the functions into the corresponding data structures in advance; The transformation module is used to formulate algorithmic rules for transforming the smart contract code into a Petri net, thereby transforming the sequential structure, selection structure, loop structure, and call structure in the smart contract code into a Petri net structure. The method for transforming the selection structure into a Petri net structure includes: when an if statement in a selection structure appears, calling `dealIf()` to create a transition library and corresponding arc representing the if branch; calling `getEndIf()` to obtain the line number `endIf` where the if statement ends, and looping from line `if-1` to line `endIf` to process the statements within the if branch; processing sequential statements to create corresponding transition libraries and corresponding arcs, ending the if branch processing; determining whether the selection structure contains an `else` statement in the `endIf` line; if not, the selection structure is a single if statement, creating a transition library and arc representing the non-execution of the statements within the if statement, ending the `else` branch processing; if so, determining whether it is a cascading if-else structure; if so, recursively calling `dealIf()` for processing; otherwise, looping through `else-` Process all statements from line 1 to line 'endElse', and call the corresponding functions to handle nested if selection statements, nested loop statements, function call statements, and ordinary execution statements; after the if branch is processed, record the last library created by that if branch, 'endIfPlace'; after the else branch is processed, record the last library created by that else branch, 'endElsePlace'; find all transitions connected to the 'endElsePlace' library, delete 'endElsePlace' and all arcs connected to it, and reconnect arcs to 'endIfPlace' starting from all found transitions, completing the branch subnet of the selection structure or loop structure's loop judgment and outputting it; The method for converting the loop structure into a Petri net structure includes: when a line begins with a keyword statement of the loop structure, recording the loop start line `loopStart`, and calling `dealLoop()`; creating the first transition place and corresponding arc that satisfy the loop condition branch, calling `getEndLoop()` to get the loop end line number `endLoop`, and looping through all loop-processing code from line `loopStart - 1` to line `endLoop`. The code processes all loop statements from line 1 to line endLoop, including: checking if the current loop statement is a nested loop structure; if so, recursively calling dealLoop(); otherwise, proceeding to the next step; checking if the current loop statement is an if statement; if so, calling dealIf(); otherwise, proceeding to the next step; checking if the current loop statement is a function call; if so, calling dealFunCall(); otherwise, processing ordinary statements within the loop; after all statements within the loop have been processed, connecting the last transition string created within the loop to the loop's initial string; creating a second transition string that does not satisfy the loop condition, starting from the loop's initial string and ending with the transition that does not satisfy the loop condition, to complete the transformation of the loop structure; The method for converting the call structure into a Petri net structure includes: obtaining all the code of the called function; traversing all the code from the beginning line to the end line of the function until the end; the method includes: determining whether the current function line is an if statement, if so, calling dealIf(); otherwise, continuing to the next step; determining whether the current function line is a loop structure, if so, calling dealLoop(); otherwise, continuing to the next step; determining whether the current function line is a function call, if so, processing the ordinary statements within the loop and creating its transition library arc; if so, continuing to the next step; determining whether the current function line is a recursive function call, if so, recursively calling dealLoop(); if so, directly connecting the transitions created by the recursive call line to the function's initial library. The detection module inputs the obtained Petri net structure into the PIPE tool for defect detection.
5. A computer device, characterized in that, The device includes: a memory and a processor; the memory stores a computer program, and the processor executes the computer program to implement the method as described in any one of claims 1-3.
6. A computer-readable storage medium, characterized in that, It stores a computer program that, when executed by a processor, implements the method as described in any one of claims 1-3.
Citation Information
Patent Citations
Intelligent contract formal verification system and method based on state space
CN111062038A