Smart contract code reconstruction method, device and equipment and readable storage medium

Through multi-agent architecture collaboration, smart contract code reconstruction is solved, and the problems of scalability and automation limitations in the existing technology are realized, efficient and automated smart contract code reconstruction is realized, and semantic understanding and security in complex scenarios are improved.

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

Patent Information

Application Number
CN202510695762.6
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

The existing smart contract code reconstruction methods have poor scalability. The automation method is limited by neural network training data, which is difficult to effectively apply in complex scenarios, and requires manual participation. The semi-automated method has poor scalability.

Method used

Using a multi-agent architecture based on a large language model, smart contract code reconstruction is carried out through multi-agent collaboration, including reviewing, developing and verifying agents, automating complex scenarios, and reducing manual participation.

Benefits of technology

It improves the efficiency and quality of smart contract code reconstruction, ensures the semantic understanding and security of the reconstruction results, reduces the limitations of manual participation and specific modes, and realizes automated smart contract code reconstruction.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120386554A_ABST
    Figure CN120386554A_ABST
Patent Text Reader

Abstract

The invention discloses an intelligent contract code reconstruction method, device and equipment and a readable storage medium, and is applied to the technical field of computers, and the method comprises the following steps: obtaining an intelligent contract code reconstruction request; obtaining a multi-agent architecture pre-constructed based on the intelligent contract code reconstruction workflow; the multi-agent architecture is a multi-agent architecture based on a large language model; and based on the smart contract code reconstruction request, performing smart contract code reconstruction by using a multi-agent architecture to obtain a reconstructed smart contract code. According to the method, based on the standard workflow of intelligent contract code reconstruction and in combination with a multi-agent architecture of a large language model, a universal intelligent contract code reconstruction workflow and multiple agents cooperate together, so that semantic understanding of a complex intelligent contract scene and implementation of a reconstruction target are improved, the method is an automatic reconstruction process, and the implementation efficiency is improved. A user does not need to customize a template, and the limitation that manual participation and implementation can only be achieved in a specific code mode is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and particularly relates to a method, device, equipment and readable storage medium for intelligent contract code refactoring. Background Art

[0002] Similar to ordinary code refactoring, intelligent contract refactoring aims to improve code quality and maintainability while ensuring code equivalence. Due to its operating platform, especially the high requirements for the security and performance of the blockchain, higher requirements are also imposed on intelligent contract code refactoring. Currently, there are automated and semi-automated intelligent contract code refactoring methods. The semi-automated method requires human participation and has poor scalability; while the automated method is limited by the training data under the neural network, resulting in limited practical scenarios, especially with limitations in understanding code semantics and business logic.

[0003] Therefore, how to provide an intelligent contract code refactoring method with strong scalability and wide application scope is a technical problem that urgently needs to be solved currently. Summary of the Invention

[0004] In view of this, the purpose of the present invention is to provide a method, device, equipment and readable storage medium for intelligent contract code refactoring, which solves the problems of poor scalability and limitations in the existing intelligent contract code refactoring methods.

[0005] To solve the above technical problems, the present invention provides an intelligent contract code refactoring method, including:

[0006] Obtaining an intelligent contract code refactoring request;

[0007] Obtaining a multi-agent architecture pre-constructed based on an intelligent contract code refactoring workflow; the multi-agent architecture is a multi-agent architecture based on a large language model;

[0008] Based on the intelligent contract code refactoring request, using the multi-agent architecture to perform intelligent contract code refactoring to obtain the refactored intelligent contract code.

[0009] Optionally, based on the intelligent contract code refactoring request, using the multi-agent architecture to perform intelligent contract code refactoring to obtain the refactored intelligent contract code, including:

[0010] Step 1, parsing the intelligent contract code refactoring request to obtain the intelligent contract code to be refactored and test cases;

[0011] Step 2, based on the intelligent contract code to be refactored, calling the review agent of the multi-agent architecture to generate intelligent contract code refactoring opinions;

[0012] Step 3: Based on the intelligent contract code refactoring opinions, call the development agent of the multi-agent architecture to generate the refactored intelligent contract code;

[0013] Step 4: Based on the test cases, call the verification agent of the multi-agent architecture to determine whether the refactored intelligent contract code passes the verification; if it passes, execute Step 5; if it does not pass, execute Step 6;

[0014] Step 5: Output the refactored intelligent contract code;

[0015] Step 6: Use the refactored intelligent contract code as the intelligent contract code to be refactored, and repeat Steps 2 to 4 based on the operation report output by the verification agent.

[0016] Optionally, in Step 2: Based on the intelligent contract code to be refactored, call the review agent of the multi-agent architecture to generate intelligent contract code refactoring opinions, including:

[0017] Call the review agent to obtain review prompt words according to the intelligent contract code to be refactored and the intelligent contract refactoring knowledge base;

[0018] Based on the review prompt words, call the large language model to generate the intelligent contract code refactoring opinions.

[0019] Optionally, in Step 3: Based on the intelligent contract code refactoring opinions, call the development agent of the multi-agent architecture to generate the refactored intelligent contract code, including:

[0020] Call the development agent to obtain development prompt words according to the intelligent contract code to be refactored, the intelligent contract code refactoring opinions, and the intelligent contract code knowledge base;

[0021] Based on the development prompt words, call the large language model to generate the refactored intelligent contract code.

[0022] Optionally, in Step 4: Based on the test cases, call the verification agent of the multi-agent architecture to determine whether the refactored intelligent contract code passes the verification, including:

[0023] Call the verification agent to obtain verification prompt words according to the refactored intelligent contract code and the test cases;

