Intelligent contract test script generation method and system, medium and equipment

By obtaining the key information of the smart contract, and generating test scripts in the template using a large language model, the smart contract test scripts are solved, and the problem of time-consuming, error-prone and incomplete coverage is achieved, achieving efficient and comprehensive testing results.

CN120386738APending Publication Date: 2025-07-29OXFORD (HAINAN) BLOCKCHAIN RES INST CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510696715.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-28
Publication Date
2025-07-29

AI Technical Summary

Technical Problem

Smart contract test scripts are time-consuming and error-prone, the test scenarios are not comprehensive, and the professionalism requirements are high, making it difficult to achieve efficient and comprehensive testing.

Method used

By obtaining the contract key information of the target smart contract, generating a test requirement list, and filtering matching test templates from the standardized template library, using a large language model to generate test scripts within the template to ensure the accuracy and coverage of the test scripts.

Benefits of technology

It significantly reduces the randomness and error rate of test scripts, improves the direct availability and coverage of test scripts, and enables the generation of comprehensive test scripts that are suitable for different types of smart contracts.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120386738A_ABST
    Figure CN120386738A_ABST
Patent Text Reader

Abstract

The invention provides an intelligent contract test script generation method and system, a medium and equipment, and relates to the technical field of block chains, and the method comprises the steps: obtaining a target intelligent contract; contract key information of the target smart contract is extracted; generating a test demand list according to the contract key information; screening a matching test template which is matched with the contract type of the target smart contract and meets the test requirement list from a standardized template library; and inputting the matching test template, the contract key information and the test demand as contexts into a set large language model, and guiding the set large language model to generate the test script in the matching test template. According to the method and the device, the randomness and the error rate of generating the test script are remarkably reduced, the direct availability of the test script is improved, a more comprehensive test script can be generated, and the method and the device can adapt to different types of smart contracts.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of blockchain technology, and particularly to a method, system, medium, and device for generating intelligent contract test scripts. Background Art

[0002] As a core component of blockchain technology, the security and stability of intelligent contracts are crucial for the entire blockchain ecosystem. Since it is usually difficult to modify intelligent contracts once they are deployed on the blockchain, it is particularly necessary to conduct comprehensive tests before deployment. However, there are the following problems in the process of intelligent contract testing:

[0003] Time-consuming in writing test scripts: Under the traditional method, developers need to manually write test scripts, which is time-consuming and error-prone for complex contracts.

[0004] Incomplete coverage of test scenarios: Manually written test scripts may not cover all edge cases and abnormal scenarios, resulting in insufficient testing.

[0005] High requirement for testing professionalism: Writing high-quality intelligent contract test scripts requires knowledge of both blockchain technology and testing methodology, and professional talents are scarce.

[0006] Therefore, how to achieve the flexibility of intelligent contract test scripts to improve the test efficiency and quality is a technical problem that needs to be solved urgently by those skilled in the art. Summary of the Invention

[0007] The purpose of this application is to provide a method, system, computer-readable storage medium, and electronic device for generating intelligent contract test scripts, which can improve the test efficiency and test accuracy of test scripts for intelligent contracts.

[0008] To solve the above technical problems, this application provides a method for generating intelligent contract test scripts, and the specific technical solutions are as follows:

[0009] Obtain the target intelligent contract;

[0010] Extract the contract key information of the target intelligent contract;

[0011] Generate a test requirement list according to the contract key information;

[0012] Screen from the standardized template library a matching test template that matches the contract type of the target intelligent contract and meets the test requirement list;

[0013] Input the matching test template, the contract key information, and the test requirements as context into a set large language model to guide the set large language model to generate the test script within the matching test template.

[0014] Optionally, extracting the contract key information of the target smart contract includes:

[0015] Parsing the contract source code of the target smart contract to construct an abstract syntax tree corresponding to the contract source code;

[0016] Extracting all public function interfaces of the target smart contract and their parameter types;

[0017] Identifying all state variables included in the target smart contract and their access modifiers;

[0018] Identifying all event definitions and triggering conditions of the target smart contract;

[0019] Identifying the permission control mechanism of the target smart contract;

[0020] Identifying the base classes inherited by the target smart contract and the interfaces implemented;

[0021] Structurally processing the abstract syntax tree, the public function interfaces and their parameter types, the state variables and their access modifiers, the event definitions and triggering conditions, the permission control mechanism, the inherited base classes and the interfaces implemented to obtain the contract key information.

[0022] Optionally, generating a test requirement list based on the contract key information includes:

[0023] Determining functional test requirements according to the public function interfaces and their parameter types and the inherited base classes and the interfaces implemented;

