Block chain intelligent contract compiling and checking method and system

By calculating and storing digest values ​​during smart contract compilation and verifying them during deployment and calling, the problem of inconsistency between smart contract bytecode tampering and source code bytecode is solved, and the security of smart contracts is improved.

CN119718332APending Publication Date: 2025-03-28DAREWAY SOFTWARE
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411871672.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-18
Publication Date
2025-03-28

AI Technical Summary

Technical Problem

The bytecode of the smart contract may be tampered with or poor management during the smart contract compilation and deployment process, resulting in inconsistency between the source code and bytecode, which in turn leads to deviations from expectations in the execution results of the smart contract.

Method used

By preprocessing, compiling and assembling the smart contract source code, calculating its compiled digest value H, and assigning it to a predefined symbol C, generating bytecode. When deploying and calling a smart contract, the consistency between the source code and bytecode is verified by comparing the digest value with the value of the symbol C that can be exported in the bytecode.

Benefits of technology

It effectively avoids execution deviations caused by the replacement of smart contract bytecode or inconsistent source code and bytecode, and improves the security of smart contracts.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119718332A_ABST
    Figure CN119718332A_ABST
Patent Text Reader

Abstract

The invention belongs to the field of block chains, and provides a block chain intelligent contract compiling and checking method and system. The method comprises the steps of deploying an intelligent contract according to scene requirements, obtaining an abstract value when the intelligent contract is compiled by accessing a symbol C which can be exported from a byte code of the intelligent contract, comparing the abstract value with an actual abstract value of a source code of the intelligent contract, and further determining whether deployment is completed or not according to a comparison result. And when the smart contract is called, comparing the abstract value of the source code of the smart contract directly obtained from the chain or the abstract value calculated according to the source code of the smart contract obtained from the chain with the assignment of the symbol C which can be exported in the byte code of the smart contract. The problem that the intelligent contract is not executed according to expectation due to inconsistency of the source code and the byte code of the intelligent contract caused by replacement and tampering of the byte code of the intelligent contract or poor compiling and deployment process management of the intelligent contract can be effectively avoided, and the security of the intelligent contract is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of blockchain, and in particular to a blockchain smart contract compilation and verification method and system. Background Art

[0002] The statements in this section merely provide background information related to the present invention and do not necessarily constitute prior art.

[0003] Smart contracts are an important part of blockchain technology and occupy a vital position in the blockchain ecosystem. Their security is related to the security and credibility of the entire blockchain system. In the blockchain system, there are many types of smart contracts. Script-type smart contracts directly deploy smart contract scripts on the chain, which can implement simple logic; for scenarios that need to support complex logic processing, compiling smart contract source code into executable bytecode on the chain is also a common way to implement smart contracts. Since the compiled smart contract bytecode may be large, some blockchain systems do not store the smart contract bytecode on the chain, but store and manage it on local nodes, which may cause the smart contract bytecode to be tampered with and replaced. In order to facilitate the update and audit of smart contracts, some blockchain systems upload the smart contract source code to the blockchain system together with the bytecode, but there is no anchor relationship between the source code and the bytecode. If the smart contract is not well managed during the writing and deployment process, there will be inconsistencies between the smart contract source code and the bytecode, resulting in deviations from the expected execution results of the smart contract. For example, before a contract is deployed or upgraded, the source code is modified but not recompiled. At this time, there are no loopholes in the logic of either the source code or the bytecode. However, after being deployed to the blockchain system, the smart contract cannot perform business processing as expected.

[0004] In terms of smart contract security, common smart contract vulnerability detection methods usually identify vulnerabilities in the code logic by scanning the smart contract source code or compiled smart contract bytecode, thereby avoiding security issues in the smart contract logic that are intentionally or unintentionally introduced by smart contract developers to a certain extent. However, the code audit method cannot detect security vulnerabilities in the inconsistency between the smart contract source code and bytecode. Summary of the invention

[0005] In order to solve the above technical problems, the present invention provides a blockchain smart contract compilation and verification method and system, which can effectively avoid the problem of smart contracts not being executed as expected due to the replacement and tampering of smart contract bytecode or the inconsistency between smart contract source code and bytecode caused by poor management of smart contract compilation and deployment process, thereby improving the security of smart contracts.

