Smart contract formal specification method and device, electronic equipment and storage medium

Through the formal specification method of smart contracts, the problem of difficulty in ensuring security before the deployment of smart contracts is solved, and formal specifications are embedded in the form of annotations, which improves the development efficiency and rigor of specifications.

CN119440485APending Publication Date: 2025-02-14THE THIRD RES INST OF MIN OF PUBLIC SECURITY +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411455586.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-17
Publication Date
2025-02-14

AI Technical Summary

Technical Problem

The existing technology is difficult to effectively ensure that smart contracts are highly secure before deployment, and the learning threshold for formal verification languages ​​is high, the popularity is limited, and developers are inefficient in building formal specifications.

Method used

Provide a formal specification method of smart contracts, which can obtain object code, determine mathematical logic types and statement types, configure target statement specifications based on behavioral interface specifications, and embed target logical expressions and target statement specifications into the target in the form of comments. Code.

Benefits of technology

Ensure that formal specifications are rigorous in describing smart contract behaviors and are closely integrated with actual programming practices, reducing the need for developers to learn formal languages ​​and improving the efficiency of building formal specification frameworks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119440485A_ABST
    Figure CN119440485A_ABST
Patent Text Reader

Abstract

The invention relates to an intelligent contract formalized specification method and device, electronic equipment and a storage medium, and the method comprises the steps: determining a mathematical logic type and a statement type contained in a target code according to the target code of an intelligent contract, and determining a target logic expression of a Solidiity language corresponding to the mathematical logic type, according to the method, the formalized specification is expressed by adopting a behavior interface specification language, the accuracy of mathematical logic and the unique attribute of a Solidiity language can be fused, a target logic expression and a target statement specification are embedded into a target code in an annotation form, and it can be ensured that the formalized specification has necessary preciseness when describing the intelligent contract behavior, and the formalized specification can also be embedded into the target code in the annotation form. And the method can be closely combined with actual programming practice, and developers do not need to learn specific formalized languages, so that the efficiency of the developers for constructing the formalized specification framework for the smart contract is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of blockchain technology, and in particular to a smart contract formalization specification method, device, electronic device and storage medium. Background Art

[0002] Since smart contracts on the blockchain are public, their security is difficult to fully guarantee. Hackers may exploit vulnerabilities in the contract to launch attacks, and once a smart contract is deployed on the blockchain, the cost of modifying it is extremely high, which means that any security vulnerability may quickly turn into a significant loss. Therefore, ensuring that smart contracts are highly secure before deployment has become a top priority for blockchain application developers. In order to address this challenge, relevant researchers have introduced the method of formal verification. This method uses mathematical tools to comprehensively prove or verify whether the model meets the established specifications, thereby covering the entire life cycle of smart contracts from design to implementation, and becoming a key tool to ensure its security.

[0003] Currently, formal verification usually relies on specific formal languages, such as TLA+, Coq, and Isabelle / HOL. Although these languages ​​are extremely powerful in logical reasoning and verification, they have a high learning threshold for most smart contract developers, and there are significant differences from actual contract programming languages ​​such as Solidity. This difference has limited the popularity of formal verification languages ​​in smart contract development, and developers are inefficient in building formal verification for smart contracts. Developers need to convert between formal verification and actual code, which may lead to misunderstandings or errors. Therefore, before designing a formal verification tool for smart contracts, the first task is to establish a reasonable set of formal specifications to avoid the above problems. Summary of the invention

[0004] The present application provides a smart contract formal specification method, device, electronic device and storage medium to solve the problem of how to build a formal specification framework for smart contracts.

[0005] In a first aspect, the present application provides a smart contract formal specification method, the method comprising:

[0006] Obtaining the target code of the formally specified smart contract to be configured; wherein the smart contract is written in the Solidity language;

[0007] Determining the mathematical logic type contained in the target code and determining the statement type of the target code;

[0008] Determine a target logical expression in the Solidity language corresponding to the mathematical logic type;

[0009] Configuring a corresponding target statement specification based on a behavioral interface specification for the statement type;

[0010] The target logic expression and the target statement specification are embedded into the target code in the form of comments.