[0024] Determining security test requirements according to the state variables and their access modifiers, the abstract syntax tree and the permission control mechanism;

[0025] Determining business logic test requirements according to the abstract syntax tree, the public function interfaces and their parameter types, the state variables and their access modifiers and the event definitions and triggering conditions;

[0026] Generating a test requirement list based on the functional test requirements, the security test requirements and the business logic test requirements.

[0027] Optionally, before screening a matching test template that matches the contract type of the target smart contract and meets the test requirement list from a standardized template library, further includes:

[0028] Set a standardized template that includes a fixed content partition, a content customization partition, and a content to be filled partition; the fixed content partition includes template header information, an import area, an environment setting area, and a cleanup area, the content customization partition includes an initialization area, and the content to be filled partition is a parameter definition area, a business logic test area, and an assertion area for the set large language model to fill;

[0029] Configure the intelligent contract test scenarios corresponding to each of the standardized templates to generate the standardized template library.

[0030] Optionally, screening the matching test templates that match the contract type of the target intelligent contract and meet the test requirement list from the standardized template library includes:

[0031] Determine the template range that matches the contract type of the target intelligent contract;

[0032] Determine the requirement coverage rate of each test template for the test requirement list within the template range;

[0033] Obtain the historical test success rate of each test template within the template range;

[0034] Calculate the matching degree of each test template based on the requirement coverage rate and its corresponding first parameter, and the historical test success rate and its corresponding second parameter; the weight value of the first parameter is greater than that of the second parameter;

[0035] Use the test templates with a matching degree greater than the set matching threshold as the matching test templates.

[0036] Optionally, after guiding the set large language model to generate the test script within the matching test template, it further includes:

[0037] Detect the code detection coverage rate of the test script for the target intelligent contract and identify the uncovered areas;

[0038] Update the execution logic of the test script based on the uncovered areas, or add a loop structure to the test script.

[0039] Optionally, after guiding the set large language model to generate the test script within the matching test template, it further includes:

[0040] Execute the test script and collect the test results and / or error information;

[0041] Adjust the first parameter and / or the second parameter according to the test results and / or the error information to update the matching test template.

[0042] This application also provides a system for generating an intelligent contract test script, including:

[0043] An acquisition module, configured to acquire a target smart contract;

[0044] A contract analysis module, configured to extract contract key information of the target smart contract;

[0045] A test requirement matching module, configured to generate a test requirement list according to the contract key information;

[0046] A target matching module, configured to screen a matching test template from a standardized template library that matches the contract type of the target smart contract and meets the test requirement list;

[0047] A script generation module, configured to input the matching test template, the contract key information, and the test requirements as context into a set large language model, and guide the set large language model to generate the test script within the matching test template.

[0048] This application also provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the method described above are implemented.

[0049] This application also provides an electronic device, including a memory and a processor. A computer program is stored in the memory. When the processor calls the computer program in the memory, the steps of the method described above are implemented.

[0050] This application provides a method for generating a smart contract test script, including: acquiring a target smart contract; extracting contract key information of the target smart contract; generating a test requirement list according to the contract key information; screening a matching test template from a standardized template library that matches the contract type of the target smart contract and meets the test requirement list; inputting the matching test template, the contract key information, and the test requirements as context into a set large language model, and guiding the set large language model to generate the test script within the matching test template.

[0051] When this application generates a smart contract test script, it first generates a test requirement list based on the contract key information of the target smart contract, and then screens a matching test template. By using the matching test template to constrain the test script generation process of the preset large language model, the randomness and error rate of generating the test script are significantly reduced, and the direct usability of the test script is improved. At the same time, the structured matching test can ensure that the generated test script meets the requirements of the standard framework, reducing the workload of manual adjustment. At the same time, relying on the flexibility of the set large language model, a more comprehensive test script can be generated, which can not only adapt to different types of smart contracts, but also ensure the script quality of the test script by updating the standardized template library on this basis.

[0052] The present application also provides a generation system for intelligent contract test scripts, a computer-readable storage medium, and an electronic device, which have the above beneficial effects and will not be elaborated here. BRIEF DESCRIPTION OF THE DRAWINGS

[0053] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the drawings in the following description are only the embodiments of the present application. For those of ordinary skill in the art, other drawings can be obtained according to the provided drawings without creative efforts.

[0054] Figure 1 It is a flowchart of a method for generating an intelligent contract test script provided by an embodiment of the present application;

[0055] Figure 2 It is a schematic diagram of an exemplary matching test template provided by an embodiment of the present application;