[0006] In order to achieve the above object, the present invention adopts the following technical solution:

[0007] The first aspect of the present invention provides a blockchain smart contract compilation and verification method.

[0008] In one or more embodiments, a blockchain smart contract compilation and verification method is provided, including:

[0009] Preprocess, compile and assemble the smart contract source code, calculate the digest value H when the smart contract source code is compiled, assign the value of the predefined symbol C in the smart contract source code to the digest value H, and obtain the smart contract in bytecode format; wherein the predefined symbol C is a symbol that is unrelated to the business logic and can be exported;

[0010] Deploy smart contracts according to scenario requirements, obtain the summary value of the smart contract at compile time by accessing the symbol C that can be exported from the smart contract bytecode, and compare it with the actual summary value of the smart contract source code, and then determine whether the deployment is completed based on the comparison result;

[0011] When calling a smart contract, the summary value of the smart contract source code directly obtained from the chain or the summary value calculated based on the smart contract source code obtained from the chain is compared with the assignment of the symbol C that can be derived in the smart contract bytecode. The consistency of the smart contract source code and the smart contract bytecode is verified based on the consistency of the two. If the verification passes, the smart contract is executed.

[0012] As an implementation method, in the process of deploying smart contracts according to scenario requirements, for scenarios where the smart contract source code has privacy protection requirements, only the summary value of the smart contract source code is uploaded to the chain, and the compiled smart contract bytecode is deployed to an independent smart contract running environment.

[0013] As an implementation method, in the process of deploying smart contracts according to scenario requirements, for scenarios where there is a need for smart contract source code auditing, both the smart contract source code and the smart contract bytecode are uploaded to the chain.

[0014] As an implementation method, when calling a smart contract, the symbol C loaded in the smart contract bytecode is accessed through the smart contract export function or export variable.

[0015] As an implementation method, if the digest value of the smart contract when it is compiled is consistent with the actual digest value of the smart contract source code, the deployment is completed; otherwise, the deployment is refused.

[0016] As an implementation method, the value of the predefined symbol C in the smart contract source code is assigned to the summary value H through compilation and linking.

[0017] A second aspect of the present invention provides a blockchain smart contract compilation and verification system.

[0018] In one or more embodiments, a blockchain smart contract compilation and verification system includes:

[0019] The source code compilation module is used to preprocess, compile and assemble the smart contract source code, calculate the summary value H when the smart contract source code is compiled, assign the value of the predefined symbol C in the smart contract source code to the summary value H, and obtain the smart contract in bytecode format; wherein the predefined symbol C is a symbol that is unrelated to the business logic and can be exported;

[0020] The smart contract deployment module is used to deploy smart contracts according to scenario requirements. By accessing the symbol C that can be exported from the smart contract bytecode, the digest value of the smart contract is obtained during compilation and compared with the actual digest value of the smart contract source code, and then the deployment is determined based on the comparison result.

[0021] The smart contract verification module is used to compare the summary value of the smart contract source code directly obtained from the chain or the summary value calculated based on the smart contract source code obtained from the chain with the assignment of the symbol C that can be derived in the smart contract bytecode when calling the smart contract. The consistency of the smart contract source code and the smart contract bytecode is verified based on the consistency of the two. If the verification passes, the smart contract is executed.

[0022] A third aspect of the present invention provides a computer-readable storage medium.

[0023] A computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps in the blockchain smart contract compilation and verification method as described above.

[0024] A fourth aspect of the present invention provides a computer program product.

[0025] A computer program product includes a computer program / instruction, which, when executed by a processor, implements the steps in the blockchain smart contract compilation and verification method as described above.

[0026] A fifth aspect of the present invention provides an electronic device.

[0027] An electronic device comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the steps in the blockchain smart contract compilation and verification method as described above are implemented.

[0028] Compared with the prior art, the present invention has the following beneficial effects:

[0029] The present invention uses a specific smart contract compilation and verification method to establish a definite correspondence between the compiled bytecode and the smart contract source code and verify them when the contract is called or loaded, so that the consistency of the smart contract source code and the bytecode can be verified, which effectively avoids the problem of the smart contract not being executed as expected due to the replacement and tampering of the smart contract bytecode or the inconsistency between the smart contract source code and the bytecode caused by poor management of the smart contract compilation and deployment process, thereby improving the security of the smart contract. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] The accompanying drawings in the specification, which constitute a part of the present invention, are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute improper limitations on the present invention.