[0011] Optionally, determining a target logical expression in the Solidity language corresponding to the mathematical logic type includes:

[0012] Obtaining a preset mapping relationship; wherein the mapping relationship is a mapping relationship between a mathematical logic type and a logical expression;

[0013] The target logical expression corresponding to the mathematical logic type is determined according to the mathematical logic type and the mapping relationship.

[0014] Optionally, determining the mathematical logic type contained in the target code includes:

[0015] Determine the proposition type contained in the target code; wherein the proposition type includes at least one of a negative proposition, a relational proposition, an AND proposition, an OR proposition, a conditional proposition, a universal quantifier proposition, a particular quantifier proposition and a unary proposition;

[0016] Taking the proposition type as the mathematical logic type;

[0017] Accordingly, the target logical expression includes at least one of a negation expression, a comparison expression, a logical AND expression, a logical OR expression, a derivation expression, a quantifier expression and a Boolean type basic expression.

[0018] Optionally, the statement type includes at least one of a precondition statement, a postcondition statement, a permission modification statement, an exception handling statement, a loop invariant statement, a loop end condition statement, an assertion statement, an assumption statement, a reentry control statement, and a state snapshot statement;

[0019] Configuring a corresponding target statement specification based on a behavioral interface specification for the statement type includes:

[0020] Configuring a precondition specification for the precondition statement; wherein the precondition specification is used to describe a condition that must be satisfied before executing the target code;

[0021] Configuring a post-condition specification for the post-condition statement; wherein the post-condition specification is used to describe an expected state after executing the target code;

[0022] Configuring a modification permission specification for the modification permission statement; wherein the modification permission specification is used to describe state variables that are allowed to be modified during the execution of the target code;

[0023] Configuring an exception handling specification for the exception handling statement; wherein the exception handling specification is used to describe an exception situation during the execution of the target code;

[0024] Configuring a loop invariant specification for the loop invariant statement; wherein the loop invariant specification is used to verify that the smart contract state of the target code remains consistent during each iteration;

[0025] Configuring a loop end condition specification for the loop end condition statement; wherein the loop end condition specification is used for the end condition that must be met during the loop execution process;

[0026] Configuring an assertion statement specification for the assertion statement; wherein the assertion statement specification is used to ensure that the state remains expected when the smart contract is executed;

[0027] Configuring an assumption statement specification for the assumption statement; wherein the assumption statement specification is used to ensure that the state remains expected during smart contract verification;

[0028] Configuring a reentrancy control statement specification for the reentrancy control statement; wherein the reentrancy control statement specification is used to prevent reentrancy attacks in smart contracts;

[0029] A state snapshot statement specification is configured for the state snapshot statement; wherein the state snapshot statement specification is used to maintain a state when the target code is executed.

[0030] Optionally, configuring a corresponding target statement specification based on a behavior interface specification for the statement type includes:

[0031] Determining a target keyword corresponding to the sentence type;

[0032] The target statement specification based on the behavior interface specification corresponding to the statement type is configured according to the target keyword.

[0033] Optionally, after embedding the target logical expression and the target statement specification into the target code in the form of comments, the method further comprises:

[0034] Get the verification request of the smart contract;

[0035] Determine the location of the target annotation corresponding to the verification request;

[0036] The smart contract code to be verified is located according to the position of the target annotation.

[0037] Optionally, after locating the smart contract code to be verified according to the position of the target annotation, the method further includes:

[0038] Generate formal verification report.

[0039] In a second aspect, the present application provides a smart contract formal specification device, the device comprising:

[0040] An acquisition module, used to acquire the target code of the smart contract to be configured with a formal specification; wherein the smart contract is written in Solidity language;

[0041] A first determination module, used to determine the mathematical logic type contained in the target code and determine the statement type of the target code;

[0042] A second determination module is used to determine a target logic expression in the Solidity language corresponding to the mathematical logic type;

[0043] A configuration module, used for configuring a corresponding target statement specification based on a behavioral interface specification for the statement type;

[0044] A normalization module is used to embed the target logical expression and the target statement specification into the target code in the form of comments.

[0045] In a third aspect, the present application provides an electronic device, including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other via the communication bus;

[0046] Memory, used to store computer programs;