[0056] Figure 3 It is a schematic diagram of the data flow direction of the intelligent contract test script provided by an embodiment of the present application;

[0057] Figure 4 It is a schematic diagram of the structure of a generation system for intelligent contract test scripts provided by an embodiment of the present application;

[0058] Figure 5 It is a structural diagram of an electronic device provided by an embodiment of the present application. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0059] To make the objectives, technical solutions, and advantages of the embodiments of the present application clearer, the following will clearly and completely describe the technical solutions in the embodiments of the present application with reference to the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are some, but not all, of the embodiments of the present application. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present application without creative efforts fall within the scope of protection of the present application.

[0060] See Figure 1 , Figure 1 It is a flowchart of a method for generating an intelligent contract test script provided by an embodiment of the present application. The method includes:

[0061] S101: Obtain a target intelligent contract;

[0062] S102: Extract the contract key information of the target intelligent contract;

[0063] S103: Generate a test requirement list according to the contract key information;

[0064] S104: Screen the matching test templates in the standardized template library that match the contract type of the target smart contract and meet the test requirement list;

[0065] S105: Input the matching test template, the contract key information, and the test requirements as context into a set large language model to guide the set large language model to generate the test script within the matching test template.

[0066] Here, there is no limitation on how to obtain the target smart contract, which can be the source code of the smart contract input by the user or the target smart contract corresponding to the specified address.

[0067] After obtaining the target smart contract, it is necessary to parse the target smart contract to extract the contract key information. Here, there is no limitation on the specific content of the contract key information, and all information that can point to the test requirements can be used as the contract key information.

[0068] In a feasible implementation manner, extracting the contract key information of the target smart contract may include the following steps:

[0069] A1. Parse the contract source code of the target smart contract and construct an abstract syntax tree corresponding to the contract source code;

[0070] A2. Extract all public function interfaces of the target smart contract and their parameter types;

[0071] A3. Identify all state variables included in the target smart contract and their access modifiers;

[0072] A4. Identify all event definitions and trigger conditions of the target smart contract;

[0073] A5. Identify the permission control mechanism of the target smart contract;

[0074] A6. Identify the base classes inherited by the target smart contract and the interfaces implemented;

[0075] A7. Structurally process the abstract syntax tree, the public function interfaces and their parameter types, the state variables and their access modifiers, the event definitions and trigger conditions, the permission control mechanism, the inherited base classes and the implemented interfaces to obtain the contract key information.

[0076] It should be noted that steps A1 - A6 are parallel execution steps, and changing the execution order of these steps does not affect the process of extracting the contract key information.

[0077] The above process includes processes such as lexical and syntactic analysis, interface recognition, state variable recognition, event analysis, access control analysis, and inheritance relationship analysis, so as to obtain the key information of the contract. The key information of the contract can be stored in the set storage space and used as the basis for subsequent test requirement extraction and test script generation.

[0078] By extracting the key information of the target smart contract, the generated test requirement list can accurately reflect the core functions and features of the contract. Screening out templates that match the contract type and meet the test requirements from the standardized template library ensures that the test script is closely related to the actual functions of the contract, avoiding irrelevant or inapplicable test content and improving the usability of the test script. Extracting the key information of the target smart contract, including contract functions, data structures, interface definitions, etc., ensures the integrity of the test requirement list, thus providing a basis for generating comprehensive test scripts.

[0079] After that, a test requirement list is generated according to the key information of the contract. The test requirement list mainly includes two parts, namely the key functions and potential vulnerabilities to be tested. It should be noted that the generated test requirement list is closely related to the extracted key information of the contract.

[0080] In a feasible implementation, taking the test requirement list including functional test requirements, security test requirements, and business logic test requirements as an example, the following steps can be included when generating the test requirement list:

[0081] B1. Determine the functional test requirements according to the public function interface and its parameter types, and the inherited base class and implemented interfaces;

[0082] B2. Determine the security test requirements according to the state variables and their access modifiers, the abstract syntax tree, and the permission control mechanism;

[0083] B3. Determine the business logic test requirements according to the abstract syntax tree, the public function interface and its parameter types, the state variables and their access modifiers, and the event definition and triggering conditions;

[0084] B4. Generate a test requirement list based on the functional test requirements, the security test requirements, and the business logic test requirements.

[0085] By analyzing the public function interface and its parameter types, the functions and services provided by the contract to the outside can be clarified. This enables targeted generation of test cases for each function to ensure that each function is fully tested and improve the coverage of functional testing.