[0024] Based on the verification prompt words, call the large language model to determine whether the intelligent contract code passes the verification.

[0025] Optionally, calling the verification agent to obtain verification prompt words according to the refactored intelligent contract code and the test cases includes:

[0026] Invoke the verification agent, and in the smart contract runtime environment, run the refactored smart contract code based on the input of the test case to obtain the running output result and the running report;

[0027] Obtain the verification prompt word based on the test case, the running output result, and the running report.

[0028] Optionally, in step 6, use the refactored smart contract code as the smart contract code to be refactored, and repeat steps 2 to 4 based on the running report output by the verification agent, including:

[0029] Use the refactored smart contract code as the refactored smart contract code; and repeat steps 2 to 4 based on the running report;

[0030] Correspondingly, step 2 includes:

[0031] Invoke the review agent to obtain a review prompt word according to the smart contract code to be refactored, the smart contract refactoring knowledge base, and the running report;

[0032] Based on the review prompt word, invoke the large language model to generate the smart contract code refactoring opinion.

[0033] The present invention also provides a smart contract code refactoring device, including:

[0034] The first acquisition module is used to acquire a smart contract code refactoring request;

[0035] The second acquisition module is used to acquire a multi-agent architecture pre-constructed based on the smart contract code refactoring workflow; the multi-agent architecture is a multi-agent architecture based on a large language model;

[0036] The refactoring module is used to perform smart contract code refactoring based on the smart contract code refactoring request by using the multi-agent architecture to obtain the refactored smart contract code.

[0037] The present invention also provides a smart contract code refactoring device, including:

[0038] A memory for storing a computer program;

[0039] A processor for implementing the smart contract code refactoring method as described above when executing the computer program.

[0040] The present invention also provides a computer-readable storage medium, in which computer-executable instructions are stored. When the computer-executable instructions are loaded and executed by a processor, the intelligent contract code reconstruction method as described above is implemented.

[0041] It can be seen that the present invention obtains an intelligent contract code reconstruction request; obtains a multi-agent architecture pre-constructed based on an intelligent contract code reconstruction workflow; the multi-agent architecture is a multi-agent architecture based on a large language model; based on the intelligent contract code reconstruction request, uses the multi-agent architecture to reconstruct the intelligent contract code to obtain the reconstructed intelligent contract code. The present invention is based on the standard workflow of intelligent contract code reconstruction and combines the multi-agent architecture of the large language model to jointly cooperate the general intelligent contract code reconstruction workflow and multi-agents, thereby improving the semantic understanding of complex intelligent contract scenarios and the realization of reconstruction goals. Moreover, the present invention is an automated reconstruction process, does not require user-defined templates, reduces manual participation and the limitations that can only be achieved in specific code modes.

[0042] In addition, the present invention also provides an intelligent contract code reconstruction device, equipment and readable storage medium, which also have the above beneficial effects. BRIEF DESCRIPTION OF THE DRAWINGS

[0043] In order to more clearly illustrate the technical solutions in the embodiments of the present invention 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 invention. For those of ordinary skill in the art, other drawings can be obtained according to the provided drawings without creative efforts.

[0044] Figure 1 It is a flowchart of an intelligent contract code reconstruction method provided by an embodiment of the present invention;

[0045] Figure 2 It is a flow example diagram of an intelligent contract code reconstruction method provided by an embodiment of the present invention;

[0046] Figure 3 It is a flow example diagram of another intelligent contract code reconstruction method provided by an embodiment of the present invention;

[0047] Figure 4 It is a structural schematic diagram of an intelligent contract code reconstruction device provided by an embodiment of the present invention;

[0048] Figure 5 It is a structural schematic diagram of an intelligent contract code reconstruction device provided by an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0049] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all of them. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the scope of protection of the present invention.

[0050] A blockchain is a distributed system with characteristics such as decentralization and immutability. The blockchain system ensures the security and reliability of data through cryptographic techniques. The data stored in the blockchain system is organized and stored through a data structure called a "block". Each block has a number, and in addition to the data of the current block, the block also contains the hash value of the previous numbered block. The data stored on the blockchain is called the world state or on-chain state of the blockchain. A smart contract is a program running on the blockchain. Smart contracts can be used to implement various application logics. Usually, a smart contract deployed on the blockchain will have a unique contract address to identify the contract identity. Smart contract users trigger the interfaces of the smart contracts through transactions to automatically execute the corresponding application logics. Smart contracts can store data, and the data stored in smart contracts is called the contract state. Smart contracts can call each other under certain conditions.

[0051] Smart contract code refactoring, like ordinary code refactoring, aims to improve code quality and maintainability while ensuring code equivalence. Due to its operating platform, especially the high requirements for the security and performance of the blockchain, higher requirements are imposed on smart contract code refactoring. The goals of code refactoring activities for smart contract code include two major categories: (1) Code quality: The running time, space complexity performance, error handling ability, and reliability of the code. Particularly for Gas optimization in smart contract code refactoring, Gas optimization refers to reducing the amount of Gas consumed (i.e., the computational resource cost) during contract execution by improving the code logic, data structure, storage method, etc. of the smart contract; (2) Code maintainability: The readability of the code, the syntax style of the code, redundant code, etc. Particularly for refactoring smart contracts into an upgradable contract form in smart contract code refactoring. Current smart contract code refactoring methods include templated refactoring based on specific code pattern recognition and code refactoring based on deep learning. Templated smart contract code refactoring is an automated or semi-automated code improvement method that identifies and modifies specific structures in smart contract code through predefined templates. Its effectiveness depends on the design of the template and the choice of tools. To achieve the best results, developers need to combine automated tools and manual judgment to ensure that the refactored code meets both technical standards and business requirements. Code refactoring of smart contracts based on deep learning, such as using NMT (Neural Machine Translation) to learn the code data before and after modifying the code repository to learn the characteristics of code modification, and then predicting and performing code modification tasks; using deep learning to solve problems in real applications faces great challenges. Current work is all carried out in training experiments on some very simple examples, avoiding the complexity of code in real scenarios.