[0047] A processor is used to implement the smart contract formalization specification method described in any one of the embodiments of the first aspect when executing a program stored in the memory.

[0048] In a fourth aspect, the present application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the smart contract formalization specification method as described in any one of the embodiments of the first aspect.

[0049] The above technical solution provided by the embodiment of the present application has the following advantages over the prior art: the method provided by the embodiment of the present application obtains the target code of the smart contract to be configured with formal specifications; wherein the smart contract is written in Solidity language; determines the mathematical logic type contained in the target code, and determines the statement type of the target code; determines the target logic expression of the Solidity language corresponding to the mathematical logic type; configures the corresponding target statement specification based on the behavioral interface specification for the statement type; embeds the target logic expression and the target statement specification into the target code in the form of comments. The method determines the mathematical logic type and statement type contained in the target code according to the target code of the smart contract, determines the target logic expression of the Solidity language corresponding to the mathematical logic type, and uses the behavioral interface specification language to express the formal specification, which can integrate the accuracy of mathematical logic and the unique properties of the Solidity language, and embeds the target logic expression and the target statement specification into the target code in the form of comments, which can ensure that the formal specification has the necessary rigor when describing the behavior of the smart contract, and can be closely integrated with the actual programming practice. Developers do not need to learn a specific formal language, which improves the efficiency of developers in building a formal specification framework for smart contracts. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0051] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0052] One or more embodiments are exemplarily described by pictures in the corresponding drawings, and these exemplified descriptions do not constitute limitations on the embodiments. Elements with the same reference numerals in the drawings represent similar elements, and unless otherwise stated, the figures in the drawings do not constitute proportional limitations.

[0053] Figure 1 A system architecture diagram of a smart contract formal specification method provided for one embodiment of the present application;

[0054] Figure 2 A flowchart of a smart contract formalization method provided for one embodiment of the present application;

[0055] Figure 3A schematic diagram of the structure of a smart contract formalization specification device provided for one embodiment of the present application;

[0056] Figure 4 A schematic diagram of the structure of an electronic device provided for one embodiment of the present application. DETAILED DESCRIPTION

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

[0058] The disclosure below provides many different embodiments or examples to realize the different structures of the present application. In order to simplify the disclosure of the present application, the parts and settings of specific examples are described below. Of course, they are only examples, and the purpose is not to limit the present application. In addition, the present application can repeat reference numbers and / or letters in different examples. This repetition is for the purpose of simplification and clarity, and does not itself indicate the relationship between the various embodiments and / or settings discussed.

[0059] In order to solve the technical problem of how to build a formal specification framework for smart contracts in the prior art, the present application provides a smart contract formal specification method, device, electronic device and storage medium, which can ensure that the formal specification has the necessary rigor when describing the behavior of the smart contract and can be closely integrated with the actual programming practice. Developers do not need to learn a specific formal language, which improves the efficiency of developers in building a formal specification framework for smart contracts.

[0060] The first embodiment of the present application provides a smart contract formal specification method, which can be applied to Figure 1 The system architecture shown in the figure includes at least a terminal 101 and a server 102, and the terminal 101 and the server 102 establish a communication connection. The method can be applied to the terminal 101 or the server 102 in the system architecture, wherein the terminal 101 can be a desktop computer, a tablet computer, a notebook, a supercomputer and other devices, and the server 102 can be a local server, a cloud server, or a server cluster.

[0061] Next, based on the system architecture, the formal specification method of the smart contract is explained in detail, such as Figure 2 , the smart contract formal specification method includes:

[0062] Step 201, obtaining the target code of the smart contract to be configured with a formal specification; wherein the smart contract is written in the Solidity language.

[0063] The smart contract to be configured with formal specifications refers to a smart contract that has not been deployed on the blockchain. Specifically, it can be an undeployed smart contract written in the Solidity language.

[0064] Step 202, determining the mathematical logic type contained in the target code, and determining the statement type of the target code.

[0065] In one embodiment, determining the mathematical logic type contained in the target code includes: determining the proposition type contained in the target code; wherein the proposition type includes at least one of a negative proposition, a relational proposition, an AND proposition, an OR proposition, a conditional proposition, a universal quantifier proposition, a particular quantifier proposition and a unary proposition; and taking the proposition type as the mathematical logic type.