[0086] By comprehensively considering the abstract syntax tree, public function interfaces and their parameter types, state variables and their access modifiers, and event definitions and triggering conditions, the business logic of the contract can be comprehensively analyzed from multiple dimensions. This helps to identify key nodes and complex logic in the business process, generate test cases that can cover various business scenarios and state transitions, and ensure the integrity and correctness of the business logic.

[0087] Combining state variables and their access modifiers, the abstract syntax tree, and the permission control mechanism to determine security test requirements can deeply explore potential security risk points in the contract.

[0088] After obtaining the test requirement list, matching test templates that match the contract type of the target smart contract and meet the test requirement list can be screened from the standardized template library. In this embodiment, it is default that before executing step S104, the standardized template library has been constructed and can be accessed to obtain test templates.

[0089] When performing the screening of matching test templates, the screening can be carried out according to the set matching criteria. Here, the matching criteria are not limited. For example, they can include contract type matching degree, test requirement coverage rate, and template historical success rate, etc.

[0090] In a feasible implementation manner, the following method can be adopted:

[0091] C1. Determine the template range that matches the contract type of the target smart contract;

[0092] C2. Determine the requirement coverage rate of each test template for the test requirement list within the template range;

[0093] C3. Obtain the historical test success rate of each test template within the template range;

[0094] C4. Calculate the matching degree of each test template based on the requirement coverage rate and its corresponding first parameter, and the historical test success rate and its corresponding second parameter; the weight value of the first parameter is greater than that of the second parameter;

[0095] C5. Use the test templates with a matching degree greater than the set matching threshold as the matching test templates.

[0096] Here, the specific values of the first parameter and the second parameter are not limited and can be set by those skilled in the art themselves.

[0097] It should be noted that there is at least one matching test template, that is, there can be multiple, so the generated test scripts are also at least one.

[0098] When generating a test script, the matching test template, the contract key information, and the test requirements can be used as context and input into a specified large language model, thereby guiding the specified large language model to generate the test script within the matching test template.

[0099] Specifically, the input format accepted by the large language model can be determined, which is usually context information presented in text form. Organize the matching test template, contract key information, and test requirements into a piece of text according to a certain logical structure. At the beginning of the input text, clearly describe the task objective to the large language model, that is, require it to generate the corresponding test script based on the matching test template, combined with the contract key information and test requirements. Write the matching test template, contract key information, and test requirements into the input text in sequence. For each part, make appropriate annotations or explanations so that the model can clearly identify the content and role of each part.

[0100] When guiding the specified large language model to generate a test script, the generation requirements for the test script can be further refined in the input text. For example, require the model to ensure that the test script can cover all functional points, security verification points, and business logic scenarios in the test requirements, and require the script to have correct syntax and clear logic, etc.

[0101] In a feasible implementation manner, some test script examples similar to the current task can also be provided and included in the context. This can help the specified large language model better understand the expected output format and content style, and improve the accuracy of the generated results. Input the organized context text into the specified large language model, and start the generation process by calling the model's interface. The specified large language model can generate a test script according to the input context.

[0102] In the embodiment of this application, when generating an intelligent contract test script, first generate a test requirement list based on the contract key information of the target intelligent contract, and then screen for a matching test template. By using the matching test template to constrain the test script generation process of the preset large language model, the randomness and error rate of generating the test script are significantly reduced, and the direct usability of the test script is improved. At the same time, the structured matching test can ensure that the generated test script meets the requirements of the standard framework, reducing the workload of manual adjustment. At the same time, relying on the flexibility of the specified large language model, a more comprehensive test script can be generated, which can not only adapt to different types of intelligent contracts, but also ensure the script quality of the test script by updating the standardized template library on this basis.

[0103] The following describes how to generate a standardized template library:

[0104] In the first step, a standardized template including a fixed content partition, a content customization partition, and a content to be filled partition is set; the fixed content partition includes a template header information area, an import area, an environment setting area, and a cleanup area, the content customization partition includes an initialization area, and the content to be filled partition is a parameter definition area, a business logic test area, and an assertion area for setting parameters to be filled by the large language model;

[0105] In the second step, configure the intelligent contract test scenarios corresponding to each of the standardized templates to generate the standardized template library.

[0106] See Figure 2 , Figure 2 , which is an exemplary matching test template schematic diagram provided by an embodiment of the present application. The matching test template includes:

[0107] 1. Template header information: including metadata such as template ID, applicable contract type, test target description, etc.;

[0108] 2. Import area: Pre-define the import statements for the required test frameworks, libraries, and tools;

[0109] 3. Environment setting area: Define basic settings such as test environment, network configuration, and test accounts;

