A smart contract detection method and device

By decompiling and comparing the operation code records of smart contracts and obtaining the location information of state variables, the problem of service interruption after smart contract upgrade is solved, ensuring that the new contract inherits the services of the old contract and improving the continuity of the service.

CN116127480BActive Publication Date: 2025-09-02WEBANK (CHINA) +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211454071.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-11-21
Publication Date
2025-09-02
Estimated Expiration
2042-11-21

AI Technical Summary

Technical Problem

Due to the immutability of blockchain, it is impossible to ensure that the state variables in the new smart contract inherit the static and dynamic data of the old smart contract after the upgrade, resulting in service interruption and users need to manually transfer digital assets.

Method used

By decompiling the opcode records of the old smart contract and the new smart contract, obtain the storage location information of each state variable, and compare the differences, ensure that the storage location of the new smart contract is consistent with the old smart contract and inherit its services.

Benefits of technology

The new smart contract can fully inherit the old smart contract services on the blockchain, avoiding the hassle of users manually transferring assets and improving the continuity and reliability of services.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116127480B_ABST
    Figure CN116127480B_ABST
Patent Text Reader

Abstract

The embodiment of the present application provides a smart contract detection method and device, which includes: the new smart contract is an updated version of the old smart contract; the first operation code record corresponding to the old smart contract and the second operation code record corresponding to the new smart contract are obtained by decompiling, and the first operation code record and the second operation code record are used to record the operation codes executed when the old smart contract and the new smart contract are decompiled, and the execution order of each operation code; according to the first operation code record and the second operation code record, the first storage location information of each first state variable in the old smart contract and the second storage location information of each second state variable in the new smart contract are obtained respectively; and each first storage location information is compared with the corresponding second storage location information to see whether there is a difference. If there is a difference, the update fails. In the above method, it can be guaranteed that the new smart contract obtained after the update can inherit the services of the old smart contract.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of network technology, and in particular to a smart contract detection method and device. Background Art

[0002] In recent years, with the development of computer technology, more and more technologies have been applied in the financial sector. Traditional finance is gradually shifting towards financial technology (Fintech). However, the security and real-time requirements of the financial industry also place higher demands on technology. For example, the application of blockchain technology in the financial sector is becoming increasingly common.

[0003] Blockchain is a distributed storage system maintained by multiple nodes. The underlying blockchain consists of a series of blocks connected by hash values. Each block consists of a header and a body, and the contents of the block are immutable. A smart contract is a computer program deployed and executed on a blockchain to disseminate, verify, or execute contracts in an information-based manner. A smart contract contains state variables, which are persistently stored on the blockchain. Generally, during the execution of a smart contract, a series of sequentially executed opcodes are used to write or read the static and / or dynamic data of each state variable to or from the storage slots. As user needs change or smart contract programs may have vulnerabilities, smart contracts deployed on the blockchain need to be upgraded. Due to the immutability of the blockchain, smart contracts cannot be modified on the blockchain. However, smart contracts modified under the blockchain cannot guarantee that the state variables in the modified smart contract can inherit the static data and dynamic data of the state variables in the storage space of the original smart contract on the blockchain. As a result, the modified smart contract cannot inherit the services of the original smart contract, resulting in service interruption. Accordingly, users of the service need to manually transfer user information to the new smart contract.

[0004] Therefore, there is an urgent need for a smart contract detection method and device that can ensure that the new smart contract obtained after the update can inherit the services of the old smart contract and ensure the service quality. Summary of the Invention

[0005] The embodiments of the present application provide a smart contract detection method and device, which can ensure that the new smart contract obtained after the update can inherit the services of the old smart contract and ensure the service quality.

[0006] In a first aspect, an embodiment of the present application provides a smart contract detection method, the method comprising:

[0007] Obtaining an old smart contract and a new smart contract, where the new smart contract is an updated version of the old smart contract;

[0008] Decompiling the old smart contract and the new smart contract to obtain a first opcode record corresponding to the old smart contract and a second opcode record corresponding to the new smart contract, respectively, wherein the first opcode record and the second opcode record are used to record the opcodes executed and the execution order of the opcodes when the old smart contract and the new smart contract are decompiled;

[0009] Obtaining, according to the first operation code record and the second operation code record, first storage location information of each first state variable in the old smart contract and second storage location information of each second state variable in the new smart contract;

[0010] Compare each first storage location information with the corresponding second storage location information to see if there is a difference. If there is a difference, the new smart contract update fails.

[0011] In the above method, the new smart contract is an updated version of the old smart contract. To verify whether the storage location information of the state variables of the new and old smart contracts is identical, the new and old smart contracts are decompiled to obtain their respective operation code records. The operation code records record the storage location information corresponding to the operations performed on the state variables in the smart contract during runtime. Accordingly, the operation code records of the smart contract can be analyzed to obtain the state variables corresponding to the operation performed by the operation code, and the storage location information corresponding to these state variables can be obtained. If the storage location information of the state variables of the new and old smart contracts for the same state variables is the same, the new smart contract can be deployed and executed on the blockchain and can inherit the services of the old smart contract. If the storage location information of the state variables of the new and old smart contracts for the same state variables is different, the new smart contract cannot inherit the services of the old smart contract and the update of the new smart contract fails.

[0012] Optionally, obtaining first storage location information of each first state variable in the old smart contract and second storage location information of each second state variable in the new smart contract according to the first operation code record and the second operation code record includes:

[0013] Obtain, according to the first opcode record and the second opcode record, first static data location information of each first static variable and first dynamic data location information of each first dynamic variable in the old smart contract, as well as second static data location information of each second static variable and second dynamic data location information of each second dynamic variable in the new smart contract;

[0014] Comparing each first storage location information with the corresponding second storage location information to see if there is a difference; if there is a difference, the new smart contract update fails, including:

[0015] Compare each of the first static data location information with the corresponding second static data location information, and each of the first dynamic data location information with the corresponding second dynamic data location information to see whether there are differences. If there are differences, the new smart contract update fails.

[0016] In the above method, state variables include static variables and dynamic variables. Since the reading and writing methods of static variables and dynamic variables are not exactly the same, the static data location information and dynamic data location information are obtained separately to ensure the accuracy of the obtained static data location information and dynamic data location information.

[0017] Optionally, obtaining first static data location information of each first static variable in the old smart contract and second static data location information of each second static variable in the new smart contract according to the first opcode record and the second opcode record, respectively, includes:

[0018] Obtaining a first write operation code and / or a first read operation code, and a second write operation code and / or a second read operation code from the first operation code record and the second operation code record respectively;

[0019] Obtaining first static data location information of each of the first static variables according to the obtained first read / write addresses of each of the first write operation codes and / or each of the first read operation codes;

[0020] According to the obtained second read / write addresses of the respective second write operation codes and / or the respective second read operation codes, the second static data position information of the respective second static variables is obtained.

[0021] In the above method, the write operation code and the read operation code are specific operations on the static data of the static variable, and the static data location information can be obtained according to the read and write addresses of the write operation code and the read operation code.

[0022] Optionally, obtaining first static data location information of each of the first static variables according to the first read / write addresses of each of the obtained first write operation codes and / or each of the first read operation codes includes:

[0023] determining, based on the respective first read / write addresses of the respective first write operation codes and / or the respective first read operation codes, first variable declarations corresponding to the respective first static variables of the respective first write operation codes and / or the respective first read operation codes, thereby obtaining a data length of the corresponding first static data based on a declaration position in each first variable declaration;

[0024] Obtaining a first storage space and first slots in the first storage space that are arranged in sequence according to the first read / write addresses;

[0025] According to the size of each first slot and the arrangement order of each first slot, as well as the data length of each first static data and the order of each corresponding operation code, the slot number of the first slot where each first static data is located, as well as the starting position and ending position in the first slot are determined.

