Test case generation method, system and device of block chain virtual machine

By generating bytecode based on the blockchain virtual machine operation code and parsing the control flow graph associated transactions, the problem of mismatch between smart contract bytecode and transactions is solved, and the effectiveness and efficiency of blockchain virtual machine testing are improved.

CN118535447APending Publication Date: 2024-08-23SHENZHEN YUNYIN DIGITAL INTELLIGENCE TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410263319.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-03-08
Publication Date
2024-08-23

AI Technical Summary

Technical Problem

When the existing technology generates blockchain virtual machine test cases, the smart contract bytecode and transactions do not match, causing the test to be invalid, unable to effectively cover all situations, and the test efficiency is low.

Method used

By generating bytecode based on the combination of blockchain virtual machine operation codes, parsing and generating control flow graphs, and correlating smart contract transactions, the generation of invalid test cases can be avoided.

Benefits of technology

It improves the effectiveness of test cases and the efficiency of blockchain virtual machine testing, ensuring the accuracy and reliability of test results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118535447A
    Figure CN118535447A
Patent Text Reader

Abstract

The invention specifically discloses a test case generation method, system and device for a block chain virtual machine, and the method comprises the steps: carrying out the combination based on operation codes of the block chain virtual machine, and generating byte codes; and based on the generated byte code, analyzing the smart contract, generating a control flow diagram corresponding to the byte code of the smart contract, and generating a smart contract transaction for information in the control flow diagram to obtain a test case for the block chain virtual machine. According to the method, the effectiveness of the test case is improved, so that more efficient block chain virtual machine test is realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a test case generation method, system and device for a blockchain virtual machine. Background Art

[0002] Blockchain:

[0003] Blockchain is a distributed ledger technology that links data together in blocks to form an immutable, publicly verifiable record of data. The main features of blockchain include decentralization, security, transparency, and immutability. It is often used to record and verify transactions or events without the need to trust an intermediary.

[0004] Blockchain Virtual Machine:

[0005] The blockchain virtual machine is part of the blockchain system and is responsible for executing smart contracts. It is a virtual computing environment that allows smart contract code to run on the blockchain. Its input mainly includes: smart contract bytecode and smart contract transactions. According to the specific information of the transaction, the smart contract bytecode is executed and the execution results are recorded.

[0006] Smart Contracts:

[0007] A smart contract is an automated, self-executing code. Smart contracts usually run on a blockchain and execute transactions or agreements according to predetermined rules and conditions without the need for intermediaries. They can be used for a variety of purposes, such as digital currency transactions, supply chain management, crowdfunding, etc.

[0008] Blockchain virtual machine operation code:

[0009] It is the basic instruction set used to execute blockchain smart contracts. These opcodes are part of the blockchain virtual machine, and they define the operations and behaviors of smart contracts running on the blockchain. Different blockchain platforms may use different virtual machines and opcodes, but their basic concepts are similar.

[0010] Smart contract bytecode:

[0011] Smart contract bytecode is the compiled form of a smart contract, usually a low-level, binary representation. Bytecode is a sequence of opcodes. Smart contract bytecode is code that a blockchain virtual machine can understand and execute. Developers write smart contract code in a high-level language, which is then converted into bytecode by a compiler for deployment and execution on the blockchain.

[0012] Smart Contract Transactions:

[0013] Smart contract transactions are blockchain transactions that contain instructions for executing smart contracts. When a user wishes to interact with a smart contract, they will create a smart contract transaction, which includes input data and the contract address. These transactions are submitted to the blockchain and then executed by the blockchain virtual machine, which processes the transaction according to the rules of the smart contract and updates the blockchain state when necessary.

[0014] In general, the blockchain virtual machine is the environment for executing smart contracts, smart contracts are automated codes, smart contract bytecode is the compiled form of smart contracts, and smart contract transactions are the way to interact with smart contracts. These concepts together constitute the core components of smart contracts in blockchain technology.

[0015] Different from the traditional code test cases which are usually single function calls, the blockchain virtual machine test cases consist of two parts: smart contract bytecode and smart contract transactions, i.e. smart contract function calls. If these two parts are generated separately during the test case generation process, the probability of invalid test cases will be too high, and the blockchain virtual machine cannot be effectively tested.