[0110] 4. Parameter definition area: The parameter definition space reserved for the large language model, and the large language model needs to fill in the test required parameters according to the specific contract;

[0111] 5. Initialization area: The template for contract deployment and initial state setting;

[0112] 6. Business logic test area: The main test logic filling space reserved for the large language model, including the test scenario structure and test case framework;

[0113] 7. Assertion area: The predefined result verification method, and the large language model needs to fill in the specific assertion conditions;

[0114] 8. Cleanup area: The resource release and state reset operations after the test is completed.

[0115] Among them, the template header information area, the import area, the environment setting area, and the cleanup area are fixed content partitions, while the initialization area is a content customization partition, and the parameter definition area, the business logic test area, and the assertion area are content to be filled partitions.

[0116] Based on the matching test template shown above, an exemplary test script generation process can be as follows:

[0117] In the first step, combine the contract key information, test requirements, and selected template into a structured prompt;

[0118] Step 2: Use the structured prompt as context to guide the large language model to generate content in segments according to the template area. First, generate the content of the parameter definition area; then generate the specific implementation of the initialization area; then generate the content of the business logic test area; and finally generate the specific conditions of the assertion area.

[0119] Step 3: Conduct real-time verification after each segment is generated to ensure compliance with the template specifications and grammar requirements.

[0120] Step 4: Provide feedback and adjustment for the non-compliant parts until the content that meets the standards is generated to obtain the test script.

[0121] Based on the above embodiments, as a preferred embodiment, after guiding the set large language model to generate the test script within the matching test template, it is also possible to detect the code detection coverage rate of the test script for the target smart contract and identify the uncovered areas, so as to update the execution logic of the test script based on the uncovered areas or add a loop structure to the test script.

[0122] Determine the uncovered contract code areas. For the uncovered functions, check their functions, parameters, return value types, and interaction methods with other code parts. For the uncovered branches, analyze the branch conditions to determine which condition combinations are not covered by the test cases.

[0123] For the uncovered functions, if the uncovered function is an independent functional implementation, write test cases to directly call the function and input different types of parameter combinations. For example, if the function is used to calculate the earnings of a certain cryptocurrency, input different principal, interest rate, and time parameters to verify whether the calculation results are correct. If the uncovered function involves interactions with other contracts, simulate the call scenarios of other contracts and construct corresponding contract objects in the test script for interaction testing.

[0124] For the uncovered branches, identify the boundary values in the branch conditions, write test cases for each branch condition, input the boundary values and the values near them to ensure that all branch paths are executed. It is also possible to divide the input space into equivalence classes and select appropriate test cases for each equivalence class to cover different branch logics.

[0125] If there are still uncovered state variables and events, state variable tests and event trigger tests can be executed. State variable tests can write test cases to perform read and write operations on the uncovered state variables and verify their access modifiers and data consistency. For example, for a state variable representing the user's asset balance, test whether its updates are correct in different transaction scenarios. Event trigger tests are used to analyze the triggering conditions of uncovered events, write test cases to simulate the occurrence of these conditions, and verify whether the events are triggered correctly. For example, for a transaction success event, simulate the transaction scenario and check whether the event broadcasts with the correct parameters.

[0126] According to the execution logic of the contract, adjust the execution order of the test cases in the test script so that the code in the uncovered area is accessed at an appropriate time.

[0127] Looping structures can also be added to the test script to perform multiple tests on functions that need to be executed repeatedly to increase the coverage rate. For example, for contract functions that need to be called multiple times, such as loop transfers, loop through the test cases to cover more code paths.

[0128] The optimized test script is incorporated into the continuous integration process, and the test script is automatically run every time the smart contract code is updated. Monitor the coverage results of each test run, analyze the changing trend of the coverage rate, promptly discover and solve newly emerging uncovered areas, and further improve the test coverage rate and test accuracy of the test script.

[0129] After actually applying the test script, test results and / or error information can be collected, and based on the test results and / or the error information, the first parameter and / or the second parameter can be adjusted to update the matching test template.

[0130] Collecting test results and error information can include test pass / fail status, error messages and exception stacks, coverage reports, and execution time and resource consumption. Correspondingly, the weights of the template matching algorithm can be adjusted, or the prompting strategies of the large language model can be optimized.

[0131] For a better understanding of the implementation process of this application, refer to Figure 3 , Figure 3 which is a schematic diagram of the data flow direction of the smart contract test script provided by the embodiment of this application. The following further describes this application in terms of the data flow direction. Figure 3 It includes the following modules or data flow processing entities:

[0132] Smart contract analyzer: Perform static analysis on the target smart contract code to extract key information such as contract interfaces, functions, state variables, and events.

[0133] Standardized Template Library: Stores standardized templates for various smart contract test scenarios. Each template contains a fixed test structure, parameter definition area, business logic test area, assertion area, etc.

[0134] Test Requirement Extractor: Based on the contract analysis results, identify key function points and potential vulnerability points that need to be tested.

[0135] Template Matching Engine: Select the most suitable test template from the template library according to the contract type and test requirements.

[0136] Large Language Model Generation Controller: Guide the large language model to generate test scripts that meet the requirements within the template framework.

[0137] Script Verification Optimizer: Perform syntax checking, logic verification, and coverage analysis on the generated test scripts, and optimize and adjust them.

[0138] Test Executor: Execute the generated test scripts and collect test results and error information.

[0139] Feedback Learning Module: Analyze the test results and feedback them to the template library for continuous optimization.

[0140] Figure 3 The direction of the middle arrow indicates the data flow:

[0141] F1: The source code of the smart contract provided by the user is used as the system input.

[0142] F2: The key information of the contract after analysis, including function interface list, parameter types, return values, state variables, event definitions, access control mechanisms, inheritance relationships, etc.

[0143] F3: Determine the test requirement list according to the contract key information, including key function points and potential vulnerability points that need to be tested.

[0144] F4: The structures and contents of various contract test templates stored in the standardized template library, including a fixed test framework and reserved variable content areas. The template matching engine obtains the matching contract test template according to the test requirement content.

[0145] F5: Screen the matching test template to provide a structured framework for the set large language model.

[0146] F6: A structured prompt word containing contract key information, test requirements, and the selected template, synthesized to guide the large language model to generate prompt words within the template framework.

[0147] F7: The content generated by the set large language model within the template framework according to the prompt words, including parameter definitions, initialization code, test scenarios, and assertion conditions, etc.

[0148] F8: The initial draft of the test script generated by the large language model based on the template, including the complete test structure and test content based on the contract business logic. Submitted to the script verification optimizer.

[0149] F9: The test script optimized and adjusted after syntax checking, logical verification, and coverage analysis.

[0150] F10: The automatically generated script that can be used for smart contract testing and is the final result of the entire system processing.

[0151] F11: The results and error messages of the test script execution, including data such as test pass / fail status, coverage report, and execution time.

[0152] F12: The template optimization data based on the test results, used to improve and expand the standardized template library.

[0153] F13: The optimization data for the template matching algorithm based on the test results, used to improve the accuracy of template selection and finally output the test script.

[0154] See Figure 4 , Figure 4 which is the structural schematic diagram of a smart contract test script generation system provided by an embodiment of the present application. The system includes:

[0155] An acquisition module, configured to acquire a target smart contract;

[0156] A contract analysis module, configured to extract the contract key information of the target smart contract;

[0157] A test requirement matching module, configured to generate a test requirement list according to the contract key information;

[0158] A target matching module, configured to screen out matching test templates from the standardized template library that match the contract type of the target smart contract and meet the test requirement list;

[0159] A script generation module, configured to input the matching test template, the contract key information, and the test requirements as context into a set large language model, and guide the set large language model to generate the test script within the matching test template.

[0160] Based on the above embodiment, as a preferred embodiment, the contract analysis module is a module for performing the following steps:

[0161] Parse the contract source code of the target smart contract and construct an abstract syntax tree corresponding to the contract source code;

[0162] Extract all public function interfaces of the target smart contract and their parameter types;

[0163] Identify all state variables included in the target smart contract and their access modifiers;

[0164] Identify all event definitions and triggering conditions of the target smart contract;

[0165] Identify the permission control mechanism of the target smart contract;

[0166] Identify the base classes inherited by the target smart contract and the interfaces implemented;

[0167] Structurally process the abstract syntax tree, the public function interfaces and their parameter types, the state variables and their access modifiers, the event definitions and triggering conditions, the permission control mechanism, the inherited base classes and the interfaces implemented to obtain the key contract information.

[0168] Based on the above embodiments, as a preferred embodiment, the test requirement matching module is a module for performing the following steps:

[0169] Determine the functional test requirements according to the public function interfaces and their parameter types and the inherited base classes and the interfaces implemented;

[0170] Determine the security test requirements according to the state variables and their access modifiers, the abstract syntax tree and the permission control mechanism;

[0171] Determine the business logic test requirements according to the abstract syntax tree, the public function interfaces and their parameter types, the state variables and their access modifiers and the event definitions and triggering conditions;

[0172] Generate a test requirement list based on the functional test requirements, the security test requirements and the business logic test requirements.