[0052] Therefore, compared with ordinary code refactoring, smart contract code refactoring requires combining the special platform characteristics of the blockchain under the scenario and also imposes higher requirements in terms of security and performance. Generally speaking, smart contract code refactoring poses higher requirements in the following three aspects: (1) Whether new problems are introduced; (2) The equivalence problem of the refactored smart contract code; (3) Code structure / semantics / logic complexity. Existing automation can only solve some simple problems and has poor applicability in actual scenarios. Existing automated and semi-automated methods either have poor scalability because they require manual participation, or have limited practical scenarios due to the limitations of training data under neural networks, especially the limitations in understanding code semantics and business logic.

[0053] This paper proposes a smart contract code refactoring method. This method is based on a Large Language Models (LLM)-Multi-Agent framework constructed based on a standard workflow for smart contract code refactoring. It aims to improve the efficiency, functionality, and security of smart contract code review through a standardized workflow (SOP), and can help developers automatically refactor smart contract code.

[0054] Please refer to Figure 1 , Figure 1 A flowchart of a smart contract code reconstruction method provided by an embodiment of the present invention. The method may include:

[0055] S101: Obtain a smart contract code reconstruction request.

[0056] The execution entity of this embodiment is a server. For example, a Multi-Agent service backend can be used to reconstruct a smart contract based on SOP. Specifically, a developer initiates a smart contract code reconstruct request on the front-end page of the smart contract development platform, and the back-end receives the request. It should be noted that in this embodiment, the back-end parses the received smart contract code reconstruct request to obtain a test case and the smart contract code to be reconstructed. It is understood that when a developer initiates a smart contract code reconstruct request on the front-end page of the smart contract development platform, the smart contract code to be reconstructed can be displayed on the front-end page in the form of a version number for the user to select.

[0057] S102: Obtain a multi-agent architecture pre-built based on a smart contract code reconstruction workflow; the multi-agent architecture is a multi-agent architecture based on a large language model.

[0058] This embodiment pre-builds a multi-agent framework based on the workflow of smart contract code reconstruction, and the multi-agent framework includes a large language model. This embodiment does not limit the agents in the multi-agent framework. For example, the multi-agent architecture in this embodiment may include a reviewer agent, a developer agent, and a verifier agent. It should be noted that each agent includes a large language model. Similarly, this embodiment does not limit the large language model. For example, the large language model in this embodiment may be a large language model of the GPT series or the BERT series, etc.

[0059] In this embodiment, through the collaborative work among multiple Agents (intelligent agents), the problem is decomposed into smaller and more manageable parts. By decomposing and assigning tasks to different Agents, multiple subtasks can be processed simultaneously. Each Agent focuses on solving specific tasks or problem areas and can quickly execute repetitive tasks, thereby improving the overall efficiency and effectiveness. Moreover, since the Multi-Agent strictly executes tasks according to the standard workflow, this ensures that all operations follow the same standards, reduces the possibility of human errors and large language model hallucinations, and improves the consistency and quality of the output. The construction mode of the multi-agent framework can add new functions and make it easier to expand existing functions.

[0060] S103: Based on the intelligent contract code refactoring request, use the multi-agent architecture to perform intelligent contract code refactoring to obtain the refactored intelligent contract code.

[0061] This embodiment uses the pre-constructed multi-agent architecture to process the intelligent contract code request, implement intelligent contract code refactoring, and thus obtain the refactored intelligent contract code.

[0062] Furthermore, in order to improve the reliability and effectiveness of the refactored intelligent contract code, the above-mentioned step of using the multi-agent architecture to perform intelligent contract code refactoring based on the intelligent contract code refactoring request to obtain the refactored intelligent contract code may include the following steps, specifically including:

[0063] Step 1: Parse the intelligent contract code refactoring request to obtain the intelligent contract code to be refactored and test cases.

[0064] Step 2: Based on the intelligent contract code to be refactored, call the review agent of the multi-agent architecture to generate intelligent contract code refactoring opinions.

[0065] Step 3: Based on the intelligent contract code refactoring opinions, call the development agent of the multi-agent architecture to generate the refactored intelligent contract code.

[0066] Step 4: Based on the test cases, call the verification agent of the multi-agent architecture to determine whether the refactored intelligent contract code passes the verification; if it passes, execute Step 5; if it does not pass, execute Step 6.

[0067] Step 5: Output the refactored intelligent contract code.

[0068] Step 6: Use the refactored intelligent contract code as the intelligent contract code to be refactored, and repeat Steps 2 to 4 based on the operation report output by the verification agent.

[0069] Specifically, for a better understanding of the above steps, reference can be made to Figure 2 .Figure 2 This is a flowchart example of a method for reconstructing smart contract code provided by an embodiment of the present invention. As can be seen from Figure 2 Figure 2 , the multi-agent architecture includes: a review agent, a development agent, and a verification agent. Moreover, the relationships among the agents are determined according to the smart contract code reconstruction workflow. It should be noted that Figure 2 V0 and VN in represent the version numbers of the smart contract code. It can be seen that the present invention realizes an efficient code submission and verification process that combines artificial intelligence and test cases through an intelligent multi-role collaboration mechanism and the capabilities of large language models, and ensures the effectiveness of the reconstructed smart contract code through automatic inspection and testing.