[0026] In the above method, the static data of the static variables occupy each slot in the storage space in a manner that, according to the order of the static variables and the size of the static data of each static variable, the slots in a certain order are occupied in sequence. If the static data occupies a full slot and some static data remains, the remaining static data is written to the next slot. If the static data does not fill a full slot, the next static data is written in sequence. In order to obtain the static data location information of each first static variable, the data length of the first static data of the first static variable is obtained through the first variable declaration of the first static variable. The first read and write address is obtained according to the first read operation code and / or the first write operation code of the first static variable, and the first storage space of the first static data is obtained. Further, each first slot in a certain order in the first storage space is obtained. Accordingly, based on the execution order of each first static variable and the data length of the first static data of each first static variable, each first static variable is sequentially assumed to be read and written in each first slot, and the slot number, starting position and ending position of each first static data in the first slot are obtained to obtain the first static data location information of the first static data.

[0027] Optionally, obtaining the second static data location information of each second static variable according to the second read / write address of each second write operation code and / or each second read operation code includes:

[0028] Determining, based on the respective second read / write addresses of the respective second write operation codes and / or the respective second read operation codes, a second variable declaration corresponding to the respective second static variables of the respective second write operation codes and / or the respective second read operation codes, thereby obtaining a data length of the corresponding second static data based on a declaration position in each second variable declaration;

[0029] Obtaining a second storage space and second slots in the second storage space in an arranged order according to the second read / write addresses;

[0030] According to the size of each second slot and the arrangement order of each second slot, as well as the data length of each second static data and the order of each corresponding operation code, the slot number of the second slot where each second static data is located, the starting position and the ending position in the second slot are determined.

[0031] In the above method, the second static data position information of the second static data of each second static variable of the new smart contract is obtained in the same way as the method of obtaining the static data position information of the first static data of each first static variable of the old smart contract. Similarly, the data length of the second static data is obtained according to the second variable declaration of the second static variable, and each second slot is obtained according to the second write operation code and / or the second read and write address of the second read operation code of the second static variable. Further, the second static data position information of each second static data is obtained according to the order of each second static variable, the data length of the second static data of each second static variable, and the order of each second slot.

[0032] Optionally, obtaining first dynamic data location information of each first dynamic variable in the old smart contract and second dynamic data location information of each second dynamic variable in the new smart contract according to the first operation code record and the second operation code record, respectively, includes:

[0033] Obtaining a first hash operation bytecode and a second hash operation bytecode from the first operation code record and the second operation code record respectively;

[0034] Obtaining first dynamic data location information of each of the first dynamic variables according to the logical basic block to which each of the obtained first hash operation bytecodes belongs;

[0035] The second dynamic data location information of each of the second dynamic variables is obtained according to the logical basic block to which each of the obtained second hash operation bytecodes belongs.

[0036] In the above method, the declaration of dynamic data is different from the declaration of static data, and the data length of the dynamic data cannot be obtained directly. However, the logical basic block to which the dynamic data belongs can be determined based on the hash operation bytecode of the dynamic data, and the dynamic data location information of the dynamic data can be obtained based on the operation logic of the dynamic data in the logical basic block.

[0037] Optionally, obtaining the first dynamic data location information of each of the first dynamic variables according to the logical basic block to which each of the obtained first hash operation bytecodes belongs includes:

[0038] Determining, based on the obtained logical basic blocks to which the first hash operation bytecodes respectively belong, an addition operation code corresponding to each of the first hash operation bytecodes, and a third write operation code and / or a third read operation code corresponding to each of the first hash operation bytecodes;

[0039] For any first hash operation bytecode, the left parameter of the addition operation code corresponding to the first hash operation bytecode is used as the slot number of the third slot where the first dynamic data of the first dynamic variable is located, and if the first hash operation bytecode corresponds to two adjacent third read operation codes, the data type of the first dynamic data corresponding to the first hash operation bytecode is a partial data type; otherwise, the data type of the first dynamic data corresponding to the first hash operation bytecode is a full data type, the partial data type is used to represent that the first dynamic data occupies part of the space in the third slot, and the full data type is used to represent that the first dynamic data occupies all of the space in the third slot.

[0040] In the above method, the manner in which the first dynamic data of the first dynamic variable occupies each third slot in the storage space is different from that of a static variable. When declaring the first dynamic data of the first dynamic variable, regardless of whether the data length of the first dynamic data can fully occupy a third slot, it is declared as occupying the entire third slot. The first hash operation bytecode of the first dynamic variable contains an addition operation code in the corresponding logical basic block, which is used to add the first dynamic data to the third slot. The addition operation code is in the form of (slot number, dynamic data key). In this logical basic block, if the first dynamic data fully occupies a third slot, there will be only one third read operation code. If the first dynamic data cannot fully occupy a third slot, to ensure that the first dynamic data is not overwritten or lost, an adjacent third read operation code will be added after the third read operation code. Therefore, for the method of obtaining the first dynamic data location information of the first dynamic data, the slot number of the third slot where the first dynamic data is located is determined according to the left parameter of the addition operation code in the logical basic block to which the first hash operation bytecode belongs, and according to the third write operation code and the third read operation code in the logical basic block corresponding to the first hash operation bytecode, it is determined whether there are two third read operation codes. If there are two reads, it is considered that this first dynamic variable does not occupy the entire space of the third slot, and the data type of the first dynamic data of the first dynamic variable is a partial data type, otherwise it is a full data type.

[0041] Optionally, obtaining the second dynamic data location information of each of the second dynamic variables according to the logical basic block to which each of the obtained second hash operation bytecodes belongs includes:

[0042] Determining, based on the obtained logical basic blocks to which the respective second hash operation bytecodes belong, an addition opcode corresponding to each of the second hash operation bytecodes, and a fourth write opcode and / or a fourth read opcode corresponding to each of the second hash operation bytecodes;

[0043] For any second operation bytecode, the left parameter of the addition operation code corresponding to the second operation bytecode is used as the slot number of the fourth slot where the second dynamic data of the corresponding second dynamic variable is located, and if the second hash operation bytecode corresponds to two adjacent fourth read operation codes, the data type of the second dynamic data corresponding to the second hash operation bytecode is a partial data type; otherwise, the data type of the second dynamic data corresponding to the second hash operation bytecode is a full data type, the partial data type is used to represent that the second dynamic data occupies part of the space in the fourth slot, and the full data type is used to represent that the second dynamic data occupies all of the space in the fourth slot.

[0044] In the above method, for the second dynamic data of the second dynamic variable in the new smart contract, the method for obtaining the second dynamic data location information is the same as the method for obtaining the first dynamic data location information. Specifically, the left parameter of the addition opcode in the logical basic block of the second dynamic variable is determined, the slot number of the fourth slot where the second dynamic data is located is obtained, and it is determined whether there are two adjacent second read opcodes in the logical basic block of the second dynamic variable. If so, it is considered that the first dynamic data of this first dynamic variable does not occupy the entire space of the third slot where it is located, and the data type of the first dynamic data of the first dynamic variable is a partial data type; otherwise, it is a full data type.

[0045] Optionally, obtaining a first hash operation bytecode and a second hash operation bytecode from the first operation code record and the second operation code record, respectively, includes:

[0046] Obtaining each third hash operation bytecode from the first operation code record, and comparing the output of any third hash operation bytecode among the third hash operation bytecodes with the read / write address of the corresponding fifth write operation code or fifth read operation code; if the output is the same, the third hash operation bytecode is the first hash operation bytecode; otherwise, the third hash operation bytecode is discarded;

[0047] Obtain each fourth hash operation bytecode from the second operation code record, and compare the output of any fourth hash operation bytecode among the fourth hash operation bytecodes with the read / write address of the corresponding sixth write operation code or the sixth read operation code; if they are the same, the fourth hash operation bytecode is the second hash operation bytecode; otherwise, the fourth hash operation bytecode is eliminated.