[0031] Figure 1 It is a flowchart of a blockchain smart contract compilation and verification method according to an embodiment of the present invention;

[0032] Figure 2 It is a schematic diagram of the structure of the blockchain smart contract compilation and verification system according to an embodiment of the present invention;

[0033] Figure 3 is a schematic diagram of an electronic device according to an embodiment of the present invention. DETAILED DESCRIPTION

[0034] The present invention will be further described below in conjunction with the accompanying drawings and embodiments.

[0035] It should be noted that the following detailed descriptions are all illustrative and intended to provide further explanation of the present invention. Unless otherwise specified, all technical and scientific terms used herein have the same meanings as those commonly understood by those skilled in the art to which the present invention belongs.

[0036] It should be noted that the terms used herein are only for describing specific embodiments and are not intended to limit exemplary embodiments according to the present invention. As used herein, unless the context clearly indicates otherwise, the singular form is also intended to include the plural form. In addition, it should be understood that when the terms "comprising" and / or "including" are used in this specification, it indicates the presence of features, steps, operations, devices, components and / or combinations thereof.

[0037] In order to solve the problem that the blockchain smart contract source code and the compiled bytecode are inconsistent in logic, or the local management of the smart contract bytecode may be replaced and tampered with, resulting in the inability to execute according to the original logic of the developer, the present invention provides a blockchain smart contract compilation and verification method and system.

[0038] Specifically, a blockchain smart contract compilation and verification method anchors a source code by performing a digest calculation on the smart contract source code, and in the linking stage of the compilation process, the digest value of the source code is imported into the finally generated smart contract bytecode, thereby establishing a unique correspondence between the source code and the bytecode;

[0039] When deploying a smart contract, the correspondence between the smart contract source code and bytecode is verified before being uploaded to the chain. When calling and loading the smart contract, the correspondence between the source code and bytecode is also verified to prevent the smart contract bytecode from being tampered with.

[0040] The summary value recorded in the bytecode can be accessed from outside the bytecode by exporting functions or variables, so that the blockchain system can compare the summary value exported from the smart contract bytecode with the summary value recalculated from the source code to verify whether the two are consistent.

[0041] The export function or variable involved in the present invention refers to the function or variable in the current module or file being accessed or called by other modules or files through specific syntax or tools in programming. In this way, the blockchain system can access the identifier used to anchor the source code in the bytecode of the smart contract, thereby realizing the verification of the correspondence between the source code and the bytecode.

[0042] Figure 1 : is a flowchart of a blockchain smart contract compilation and verification method in an embodiment of the present invention, such as Figure 1 The blockchain smart contract compilation and verification method in the present embodiment shown may include:

[0043] S101, preprocessing, compiling and assembling the smart contract source code, calculating the digest value H when the smart contract source code is compiled, assigning the value of the predefined symbol C in the smart contract source code to the digest value H, and obtaining the smart contract in bytecode format; wherein the predefined symbol C is a symbol that is irrelevant to the business logic and can be exported;

[0044] S102, deploying the smart contract according to the scenario requirements, obtaining the digest value of the smart contract when it is compiled by accessing the symbol C that can be exported from the smart contract bytecode, and comparing it with the actual digest value of the smart contract source code, and then determining whether the deployment is completed according to the comparison result;

[0045] S103, when calling a smart contract, the summary value of the smart contract source code directly obtained from the chain or the summary value calculated based on the smart contract source code obtained from the chain is compared with the assignment of the symbol C that can be derived from the smart contract bytecode, and the consistency of the smart contract source code and the smart contract bytecode is verified based on the consistency of the two. If the verification passes, the smart contract is executed.

[0046] This embodiment uses a specific smart contract compilation and verification method to establish a definite correspondence between the compiled bytecode and the smart contract source code and verify it when the contract is called or loaded, so as to verify the consistency of the smart contract source code and bytecode, effectively avoiding the problem of the smart contract not being executed as expected due to the replacement and tampering of the smart contract bytecode or the inconsistency between the smart contract source code and the bytecode caused by poor management of the smart contract compilation and deployment process, thereby improving the security of the smart contract.