[0173] Based on the above embodiments, as a preferred embodiment, it further includes:

[0174] A template setting module for setting a standardized template including a fixed content partition, a content customization partition and a content to be filled partition; the fixed content partition includes template header information, an import area, an environment setting area and a cleaning area, the content customization partition includes an initialization area, and the content to be filled partition is a parameter definition area, a business logic test area and an assertion area for setting the large language model to fill; configure the smart contract test scenarios corresponding to each standardized template to generate the standardized template library.

[0175] Based on the above embodiments, as a preferred embodiment, the target matching module is a module for performing the following steps:

[0176] Determine a template range that matches the contract type of the target smart contract;

[0177] Determine the requirement coverage rate of each test template for the test requirement list within the template range;

[0178] Obtain the historical test success rate of each test template within the template range;

[0179] Calculate the matching degree of each test template based on the requirement coverage rate and its corresponding first parameter, and the historical test success rate and its corresponding second parameter; the weight value of the first parameter is greater than that of the second parameter;

[0180] Use the test templates with a matching degree greater than the set matching threshold as the matching test templates.

[0181] Based on the above embodiments, as a preferred embodiment, it further includes:

[0182] A script adjustment module for detecting the code detection coverage rate of the test script for the target smart contract and identifying the uncovered areas;

[0183] Update the execution logic of the test script based on the uncovered areas, or add a loop structure to the test script.

[0184] Based on the above embodiments, as a preferred embodiment, it further includes:

[0185] A template update module for executing the test script, collecting test results and / or error information; adjusting the first parameter and / or the second parameter according to the test results and / or the error information to update the matching test templates.

[0186] This application also provides an embodiment corresponding to a computer-readable storage medium. A computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the steps of the method described in the above method embodiment are implemented.

[0187] It can be understood that if the methods in the above embodiments are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of this technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and executes all or part of the steps of the methods described in various embodiments of this application. The aforementioned storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical discs that can store program codes.

[0188] The computer-readable storage medium provided in this embodiment includes the methods mentioned above, and the effects are the same.

[0189] This application also provides an electronic device. Refer to Figure 5 , the structural diagram of an electronic device provided in an embodiment of this application, as Figure 5 shown, may include a processor 1410 and a memory 1420.

[0190] Among them, the processor 1410 may include one or more processing cores, such as a 4-core processor, an 8-core processor, etc. The processor 1410 may be implemented in at least one hardware form of DSP (Digital Signal Processing), FPGA (Field-Programmable Gate Array), or PLA (Programmable Logic Array). The processor 1410 may also include a main processor and a coprocessor. The main processor is a processor used to process data in the wake state, also known as the CPU (Central Processing Unit); the coprocessor is a low-power processor used to process data in the standby state. In some embodiments, the processor 1410 may be integrated with a GPU (Graphics Processing Unit), and the GPU is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 1410 may also include an AI (Artificial Intelligence) processor, and this AI processor is used to process computational operations related to machine learning.

[0191] The memory 1420 may include one or more computer-readable storage media, which may be non-transitory. The memory 1420 may also include high-speed random access memory and non-volatile memory, such as one or more magnetic disk storage devices and flash storage devices. In this embodiment, the memory 1420 is at least used to store the following computer program 1421. After the computer program is loaded and executed by the processor 1410, it can implement the relevant steps in the methods executed by the electronic device side disclosed in any of the foregoing embodiments. In addition, the resources stored in the memory 1420 may also include an operating system 1422, data 1423, etc., and the storage method may be transient storage or permanent storage. Among them, the operating system 1422 may include Windows, Linux, Android, etc.

[0192] In some embodiments, the electronic device may further include a display screen 1430, an input / output interface 1440, a communication interface 1450, a sensor 1460, a power supply 1470, and a communication bus 1480.

[0193] Of course, Figure 5 The structure of the illustrated electronic device does not constitute a limitation on the electronic device in the embodiments of the present application. In practical applications, the electronic device may include more or fewer components than Figure 5 those shown, or combine certain components.

[0194] The various embodiments in the specification are described in a progressive manner. The key point of each embodiment is to illustrate the differences from other embodiments. The same or similar parts among the various embodiments may be referred to each other. For the system provided in the embodiments, since it corresponds to the method provided in the embodiments, the description is relatively simple, and the relevant parts may be referred to the description of the method part.

[0195] Specific examples are used in this article to elaborate on the principles and implementation manners of the present application. The descriptions of the above embodiments are only used to help understand the method and its core idea of the present application. It should be noted that for those of ordinary skill in the art in the technical field of the present application, without departing from the principle of the present application, several improvements and modifications can be made to the present application, and these improvements and modifications also fall within the protection scope of the present application.