[0048] In the above method, if the output of the hash operation bytecode is different from the read / write address of the corresponding write operation code or read operation code, it means that this hash operation bytecode is not an operation on the dynamic variable and is useless for obtaining the location information of the dynamic data, so it is discarded. Accordingly, each third hash operation bytecode in the first operation code record is obtained. If the output of the third hash operation bytecode is different from the read / write address of the corresponding fifth write operation code or fifth read operation code, it is confirmed that the third hash operation bytecode is not a hash operation bytecode for the first dynamic variable and is discarded. Each fourth hash operation bytecode in the second operation code record is obtained. If the output of the fourth hash operation bytecode is different from the read / write address of the corresponding sixth write operation code or sixth read operation code, it is confirmed that the fourth hash operation bytecode is not a hash operation bytecode for the second dynamic variable and is discarded.

[0049] Optionally, before respectively obtaining the first dynamic data location information of each first dynamic variable in the old smart contract and the second dynamic data location information of each second dynamic variable in the new smart contract according to the first operation code record and the second operation code record, the method further includes:

[0050] According to the set block method, the logical basic block to which each operation code in the first operation code record belongs is determined, and the logical basic block to which each operation code in the second operation code record belongs is determined.

[0051] In the above method, the block method can be set in the smart contract, or it can be set separately to block the operation code. In this way, it can be used to determine the addition operation code and the read and write operation codes in the logical basic block where the hash operation bytecode is located, and to obtain the data type in the dynamic data location information.

[0052] In a second aspect, an embodiment of the present application provides a smart contract detection device, which includes:

[0053] An acquisition module, configured to acquire an old smart contract and a new smart contract, wherein the new smart contract is an updated version of the old smart contract;

[0054] a detection module, configured to decompile the old smart contract and the new smart contract, and obtain a first operation code record corresponding to the old smart contract and a second operation code record corresponding to the new smart contract, respectively, wherein the first operation code record and the second operation code record are respectively used to record each operation code executed when the old smart contract and the new smart contract are decompiled, and the execution order of each operation code;

[0055] The detection module is further configured to obtain, based on the first operation code record and the second operation code record, first storage location information of each first state variable in the old smart contract and second storage location information of each second state variable in the new smart contract;

[0056] The detection module is further configured to compare each first storage location information with the corresponding second storage location information to determine whether there is a difference. If there is a difference, the new smart contract fails to be updated.

[0057] In a third aspect, an embodiment of the present application further provides a computing device, comprising: a memory for storing programs; a processor for calling the programs stored in the memory, and executing the methods described in the various possible designs of the first aspect according to the obtained programs.

[0058] In a fourth aspect, an embodiment of the present application further provides a computer-readable non-volatile storage medium, comprising a computer-readable program. When a computer reads and executes the computer-readable program, the computer executes the method described in the various possible designs of the first aspect.

[0059] These implementations or other implementations of the present application will be more concise and understandable in the description of the following embodiments. BRIEF DESCRIPTION OF THE DRAWINGS

[0060] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0061] Figure 1 A schematic diagram of the architecture of a smart contract detection system provided in an embodiment of the present application;

[0062] Figure 2 A flowchart of a smart contract detection method provided in an embodiment of the present application;

[0063] Figure 3 A flowchart of a smart contract detection method provided in an embodiment of the present application;

[0064] Figure 4 A flowchart of a smart contract detection method provided in an embodiment of the present application;

[0065] Figure 5 A schematic diagram of the storage relationship between static data and slots provided in an embodiment of the present application;

[0066] Figure 6A schematic diagram of the storage relationship between static data and slots provided in an embodiment of the present application;

[0067] Figure 7 A schematic diagram of the storage relationship between static data and slots provided in an embodiment of the present application;

[0068] Figure 8 A schematic diagram of the storage relationship between static data and slots provided in an embodiment of the present application;

[0069] Figure 9 A flowchart of a smart contract detection method provided in an embodiment of the present application;

[0070] Figure 10 A flowchart of a smart contract detection method provided in an embodiment of the present application;

[0071] Figure 11 A schematic diagram of a smart contract detection device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0072] To make the objectives, technical solutions, and advantages of this application more clear, this application will be further described in detail below with reference to the accompanying drawings. Obviously, the embodiments described are only some of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making any creative efforts are within the scope of protection of this application.

[0073] For ease of understanding, the terms involved in the embodiments of the present invention are explained below.

[0074] Blockchain: A blockchain is a distributed storage system maintained by multiple nodes. The underlying blockchain consists of a chain of blocks connected by hash values. Each block consists of a header and a body, and the contents of the block are immutable. Blockchain transactions are atomic; if a transaction ultimately fails, all modified data is rolled back to its pre-transaction state. In terms of data structure, the underlying blockchain consists of a chain of blocks connected by hash values. Each block consists of a header and a body. The header cryptographically links the block to the preceding blocks, ensuring the immutability of the contents of all preceding blocks.

[0075] Smart contract: A smart contract is a computer program that runs on the blockchain and is designed to disseminate, verify or execute contracts in an information-based manner. Smart contracts allow for trusted transactions without a third party, and these transactions are traceable and irreversible. Smart contracts support users to customize and call some complex logic to create distributed applications based on various application scenarios. Smart contracts contain state variables, which will be persistently stored on the blockchain. Specifically, the state data of state variables is stored in the storage space (Storage) of the smart contract. The smart contract code declares the class in the program. After the smart contract code is deployed to the blockchain, the class will be instantiated into an object. When the user calls the deployed smart contract, the object will call the corresponding method to maintain the environment variable information in the contract, including reading and writing operations on the storage space (Storage) corresponding to the state variables. Currently, the storage space (Storage) design of most smart contracts is similar to that of Ethereum. Among them, the smart contract Storage is a capacity of 2 256 Each element in the array is called a slot, and its initial value is 0. The slots in Storage are used to store the status data of the smart contract.

[0076] Opcode: The part of the instruction or field (usually represented by code) in a computer program that specifies the operation to be performed. It is actually the instruction sequence number used to indicate which instruction the CPU needs to execute.

[0077] Byte-code is a binary file that contains an executable program and consists of a sequence of op (operation code) code / data pairs.

[0078] The following is an introduction to the design concept of the embodiments of the present application.

[0079] Due to changing user needs or program vulnerabilities, smart contracts need to be continuously upgraded to adapt to these changes or address them. Due to the immutability of blockchains, this typically involves copying the old smart contract off-chain, modifying it, and obtaining a new smart contract. However, there's no guarantee that the state variables in the new smart contract will inherit the static and dynamic data stored in the old smart contract's storage space on the blockchain. This results in the new smart contract being unable to inherit the services of the old smart contract, leading to service interruptions. For example, a decentralized financial project based on smart contracts had an overly simplistic virtual currency pricing algorithm in the old smart contract, which contained logical vulnerabilities and was vulnerable to hacker attacks. Therefore, an upgrade to a new smart contract was required. However, since the state variables of the new smart contract were not completely consistent with those of the old smart contract, the new smart contract, after being uploaded to the blockchain, could no longer inherit the services of the old smart contract, forcing users to manually transfer their digital assets to the new smart contract.

[0080] In view of this, the embodiment of the present application provides a system architecture diagram of a smart contract detection system, such as Figure 1 As shown, the smart contract detection system includes: a smart contract acquisition module, a decompilation module, a location analysis module and a comparison module;