[0066] The essence of formal specification is to use mathematical language to accurately define the functions of the program. The specification that only relies on the Solidity language is not enough to constitute a complete formal specification. In order to enhance the expressiveness of the specification, mathematical logic must be incorporated to flexibly and comprehensively describe the extensive functions of the Solidity smart contract. Therefore, the proposition type contained in the target code can be determined first, and the proposition type can be used as a mathematical logic type.

[0067] Statement types of the target code may include precondition statements, postcondition statements, permission modification statements, exception handling statements, loop invariant statements, loop end condition statements, assertion statements, assumption statements, reentry control statements, and state snapshot statements, etc., without limitation.

[0068] Step 203: determine a target logic expression in the Solidity language corresponding to the mathematical logic type.

[0069] The fundamental purpose of formal specifications is to ensure consistency between the code and its specifications. From the perspective of mathematical logic, the specification can be intuitively regarded as a proposition, that is, a statement that can be judged as true or false. In the field of logic, a proposition is a statement that can be judged as true or false. In mathematics, a proposition is usually expressed as an explicit declarative sentence. In modern logic, classical first-order predicate logic provides a basic analytical framework for the form of propositions, which is sufficient to cover the description required for most Solidity smart contract functions. The basic proposition types are mainly divided into four types: unary propositions, polynomial propositions, universal propositions, and particular propositions. Furthermore, by using logical connectives such as "and", "or", and "not", more complex proposition structures can be constructed, and these newly constructed propositions usually belong to basic compound propositions. Finally, by applying conditional statements such as "if...then...", "only if...", "..., if and only if...", more complex proposition structures can be further constructed.

[0070] In the formal specification, propositions can be expressed through Solidity's logical expressions, because logical expressions are Boolean expressions, which are either true or false. The logical expressions in Solidity are shown in Table 1 below.

[0071] Table 1

[0072]

[0073]

[0074] Among them, the negation expression (line 2) corresponds to the negative proposition, that is, the negation of a proposition. The comparison expression (line 3 and line 4) corresponds to the relational proposition, which is used to express the comparative relationship between propositions. The logical AND expression (line 5) corresponds to the AND proposition, indicating that multiple propositions are true at the same time. The logical OR expression (line 6) corresponds to the OR proposition, indicating that at least one proposition is true. Derivation expressions, such as "==>", "<==", "<==>" (line 7), correspond to conditional propositions, describing the conditional relationship between propositions. The quantifier expressions "forall" (universal quantifier) ​​and "exist" (existential quantifier) ​​(line 8) correspond to the universal quantifier proposition and the particular quantifier proposition, respectively, to express statements about all individuals or certain individuals. The basic expression of Boolean type (line 9) corresponds to a unary proposition, involving the truth value of a single proposition. That is, all common proposition types in mathematical logic can find corresponding forms in Solidity expressions, so the mathematical logic type can be determined based on the proposition type contained in the target code.

[0075] In one embodiment, determining a target logical expression in the Solidity language corresponding to a mathematical logic type includes: obtaining a preset mapping relationship; wherein the mapping relationship is a mapping relationship between a mathematical logic type and a logical expression; and determining the target logical expression corresponding to the mathematical logic type based on the mathematical logic type and the mapping relationship.

[0076] In this embodiment, the mapping relationship between the mathematical logic type and the logical expression can be preset, so as to determine the target logical expression corresponding to the mathematical logic type according to the mathematical logic type and the mapping relationship, thereby improving the processing efficiency of determining the target logical expression.

[0077] The target logical expression may include negation expression, comparison expression, logical AND expression, logical OR expression, derivation expression, quantifier expression and Boolean type basic expression, etc., without limitation.

[0078] Step 204: configure a corresponding target statement specification based on the behavior interface specification for the statement type.

[0079] In one embodiment, configuring a corresponding target statement specification based on a behavior interface specification for a statement type includes:

[0080] Configure a precondition specification for a precondition statement; wherein the precondition specification is used to describe the conditions that must be met before executing the target code;