[0016] The existing technology mainly implements the testing of blockchain virtual machines by randomly generating smart contract bytecodes and smart contract transactions respectively. In this process, since the smart contract bytecode is compiled from the smart contract source code, it cannot cover the situation where it cannot be compiled. And because the smart contract bytecode does not match the generated smart contract transaction, the execution is invalid and the blockchain virtual machine cannot be effectively tested. To address this problem, the present invention proposes a blockchain virtual machine test case association generation method to improve the effectiveness of the test case, thereby achieving more efficient blockchain virtual machine testing

[0017] The core technical problems to be solved by the present invention are: (1) the existing methods all compile the smart contract source code to obtain the corresponding smart contract bytecode, which cannot cover all situations well; (2) how to associate the generation process of smart contract bytecode and smart contract transactions to improve the effectiveness of blockchain virtual machine test cases, thereby improving the efficiency of blockchain virtual machine testing. Summary of the invention

[0018] The present application provides a test case generation method, system and device for a blockchain virtual machine to solve the above problems.

[0019] In a first aspect, the present application provides a test case generation method for a blockchain virtual machine, the method comprising:

[0020] Combine the opcodes based on the blockchain virtual machine to generate bytecode;

[0021] Based on the generated bytecode, the smart contract is parsed and a control flow graph corresponding to the smart contract bytecode is generated. Based on the information in the control flow graph, smart contract transactions are generated to obtain test cases for the blockchain virtual machine.

[0022] In a second aspect, the present application also provides a test case generation system for a blockchain virtual machine, the system comprising:

[0023] Smart contract bytecode generation unit, used to combine the operation codes based on the blockchain virtual machine to generate bytecode;

[0024] The smart contract transaction generation unit is used to parse the smart contract based on the generated bytecode, generate a control flow graph corresponding to the smart contract bytecode, and generate smart contract transactions based on the information in the control flow graph to obtain test cases for the blockchain virtual machine.

[0025] In a third aspect, the present application also provides a test case generation device for a blockchain virtual machine, comprising at least one storage medium and at least one processor, wherein the at least one storage medium is used to store computer instructions; and the at least one processor is used to execute the computer instructions to implement the above-mentioned method.

[0026] The present application discloses a test case generation method, system and device for a blockchain virtual machine, which generates bytecode by combining the opcodes based on the blockchain virtual machine; based on the generated bytecode, the smart contract is parsed, and a control flow graph corresponding to the smart contract bytecode is generated, and the smart contract transaction is generated according to the information in the control flow graph to obtain a test case for the blockchain virtual machine. The method improves the effectiveness of the test case, thereby achieving more efficient blockchain virtual machine testing. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are 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 work.

[0028] Figure 1 is a schematic flow chart of a method for training a target recognition model provided in an embodiment of the present application;

[0029] Figure 2 A schematic block diagram of a test case generation system for a blockchain virtual machine provided in an embodiment of the present application;

[0030] Figure 3A schematic block diagram of the structure of a test case generation device for a blockchain virtual machine provided in one embodiment of the present application. DETAILED DESCRIPTION

[0031] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0032] The flowcharts shown in the accompanying drawings are only examples and do not necessarily include all the contents and operations / steps, nor must they be executed in the order described. For example, some operations / steps may also be decomposed, combined or partially merged, so the actual execution order may change according to actual conditions.

[0033] The embodiments of the present application provide a test case generation method, system and device for a blockchain virtual machine. The generation method can be applied to a terminal or a server to automatically associate the smart contract bytecode and the corresponding smart contract transaction, thereby avoiding the generation of invalid test cases and effectively evaluating the security of the blockchain virtual machine.

[0034] In conjunction with the accompanying drawings, some embodiments of the present application are described in detail below. In the absence of conflict, the following embodiments and features in the embodiments can be combined with each other.

[0035] See also Figure 1 , Figure 1 The present invention provides a method for generating a test case for a blockchain virtual machine, wherein the method comprises steps S101 to S105.

[0036] S100, combining the operation codes based on the blockchain virtual machine to generate bytecode.

[0037] S200. Based on the generated bytecode, the smart contract is parsed, and a control flow graph corresponding to the smart contract bytecode is generated. Based on the information in the control flow graph, a smart contract transaction is generated to obtain a test case for the blockchain virtual machine.

[0038] The core of the present invention is that it automatically associates the smart contract bytecode and the corresponding smart contract transaction, which can avoid generating invalid test cases and effectively evaluate the security of the blockchain virtual machine.

[0039] The following is a detailed description of S100:

[0040] First, the blockchain virtual machine operation code is given, which includes: stack storage type, stack usage type, heap storage type, heap extraction type and environment value type;