[0081] The smart contract detection system obtains an old smart contract and a new smart contract based on the smart contract acquisition module, wherein the new smart contract is an updated version of the old smart contract. Furthermore, based on the decompilation module, the old smart contract and the new smart contract are decompiled respectively to obtain a first operation code record corresponding to the old smart contract and a second operation code record corresponding to the new smart contract, respectively. The first operation code record and the second operation code record respectively record the operation codes executed when the old smart contract and the new smart contract are decompiled, as well as the execution order of the operation codes. Furthermore, based on the location analysis module, the first storage location information of each first state variable in the old smart contract and the second storage location information of each second state variable in the new smart contract are obtained based on the first operation code record and the second operation code record. Finally, based on the comparison module, the first storage location information obtained by the location analysis module is compared with the corresponding second storage location information to determine whether there is a difference. If there is a difference, the update of the new smart contract fails. In this way, compared with the existing technology, since the storage location information of the state variables in the new smart contract is not exactly the same as the storage location information of the state variables in the old smart contract, the new smart contract will abandon the services provided by the old smart contract after it is put on the chain, and the user needs to manually transfer the digital assets to the new smart contract. This problem, the present application can detect whether the new smart contract can completely inherit the old smart contract. If it cannot be inherited, it can also locate the storage location information of the state variables of the new smart contract and the old smart contract that are different. This can also facilitate developers to accurately locate the state variables that cause the service to be unable to inherit, make corresponding modifications, and enable the new smart contract to inherit the services of the old smart contract.

[0082] Based on the above system architecture, the present application implements a smart contract detection method, the method flow is as follows: Figure 2 Shown, including:

[0083] Step 201: Obtain an old smart contract and a new smart contract, where the new smart contract is an updated version of the old smart contract.

[0084] Step 202: Decompile the old smart contract and the new smart contract to obtain a first opcode record corresponding to the old smart contract and a second opcode record corresponding to the new smart contract, respectively. The first opcode record and the second opcode record are used to record the opcodes executed and the execution order of the opcodes when the old smart contract and the new smart contract are decompiled, respectively.

[0085] Here, when decompiling the old and new smart contracts, the opcodes executed during the runtime of the old smart contract are recorded to obtain a first opcode record. The opcode records in the first opcode record can be recorded in the order of execution. The opcodes executed during the runtime of the new smart contract are recorded to obtain a first opcode record. The opcode records in the first opcode record can be recorded in the order of execution.

[0086] Step 203: Obtain first storage location information of each first state variable in the old smart contract and second storage location information of each second state variable in the new smart contract based on the first operation code record and the second operation code record;

[0087] Here, we compare the access operations of the new and old smart contracts on state variables in the same storage space. When a user calls a smart contract, the smart contract code is loaded into the Ethereum Virtual Machine and executed. When storing state variables in the storage space, the virtual machine stores the state data in the specified location according to the input related operation code (SSTORE), which is the storage location information of the state variable.

[0088] Step 204: Compare each first storage location information with the corresponding second storage location information to see if there is a difference. If there is a difference, the new smart contract update fails.

[0089] In the above method, the new smart contract is an updated version of the old smart contract. To verify whether the storage location information of the state variables of the new and old smart contracts is identical, the new and old smart contracts are decompiled to obtain their respective operation code records. The operation code records record the storage location information corresponding to the operations performed on the state variables in the smart contract during runtime. Accordingly, the operation code records of the smart contract can be analyzed to obtain the state variables corresponding to the operation performed by the operation code, and the storage location information corresponding to these state variables can be obtained. If the storage location information of the state variables of the new and old smart contracts for the same state variables is the same, the new smart contract can be deployed and executed on the blockchain and can inherit the services of the old smart contract. If the storage location information of the state variables of the new and old smart contracts for the same state variables is different, the new smart contract cannot inherit the services of the old smart contract and the update of the new smart contract fails.

[0090] The embodiment of the present application also provides a smart contract detection method. In step 203 of the above method flow, according to the first operation code record and the second operation code record, the first storage location information of each first state variable in the old smart contract and the second storage location information of each second state variable in the new smart contract are obtained respectively, such as Figure 3 Shown, including:

[0091] Step 301: Obtain, based on the first opcode record and the second opcode record, first static data location information of each first static variable and first dynamic data location information of each first dynamic variable in the old smart contract, as well as second static data location information of each second static variable and second dynamic data location information of each second dynamic variable in the new smart contract.

[0092] Here, we compare the access operations of static variables (such as int and bool types) and dynamic variables (such as list and map types) in the same storage space (Storage) between the new smart contract and the old smart contract. When a user calls a smart contract, the smart contract code is loaded into the Ethereum Virtual Machine and executed. When storing environment variables in the storage space, the virtual machine will store the static data of static variables and the dynamic data of dynamic variables in the specified locations according to the input relevant operation code (SSTORE).

[0093] In step 204 of the above method flow, each first storage location information is compared with the corresponding second storage location information to see if there is a difference. If there is a difference, the new smart contract update fails. Figure 3Shown, including:

[0094] Step 302: Compare each of the first static data location information with the corresponding second static data location information, and each of the first dynamic data location information with the corresponding second dynamic data location information to see if there are any differences. If there are any differences, the new smart contract update fails.

[0095] Here, static variables and dynamic variables have different reading and writing methods. Therefore, the static and dynamic variables of the old and new smart contracts can be processed separately. The first static data location information of each item in the old smart contract is compared with the second static data location information corresponding to the new smart contract, and the first dynamic data location information of each item in the old smart contract is compared with the second dynamic data location information corresponding to the new smart contract to ensure the accuracy of the results.

[0096] The embodiment of the present application also provides a smart contract detection method. In step 301 of the above method flow, according to the first operation code record and the second operation code record, the first static data location information of each first static variable in the old smart contract and the second static data location information of each second static variable in the new smart contract are respectively obtained, such as Figure 4 Shown, including:

[0097] Step 401: Obtain a first write operation code and / or a first read operation code, and a second write operation code and / or a second read operation code from the first operation code record and the second operation code record, respectively.

[0098] Here, the opcodes for reading and writing static variables in the old smart contract are obtained from the first opcode record, including the first write opcode and / or the first read opcode. The opcodes for reading and writing static variables in the new smart contract are obtained from the second opcode record, including the second write opcode and / or the second read opcode.

[0099] Step 402: Obtain first static data location information of each first static variable according to the first read / write addresses of each first write operation code and / or each first read operation code.

[0100] Here, the first write opcode and the first read opcode are the programs used in the old smart contract to write and read storage space. The first read / write address corresponding to the first write opcode and the first read opcode can obtain the first static data location information of the first static variable of the corresponding operation.

[0101] Step 403: Obtain second static data location information of each second static variable according to the second read / write addresses of each second write operation code and / or each second read operation code.

[0102] Here, the second write opcode and the second read opcode are the programs used in the old smart contract to write and read storage space. The second read / write address corresponding to the second write opcode and the second read opcode can obtain the second static data location information of the second static variable of the corresponding operation.

[0103] The present application also provides a smart contract detection method. In step 402 of the above method flow, first static data location information of each first static variable is obtained based on the first read / write address of each first write operation code and / or each first read operation code, including:

[0104] Step 1: Determine, based on the respective first read / write addresses of the respective first write operation codes and / or the respective first read operation codes, the first variable declaration corresponding to the respective first static variables of the respective first write operation codes and / or the respective first read operation codes, thereby obtaining the data length of the corresponding first static data according to the declaration position in each first variable declaration;

[0105] In an embodiment of the present application, the first read / write address of the first write opcode (SSTORE) and / or the first read opcode (SLOAD) includes a storage space location, and the first write opcode and / or the first read opcode also include the value of the first static data; according to the first variable declaration of the first static variable, based on the declaration position in the first variable declaration, StorageVariableInfo (storage space variable information) is inferred to obtain the data length of the first static data of the first static variable. Among them, for static variables, different static data types may occupy different storage lengths in the storage space. For example, static data of type int8 and static data of type int32 will occupy different storage lengths when stored.

[0106] Step 2: obtaining a first storage space and first slots in the first storage space that are arranged in order according to the first read / write addresses;

[0107] Step 3. Determine the slot number of the first slot where each first static data is located, as well as the starting position and ending position in the first slot based on the size of each first slot and the arrangement order of each first slot, as well as the data length of each first static data and the order of each corresponding operation code.