[0070] Furthermore, in step 2 above, based on the smart contract code to be reconstructed, calling the review agent of the multi-agent architecture to generate smart contract code reconstruction opinions may include the following steps, specifically including:

[0071] Step 21: Call the review agent to obtain a review prompt word according to the smart contract code to be reconstructed and the smart contract reconstruction knowledge base;

[0072] Step 22: Based on the review prompt word, call the large language model to generate smart contract code reconstruction opinions.

[0073] Similarly, reference can be made to Figure 2 . When the smart contract reconstruction Multi-Agent service backend based on SOP receives a smart contract code reconstruction request initiated by the front end, it calls the review agent to obtain a review prompt word according to the smart contract code to be reconstructed and the smart contract reconstruction knowledge base, calls the large language model with the review prompt word, and generates smart contract code reconstruction opinions based on the review prompt word. Among them, the smart contract reconstruction knowledge base enables the review agent to have the ability for specific scenarios of smart contract code reconstruction.

[0074] It should be noted that the smart contract code V-before to be reconstructed is the initial smart contract code of the reconstruction task for the first call, V-before = V0, and in subsequent iterations, it is the code of the contract version reconstructed last time, V-before = Vn.

[0075] Exemplarily, the review prompt word (PromptReviewer):

[0076] You are an experienced smart contract reconstruction engineer. According to the knowledge of smart contract reconstruction you have mastered:

[0077]

Smart contract reconstruction knowledge base

[0078] The following smart contract needs to be reconstructed:

[0079] ​

V-before

[0080] The performance and error messages of the execution of this smart contract are as follows:

[0081]

Information

[0082] Please provide opinions on code refactoring.

[0083] Exemplarily, the smart contract refactoring knowledge base may include:

[0084] Optimize each function in the GCL code according to the following readability optimization points: 1. Naming convention: Keep the variable names in the original format specification, with a focus on improving naming readability to clearly express their purposes - use uppercase letters and underscores for constants (MAX_VALUE) - function names should reflect the actions (getBalance, transferToken) - scope markers (@global, @address, @shard) should be placed consistently. 2. Comment optimization: Add comments to the function headers to explain the function, parameters, and return values. The function header comments should be uniformly placed above the function - Add comments to relay calls to explain the purpose and expected results - Add clear amount and purpose descriptions for token operations - Add descriptions for the main processing procedures in the function. 3. Code structure and format: - Related state variables and functions should be grouped together - Functions in different scopes should be clearly grouped - Use a consistent indentation (4 spaces) and bracket style - Complex expressions should be decomposed into multiple lines to improve readability. Important limitations: 1. Do not refactor the code structure or change the code logic organization; 2. Keep the original parameter list and return values unchanged; 3. Your main tasks are: add meaningful comments, simplify complex expressions, and optimize the format; 4. Focus on improving readability while keeping the code form and function exactly the same. / / / / / / Security prompt += `Security optimization. Please optimize each function in the GCL code according to the following security optimization points: 1. Token security: Verify the balance and permissions before all token operations - Add a limit check for token transfers to prevent overflow - Prevent re - entry attacks to ensure that state updates precede external calls. 2. Input validation: Verify all external inputs, including transaction parameters and interface parameters - Add correctness validation for sensitive types such as addresses and hashes - Add range checks to prevent integer overflow and underflow - Verify the id of the incoming token to prevent malicious token attacks. 3. Permission control: Use __transaction.get_sender() to verify the caller's identity - Implement proper permission checks to prevent unauthorized access - Add multiple validations for critical operations - Avoid using hard - coded addresses. 4. Error handling: Add an exception checking and handling mechanism - Use __debug.assert() for critical condition verification. / / / / / / / / / / Performance optimization prompt += `Performance optimization. Please optimize each function in the GCL code according to the following performance optimization points: 1. Algorithm optimization: Improve the algorithm complexity to reduce time and space complexity - Reduce the number of nested loops and prefer O(n) algorithms over O(n²) algorithms. 2. Data structure optimization: Select appropriate integer types for different numerical ranges (uint8 to uint512) - Optimize the use of arrays and maps to avoid frequent resizing - Avoid unnecessary complex data structures.3. Code execution optimization: Avoid repeated calculations and give priority to using cached variables - Simplify conditional expressions and reduce unnecessary branches - Avoid performing complex calculations or relay calls in loops.

[0085] Furthermore, in step 3 above, based on the intelligent contract code refactoring opinions, call the development agent of the multi-agent architecture to generate the refactored intelligent contract code, which may include the following steps, specifically:

[0086] Step 31: Call the development agent to obtain development prompt words according to the intelligent contract code to be refactored, the intelligent contract code refactoring opinions, and the intelligent contract code knowledge base;

[0087] Step 32: Based on the development prompt words, call the large language model to generate the refactored intelligent contract code.

[0088] Similarly, it can refer to Figure 2 . Call the development agent to obtain development prompt words based on the following information: (1) The intelligent contract code V-before to be refactored, the initial code of the contract for the first call of the refactoring task, V-before = V0, and in subsequent iterations, it is the code of the previous refactored contract version, V-before = Vn; (2) The intelligent contract code refactoring opinions Comment output by the audit agent; (3) The intelligent contract code knowledge base, enabling the development agent to have the ability for specific scenarios of intelligent contract code development. Call the large language model based on the development prompt words and generate the refactored intelligent contract code Vn according to the development prompt words.

[0089] Exemplarily, the development prompt words (PromptDeveloper):