[0081] Configure a post-condition specification for a post-condition statement; wherein the post-condition specification is used to describe the expected state after executing the target code;

[0082] Configure a modification permission specification for the modification permission statement; wherein the modification permission specification is used to describe the state variables that are allowed to be modified during the execution of the target code;

[0083] Configure an exception handling specification for an exception handling statement; wherein the exception handling specification is used to describe an exception situation during the execution of the target code;

[0084] Configure loop invariant specifications for loop invariant statements. The loop invariant specifications are used to verify that the smart contract state remains consistent during each iteration of the target code.

[0085] Configure loop end condition specification for loop end condition statement; wherein the loop end condition specification is used for the end condition that must be met during loop execution;

[0086] Configure assertion statement specifications for assertion statements. The assertion statement specifications are used to ensure that the state remains as expected when the smart contract is executed.

[0087] Configure an assumption statement specification for the assumption statement; the assumption statement specification is used to ensure that the state remains expected during smart contract verification;

[0088] Configure the reentrancy control statement specification for the reentrancy control statement; the reentrancy control statement specification is used to prevent reentrancy attacks in smart contracts;

[0089] A state snapshot statement specification is configured for a state snapshot statement; wherein the state snapshot statement specification is used to maintain a state when the target code is executed.

[0090] In one embodiment, configuring a corresponding target statement specification based on a behavior interface specification for a statement type includes: determining a target keyword corresponding to the statement type; and configuring a corresponding target statement specification based on a behavior interface specification for the statement type according to the target keyword.

[0091] In this embodiment, the formal specification is configured for the Solidity smart contract, and the principles of the behavioral interface specification can be followed, such as following the structure and elements described below to ensure the completeness and rigor of the specification. The formal specification framework will cover all aspects of the contract, from the description of methods and data types to the control of state variables and the definition of loop invariants.

[0092] Precondition Specification

[0093] Preconditions are defined using the requires keyword and are used to describe conditions in the code that must be met before a function can be executed. Preconditions ensure that a function is only called under certain conditions, thus avoiding potential incorrect calls or insufficient resources. Precondition specifications can be placed between the function signature and the function body, and their syntax format is as follows:

[0094] requiresStatement

[0095] :RequiresKeyword predicate';';

[0096] Among them, predicate is a Boolean expression in Solidity syntax, which represents a logical judgment condition. The precondition is verified by the development tool before the function is executed to ensure that the execution of the contract is always in a safe state.

[0097] Postcondition Specification

[0098] Postconditions are defined using the ensures keyword and are used to describe the expected state after a function is executed in the code. Postconditions are used to verify the correctness of the contract state after the function is executed, ensuring that all changes to state variables are in line with expectations. The location of the postcondition specification is also between the function signature and the function body, and its syntax format is as follows:

[0099] ensuresStatement

[0100] :EnsuresKeyword predicate';';

[0101] Postconditional statements are verified after a function is executed to ensure that the result of the function execution meets the design requirements.

[0102] Modify permission specifications

[0103] Modification permissions are defined by the modifies keyword, which is used to clarify the state variables that can be modified by functions in the code during execution. This specification provides explicit control over state modifications, prevents unauthorized state changes, and enhances the security and predictability of smart contracts. Its grammatical format is as follows:

[0104] modifiesStatement

[0105] :ModifiesKeyword modifiesStateVariableList';';

[0106] By extending the Solidity syntax to support richer mathematical logic, a more precise and comprehensive framework is provided for the formal specification of smart contracts.

[0107] Exception handling specifications

[0108] Exception handling is defined by the throws keyword, which describes the exceptions that a function in the code may throw under certain conditions. The exception handling specification is also located between the function signature and the function body, and the syntax format is as follows:

[0109] throwsStatement

[0110] :ThrowsKeyword exceptionCondition';';

[0111] The exception handling specification requires developers to clearly list potential exception situations when designing functions, improve the transparency and consistency of exception handling, and enhance the robustness of smart contracts.

[0112] Loop Invariant Specification

[0113] Loop invariants are used to ensure that the contract state remains consistent during each iteration of the loop. The loop invariant specification is located between the loop header and the loop body. The specification is defined by the loop keyword and its syntax format is as follows:

[0114] LoopInvariant

[0115] :LoopKeyword predicate';';

[0116] By defining loop invariants, developers can verify the correctness of each loop iteration and prevent potential logic errors or security vulnerabilities.

[0117] Loop end condition specification

[0118] The loop termination condition is used to prevent deadlock or resource exhaustion caused by infinite loops. A new loop termination condition specification is proposed. This specification is defined by the termination keyword and clarifies the termination condition that must be met during the loop execution. Its syntax format is as follows:

[0119] terminationStatement

[0120] :TerminationKeyword predicate';';

[0121] The introduction of loop end condition specifications ensures the security and efficiency of contracts in complex loop operations, avoiding performance issues and security risks caused by improper loop design.

[0122] Assert Statement Specification

[0123] Assert statements are defined using the assert keyword, allowing developers to check the current state during contract execution. Assert statements can appear at any Solidity statement location, and their syntax is as follows:

[0124] assertStatement

[0125] :AssertKeyword predicate';';

[0126] Assert statements ensure that the state remains as expected when the contract is executed, thereby improving the robustness of the code.

[0127] Assumption statement specification

[0128] Assumption statements are defined using the assume keyword to describe the expected state of the program under specific circumstances. Assumption statements are used to optimize the verification process and reduce unnecessary complexity. Their syntax format is as follows:

[0129] assumeStatement

[0130] :AssumeKeyword predicate';';

[0131] Assumption statements provide developers with an efficient tool to help optimize the verification of smart contracts.

[0132] Reentrancy Control Statement Specification

[0133] Reentry control statements are defined using the reentry keyword to prevent reentry attacks in smart contracts. Its syntax is as follows:

[0134] reentryStatement

[0135] :ReentryKeyword simpleStatement;

[0136] Reentrancy control statements enhance the security of contracts and prevent reentrancy attacks by explicitly controlling the execution process of contracts.

[0137] State snapshot statement specification

[0138] A new state snapshot statement specification is proposed. The state snapshot statement is defined by the snapshot keyword, which allows saving a specific state during execution for use in subsequent verification. Its syntax format is as follows:

[0139] snapshotStatement

[0140] :SnapshotKeyword identifier'='expression';';

[0141] The introduction of state snapshot statements helps developers better manage state changes in complex contracts and ensure that the execution of contracts meets design expectations.

[0142] Step 205: embed the target logical expression and the target statement specification into the target code in the form of comments.

[0143] The method determines the mathematical logic type and statement type contained in the target code according to the target code of the smart contract, determines the target logic expression of the Solidity language corresponding to the mathematical logic type, and uses the behavioral interface specification language to express the formal specification, which can integrate the accuracy of mathematical logic and the unique properties of the Solidity language, and embed the target logic expression and the target statement specification into the target code in the form of comments, which can ensure that the formal specification has the necessary rigor when describing the behavior of the smart contract and can be closely integrated with the actual programming practice. Developers do not need to learn a specific formal language, which improves the efficiency of developers in building a formal specification framework for smart contracts.

[0144] In one embodiment, after the target logical expression and the target statement specification are embedded into the target code in the form of comments, the method also includes: obtaining a verification request for the smart contract; determining the location of the target comment corresponding to the verification request; and locating the smart contract code to be verified according to the location of the target comment.

[0145] In this embodiment, after the target logical expression and the target statement specification are embedded in the target code in the form of comments, the verification request of the smart contract can be obtained; the location of the target annotation corresponding to the verification request is determined; and the smart contract code to be verified is located according to the location of the target annotation. The use of comments to write formal specifications allows the formal specifications to be separated from the actual code during the compilation and running stages, thereby avoiding interference with code execution, and during the verification process, the verification tool can easily locate and match the smart contract code that needs to be verified based on the location of the specification annotation. In this way, the formal specification maintains a logical connection with the code and ensures independence and operability at different development stages. This annotation-based specification not only improves development efficiency, but also enhances the maintainability and verifiability of the code.

[0146] In one embodiment, after locating the smart contract code to be verified according to the location of the target annotation, the method also includes: generating a formal verification report.

[0147] The formal verification report can be used to quickly determine the results of formal verification.