[0108] In the embodiments of the present application, only one first static data S is included in one first write operation code and one first read operation code i value, obtain the size and arrangement order of each first slot, as well as the data length and arrangement order of each first static data, and the static data size adapts to the slot size, and store them in the slot in order. That is, each partial static data in one first static data can be respectively located in different first slots. Then, the first static data position information can be obtained through the following method:

[0109] Assume that the size of the first slot is 32 bytes. In the queue of each first static data, first sum the data lengths length of the nth first static data S n and the (n + 1)th first static data S n+1 .

[0110] If length n (data length of the first static data S n ) + length n+1 (data length of the first static data S n+1 ) > 32 bytes, then push S n out of the queue, and assign S n = <SlotNumCount (slot number), 0, length n - 1 (data length of the first static data S n-1 ), and push S n+2 into the queue. As shown in Figure 5 and Figure 6 , respectively, are schematic diagrams of the possible storage relationships between S n and the first slot provided by the embodiments of the present application.

[0111] If length n + length n+1 ≤ 32, then push both S n and S n+1 out of the queue, and assign S n = <SlotNumCount, 0, length n - 1>, S n+1 = <SlotNumCount, length n , length n + length n+1 - 1>, and push S n+2 and S n+3 into the queue. As shown in Figure 7 , Figure 8 , respectively, are a kind of S n , S n+1Schematic diagram of possible storage relationships with the first slot.

[0112] After that, increment SlotNumCount to analyze the data storage in the next first slot. After all the first static data is output from the queue, the <SlotNum (slot number), StartByte (starting position), EndByte (ending position)> in the triple of the first static data position information have all been assigned, and the first static data position information of each first static data is obtained.

[0113] It should be noted here that in this embodiment, it is assumed that the maximum data length of the static data is less than 32 bytes, but this does not limit the implementation of this solution. For example, if the data length of the static data is greater than 32 directly, the slot number and starting position of this static data in the first slot, as well as the slot number and ending position in the next first slot, can be recorded.

[0114] In the embodiment of the present application, a method for detecting smart contracts is also provided. In step 403 of the above method flow, according to the obtained second read / write addresses of each second write opcode and / or each second read opcode, the second static data position information of each second static variable is obtained, including:

[0115] Step 1: According to the second read / write addresses of each second write opcode and / or each second read opcode, determine the second variable declarations corresponding to the second static variables of each second write opcode and / or each second read opcode, and thus obtain the data length of the corresponding second static data according to the declaration positions in each second variable declaration;

[0116] In the embodiment of the present application, the second read / write address of the second write opcode (SSTORE) and / or the second read opcode (SLOAD) contains the storage space position, and the second write opcode and / or the second read opcode also includes the value of the second static data; according to the second variable declaration of the second static variable, based on the declaration position in the second variable declaration, StorageVariableInfo (storage space variable information) inference is performed to obtain the data length of the second static data of the second static variable.

[0117] Step 2: Obtain the second storage space according to each second read / write address, and each second slot with an arrangement order in the second storage space;

[0118] Step 3: According to the sizes of each second slot and the arrangement order of each second slot, as well as the data lengths of each second static data and the order of their respective corresponding opcodes, determine the slot number of the second slot where each second static data is located, the starting position and the ending position in the second slot.

[0119] In the above-mentioned embodiment of step 402, an embodiment is provided for obtaining the first static data location information of the first static data of the old smart contract. Here, the method for obtaining the second static data location information of the second static data can be the same as the first static data location information of the first static data, and will not be repeated here.

[0120] The embodiment of the present application also provides a smart contract detection method. In step 301 of the above method flow, according to the first operation code record and the second operation code record, the first dynamic data location information of each first dynamic variable in the old smart contract and the second dynamic data location information of each second dynamic variable in the new smart contract are respectively obtained, such as Figure 9 Shown, including:

[0121] Step 901: Obtain a first hash operation bytecode and a second hash operation bytecode from the first operation code record and the second operation code record respectively;

[0122] Step 902: Obtain first dynamic data location information of each first dynamic variable according to the logical basic block to which each first hash operation bytecode belongs.

[0123] In the embodiment of the present application, dynamic variables occupy an entire slot when declared, and the data length of the dynamic data cannot be determined directly based on the declaration. However, since the virtual machine accesses the dynamic data as a key-value pair, it will perform a hash mapping (SHA3) operation based on the key of the dynamic data to determine the actual storage location of the value of the dynamic data. Among them, the logical basic block contains each operation code of a logic in the smart contract, and the dynamic data location information can be obtained based on the corresponding operation (addition operation, read and write operation) in the logical basic block corresponding to the hash operation bytecode. The first hash operation bytecode in the first operation code record is obtained, the logical basic block to which this hash operation byte belongs is determined, the operation codes related to the corresponding first dynamic variable are determined, and the first dynamic data location information of the first dynamic variable is determined.

[0124] Step 903: Obtain second dynamic data location information of each second dynamic variable according to the logical basic block to which each second hash operation bytecode belongs.

[0125] In an embodiment of the present application, the first hash operation bytecode in the second operation code record is obtained, the logical basic block to which the two hash operation bytes belong is determined, each operation code related to the corresponding second dynamic variable is determined, and the second dynamic data location information of the second dynamic variable is determined.

[0126] The present application also provides a smart contract detection method. In step 902 of the above method flow, first dynamic data location information of each first dynamic variable is obtained based on the logical basic block to which each first hash operation bytecode belongs, including:

[0127] Step 1: Determine, based on the obtained logical basic blocks to which the first hash operation bytecodes belong, the addition opcodes corresponding to the first hash operation bytecodes, and the third write opcodes and / or third read opcodes corresponding to the first hash operation bytecodes.

[0128] In the embodiment of the present application, dynamic variables occupy an entire slot when declared, and the data length of the dynamic data cannot be directly inferred based on the declaration. However, since the virtual machine accesses dynamic data in the form of key-value pairs, a hash mapping (SHA3) operation is performed according to the key of the dynamic data to determine the specific storage location of the value of the dynamic data. In this process, the writing (SSTORE) method of dynamic data of different data lengths is different. For example, for dynamic data of a string type shorter than 16 bytes, multiple dynamic data are stored in one slot. After executing the hash mapping to obtain the target slot position, the data will not be directly stored (SSTORE). Instead, the original dynamic data of the target slot position must be read (SLOAD) first, and after modifying (ADD, SUB, EXP, etc.) the original dynamic data, the dynamic data of the entire slot is re-stored to ensure that when storing dynamic data, other dynamic data in the same slot will not be overwritten. However, for dynamic data with a data length of the entire slot, it is only necessary to directly store (SSTORE) the slot according to the mapping (SHA3) position. In the embodiment of the present application, for the first dynamic variable in the old smart contract, the first dynamic data location information is obtained, and the first hash operation bytecode corresponding to the first dynamic variable can be determined, and the logical basic block to which the first hash operation bytecode belongs can be determined. From the logical basic block, the identification, access and other operation codes for the first dynamic variable are determined, including the addition operation code, the third write operation code and / or the third read operation code corresponding to the first hash operation bytecode.

[0129] Step 2. For any first hash operation bytecode, use the left parameter of the addition operation code corresponding to the first hash operation bytecode as the slot number of the third slot where the first dynamic data of the first dynamic variable is located. If the first hash operation bytecode corresponds to two adjacent third read operation codes, the data type of the first dynamic data corresponding to the first hash operation bytecode is a partial data type. Otherwise, the data type of the first dynamic data corresponding to the first hash operation bytecode is a full data type. The partial data type is used to represent that the first dynamic data occupies part of the space in the third slot, and the full data type is used to represent that the first dynamic data occupies all of the space in the third slot.