[0196] It should also be noted that in this specification, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article or device comprising a series of elements not only includes those elements, but also includes other elements not expressly listed, or further includes elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "comprising an..." does not exclude the presence of additional identical elements in the process, method, article or device comprising the said element.

Claims

1. A method for generating an intelligent contract test script, characterized in that, Including: Obtain the target smart contract; Extract the contract key information of the target smart contract; Generate a test requirement list according to the contract key information; Screen from the standardized template library a matching test template that matches the contract type of the target smart contract and meets the test requirement list; Input the matching test template, the contract key information, and the test requirements as context into a set large language model to guide the set large language model to generate the test script within the matching test template.

2. The generation method according to claim 1, wherein Extracting the contract key information of the target smart contract includes: Parse the contract source code of the target smart contract and construct an abstract syntax tree corresponding to the contract source code; Extract all public function interfaces of the target smart contract and their parameter types; Identify all state variables included in the target smart contract and their access modifiers; Identify all event definitions and triggering conditions of the target smart contract; Identify the permission control mechanism of the target smart contract; Identify the base classes inherited by the target smart contract and the interfaces implemented; Structurally process the abstract syntax tree, the public function interfaces and their parameter types, the state variables and their access modifiers, the event definitions and triggering conditions, the permission control mechanism, the inherited base classes and the implemented interfaces to obtain the contract key information.

3. The generation method according to claim 2, wherein Generating a test requirement list according to the contract key information includes: Determine functional test requirements according to the public function interfaces and their parameter types and the inherited base classes and the interfaces implemented; Determine security test requirements according to the state variables and their access modifiers, the abstract syntax tree, and the permission control mechanism; Determine business logic test requirements according to the abstract syntax tree, the public function interfaces and their parameter types, the state variables and their access modifiers, and the event definitions and triggering conditions; Generate a test requirement list based on the functional test requirements, the security test requirements, and the business logic test requirements.

4. The generation method according to claim 1, wherein Before screening from the standardized template library a matching test template that matches the contract type of the target smart contract and meets the test requirement list, it also includes: Set a standardized template including a fixed content partition, a content customization partition, and a content to be filled partition; the fixed content partition includes template header information, an import area, an environment setting area, and a cleanup area, the content customization partition includes an initialization area, and the content to be filled partition is a parameter definition area, a business logic test area, and an assertion area for the set large language model to fill; Configure the smart contract test scenarios corresponding to each standardized template to generate the standardized template library.

5. The generation method according to claim 4, characterized in that, Screening from the standardized template library a matching test template that matches the contract type of the target smart contract and meets the test requirement list includes: Determine the template range that matches the contract type of the target smart contract; Determine the requirement coverage rate of each test template for the test requirement list within the template range; Obtain the historical test success rate of each test template within the template range; Calculate the matching degree of each test template based on the requirement coverage rate and its corresponding first parameter, and the historical test success rate and its corresponding second parameter; the weight value of the first parameter is greater than that of the second parameter; Use the test templates with a matching degree greater than the set matching threshold as the matching test templates.

6. The generation method according to claim 1, characterized in that, After guiding the set large language model to generate the test script within the matching test template, it further includes: Detect the code detection coverage rate of the test script for the target smart contract and identify the uncovered areas; Update the execution logic of the test script based on the uncovered areas or add a loop structure to the test script.

7. The generation method according to claim 5, wherein After guiding the set large language model to generate the test script within the matching test template, it further includes: Execute the test script and collect test results and / or error information; Adjust the first parameter and / or the second parameter according to the test results and / or the error information to update the matching test template.

8. A generation system for intelligent contract test scripts, characterized in that, It includes: An acquisition module for acquiring a target smart contract; A contract analysis module for extracting the contract key information of the target smart contract; A test requirement matching module for generating a test requirement list according to the contract key information; A target matching module for screening matching test templates that match the contract type of the target smart contract and meet the test requirement list from a standardized template library; A script generation module for inputting the matching test template, the contract key information, and the test requirements as context into a set large language model to guide the set large language model to generate the test script within the matching test template.

9. An electronic device, characterized in that, It includes: A memory for storing computer programs; A processor for implementing the steps of the method according to any one of claims 1 to 7 when executing the computer program.

10. A computer-readable storage medium, characterized in that, A computer program is stored on the computer-readable storage medium, and when the computer program is executed, it implements the steps of the method according to any one of claims 1 to 7.