[0041] After given the blockchain virtual machine opcodes that are well-classified, the opcodes are generated one by one to form the final smart contract.

[0042] The stack and the heap are two types of storage spaces maintained by the virtual machine during execution. Different opcodes implement the logic of storing, transferring, and modifying the corresponding data in the storage space. It is worth noting that the opcode of the extraction type may also have a storage action when extracting. The present invention regards the opcode with the extraction action as the extraction type. The environment value type refers to extracting the value in the environment and storing it in the storage space. The environment value includes the block number, block timestamp, transaction call data, etc.

[0043] After the blockchain virtual machine opcodes are given and classified, the opcodes are generated one by one and the final smart contract bytecode is formed. The specific algorithm is as follows:

[0044]

[0045] Based on the above algorithm, an operation code sequence for any type of blockchain virtual machine, namely, smart contract bytecode, can be obtained.

[0046] S200 is described in detail below. It includes:

[0047] Generate smart contract transaction call parameters according to the smart contract bytecode to obtain a complete smart contract transaction;

[0048] Combine the smart contract bytecode with the smart contract transaction to obtain the test case of the blockchain virtual machine.

[0049] Specifically, smart contract transactions consist of four main parts: target bytecode, transaction initiator, transaction call parameters, and transaction parameters. The target bytecode is set to the bytecode generated by S100. The transaction initiator is set to any user address. Transaction parameters include transaction amount, transaction fee cap, and transaction fee price. This step uses a random number generator to generate random parameters based on the transaction parameter type. Then, based on the above-mentioned generation of smart contract bytecode, the transaction call parameter part is generated accordingly. The specific algorithm is as follows:

[0050]

[0051] Based on the above algorithm, the generated smart contract bytecode can be used to generate smart contract transaction call parameters, thereby obtaining a complete smart contract transaction. The smart contract bytecode and smart contract transaction are combined to obtain a test case for the blockchain virtual machine.

[0052] The smart contract bytecode generation algorithm proposed in the present invention does not need to be directly generated through smart contract source code compilation; it is based on the analysis of the opcode in the bytecode to generate corresponding valid smart contract transactions. The problem of blockchain virtual machine test case failure caused by the mismatch between bytecode and transaction is avoided, and the test case generation efficiency of blockchain virtual machine is high.

[0053] See also Figure 2 , Figure 2 It is a schematic block diagram of a test case generation system for a blockchain virtual machine provided in an embodiment of the present application. The system 300 includes: a smart contract bytecode generation unit 301 and a smart contract transaction generation unit 302.

[0054] Smart contract bytecode generation unit, used to combine the operation codes based on the blockchain virtual machine to generate bytecode;

[0055] The smart contract transaction generation unit is used to parse the smart contract based on the generated bytecode, generate a control flow graph corresponding to the smart contract bytecode, and generate smart contract transactions based on the information in the control flow graph to obtain test cases for the blockchain virtual machine.

[0056] It should be noted that those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the system and each unit described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.

[0057] The above system can be implemented in the form of a computer program. Figure 3 The test case generation device of the blockchain virtual machine shown is run on it.

[0058] See also Figure 3 , Figure 3 The present invention is a schematic block diagram of a test case generation device for a blockchain virtual machine provided in an embodiment of the present invention. The test case generation device for the blockchain virtual machine may be a server or a terminal.

[0059] See also Figure 3 , the test case generation device of the blockchain virtual machine includes a processor, a memory and a network interface connected through a system bus, wherein the memory may include a non-volatile storage medium and an internal memory.

[0060] The non-volatile storage medium can store an operating system and a computer program. The computer program includes program instructions, and when the program instructions are executed, the processor can execute any moving target detection method.

[0061] The processor is used to provide computing and control capabilities and support the operation of the entire computer equipment.

[0062] The internal memory provides an environment for the operation of the computer program in the non-volatile storage medium. When the computer program is executed by the processor, the processor can execute any test case generation method for the blockchain virtual machine.

[0063] The network interface is used for network communication, such as sending assigned tasks, etc. Those skilled in the art will understand that Figure 3 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0064] It should be understood that the processor may be a central processing unit (CPU), and the processor may also be other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. Among them, the general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.

[0065] In one embodiment, the processor is used to run a computer program stored in the memory to implement the following steps:

[0066] The opcodes of the blockchain virtual machine are combined to generate bytecodes. Based on the generated bytecodes, the smart contract is parsed and a control flow graph corresponding to the smart contract bytecode is generated. Based on the information in the control flow graph, smart contract transactions are generated to obtain test cases for the blockchain virtual machine.

[0067] A computer-readable storage medium is also provided in an embodiment of the present application, wherein the computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and the processor executes the program instructions to implement any test case generation method for a blockchain virtual machine provided in an embodiment of the present application.

[0068] The computer-readable storage medium may be an internal storage unit of the computer device described in the foregoing embodiment, such as a hard disk or memory of the computer device. The computer-readable storage medium may also be an external storage device of the computer device, such as a plug-in hard disk, a smart memory card (SmartMedia Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc., equipped on the computer device.

[0069] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed in the present application, and these modifications or replacements should be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be based on the protection scope of the claims.

Claims

1. Test case generation method for blockchain virtual machine, It is characterized in that include: Combine the opcodes based on the blockchain virtual machine to generate bytecode; Based on the generated bytecode, the smart contract is parsed and a control flow graph corresponding to the smart contract bytecode is generated. Based on the information in the control flow graph, smart contract transactions are generated to obtain test cases for the blockchain virtual machine.

2. The generation method according to claim 1, It is characterized in that The operations based on the blockchain virtual machine are combined to generate bytecode, including the following steps: First, the blockchain virtual machine operation code is given, which includes stack storage type, stack usage type, heap storage type, heap extraction type and environment value type; After given the blockchain virtual machine opcodes that are well-classified, the opcodes are generated one by one to form the final smart contract.

3. The generation method according to claim 2, It is characterized in that In the generation method, all operation codes with fetch actions are regarded as fetch types.

4. The generation method according to claim 2, It is characterized in that The specific algorithm for generating bytecode by combining the operations of the blockchain virtual machine includes: Input: stack storage type opcode set A1, stack extraction type opcode set A2, heap storage type opcode set B1, heap extraction type opcode set B2, environment value type opcode set C, sequence target length d; 1. Initialize the opcode sequence S, set it to a completely empty sequence, initialize the heap space, and set the stack space to be completely empty; 2. Randomly select an opcode op and determine its type; 3. Record the corresponding stack / heap space as non-empty; 4. Add op to the end of S, that is, S = S + op; 5. If op belongs to A2 or op belongs to B2; 6. If op belongs to A2 or op belongs to B2; 7, only when the corresponding stack / heap space is not empty; 8. Add op to the end of S, that is, S = S + op; 9. Randomly generate a number t from 0 to d. If t is not equal to 0; 10, return to 2 and continue the cycle; 11. Return the generated operation code sequence S.

5. The generation method according to claim 1, It is characterized in that In the above, the smart contract transaction is generated for the information in the control flow graph to obtain a test case for the blockchain virtual machine, including: Generate smart contract transaction call parameters according to the smart contract bytecode to obtain a complete smart contract transaction; Combine the smart contract bytecode with the smart contract transaction to obtain the test case of the blockchain virtual machine.

6. The generation method according to claim 5, It is characterized in that The algorithm for generating the smart contract transaction call parameters includes: Input: Operation code sequence S; Output: transaction call parameter set T; 1. Initialize the operation code sequence t, set it to a completely empty sequence, and initialize the set A; 2. Construct a control flow graph G based on the opcode sequence; 3. Traverse each path s in the control flow graph G one by one; 4. If s contains an environment value operation code and the value target is a transaction call parameter; 5. Record the transaction call parameter range judgment taken by the value operation code, recorded as (a0, a1); 6. Record (a0, a1) into A; 7,L=0; 8. Take out the value in A and record it as (a0, a1); 9, L = MAX (L, a1); 10. Use a random number generator to randomly generate a hexadecimal sequence t of length L; 11. Store t into T.

7. A test case generation system for blockchain virtual machines, It is characterized in that include: Smart contract bytecode generation unit, used to combine the operation codes based on the blockchain virtual machine to generate bytecode; The smart contract transaction generation unit is used to parse the smart contract based on the generated bytecode, generate a control flow graph corresponding to the smart contract bytecode, and generate smart contract transactions based on the information in the control flow graph to obtain test cases for the blockchain virtual machine.

8. A test case generation device for a blockchain virtual machine, It is characterized in that It includes at least one storage medium and at least one processor, wherein the at least one storage medium is used to store computer instructions; and the at least one processor is used to execute the computer instructions to implement the test case generation method for the blockchain virtual machine as described in claims 1-6.