[0130] In the above embodiment, the addition operation code is in the form of: (slot number, key of the first dynamic data), then the left parameter of the addition operation code can be used as the slot number, and if the first hash operation bytecode corresponds to two third read operation codes, it means that the first dynamic data is read repeatedly, and the corresponding situation is that multiple dynamic data are stored in one slot: 'After executing the hash mapping to obtain the target slot position, the data will not be stored directly (SSTORE), but the original dynamic data of the target slot position will be read (SLOAD) first, and the original dynamic data will be modified (ADD, SUB, EXP, etc.), and then the dynamic data of the entire slot will be re-stored to ensure that other dynamic data in the same slot will not be overwritten when storing dynamic data', therefore, the data type of the first dynamic data corresponding to the first hash operation bytecode is a partial data type, otherwise, the data type of the first dynamic data corresponding to the first hash operation bytecode is a full data type.

[0131] The present application also provides a smart contract detection method. In step 903 of the above method flow, obtaining the second dynamic data location information of each second dynamic variable according to the logical basic block to which each second hash operation bytecode belongs includes:

[0132] Step 1: Determine, based on the obtained logical basic blocks to which the respective second hash operation bytecodes belong, the addition opcode corresponding to each of the second hash operation bytecodes, and the fourth write opcode and / or the fourth read opcode corresponding to each of the second hash operation bytecodes;

[0133] In the above embodiment, the operation method of the dynamic variable has been explained and will not be repeated here. In the embodiment of the present application, for the second dynamic variable in the new smart contract, the second dynamic data location information is obtained, the second hash operation bytecode corresponding to the second dynamic variable can be determined, and the logical basic block to which the second hash operation bytecode belongs can be determined. From the logical basic block, the identification, access and other operation codes for the second dynamic variable are determined, including the addition operation code, the fourth write operation code and / or the fourth read operation code corresponding to the second hash operation bytecode.

[0134] Step 2. For any second operation bytecode, use the left parameter of the addition operation code corresponding to the second operation bytecode as the slot number of the fourth slot where the second dynamic data of the corresponding second dynamic variable is located, and if the second hash operation bytecode corresponds to two adjacent fourth read operation codes, then the data type of the second dynamic data corresponding to the second hash operation bytecode is a partial data type; otherwise, the data type of the second dynamic data corresponding to the second hash operation bytecode is a full data type, the partial data type is used to represent that the second dynamic data occupies part of the space in the fourth slot, and the full data type is used to represent that the second dynamic data occupies all of the space in the fourth slot.

[0135] In the above embodiment, the addition operation code is in the form of (slot number, second dynamic data key). The left parameter of the addition operation code can be used as the slot number. If the second hash operation bytecode corresponds to two fourth read operation codes, it means that the second dynamic data is read repeatedly, which corresponds to the case where multiple dynamic data are stored in one slot. Therefore, the data type of the second dynamic data corresponding to the second hash operation bytecode is a partial data type. Otherwise, the data type of the second dynamic data corresponding to the second hash operation bytecode is a full data type.

[0136] The present application embodiment provides a smart contract detection method. Figure 9 In step 901 of the corresponding method flow, obtaining a first hash operation bytecode and a second hash operation bytecode from the first operation code record and the second operation code record respectively includes:

[0137] Step 1: Obtain each third hash operation bytecode from the first operation code record, and compare the output of any third hash operation bytecode among the third hash operation bytecodes with the read / write address of the corresponding fifth write operation code or fifth read operation code. If they are the same, the third hash operation bytecode is the first hash operation bytecode; otherwise, the third hash operation bytecode is discarded.

[0138] In the embodiment of the present application, the hash operation bytecode SHA3 has three parameters: the first parameter is the declaration location of the hash operation (SHA3Stmt), the second parameter is the original variable of the hash calculation (SHA3Input), and the third parameter is the result variable of the hash calculation (SHA3Output). Since the hash operation is required in addition to dynamic data storage, there may be other hash operations related to business logic in the smart contract. Therefore, it is necessary to match the third parameter of SHA3 with the second parameter of SSTORE (which contains the first parameter as the value of the target data of the state variable of the operation and the second parameter as the specific location of the operation in the storage space, that is, the read / write address). If the same data is matched, it means that the hash calculation result is directly used for state variable storage; otherwise, it is other business logic-related code. The filtered SHA3 will be used to analyze the dynamic variables. If there is no SHA3 for dynamic data operation here, it means that there is no access operation to the dynamic variable in the smart contract. When obtaining the first hash operation bytecode, the third hash operation bytecode used for other business logic is eliminated.

[0139] Step 2: Obtain each fourth hash operation bytecode from the second operation code record, and compare the output of any fourth hash operation bytecode among the fourth hash operation bytecodes with the read / write address of the corresponding sixth write operation code or the sixth read operation code. If they are the same, the fourth hash operation bytecode is the second hash operation bytecode; otherwise, the fourth hash operation bytecode is eliminated.

[0140] In an embodiment of the present application, if the output of the fourth hash operation bytecode (in the screening description of the hash operation bytecode corresponding to the above dynamic data, it is the third parameter of the hash operation bytecode: the result variable of the hash calculation (SHA3Output)) is compared with the read / write address of the corresponding sixth write operation code or the sixth read operation code (in the screening description of the hash operation bytecode corresponding to the above dynamic data, it is the second parameter of the operation code: the specific location of the operation in the storage space, that is, the read / write address), if they are the same, then the fourth hash operation bytecode is the bytecode of the second dynamic variable, then it is the second hash operation bytecode; if they are different, then the different corresponding fourth hash operation bytecode is deleted. In this way, useless redundant hash operation bytecodes are eliminated, and the efficiency and accuracy of smart contract detection are improved.

[0141] This application embodiment provides another smart contract detection method. Figure 3 In step 301 of the method flow, before respectively obtaining the first dynamic data location information of each first dynamic variable in the old smart contract and the second dynamic data location information of each second dynamic variable in the new smart contract according to the first operation code record and the second operation code record, the method further includes:

[0142] Based on the configured block division method, the logical basic block to which each opcode in the first opcode record belongs is determined, and the logical basic block to which each opcode in the second opcode record belongs is determined. The block division method can be configured within the smart contract or independently within the smart contract detection system to divide the opcodes into blocks. This allows for determining the addition opcode and the read and write opcodes within the logical basic block containing the hash operation bytecode, and for obtaining the data type in the dynamic data location information.

[0143] Based on the above methods and processes, the present application embodiment provides a smart contract detection method process, such as Figure 10 Shown, including:

[0144] Step 1001: Obtain the old smart contract and the new smart contract.

[0145] Step 1002: Decompile the old smart contract and the new smart contract respectively to obtain the first operation code record of the old smart contract and the second operation code record of the new smart contract.

[0146] Step 1003: Obtain a first write operation code and / or a first read operation code, and a second write operation code and / or a second read operation code from the first operation code record and the second operation code record, respectively.

[0147] Step 1004: Determine the first variable declaration corresponding to the first static variable of each first write opcode and / or each first read opcode based on the first read / write address obtained, thereby obtaining the data length of the corresponding first static data based on the declaration position in each first variable declaration.

[0148] Step 1005: Obtain a first storage space and first slots in the first storage space in an arrangement order according to the first read and write addresses.

[0149] Step 1006: Determine the slot number of the first slot where each first static data is located, as well as the starting position and ending position in the first slot based on the size of each first slot and the arrangement order of each first slot, as well as the data length of each first static data and the order of the corresponding operation code.

[0150] Step 1007: Determine the second variable declaration corresponding to the second static variable of each second write opcode and / or each second read opcode based on the obtained second read / write address of each second write opcode and / or each second read opcode, thereby obtaining the data length of the corresponding second static data based on the declaration position in each second variable declaration.

[0151] Step 1008: Obtain a second storage space and second slots in an arrangement order in the second storage space according to the second read / write addresses.

[0152] Step 1009: Determine the slot number of the second slot where each second static data is located, as well as the starting position and ending position in the second slot based on the size of each second slot and the arrangement order of each second slot, as well as the data length of each second static data and the order of the corresponding operation code.

[0153] Step 1010: Determine the logical basic block to which each operation code in the first operation code record belongs according to the set block partitioning method, and determine the logical basic block to which each operation code in the second operation code record belongs.