[0148] Based on the same technical concept, the second embodiment of the present application provides a smart contract formalization specification device, such as Figure 3 , the device comprises:

[0149] An acquisition module 301 is used to acquire the target code of the smart contract to be configured with a formal specification; wherein the smart contract is written in Solidity language;

[0150] A first determination module 302, used to determine the mathematical logic type contained in the target code and determine the statement type of the target code;

[0151] A second determination module 303 is used to determine a target logic expression in the Solidity language corresponding to the mathematical logic type;

[0152] A configuration module 304, configured to configure a corresponding target statement specification based on a behavior interface specification for the statement type;

[0153] The normalization module 305 is used to embed the target logical expression and the target statement specification into the target code in the form of comments.

[0154] The device determines the mathematical logic type and statement type contained in the target code according to the target code of the smart contract, determines the target logic expression of the Solidity language corresponding to the mathematical logic type, and uses a behavioral interface specification language to express the formal specification, which can integrate the accuracy of mathematical logic and the unique properties of the Solidity language, and embeds the target logic expression and the target statement specification into the target code in the form of comments, which can ensure that the formal specification has the necessary rigor when describing the behavior of the smart contract and can be closely integrated with the actual programming practice. Developers do not need to learn a specific formal language, which improves the efficiency of developers in building a formal specification framework for smart contracts.

[0155] like Figure 4 As shown, an embodiment of the present application provides an electronic device, including a processor 111, a communication interface 112, a memory 113 and a communication bus 114, wherein the processor 111, the communication interface 112, and the memory 113 communicate with each other through the communication bus 114.

[0156] A memory 113, for storing computer programs;

[0157] In one embodiment of the present application, the processor 111 is used to implement the smart contract formal specification method provided by any of the above method embodiments when executing the program stored in the memory 113, including:

[0158] Obtaining the target code of the formally specified smart contract to be configured; wherein the smart contract is written in the Solidity language;

[0159] Determining the mathematical logic type contained in the target code and determining the statement type of the target code;

[0160] Determine a target logical expression in the Solidity language corresponding to the mathematical logic type;

[0161] Configuring a corresponding target statement specification based on a behavioral interface specification for the statement type;

[0162] The target logic expression and the target statement specification are embedded into the target code in the form of comments.

[0163] The communication bus mentioned in the above terminal can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.

[0164] The communication interface is used for communication between the above terminal and other devices.

[0165] The memory may include a random access memory (RAM) or a non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.

[0166] The above-mentioned processor 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, discrete hardware components.

[0167] An embodiment of the present application also provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, it implements the smart contract formalization specification method provided by any of the aforementioned method embodiments.

[0168] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0169] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a general hardware platform, and of course, by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the relevant technology can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0170] It should be understood that the terms used herein are only for the purpose of describing specific example embodiments and are not intended to be limiting. Unless the context clearly indicates otherwise, the singular forms "one", "an" and "said" as used herein may also be meant to include plural forms. The terms "include", "comprise", "contain", and "have" are inclusive, and therefore specify the existence of stated features, steps, operations, elements and / or parts, but do not exclude the existence or addition of one or more other features, steps, operations, elements, parts, and / or combinations thereof. The method steps, processes, and operations described herein are not interpreted as necessarily requiring them to be performed in the specific order described or illustrated, unless the execution order is clearly indicated. It should also be understood that additional or alternative steps may be used.

[0171] It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application. In the description, the suffixes such as "module", "component" or "unit" used to represent the elements are only used to facilitate the description of the present application and have no specific meaning in themselves. Therefore, "module", "component" or "unit" can be used in a mixed manner.

[0172] The above description is only a specific implementation of the present application, so that those skilled in the art can understand or implement the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein can be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to the embodiments shown herein, but will conform to the widest range consistent with the principles and novel features applied for herein.

Claims

1. A smart contract formal specification method, characterized in that: The method comprises: Obtaining the target code of the formally specified smart contract to be configured; wherein the smart contract is written in the Solidity language; Determining the mathematical logic type contained in the target code and determining the statement type of the target code; Determine a target logical expression in the Solidity language corresponding to the mathematical logic type; Configuring a corresponding target statement specification based on a behavioral interface specification for the statement type; The target logic expression and the target statement specification are embedded into the target code in the form of comments.