[0047] In step S101, which belongs to the compilation stage, the source code is preprocessed, compiled and assembled to obtain the segment table and symbol table, etc. The compiler should be able to identify unused global variables but still be able to compile. At the same time, the compiler should avoid character obfuscation of the names of global variables. In the linking stage, the value of the symbol C is assigned to the digest value H to obtain the final bytecode format smart contract.

[0048] The value of the predefined symbol C in the smart contract source code is assigned to the summary value H through compilation and linking.

[0049] In step S102, the smart contract source code or source code summary value and the smart contract bytecode are stored according to the scenario requirements. According to the scenario requirements, the smart contract source code summary value or source code can be stored by the blockchain account book, and the smart contract bytecode can be stored by the blockchain account book or a specific blockchain node or a specific smart contract runtime environment.

[0050] Specifically, in the process of deploying smart contracts according to scenario requirements, for scenarios where the smart contract source code has privacy protection requirements, only the summary value of the smart contract source code is uploaded to the chain, and the compiled smart contract bytecode is deployed to an independent smart contract runtime environment. Correspondingly, when calling a smart contract, the symbol C that can be exported in the smart contract bytecode is compared with the summary value of the smart contract source code uploaded to the chain.

[0051] In the process of deploying smart contracts according to scenario requirements, for scenarios with smart contract source code audit requirements, both the smart contract source code and the smart contract bytecode are uploaded to the chain. When calling the smart contract, the value of the symbol C that can be exported in the smart contract bytecode is compared with the summary value calculated by the smart contract source code on the chain.

[0052] In step S103, when calling the smart contract, the bytecode loading symbol C is accessed through the smart contract export function or export variable.

[0053] Figure 2 This is a schematic diagram of the structure of a blockchain smart contract compilation and verification system in an embodiment of the present invention. Figure 1 The blockchain smart contract compilation and verification methods correspond to Figure 2As shown, the blockchain smart contract compilation and verification system in this embodiment may include:

[0054] The source code compilation module 201 is used to preprocess, compile and assemble the smart contract source code, calculate the digest value H when the smart contract source code is compiled, assign the value of the predefined symbol C in the smart contract source code to the digest value H, and obtain the smart contract in bytecode format; wherein the predefined symbol C is a symbol that is irrelevant to the business logic and can be exported;

[0055] A smart contract deployment module 202 is used to deploy smart contracts according to scenario requirements, obtain the digest value of the smart contract when it is compiled by accessing the symbol C that can be exported from the smart contract bytecode, and compare it with the actual digest value of the smart contract source code, and then determine whether the deployment is completed according to the comparison result;

[0056] The smart contract verification module 203 is used to compare the summary value of the smart contract source code directly obtained from the chain or the summary value calculated based on the smart contract source code obtained from the chain with the assignment of the symbol C that can be derived from the smart contract bytecode when calling the smart contract, and verify the consistency of the smart contract source code and the smart contract bytecode based on the consistency of the two. If the verification passes, the smart contract is executed.

[0057] It should be noted here that Figure 2 The various modules in the blockchain smart contract compilation and verification system are Figure 1 The various steps in the blockchain smart contract compilation and verification method in the embodiment correspond one to one, and the specific implementation process is the same, so it will not be repeated here.

[0058] Reference Figure 3 , a schematic diagram of an electronic device is given. It should be noted that, Figure 3 The electronic device 300 shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.

[0059] like Figure 3 As shown, electronic device 300 includes a central processing unit (CPU) 301, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 302 or a program loaded from a storage part 308 into a random access memory (RAM) 303. In RAM 303, various programs and data required for system operation are also stored. Central processing unit 301, ROM 302 and RAM 303 are connected to each other via a bus 304. Input / output (I / O) interface 305 is also connected to bus 304.

[0060] The following components are connected to the I / O interface 305: an input section 306 including a keyboard, a mouse, etc.; an output section 307 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 308 including a hard disk, etc.; and a communication section 309 including a network interface card such as a local area network (LAN) card, a modem, etc. The communication section 309 performs communication processing via a network such as the Internet. A drive 310 is also connected to the I / O interface 305 as needed. A removable medium 311, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 310 as needed so that a computer program read therefrom is installed into the storage section 308 as needed.