[0154] Step 1011: Obtain each third hash operation bytecode from the first operation code record, and compare the output of any third hash operation bytecode in each third hash operation bytecode with the read / write address of the corresponding fifth write operation code or the fifth read operation code. If they are the same, the third hash operation bytecode is the first hash operation bytecode; otherwise, the third hash operation bytecode is eliminated to obtain each first hash operation bytecode.

[0155] Step 1012: Obtain each fourth hash operation bytecode from the second operation code record, and compare the output of any fourth hash operation bytecode in each fourth hash operation bytecode with the read / write address of the corresponding sixth write operation code or the sixth read operation code. If they are the same, the fourth hash operation bytecode is the second hash operation bytecode; otherwise, the fourth hash operation bytecode is eliminated to obtain each second hash operation bytecode.

[0156] Step 1013: Determine the addition opcode corresponding to each first hash operation bytecode and the third write opcode and / or third read opcode corresponding to each first hash operation bytecode based on the obtained logical basic blocks to which each first hash operation bytecode belongs.

[0157] Step 1014: For any first hash operation bytecode, the left parameter of the addition operation code corresponding to the first hash operation bytecode is used as the slot number of the third slot where the first dynamic data of the first dynamic variable is located. If the first hash operation bytecode corresponds to two adjacent third read operation codes, it is determined that the data type of the first dynamic data corresponding to the first hash operation bytecode is a partial data type; otherwise, the data type of the first dynamic data corresponding to the first hash operation bytecode is a full data type.

[0158] Step 1015: Determine the addition opcode corresponding to each second hash operation bytecode and the fourth write opcode and / or fourth read opcode corresponding to each second hash operation bytecode based on the obtained logical basic blocks to which each second hash operation bytecode belongs.

[0159] Step 1016: For any second hash operation bytecode, use the left parameter of the addition operation code corresponding to the second hash operation bytecode as the slot number of the fourth slot where the second dynamic data of the second dynamic variable is located. If the second hash operation bytecode corresponds to two adjacent fourth read operation codes, determine that the data type of the second dynamic data corresponding to the second hash operation bytecode is a partial data type; otherwise, the data type of the second dynamic data corresponding to the second hash operation bytecode is a full data type.

[0160] Step 1017: Compare each first static data location information with the corresponding second static data location information, and each first dynamic data location information with the corresponding second dynamic data location information to see if there are any differences. If there are any differences, the new smart contract update fails.

[0161] It should be noted that the above method flow steps are not unique. For example, steps 1004-1006 are the process of obtaining the first static data location information, which can be performed simultaneously with or before and after the process of obtaining the second static data location information in steps 1007-1009. Step 1010 can be executed before or after any step in steps 1001-1009. Steps 1013 and 1014 are the process of obtaining the first dynamic data location information, which can be performed simultaneously with or before and after the process of obtaining the second dynamic data location information in steps 1015 and 1016. Steps 1003-1009 are the process of obtaining static data location information, which can be performed simultaneously with or before and after the process of obtaining dynamic data location information in steps 1010-1016. It can be seen that the above method flow is only an example and does not limit the specific implementation of this application.

[0162] Based on the same concept, the present application embodiment provides a smart contract detection device, such as Figure 11 As shown, the device includes:

[0163] An acquisition module 1101 is configured to acquire an old smart contract and a new smart contract, where the new smart contract is an updated version of the old smart contract.

[0164] The detection module 1102 is configured to decompile the old smart contract and the new smart contract, and obtain a first operation code record corresponding to the old smart contract and a second operation code record corresponding to the new smart contract, respectively. The first operation code record and the second operation code record are respectively used to record the operation codes executed and the execution order of the operation codes when the old smart contract and the new smart contract are decompiled;

[0165] The detection module 1102 is further configured to obtain, based on the first operation code record and the second operation code record, first storage location information of each first state variable in the old smart contract and second storage location information of each second state variable in the new smart contract;

[0166] The detection module 1102 is further configured to compare each first storage location information with the corresponding second storage location information to determine whether there is a difference. If there is a difference, the new smart contract update fails.

[0167] Optionally, the detection module 1102 is specifically used to obtain, according to the first operation code record and the second operation code record, the first static data location information of each first static variable and the first dynamic data location information of each first dynamic variable in the old smart contract, as well as the second static data location information of each second static variable and the second dynamic data location information of each second dynamic variable in the new smart contract; the detection module 1102 is specifically used to compare whether there are differences between the first static data location information and the corresponding second static data location information, and between the first dynamic data location information and the corresponding second dynamic data location information; if there are differences, the update of the new smart contract fails.

[0168] Optionally, the detection module 1102 is specifically used to obtain the first write operation code and / or the first read operation code, and the second write operation code and / or the second read operation code from the first operation code record and the second operation code record, respectively; obtain the first static data position information of each first static variable based on the first read / write address of each first write operation code and / or each first read operation code; obtain the second static data position information of each second static variable based on the second read / write address of each second write operation code and / or each second read operation code.

[0169] Optionally, the detection module 1102 is specifically used to determine the first variable declaration corresponding to the first static variable of each first write operation code and / or each first read operation code according to the first read / write address of each first write operation code and / or each first read operation code, thereby obtaining the data length of the corresponding first static data according to the declaration position in each first variable declaration; obtain the first storage space according to each first read / write address, and the first slots in the first storage space that have an arrangement order; determine the slot number of the first slot where the first static data is located, as well as the starting position and ending position in the first slot according to the size of each first slot and the arrangement order of each first slot, as well as the data length of each first static data and the order of the corresponding operation code.

[0170] Optionally, the detection module 1102 is specifically used to obtain a first hash operation bytecode and a second hash operation bytecode from the first operation code record and the second operation code record, respectively; obtain the first dynamic data location information of each of the first dynamic variables based on the logical basic block to which each of the obtained first hash operation bytecodes belongs; and obtain the second dynamic data location information of each of the second dynamic variables based on the logical basic block to which each of the obtained second hash operation bytecodes belongs.

[0171] Optionally, the detection module 1102 is specifically used to determine the addition operation code corresponding to each of the first hash operation bytecodes, and the third write operation code and / or third read operation code corresponding to each of the first hash operation bytecodes based on the obtained logical basic blocks to which each of the first hash operation bytecodes belongs; for any first hash operation bytecode, the left parameter of the addition operation code corresponding to the first hash operation bytecode is used as the slot number of the third slot where the first dynamic data of the first dynamic variable is located, and if the first hash operation bytecode corresponds to two adjacent third read operation codes, then the data type of the first dynamic data corresponding to the first hash operation bytecode is a partial data type; otherwise, the data type of the first dynamic data corresponding to the first hash operation bytecode is a full data type, the partial data type is used to represent that the first dynamic data occupies part of the space in the third slot, and the full data type is used to represent that the first dynamic data occupies all of the space in the third slot.

[0172] Optionally, the detection module 1102 is specifically used to obtain each third hash operation bytecode from the first operation code record, compare the output of any third hash operation bytecode among the third hash operation bytecodes with the read / write address of the corresponding fifth write operation code or the fifth read operation code; if they are the same, the third hash operation bytecode is the first hash operation bytecode; otherwise, the third hash operation bytecode is eliminated; obtain each fourth hash operation bytecode from the second operation code record, compare the output of any fourth hash operation bytecode among the fourth hash operation bytecodes with the read / write address of the corresponding sixth write operation code or the sixth read operation code; if they are the same, the fourth hash operation bytecode is the second hash operation bytecode; otherwise, the fourth hash operation bytecode is eliminated.

[0173] Optionally, the detection module 1102 is further used to determine the logical basic block to which each operation code in the first operation code record belongs, and to determine the logical basic block to which each operation code in the second operation code record belongs, according to the set block method.

[0174] Those skilled in the art will appreciate that the embodiments of the present application can be provided as methods, systems, or computer program products. Therefore, the present application can adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application can adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.