[0090] You are an experienced intelligent contract development engineer. According to the intelligent contract code knowledge you have mastered:

[0091]

Intelligent contract code knowledge base

[0092] According to the refactoring opinions:

[0093]

Comment

[0094] Refactor the following intelligent contract:

[0095]

V-before

[0096] Please provide the refactored intelligent contract code.

[0097] Exemplarily, the intelligent contract code knowledge base includes:

[0098] 1. The GCL type system mainly has two categories: - **Value type**: Stores the data itself, and the data is copied during assignment. - **Boolean type**: bool (1 byte, true / false). - **Integer type**: - Signed integers: int8, int16, int32, int64, int128, int256, int512. - Unsigned integers: uint8, uint16, uint32, uint64, uint128, uint256, uint512. - Arbitrary precision integer: bigint. - **Floating-point type**: float256, float512, float1024. - **Other basic value types**: blob (36 bytes), hash (32 bytes), address (36 bytes). - **Enumeration type**: Defined by the enum keyword, with a maximum of 65,535 enumeration values. - **Reference type**: Stores a reference to the data, and the reference is copied during assignment while the data is shared. - **Container type**: - array <t>: Dynamic array, accessed using the [] operator. The index must be of type uint32. - map<K, V>: Key-value mapping, accessed using the [] operator. - **string**: string, in UTF-8 format, up to 65535 characters. - **token**: Used to handle tokens. - **struct**: Defined with the struct keyword. Members cannot contain functions and there can be at most 255 members. 2. Smart contract structure - Contracts are defined using the `contract\` keyword. - All deployed contracts have a unique name in the format: "DAppName.ContractName". Contracts can contain: enum definitions, struct definitions, scope definitions, interface definitions, state variables, and functions. 3. Scope system (Scope) GCL uses scopes to define the distributed storage and execution methods of state variables and functions: - **@global**: Global scope, with only one instance. - **@shard**: Shard scope, with one instance per shard. - **@address**: Address scope, with one instance per valid address. - **@uint series**: Numeric scope, with one instance per valid value. Scope rules: - Direct access between different scopes is not allowed and requires a relay call. - The global scope can be read by any scope but can only be modified in the global scope. - The shard scope can be read and written by other scopes in the same shard. 4. Functions and state variables - Functions are defined using the `function\` keyword. - State variables are defined directly in the contract, with an optional scope prefix. - Constants are defined using the `const\` keyword. - Function access modifiers: - `export`: Can be called by a transaction. - `public`: Can be called by other contracts. 5. Special statements and functions - **relay**: relay@target function name(parameters), used to asynchronously call functions in other scopes. - **deploy**: Used to programmatically create new contracts. - **System reserved functions**: - `on_deploy()`: Automatically called when the contract is deployed. - `on_scaleout(bool)`: Called when the shard is scaled out. 6. Interface and contract interaction - Interfaces are defined using the `interface\` keyword. - Interfaces are implemented using the `implements\` keyword. - Other contracts are imported using `import`. 7. Token operations - Each contract has built-in `__mint` and `__burn` functions for managing tokens. - The token type has special methods `get_id()`, `get_amount()`, `transfer()`, `transfer_all()`.8. Context - **Execution Context**: Includes contract state, transaction context, block context - **Transaction Context**: Contains transaction metadata such as sender address, timestamp, etc. - **Block Context**: Contains block metadata such as height, shard index, etc. - **Debug Context**: Provides debugging functions.

[0099] Furthermore, in step 4 above, based on the test cases, the verification agent of the multi - agent architecture is called to determine whether the reconstructed smart contract code passes the verification, which may include the following steps, specifically:

[0100] Step 41: Call the verification agent to obtain a verification prompt word according to the reconstructed smart contract code and the test cases;

[0101] Step 42: Based on the verification prompt word, call the large - language model to determine whether the smart contract code passes the verification.

[0102] Specifically, call the verification agent to obtain a verification prompt word according to the reconstructed smart contract code and the test cases, and based on the verification prompt word, call the large - language model to determine whether it passes the verification. If it passes, end the reconstruction and output the reconstructed smart contract code Vn; if it does not pass, generate error output or error report or performance optimization information Info that needs to be corrected according to the verification prompt word, and re - enter step 2 to iterate steps 2 - 4. Furthermore, output the verified reconstructed smart contract code Vn and return it to the front - end for visual display of the smart contract code before and after reconstruction, and the user can choose to adopt, not adopt, or reconstruct again.

[0103] Furthermore, in step 6 above, take the reconstructed smart contract code as the smart contract code to be reconstructed, and repeat steps 2 to 4 based on the operation report output by the verification agent, which may specifically include:

[0104] Take the reconstructed smart contract code as the reconstructed smart contract code; and repeat steps 2 to 4 based on the operation report;

[0105] Correspondingly, the steps include: call the review agent to obtain a review prompt word according to the smart contract code to be reconstructed, the smart contract reconstruction knowledge base, and the operation report; based on the review prompt word, call the large - language model to generate opinions on the reconstruction of the smart contract code.

[0106] It can be understood that during the step iteration process, step 2 has changed slightly. For example, in the generation of the review prompt word, the operation report output by the verification agent is added, that is, the performance and error information Information of the test cases, such as gas consumption, errors during operation, etc., or the error output or error report or performance optimization information Info that needs to be corrected.

[0107] Furthermore, the above-mentioned verification agent is called to obtain verification prompt words based on the reconstructed smart contract code and test cases, which may specifically include:

[0108] Step 411: Call the verification agent to run the reconstructed smart contract code based on the input of the test case in the smart contract runtime environment to obtain a running output result and a running report;

[0109] Step 412: Obtain verification prompt words based on the test case, the running output result, and the running report.

[0110] Similarly, reference can be made to Figure 2 . Call the verification agent. First, call the smart contract runtime environment for the reconstructed smart contract code Vn and the input test case inputs to give a running output result output_new and a running report report_new, and create verification prompt words based on the following information: (1) the test case (inputs, outputs); (2) the running output result outputs_new; (3) the running report report_new.

[0111] Exemplarily, the verification prompt word (PromptVerifier):

[0112] Several inputs of the test case for the existing smart contract code:

[0113]

inputs

[0114] The expected output is:

[0115]

outputs

[0116] However, the running output result is:

[0117]

outputs_new

[0118] And the output information during the running process is:

[0119]

report_new

[0120] Please first determine whether the output is correct. If it is correct, pass the verification and output yes; otherwise, output no, and summarize the error information of the output.

[0121] Applying the intelligent contract code refactoring method provided by the embodiments of the present invention, the method includes: obtaining an intelligent contract code refactoring request; obtaining a multi-agent architecture pre-constructed based on an intelligent contract code refactoring workflow, where the multi-agent architecture is a multi-agent architecture based on a large language model; and based on the intelligent contract code refactoring request, using the multi-agent architecture to perform intelligent contract code refactoring to obtain the refactored intelligent contract code. The present invention is based on the standard workflow of intelligent contract code refactoring and combines the multi-agent architecture of the large language model to jointly cooperate the general intelligent contract code refactoring workflow and multi-agents, so as to improve the semantic understanding of complex intelligent contract scenarios and the realization of refactoring goals. Moreover, the present invention is an automated refactoring process that does not require users to customize templates, reducing manual participation and the limitations that can only be achieved in specific code modes. In addition, the verification of the refactoring results is designed. During the verification phase, combined with the intelligent contract runtime environment and test cases, only the intelligent contract code that passes the test will be output to the platform. Otherwise, multiple iterations of refactoring are required to reduce the equivalence problems of the code caused by the hallucination of the large model and the new problems introduced during the refactoring process. For the requirements of the performance and security of intelligent contracts, during the verification phase, in addition to the equivalence of the refactoring, the large model is specifically called to analyze and optimize the actual running performance and error messages. The online creation of intelligent contract code tasks can be combined with the online intelligent contract platform without adding additional development tools.

[0122] For a better understanding of the present invention, please specifically refer to Figure 3 , Figure 3 which is a flowchart example of an intelligent contract code refactoring method provided by the embodiments of the present invention, and specifically may include:

[0123] S201: The user specifies the intelligent contract code to be refactored and test cases through the front-end page;

[0124] S202: The server parses the intelligent contract code refactoring request and constructs a refactoring task;

[0125] S203: The server runs the large model - Multi Agent refactoring iteration process according to the refactoring task, performs refactoring iteration on the intelligent contract code, and the refactored intelligent contract code passes the test cases and is returned to the development platform;

[0126] S204: The online development platform compares and displays the refactored intelligent contract code and the intelligent contract code before refactoring for the user to select.

[0127] The intelligent contract code refactoring device provided by the embodiments of the present invention is introduced below. The intelligent contract code refactoring device described below can be correspondingly referred to the intelligent contract code refactoring method described above.

[0128] Please specifically refer to Figure 4 , Figure 4 This is a schematic structural diagram of an intelligent contract code refactoring device provided by an embodiment of the present invention, which may include:

[0129] A first acquisition module 100, configured to acquire an intelligent contract code refactoring request;

[0130] A second acquisition module 200, configured to acquire a multi-agent architecture pre-constructed based on an intelligent contract code refactoring workflow; the multi-agent architecture is a multi-agent architecture based on a large language model;

[0131] A refactoring module 300, configured to perform intelligent contract code refactoring based on the intelligent contract code refactoring request by using the multi-agent architecture to obtain refactored intelligent contract code.

[0132] Based on the above embodiment, the refactoring module 300 may include:

[0133] A first unit, configured to execute step 1, parse the intelligent contract code refactoring request to obtain the intelligent contract code to be refactored and test cases;

[0134] A second unit, configured to execute step 2, call the review agent of the multi-agent architecture based on the intelligent contract code to be refactored to generate an intelligent contract code refactoring opinion;

[0135] A third unit, configured to execute step 3, call the development agent of the multi-agent architecture based on the intelligent contract code refactoring opinion to generate refactored intelligent contract code;

[0136] A fourth unit, configured to execute step 4, call the verification agent of the multi-agent architecture based on the test cases to determine whether the refactored intelligent contract code passes the verification; if it passes, execute step 5; if it does not pass, execute step 6;

[0137] A fifth unit, configured to execute step 5, output the refactored intelligent contract code;

[0138] A sixth unit, configured to execute step 6, use the refactored intelligent contract code as the intelligent contract code to be refactored, and repeatedly execute steps 2 to 4 based on the operation report output by the verification agent.

[0139] Based on the above embodiment, the second unit may include:

[0140] A first call subunit, configured to call the review agent to obtain a review prompt word according to the intelligent contract code to be refactored and an intelligent contract refactoring knowledge base;

[0141] The second calling subunit is configured to call the large language model to generate the intelligent contract code refactoring opinion based on the review prompt words.

[0142] Based on the above embodiments, the third unit may include:

[0143] The third calling subunit is configured to call the development agent to obtain development prompt words according to the intelligent contract code to be refactored, the intelligent contract code refactoring opinion, and the intelligent contract code knowledge base;

[0144] The fourth calling subunit is configured to call the large language model to generate the refactored intelligent contract code based on the development prompt words.

[0145] Based on the above embodiments, the fourth unit may include:

[0146] The fifth calling subunit is configured to call the verification agent to obtain verification prompt words according to the refactored intelligent contract code and the test cases;

[0147] The sixth calling subunit is configured to call the large language model to determine whether the intelligent contract code passes the verification based on the verification prompt words.

[0148] Based on the above embodiments, the fifth calling subunit may include:

[0149] The seventh calling subunit is configured to call the verification agent to run the refactored intelligent contract code based on the input of the test cases in the intelligent contract running environment to obtain the running output result and the running report;

[0150] The eighth calling subunit is configured to obtain the verification prompt words based on the test cases, the running output result, and the running report.

[0151] Based on the above embodiments, the sixth unit may include:

[0152] The repeated iteration subunit is configured to use the refactored intelligent contract code as the refactored intelligent contract code; and repeatedly execute steps 2 to 4 based on the running report;

[0153] Correspondingly, step 2 may specifically include: calling the review agent to obtain review prompt words according to the intelligent contract code to be refactored, the intelligent contract refactoring knowledge base, and the running report; calling the large language model to generate the intelligent contract code refactoring opinion based on the review prompt words.

[0154] It should be noted that the order of the modules and units in the above intelligent contract code refactoring device can be changed before and after without affecting the logic.

[0155] Applying the intelligent contract code refactoring device provided by the embodiment of the present invention, through the first acquisition module 100, which is used to acquire an intelligent contract code refactoring request; the second acquisition module 200, which is used to acquire a multi-agent architecture pre-constructed based on the intelligent contract code refactoring workflow; the multi-agent architecture is a multi-agent architecture based on a large language model; the refactoring module 300 is used to perform intelligent contract code refactoring based on the intelligent contract code refactoring request by using the multi-agent architecture to obtain the refactored intelligent contract code. This device is based on the standard workflow of intelligent contract code refactoring and combines the multi-agent architecture of the large language model to jointly cooperate the general intelligent contract code refactoring workflow and multi-agents, so as to improve the semantic understanding of complex intelligent contract scenarios and the realization of refactoring goals. Moreover, this device is an automated refactoring process, does not require users to customize templates, reduces manual participation and the limitations that can only be achieved in specific code modes. And, the verification of the refactoring result is designed, and in the verification stage, combined with the intelligent contract runtime environment and test cases, only the intelligent contract code that passes the test will be output to the platform, otherwise multiple iterations of refactoring are required to reduce the code equivalence problem caused by the large model hallucination and new problems introduced in the refactoring process; for the requirements of the performance and security of intelligent contracts, in the verification stage, in addition to the equivalence of refactoring, the large model is specifically called to analyze and optimize the actual running performance and error messages; the online creation of intelligent contract code tasks can be combined with the online intelligent contract platform without adding additional development tools.

[0156] The intelligent contract code refactoring device provided by the embodiment of the present invention is introduced below. The intelligent contract code refactoring device described below can be correspondingly referred to the intelligent contract code refactoring method described above.

[0157] Please refer to Figure 5 , Figure 5 which is a schematic structural diagram of an intelligent contract code refactoring device provided by the embodiment of the present invention, and may include:

[0158] A memory 10, which is used to store computer programs;

[0159] A processor 20, which is used to execute the computer program to implement the above-mentioned intelligent contract code refactoring method.

[0160] The memory 10, the processor 20, and the communication interface 31 all complete mutual communication through the communication bus 32.

[0161] In the embodiment of the present invention, one or more programs are stored in the memory 10. The program may include program codes, and the program codes include computer operation instructions. In the embodiment of the present invention, the following functions may be implemented by the program stored in the memory 10:

[0162] Obtain a smart contract code refactoring request;

[0163] Obtain a multi-agent architecture pre-built based on a smart contract code refactoring workflow;

[0164] The multi-agent architecture is a multi-agent architecture based on a large language model; based on the smart contract code refactoring request, use the multi-agent architecture to perform smart contract code refactoring to obtain the refactored smart contract code.

[0165] In a possible implementation, the memory 10 may include a storage program area and a storage data area. Among them, the storage program area may store an operating system and application programs required for at least one function, etc.; the storage data area may store data created during use.

[0166] In addition, the memory 10 may include a read-only memory and a random access memory, and provide instructions and data to the processor. A part of the memory may also include NVRAM. The memory stores an operating system and operation instructions, executable modules or data structures, or subsets thereof, or extended sets thereof. Among them, the operation instructions may include various operation instructions for implementing various operations. The operating system may include various system programs for implementing various basic tasks and processing hardware-based tasks.

[0167] The processor 20 may be a central processing unit (CPU), an application-specific integrated circuit, a digital signal processor, a field programmable gate array, or other programmable logic devices. The processor 20 may be a microprocessor or any conventional processor, etc. The processor 20 may call the program stored in the memory 10.

[0168] The communication interface 31 may be an interface of a communication module for connecting to other devices or systems.

[0169] Of course, it should be noted that Figure 5 The structure shown does not constitute a limitation on the smart contract code refactoring device in the embodiments of the present invention. In practical applications, the smart contract code refactoring device may include Figure 5 more or fewer components than shown, or combine some components.

[0170] Next, the computer-readable storage medium provided by the embodiments of the present invention will be introduced. The computer-readable storage medium described below can be mutually corresponding and referred to with the smart contract code refactoring method described above.

[0171] The present invention 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 above-mentioned intelligent contract code reconstruction method are implemented.

[0172] The computer-readable storage medium may include: various media that can store program codes, such as USB flash drives, mobile hard disks, read-only memories (ROM), random access memories (RAM), magnetic disks, or optical discs.

[0173] In this specification, the various embodiments are described in a progressive manner. Each embodiment focuses on the differences from other embodiments. For the same or similar parts among the various embodiments, reference can be made to each other. For the devices disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the description is relatively simple. For the relevant parts, reference can be made to the description in the method part.

[0174] Those skilled in the art can further realize that the units and algorithm steps of the examples described in combination with the embodiments disclosed herein can be implemented by electronic hardware, computer software, or a combination of the two. To clearly illustrate the interchangeability of hardware and software, the components and steps of the examples have been generally described according to their functions in the above description. Whether these functions are executed in the form of hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the present invention.

[0175] Finally, it should also be noted that in this article, relationships 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 such actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant is intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements not only includes those elements, but also includes other elements not explicitly listed, or further includes elements inherent to such process, method, article or device.

[0176] The above has introduced in detail a method, apparatus, device and computer-readable storage medium for intelligent contract code refactoring. In this article, specific examples are used to elaborate on the principle and implementation manner of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention; at the same time, for those of ordinary skill in the art, according to the idea of the present invention, there will be changes in the specific implementation manner and application scope. In summary, the content of this specification should not be construed as a limitation on the present invention.< / t>

Claims

1. A method for refactoring smart contract code, characterized in that, Including: Obtain a smart contract code refactoring request; Obtain a multi-agent architecture pre-built based on a smart contract code refactoring workflow; The multi-agent architecture is a multi-agent architecture based on a large language model; Based on the smart contract code refactoring request, use the multi-agent architecture to perform smart contract code refactoring to obtain the refactored smart contract code.

2. The intelligent contract code refactoring method according to claim 1, wherein Based on the smart contract code refactoring request, use the multi-agent architecture to perform smart contract code refactoring to obtain the refactored smart contract code, including: Step 1, parse the smart contract code refactoring request to obtain the smart contract code to be refactored and test cases; Step 2, based on the smart contract code to be refactored, call the review agent of the multi-agent architecture to generate a smart contract code refactoring opinion; Step 3, based on the smart contract code refactoring opinion, call the development agent of the multi-agent architecture to generate the refactored smart contract code; Step 4, based on the test cases, call the verification agent of the multi-agent architecture to determine whether the refactored smart contract code passes the verification; if it passes, execute Step 5; if it does not pass, execute Step 6; Step 5, output the refactored smart contract code; Step 6, use the refactored smart contract code as the smart contract code to be refactored, and repeat Steps 2 to 4 based on the operation report output by the verification agent.

3. The smart contract code refactoring method according to claim 2, wherein Step 2, based on the smart contract code to be refactored, call the review agent of the multi-agent architecture to generate a smart contract code refactoring opinion, including: Call the review agent to obtain a review prompt based on the smart contract code to be refactored and the smart contract refactoring knowledge base; Based on the review prompt, call the large language model to generate the smart contract code refactoring opinion.

4. The intelligent contract code refactoring method according to claim 2, wherein Step 3, based on the smart contract code refactoring opinion, call the development agent of the multi-agent architecture to generate the refactored smart contract code, including: Call the development agent to obtain a development prompt based on the smart contract code to be refactored, the smart contract code refactoring opinion, and the smart contract code knowledge base; Based on the development prompt, call the large language model to generate the refactored smart contract code.

5. The method for reconstructing smart contract code according to claim 2, wherein Step 4, based on the test cases, call the verification agent of the multi-agent architecture to determine whether the refactored smart contract code passes the verification, including: Call the verification agent to obtain a verification prompt based on the refactored smart contract code and the test cases; Based on the verification prompt, call the large language model to determine whether the smart contract code passes the verification.

6. The intelligent contract code refactoring method according to claim 5, wherein Call the verification agent to obtain a verification prompt based on the refactored smart contract code and the test cases, including: Call the verification agent to run the refactored smart contract code based on the input of the test cases in the smart contract running environment to obtain the running output result and the operation report; Obtain the verification prompt based on the test cases, the running output result, and the operation report.

7. The method for reconstructing smart contract code according to claim 2, wherein Step 6, use the reconstructed smart contract code as the smart contract code to be reconstructed, and repeatedly execute Steps 2 to 4 based on the operation report output by the verification agent, including: Use the reconstructed smart contract code as the reconstructed smart contract code; and repeatedly execute Steps 2 to 4 based on the operation report; Correspondingly, Step 2 includes: Call the review agent to obtain review prompt words according to the smart contract code to be reconstructed, the smart contract reconstruction knowledge base, and the operation report; Based on the review prompt words, call the large language model to generate the smart contract code reconstruction opinion.

8. An intelligent contract code refactoring device, characterized in that, Including: The first acquisition module is used to acquire the smart contract code reconstruction request; The second acquisition module is used to acquire the multi-agent architecture pre-constructed based on the smart contract code reconstruction workflow; The multi-agent architecture is a multi-agent architecture based on the large language model; The reconstruction module is used to reconstruct the smart contract code by using the multi-agent architecture based on the smart contract code reconstruction request to obtain the reconstructed smart contract code.

9. An intelligent contract code refactoring device, characterized in that, Including: The memory is used to store computer programs; The processor is used to implement the smart contract code reconstruction method according to any one of claims 1 to 7 when executing the computer program.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are loaded and executed by the processor, the smart contract code reconstruction method according to any one of claims 1 to 7 is implemented.

Citation Information

Cited By

  • Code data set construction method based on multi-agent workflow

    CN121742818A