[0061] When the central processing unit 301 in the electronic device of this embodiment executes the program, the following is achieved: Figure 1 The steps in the blockchain smart contract compilation and verification method shown.

[0062] In particular, according to an embodiment of the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present application includes a computer program product, which includes a computer program carried on a computer readable medium, the computer program including a computer program for executing Figure 1 In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 309, and / or installed from the removable medium 311. When the computer program is executed by the central processing unit 301, various functions defined in the apparatus of the present application are executed.

[0063] in, Figure 1 The computer program instructions corresponding to the method shown may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0064] A person skilled in the art can understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program, and the program can be stored in a computer-readable storage medium, and when the program is executed, it can include the processes of the embodiments of the above-mentioned methods. The storage medium can be a disk, an optical disk, a read-only memory (ROM) or a random access memory (RAM), etc.

[0065] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A blockchain smart contract compilation and verification method, characterized in that: include: Preprocess, compile and assemble the smart contract source code, calculate the digest value H when the smart contract source code is compiled, assign the value of the predefined symbol C in the smart contract source code to the digest value H, and obtain the smart contract in bytecode format; wherein the predefined symbol C is a symbol that is unrelated to the business logic and can be exported; Deploy smart contracts according to scenario requirements, obtain the summary value of the smart contract at compile time by accessing the symbol C that can be exported from the smart contract bytecode, and compare it with the actual summary value of the smart contract source code, and then determine whether the deployment is completed based on the comparison result; When calling a smart contract, the summary value of the smart contract source code directly obtained from the chain or the summary value calculated based on the smart contract source code obtained from the chain is compared with the assignment of the symbol C that can be derived in the smart contract bytecode. The consistency of the smart contract source code and the smart contract bytecode is verified based on the consistency of the two. If the verification passes, the smart contract is executed.

2. The blockchain smart contract compilation and verification method according to claim 1, characterized in that: In the process of deploying smart contracts according to scenario requirements, for scenarios where the smart contract source code has privacy protection requirements, only the summary value of the smart contract source code is uploaded to the chain, and the compiled smart contract bytecode is deployed to an independent smart contract running environment.

3. The blockchain smart contract compilation and verification method according to claim 1, characterized in that: In the process of deploying smart contracts according to scenario requirements, for scenarios where there is a need for smart contract source code auditing, both the smart contract source code and the smart contract bytecode are uploaded to the chain.

4. The blockchain smart contract compilation and verification method according to claim 1, characterized in that: When calling a smart contract, the symbol C loaded in the smart contract bytecode is accessed through the smart contract export function or export variable.

5. The blockchain smart contract compilation and verification method according to claim 1, characterized in that: If the digest value of the smart contract when it is compiled is consistent with the actual digest value of the smart contract source code, the deployment is completed; otherwise, the deployment is rejected.

6. The blockchain smart contract compilation and verification method according to claim 1, characterized in that: The value of the predefined symbol C in the smart contract source code is assigned to the summary value H through compilation and linking.

7. A blockchain smart contract compilation and verification system, characterized in that: include: The source code compilation module is used to preprocess, compile and assemble the smart contract source code, calculate the summary value H when the smart contract source code is compiled, assign the value of the predefined symbol C in the smart contract source code to the summary value H, and obtain the smart contract in bytecode format; wherein the predefined symbol C is a symbol that is unrelated to the business logic and can be exported; The smart contract deployment module is used to deploy smart contracts according to scenario requirements. By accessing the symbol C that can be exported from the smart contract bytecode, the digest value of the smart contract is obtained during compilation and compared with the actual digest value of the smart contract source code, and then the deployment is determined based on the comparison result. The smart contract verification module is used to compare the summary value of the smart contract source code directly obtained from the chain or the summary value calculated based on the smart contract source code obtained from the chain with the assignment of the symbol C that can be derived in the smart contract bytecode when calling the smart contract. The consistency of the smart contract source code and the smart contract bytecode is verified based on the consistency of the two. If the verification passes, the smart contract is executed.

8. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the steps in the blockchain smart contract compilation and verification method as described in any one of claims 1 to 6 are implemented.

9. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instructions are executed by the processor, the steps in the blockchain smart contract compilation and verification method as described in any one of claim 16 are implemented.

10. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the steps in the blockchain smart contract compilation and verification method as described in any one of claims 1 to 6 are implemented.