[0175] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the present application. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 a process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0176] These computer program instructions may also be stored in a computer readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 a process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.

[0177] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing the instructions executed on the computer or other programmable device for implementing the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.

[0178] Obviously, those skilled in the art may make various changes and modifications to this application without departing from the spirit and scope of this application. Thus, if these modifications and variations of this application fall within the scope of the claims of this application and their equivalents, this application is intended to include these modifications and variations.

Claims

1. A smart contract detection method, characterized in that: The method comprises: Obtaining an old smart contract and a new smart contract, where the new smart contract is an updated version of the old smart contract; Decompiling the old smart contract and the new smart contract to obtain a first opcode record corresponding to the old smart contract and a second opcode record corresponding to the new smart contract, respectively, wherein the first opcode record and the second opcode record are used to record the opcodes executed and the execution order of the opcodes when the old smart contract and the new smart contract are decompiled; Obtaining, according to the first operation code record and the second operation code record, first storage location information of each first state variable in the old smart contract and second storage location information of each second state variable in the new smart contract; Comparing each first storage location information with the corresponding second storage location information to see if there is a difference; if there is a difference, the new smart contract update fails; Wherein, according to the first operation code record and the second operation code record, respectively obtaining the first storage location information of each first state variable in the old smart contract and the second storage location information of each second state variable in the new smart contract includes: Obtain, according to the first opcode record and the second opcode record, first static data location information of each first static variable and first dynamic data location information of each first dynamic variable in the old smart contract, as well as second static data location information of each second static variable and second dynamic data location information of each second dynamic variable in the new smart contract; Comparing each first storage location information with the corresponding second storage location information to see if there is a difference; if there is a difference, the new smart contract update fails, including: Compare each of the first static data location information with the corresponding second static data location information, and each of the first dynamic data location information with the corresponding second dynamic data location information to see whether there are differences. If there are differences, the new smart contract update fails.

2. The method according to claim 1, wherein Obtaining, according to the first operation code record and the second operation code record, first static data location information of each first static variable in the old smart contract and second static data location information of each second static variable in the new smart contract, respectively, including: Obtaining a first write operation code and / or a first read operation code, and a second write operation code and / or a second read operation code from the first operation code record and the second operation code record respectively; Obtaining first static data location information of each of the first static variables according to the obtained first read / write addresses of each of the first write operation codes and / or each of the first read operation codes; According to the obtained second read / write addresses of the respective second write operation codes and / or the respective second read operation codes, the second static data position information of the respective second static variables is obtained.

3. The method according to claim 2, wherein Obtaining first static data location information of each of the first static variables according to the first read / write addresses of each of the obtained first write operation codes and / or each of the first read operation codes, including: determining, based on the respective first read / write addresses of the respective first write operation codes and / or the respective first read operation codes, first variable declarations corresponding to the respective first static variables of the respective first write operation codes and / or the respective first read operation codes, thereby obtaining a data length of the corresponding first static data based on a declaration position in each first variable declaration; Obtaining a first storage space and first slots in the first storage space that are arranged in sequence according to the first read / write addresses; According to the size of each first slot and the arrangement order of each first slot, as well as the data length of each first static data and the order of each corresponding operation code, the slot number of the first slot where each first static data is located, as well as the starting position and ending position in the first slot are determined.

4. The method according to claim 1, wherein Obtaining, according to the first operation code record and the second operation code record, first dynamic data location information of each first dynamic variable in the old smart contract and second dynamic data location information of each second dynamic variable in the new smart contract, respectively, including: Obtaining a first hash operation bytecode and a second hash operation bytecode from the first operation code record and the second operation code record respectively; Obtaining first dynamic data location information of each of the first dynamic variables according to the logical basic block to which each of the obtained first hash operation bytecodes belongs; The second dynamic data location information of each of the second dynamic variables is obtained according to the logical basic block to which each of the obtained second hash operation bytecodes belongs.

5. The method according to claim 4, wherein Obtaining first dynamic data location information of each of the first dynamic variables according to the logical basic blocks to which each of the obtained first hash operation bytecodes belongs, including: Determining, based on the obtained logical basic blocks to which the first hash operation bytecodes respectively belong, an addition operation code corresponding to each of the first hash operation bytecodes, and a third write operation code and / or a third read operation code corresponding to each of the first hash operation bytecodes; For any first hash operation bytecode, the left parameter of the addition operation code corresponding to the first hash operation bytecode is used as the slot number of the third slot where the first dynamic data of the first dynamic variable is located, and if the first hash operation bytecode corresponds to two adjacent third read operation codes, the data type of the first dynamic data corresponding to the first hash operation bytecode is a partial data type; otherwise, the data type of the first dynamic data corresponding to the first hash operation bytecode is a full data type, the partial data type is used to represent that the first dynamic data occupies part of the space in the third slot, and the full data type is used to represent that the first dynamic data occupies all of the space in the third slot.

6. The method according to claim 4, wherein Obtaining a first hash operation bytecode and a second hash operation bytecode from the first operation code record and the second operation code record, respectively, includes: Obtaining each third hash operation bytecode from the first operation code record, and comparing the output of any third hash operation bytecode among the third hash operation bytecodes with the read / write address of the corresponding fifth write operation code or fifth read operation code; if the output is the same, the third hash operation bytecode is the first hash operation bytecode; otherwise, the third hash operation bytecode is discarded; Obtain each fourth hash operation bytecode from the second operation code record, and compare the output of any fourth hash operation bytecode among the fourth hash operation bytecodes with the read / write address of the corresponding sixth write operation code or the sixth read operation code; if they are the same, the fourth hash operation bytecode is the second hash operation bytecode; otherwise, the fourth hash operation bytecode is eliminated.

7. The method according to claim 1, wherein Before respectively obtaining first dynamic data location information of each first dynamic variable in the old smart contract and second dynamic data location information of each second dynamic variable in the new smart contract according to the first operation code record and the second operation code record, the method further includes: According to the set block method, the logical basic block to which each operation code in the first operation code record belongs is determined, and the logical basic block to which each operation code in the second operation code record belongs is determined.

8. A smart contract detection device, characterized in that: The device comprises: An acquisition module, configured to acquire an old smart contract and a new smart contract, wherein the new smart contract is an updated version of the old smart contract; a detection module, configured to decompile the old smart contract and the new smart contract, and obtain a first operation code record corresponding to the old smart contract and a second operation code record corresponding to the new smart contract, respectively, wherein the first operation code record and the second operation code record are respectively used to record each operation code executed when the old smart contract and the new smart contract are decompiled, and the execution order of each operation code; The detection module is further configured to obtain, based on the first operation code record and the second operation code record, first storage location information of each first state variable in the old smart contract and second storage location information of each second state variable in the new smart contract; The detection module is further configured to compare each first storage location information with the corresponding second storage location information to determine whether there is a difference. If there is a difference, the new smart contract fails to be updated. The detection module is further specifically configured to obtain, based on the first operation code record and the second operation code record, the first static data location information of each first static variable and the first dynamic data location information of each first dynamic variable in the old smart contract, as well as the second static data location information of each second static variable and the second dynamic data location information of each second dynamic variable in the new smart contract; and compare the first static data location information with the corresponding second static data location information, and the first dynamic data location information with the corresponding second dynamic data location information to determine whether there are differences. If there are differences, the update of the new smart contract fails.

9. A computing device, characterized in that include: a memory for storing program instructions; A processor, configured to call the program instructions stored in the memory and execute the method according to any one of claims 1 to 7 according to the obtained program.

10. A computer-readable non-volatile storage medium, characterized in that: The method comprises computer-readable instructions, and when a computer reads and executes the computer-readable instructions, the computer is caused to perform the method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Formal verification method of intelligent contract, electronic device and storage medium

    CN110532176A

  • Intelligent contract testing method and device

    CN111581077A