2. The method according to claim 1, characterized in that Determine a target logic expression in the Solidity language corresponding to the mathematical logic type, including: Obtaining a preset mapping relationship; wherein the mapping relationship is a mapping relationship between a mathematical logic type and a logical expression; The target logical expression corresponding to the mathematical logic type is determined according to the mathematical logic type and the mapping relationship.

3. The method according to claim 2, characterized in that Determine the type of mathematical logic contained in the target code, including: Determine the proposition type contained in the target code; wherein the proposition type includes at least one of a negative proposition, a relational proposition, an AND proposition, an OR proposition, a conditional proposition, a universal quantifier proposition, a particular quantifier proposition and a unary proposition; Taking the proposition type as the mathematical logic type; Accordingly, the target logical expression includes at least one of a negation expression, a comparison expression, a logical AND expression, a logical OR expression, a derivation expression, a quantifier expression and a Boolean type basic expression.

4. The method according to claim 1, characterized in that The statement type includes at least one of a precondition statement, a postcondition statement, a permission modification statement, an exception handling statement, a loop invariant statement, a loop end condition statement, an assertion statement, an assumption statement, a reentry control statement, and a state snapshot statement; Configuring a corresponding target statement specification based on a behavioral interface specification for the statement type includes: Configuring a precondition specification for the precondition statement; wherein the precondition specification is used to describe a condition that must be satisfied before executing the target code; Configuring a post-condition specification for the post-condition statement; wherein the post-condition specification is used to describe an expected state after executing the target code; Configuring a modification permission specification for the modification permission statement; wherein the modification permission specification is used to describe state variables that are allowed to be modified during the execution of the target code; Configuring an exception handling specification for the exception handling statement; wherein the exception handling specification is used to describe an exception situation during the execution of the target code; Configuring a loop invariant specification for the loop invariant statement; wherein the loop invariant specification is used to verify that the smart contract state of the target code remains consistent during each iteration; Configuring a loop end condition specification for the loop end condition statement; wherein the loop end condition specification is used for the end condition that must be met during the loop execution process; Configuring an assertion statement specification for the assertion statement; wherein the assertion statement specification is used to ensure that the state remains expected when the smart contract is executed; Configuring an assumption statement specification for the assumption statement; wherein the assumption statement specification is used to ensure that the state remains expected during smart contract verification; Configuring a reentrancy control statement specification for the reentrancy control statement; wherein the reentrancy control statement specification is used to prevent reentrancy attacks in smart contracts; A state snapshot statement specification is configured for the state snapshot statement; wherein the state snapshot statement specification is used to maintain a state when the target code is executed.

5. The method according to claim 1, characterized in that Configuring a corresponding target statement specification based on a behavioral interface specification for the statement type includes: Determining a target keyword corresponding to the sentence type; The target statement specification based on the behavior interface specification corresponding to the statement type is configured according to the target keyword.

6. The method according to claim 1, characterized in that After embedding the target logic expression and the target statement specification into the target code in the form of comments, the method further comprises: Get the verification request of the smart contract; Determine the location of the target annotation corresponding to the verification request; The smart contract code to be verified is located according to the position of the target annotation.

7. The method according to claim 6, characterized in that After locating the smart contract code to be verified according to the position of the target annotation, the method further includes: Generate formal verification report.

8. A smart contract formal specification device, characterized in that: The device comprises: An acquisition module, used to acquire the target code of the smart contract to be configured with a formal specification; wherein the smart contract is written in Solidity language; A first determination module, used to determine the mathematical logic type contained in the target code and determine the statement type of the target code; A second determination module is used to determine a target logic expression in the Solidity language corresponding to the mathematical logic type; A configuration module, used for configuring a corresponding target statement specification based on a behavioral interface specification for the statement type; A normalization module is used to embed the target logical expression and the target statement specification into the target code in the form of comments.

9. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, used to implement the smart contract formalization specification method described in any one of claims 1 to 7 when executing a program stored in a memory.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the smart contract formal specification method as described in any one of claims 1 to 7 is implemented.