Smart contract liquidity defect detection method and system based on large language model and dynamic co-attention network

By combining a large language model with a dynamic co-attention network, a defect knowledge base is constructed and potential defects in smart contracts are identified, which solves the problems of insufficient accuracy and adaptability of existing detection methods and achieves more accurate and stable liquidity defect detection.

CN119961131BActive Publication Date: 2025-09-19HAINAN UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510029686.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-01-08
Publication Date
2025-09-19
Estimated Expiration
2045-01-08

AI Technical Summary

Technical Problem

Existing smart contract liquidity defect detection methods have low detection accuracy and adaptability, resulting in the security audit process being inaccurate and unstable.

Method used

A method based on a large language model and dynamic co-attention network is adopted to build a defect knowledge base, extract the embedding vectors and feature vectors of smart contract code snippets, combine the large language model to identify potential defects, and generate an audit report.

Benefits of technology

Improved the accuracy and adaptability of detecting potential liquidity defects in PoL smart contracts, and enhanced the systematization of the audit process and the reliability of reports.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119961131B_ABST
    Figure CN119961131B_ABST
Patent Text Reader

Abstract

The present invention relates to a method and system for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network. The method comprises: determining the type of liquidity defect and extracting smart contract code snippets to convert them into embedding vectors, extracting feature vectors using a dynamic co-attention network, and constructing a defect knowledge base; obtaining a target embedding vector and a target feature vector generated by the smart contract to be detected, matching them based on the defect knowledge base, and obtaining a list of suspected defects; inputting the list of suspected defects into a large language model to identify and record the defect content; determining a liquidity defect document, and comprehensively analyzing the smart contract to be detected in combination with the response results to generate an audit report. By cooperating with the dynamic co-attention network and the large language model, it is possible to automatically analyze the source code of the smart contract and detect its potential liquidity defects, thereby improving the detection accuracy and adaptability of defects and enhancing the reliability and stability of the audit report.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of software engineering vulnerability detection, and in particular to a method and system for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network. Background Art

[0002] With the rapid development of blockchain technology, decentralized finance (DeFi) has gradually become a vital component of the financial technology landscape. In this context, liquidity providers, as crucial players in maintaining the liquidity of decentralized financial platforms, are increasingly impacting traditional consensus mechanisms, such as Proof of Stake (PoS). To better manage system liquidity, the Proof of Liquidity (PoL) mechanism has been proposed and has been implemented in some blockchain systems. The PoL mechanism automatically manages user staked assets through smart contracts and distributes rewards based on liquidity contribution. However, the design complexity and automated operation of PoL smart contracts also introduce numerous potential security issues. The security of PoL smart contract liquidity management relies on the correctness of their design and implementation. Design flaws in the contracts not only impact the stability of the entire system but can also lead to the loss of user assets. Common liquidity flaws include improper access control, reentrancy attacks, lax authorization mechanisms, and external contract interaction risks, all of which pose serious threats to the security and reliability of the contracts. Currently, traditional smart contract auditing tools primarily include static analysis, dynamic analysis, and formal verification.

[0003] Static analysis tools typically rely on matching known patterns in defect databases to detect security issues in contracts. However, due to the diverse nature of PoL contracts and the limited sample size of defects, this approach cannot achieve comprehensive audits. While dynamic analysis tools can generate different execution paths to cover various contract logics, their high complexity leads to high costs and limitations when dealing with large-scale contracts. While formal verification can guarantee certain security properties through mathematical proof, its complexity and scalability make it difficult to effectively apply to complex and diverse liquidity defects.

[0004] With the maturity of Large Language Models (LLMs) in natural language processing, researchers have attempted to apply them to smart contract security audits, achieving some success. While LLMs are capable of processing complex language and contextual relationships, they still suffer from unstable responses and high false positive rates when used directly for PoL smart contract code analysis. Furthermore, relying solely on LLMs for contract audits struggles to meet the needs of different contract types and application scenarios.

[0005] Therefore, traditional smart contract defect detection methods often have problems with low detection accuracy and adaptability, resulting in low accuracy of the security audit process and unstable audit reports. Summary of the Invention

[0006] Based on this, in order to solve the above technical problems, a method and system for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network are provided. This method can improve the detection accuracy and adaptability of potential liquidity defects in PoL smart contracts, make the audit process more systematic and precise, and enhance the reliability and stability of audit reports.

[0007] A method for detecting smart contract liquidity defects based on a large language model and a dynamic co-attention network, the method comprising:

[0008] Determine the type of liquidity defect based on the smart contract vulnerability database, and extract the smart contract code snippet based on the liquidity defect type;

[0009] Convert the smart contract code snippet into an embedding vector, extract a feature vector from the embedding vector using a dynamic co-attention network, and build a defect knowledge base based on the embedding vector and the feature vector;

[0010] Obtaining a smart contract to be tested, generating a target embedding vector and a target feature vector based on the smart contract to be tested, and matching them based on the defect knowledge base to obtain a suspected defect list;

[0011] Inputting the suspected defect list into a large language model, identifying and recording the defect content in the suspected defect list, and outputting a response result;

[0012] Determine the liquidity defect document, conduct a comprehensive analysis of the smart contract to be tested in combination with the response result, and generate an audit report; the audit report includes the liquidity defect.

[0013] In one embodiment, determining the liquidity defect type based on the smart contract vulnerability database and extracting the smart contract code snippet according to the liquidity defect type includes:

[0014] Analyze decentralized finance attack events, collect malicious transaction call flows based on the attack events, extract smart contracts from the malicious transaction call flows, and build a smart contract vulnerability database;

[0015] Analyze the smart contract vulnerability dataset, find the contract requirements corresponding to the PoL smart contract, and determine the type of liquidity defect;

[0016] Screen and extract smart contract codes corresponding to the liquidity defect type from open source smart contract projects;

[0017] The smart contract code is filtered to obtain a smart contract code fragment.

[0018] In one embodiment, converting the smart contract code snippet into an embedding vector and extracting a feature vector from the embedding vector using a dynamic co-attention network includes:

[0019] Inputting the smart contract code snippets into a code embedding model, and converting each of the smart contract code snippets into an embedding vector through the code embedding model;

[0020] The embedding vector is input into a dynamic co-attention network, the embedding vector is dynamically adjusted through the co-attention layer in the dynamic co-attention network, and a feature vector is extracted based on the adjusted embedding vector.

[0021] In one embodiment, constructing a defect knowledge base based on the embedding vector and the feature vector includes:

[0022] Mapping the embedding vector, the feature vector and the defect information, and constructing a liquidity defect knowledge base index based on the mapping relationship;

[0023] A defect knowledge base is constructed according to the mapping relationship, defect information, and liquidity defect knowledge base index.

[0024] In one embodiment, generating a target embedding vector and a target feature vector according to the smart contract to be detected includes:

[0025] Performing data preprocessing on the smart contract to be tested and segmenting it according to its function to obtain segmented smart contract fragments to be tested;

[0026] The smart contract fragment to be detected is converted into a target embedding vector, and a target feature vector is extracted from the target embedding vector using a dynamic co-attention network.

[0027] In one embodiment, matching is performed based on the defect knowledge base to obtain a suspected defect list, including:

[0028] Matching the target embedding and the target feature vector with entries in the defect knowledge base using Mahalanobis distance to obtain a matching result;

[0029] Calculating the similarity between the smart contract fragment to be detected and the entries in the defect knowledge base based on the matching result;

[0030] The matching results are screened and integrated according to the similarity to obtain a suspected defect list.

[0031] In one embodiment, the liquidity deficiency document is determined, and the smart contract to be tested is comprehensively analyzed in combination with the response result to generate an audit report, including:

[0032] Taking the response result and the liquidity defect document as input, reviewing the defects in the suspected defect list according to the defect content using the large language model to obtain a review result;

[0033] False positive entries in the suspected defect list are determined based on the review results, and a first preliminary report is generated with annotated information on whether the entries are true defects.

[0034] In one embodiment, determining the liquidity deficiency document, performing a comprehensive analysis of the smart contract to be tested in combination with the response result, and generating an audit report also includes:

[0035] Taking the smart contract to be tested and the liquidity defect document as input, comprehensively analyzing the smart contract to be tested using the large language model to obtain an analysis result;

[0036] Searching, in the smart contract to be tested, for potential defect types that are not stored in the suspected defect list according to the analysis result;

[0037] A second preliminary report including the potential defect type is output.

[0038] In one embodiment, the method further comprises:

[0039] Integrate the first preliminary report and the second preliminary report to generate an audit report;

[0040] Wherein, if defects are confirmed in both the first preliminary report and the second preliminary report, the audit report shall contain defects;

[0041] If there are no deficiencies in the first preliminary report and there are deficiencies in the second preliminary report, new deficiencies are marked in the audit report.

[0042] A smart contract liquidity defect detection system based on a large language model and a dynamic co-attention network, the system comprising:

[0043] A liquidity defect type determination module, configured to determine the liquidity defect type based on the smart contract vulnerability database and extract smart contract code snippets based on the liquidity defect type;

[0044] A feature extraction module, configured to convert the smart contract code snippet into an embedding vector, extract a feature vector from the embedding vector using a dynamic co-attention network, and construct a defect knowledge base based on the embedding vector and the feature vector;

[0045] A similarity matching module is used to obtain a smart contract to be tested, generate a target embedding vector and a target feature vector based on the smart contract to be tested, and match them based on the defect knowledge base to obtain a suspected defect list;

[0046] an identification module, configured to input the suspected defect list into a large language model, identify and record the defect content in the suspected defect list, and output a response result;

[0047] An audit module is configured to identify a liquidity defect document, conduct a comprehensive analysis of the smart contract to be tested in combination with the response result, and generate an audit report; the audit report includes liquidity defects.

[0048] The above-mentioned smart contract liquidity defect detection method and system based on a large language model and a dynamic co-attention network extracts feature vectors through a dynamic co-attention network and identifies and records defect content through a large language model. It can automatically analyze the source code of smart contracts and detect their potential liquidity defects. It can improve the detection accuracy and adaptability of potential liquidity defects in PoL smart contracts, make the audit process more systematic and precise, and enhance the reliability and stability of audit reports. BRIEF DESCRIPTION OF THE DRAWINGS

[0049] Figure 1 This is a diagram illustrating an application environment of a method for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network in one embodiment;

[0050] Figure 2 1. A flowchart of a method for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network in one embodiment;

[0051] Figure 3 A schematic diagram of a four-stage collaborative work prompt word strategy in one embodiment;

[0052] Figure 4 A flowchart of a method for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network in another embodiment;

[0053] Figure 5 This is a block diagram of a smart contract liquidity defect detection system based on a large language model and dynamic co-attention network;

[0054] Figure 6 FIG. 1 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION

[0055] In order to make the purpose, technical solutions and advantages of this application more clear, the following further describes this application in detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain this application and are not intended to limit this application.

[0056] It is understood that the terms "first," "second," etc., used herein may be used to describe preliminary reports, but these preliminary reports are not limited by these terms. These terms are used only to distinguish a first preliminary report from another preliminary report. For example, a first preliminary report may be referred to as a second preliminary report, and similarly, a second preliminary report may be referred to as a first preliminary report, without departing from the scope of this application. The first preliminary report and the second preliminary report are both preliminary reports, but they are not the same preliminary report.

[0057] The smart contract liquidity defect detection method based on a large language model and a dynamic co-attention network provided in the embodiment of the present application can be applied to Figure 1 In the application environment shown. Figure 1 As shown, the application environment includes a computer device 110. Computer device 110 can determine the type of liquidity defect based on a smart contract vulnerability database and extract smart contract code snippets based on the liquidity defect type. Computer device 110 can convert the smart contract code snippets into embedding vectors, extract feature vectors from the embedding vectors using a dynamic co-attention network, and construct a defect knowledge base based on the embedding vectors and feature vectors. Computer device 110 can obtain a smart contract to be tested, generate a target embedding vector and target feature vector based on the smart contract to be tested, and perform matching based on the defect knowledge base to obtain a suspected defect list. Computer device 110 can input the suspected defect list into a large language model, identify and record the defect content in the suspected defect list, and output a response result. Computer device 110 can determine the liquidity defect document, conduct a comprehensive analysis of the smart contract to be tested based on the response result, and generate an audit report. The audit report includes the liquidity defect. Computer device 110 can be, but is not limited to, various personal computers, laptops, smartphones, robots, unmanned aerial vehicles, tablet computers, and other devices.

[0058] In one embodiment, Figure 2 As shown in the figure, a method for detecting smart contract liquidity defects based on a large language model and a dynamic co-attention network is provided, which includes the following steps:

[0059] Step 202: Determine the liquidity defect type based on the smart contract vulnerability database, and extract the smart contract code snippet according to the liquidity defect type.

[0060] Before detecting smart contract liquidity flaws, it's necessary to first identify the type of liquidity flaw. Therefore, we can first organize the types of liquidity flaws and collect relevant smart contract code snippets. Computers can analyze decentralized finance permission control events and flash loan attacks in the REKT dataset to collect malicious transaction call flows involving attacking accounts and vulnerable contracts, and then extract the relevant smart contracts from these malicious transaction call flows.

[0061] Specifically, in one embodiment, a method for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network may also include determining the type of liquidity defect and obtaining a process of obtaining a smart contract code snippet. The specific process includes: analyzing decentralized financial attack events, collecting malicious transaction call flows based on the attack events, and extracting smart contracts in the malicious transaction call flows to construct a smart contract vulnerability database; analyzing the smart contract vulnerability dataset and searching for contract requirements corresponding to the PoL smart contract to determine the type of liquidity defect; screening and extracting smart contract codes corresponding to the liquidity defect type from open source smart contract projects; and filtering the smart contract codes to obtain smart contract code snippets.

[0062] Among them, liquidity defect types may include: unprotected token withdrawal; unprotected SELFDESTRUCT instruction; reentrancy attack; lack of proper signature verification; calling untrusted proxy contract (delegatecall); authorization through tx.origin; writing to arbitrary storage location; denial of service (DoS) due to call failure; denial of service (DoS) caused by block gas limit; transaction order dependency.

[0063] Specifically, in this embodiment, the computer device can analyze existing smart contract vulnerability databases and, based on the specific requirements of PoL smart contracts, identify types of liquidity-related defects. The computer device can then filter and extract code containing various defects from open-source smart contract projects, particularly those related to liquidity management. This code can be based on real-world defect cases or generated through simulations. The collected code snippets can be manually filtered to remove irrelevant code, and the defect locations in each code segment can be marked and appropriately descriptive to ensure accuracy in subsequent processing.

[0064] In this embodiment, the source code of the PoL smart contract can also be preprocessed, including removing comments and blank lines, and further dividing the complete code into independent function fragments. Then, for code fragments known to contain liquidity defects, after removing redundant information, the defect location can be marked and necessary descriptive information can be added.

[0065] In step 204, the smart contract code snippet is converted into an embedding vector, and a dynamic co-attention network is used to extract a feature vector from the embedding vector, and a defect knowledge base is constructed based on the embedding vector and the feature vector.

[0066] The computer device can generate embedding vectors based on smart contract code snippets and further extract feature vectors using a dynamic co-attention network to build a defect knowledge base. Specifically, the computer device can use a dynamic co-attention network to extract feature vectors. The dynamic co-attention network contains multiple co-attention layers to dynamically adjust the embedding vectors, achieving more refined feature extraction of code snippets. It can also identify dependencies between codes to better capture logical structure and potential defects.

[0067] In one embodiment, a method for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network is provided, which may also include a process of extracting feature vectors. The specific process includes: inputting smart contract code snippets into a code embedding model, and converting each smart contract code snippet into an embedding vector through the code embedding model; inputting the embedding vector into a dynamic co-attention network, dynamically adjusting the embedding vector through the co-attention layer in the dynamic co-attention network, and extracting a feature vector based on the adjusted embedding vector.

[0068] The computer device can generate an embedding vector for the collected defective smart contract code and extract the feature vector using a dynamic co-attention network. Specifically, the computer device can use a pre-trained code embedding model to convert each smart contract code into a corresponding embedding vector. The embedding vector can describe the semantic and functional characteristics of the code fragment; then, the dynamic co-attention network can be used to further extract high-level feature vectors from the embedding vector, establish associations between defective codes, and ensure that codes with similar defects have similar feature vectors, thereby enhancing the ability to identify defects. The code embedding model can be expressed as V = f Embedding (I), where I represents the input defective smart contract code, V represents the embedding vector, and f Embedding Represents a code embedding model.

[0069] When extracting feature vectors, a dynamic co-attention network consisting of three co-attention layers can be used to further extract high-level feature representations from the embedding vector. By defining the DCN structure, complex contextual information between code snippets can be captured and integrated.

[0070] In one embodiment, a smart contract liquidity defect detection method based on a large language model and a dynamic co-attention network is provided, which may also include a process of constructing a defect knowledge base. The specific process includes: mapping according to the embedding vector, the feature vector and the defect information, and constructing a liquidity defect knowledge base index according to the mapping relationship; constructing a defect knowledge base according to the mapping relationship, the defect information and the liquidity defect knowledge base index.

[0071] The computer device can use the embedding vector and the feature vector to construct a liquidity defect knowledge base index and realize the mapping between the defect code, defect name and other information.

[0072] Step 206: Obtain the smart contract to be tested, generate a target embedding vector and a target feature vector based on the smart contract to be tested, and match them based on the defect knowledge base to obtain a defect suspicion list.

[0073] The computer device can perform similarity matching between the embedding and feature vectors of the smart contract to be tested and the defect knowledge base, and generate a list of suspected potential defects based on contextual information. The similarity matching strategy uses dual matching of embeddings and feature vectors, combined with Mahalanobis distance calculation, to improve matching accuracy and robustness.

[0074] In one embodiment, a method for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network is provided, which may also include a process for generating a feature vector. The specific process includes: performing data preprocessing on the smart contract to be detected, and cutting it according to function to obtain the cut smart contract fragments to be detected; converting the smart contract fragments to be detected into a target embedding vector, and using a dynamic co-attention network to extract a target feature vector from the target embedding vector.

[0075] Data preprocessing can include removing comments and blank lines, further dividing the complete code into independent function fragments according to their functionality. The computer then generates target embedding vectors and target feature vectors for the segmented code fragments to be tested, ensuring that their data format is consistent with the data in the defect knowledge base.

[0076] In one embodiment, a method for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network may further include a process of obtaining a suspected defect list, the specific process including: using Mahalanobis distance to match the target embedding and target feature vector with the entries in the defect knowledge base to obtain a matching result; calculating the similarity between the smart contract fragment to be detected and the entries in the defect knowledge base based on the matching result; screening and fusing the matching results based on the similarity to obtain a suspected defect list.

[0077] Specifically, the computer device can use the Mahalanobis distance to match the target embedding vector and target feature vector of the smart contract code to be tested with the entries in the defect knowledge base. By calculating the similarity between each fragment to be tested and the entries in the knowledge base, the most similar defect entry is obtained and the possible defect information is found. Then, the computer device can filter and merge the matching results, sort and deduplicate the similarity matching results, and generate a JSON formatted suspicion list as input for subsequent detection tasks, i.e., generate a potential defect suspicion list. Each entry in the suspicion list contains the location of the potential defect, the matching defect type, and the similarity score.

[0078] Step 208: Input the suspected defect list into the large language model, identify and record the defect content in the suspected defect list, and output a response result.

[0079] Computer equipment can adopt a four-stage prompt word strategy to guide the large language model to analyze the suspicion list, thereby generating an audit report containing defect descriptions, repair suggestions and risk assessments. Figure 3 As shown, the four-stage prompt word strategy can include: stage one: identification and recording; stage two: false positive analysis; stage three: false negative analysis; stage four: final integration.

[0080] Specifically, in one embodiment, a method for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network may also include a process for generating a first preliminary report. The specific process includes: taking the response result and the liquidity defect document as input, reviewing the defects in the defect suspicion list according to the defect content through the large language model to obtain a review result; determining the false positive entries in the defect suspicion list based on the review result, and generating a first preliminary report marked with whether it is a true defect.

[0081] The process of generating the first preliminary report is the process of identification and recording. The computer device can use the suspicion list as input to guide the large language model to interpret the potential defects in the content in the suspicion list, confirm the specific defect type of each item, ensure that the model accurately understands the defect content, and output a standard JSON format response, which is the first preliminary report.

[0082] In one embodiment, a method for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network may also include a process for generating a second preliminary report. The specific process includes: taking the smart contract to be detected and the liquidity defect document as input, performing a comprehensive analysis of the smart contract to be detected through the large language model to obtain analysis results; searching for potential defect types that are not stored in the defect suspicion list in the smart contract to be detected based on the analysis results; and outputting a second preliminary report containing the potential defect types.

[0083] The process of generating the second preliminary report is the process of false positive analysis. The computer device can review the defects in the suspicion list based on the defect description through a large language model based on the defect document and the standard JSON format response, namely the first preliminary report, identify possible false positive entries, detect possible false positives, perform code path logic verification, reduce fragment isolated false positives, and generate a JSON report that indicates whether it is a true defect, namely the second preliminary report.

[0084] Specifically, the computer takes the smart contract code to be tested and the liquidity defect documentation as input, and instructs the large language model to conduct a comprehensive review of the entire contract code, identifying potential defect types not captured by the suspicion list. The output is a JSON report containing all potential defects, known as the second preliminary report. The liquidity defect documentation includes the cause, manifestation, and impact of each defect type, providing contextual support during the detection process.

[0085] Step 210: Determine the liquidity defect document, conduct a comprehensive analysis of the smart contract to be tested in combination with the response results, and generate an audit report; the audit report includes liquidity defects.

[0086] In one embodiment, a method for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network may also include a process for generating an audit report. The specific process includes: integrating a first preliminary report and a second preliminary report to generate an audit report; wherein, if defects are confirmed in both the first preliminary report and the second preliminary report, the audit report includes the defects; if there are no defects in the first preliminary report and there are defects in the second preliminary report, the new defects are marked in the audit report.

[0087] That is, the process of false negative analysis and final integration can be the process of generating the final audit report, in which the false negative analysis is to combine the liquidity defect documents, conduct a comprehensive analysis of the contract to be tested, identify the types of missed defects, integrate cross-segment information, and output the results in JSON format; the final integration is to integrate the false positive and false negative analysis results to generate the final audit report, which includes defect risk ranking to help users prioritize high-risk defects.

[0088] The computer equipment can integrate the results of the first and second preliminary reports to generate a final audit report. If both results confirm the existence of a deficiency, it will be listed in the report. If the deficiency is only confirmed in the second preliminary report and not in the first preliminary report, it will be marked as a newly discovered deficiency.

[0089] The resulting audit report includes a defect description, remediation recommendations, and a risk-based assessment. Depending on the application scenario, the detection strategy can choose strict precision, a balanced strategy, or a strict recall strategy to meet different contract security requirements. The audit report also includes the defect's risk level, assessing its likelihood and potential impact on liquidity management, thereby providing targeted audit and optimization recommendations.

[0090] In one embodiment, Figure 4 As shown, a method for detecting smart contract liquidity defects based on a large language model and a dynamic co-attention network can include: contract code preprocessing, embedding vector generation and feature vector extraction, suspicion list generation, and audit report generation. By acquiring code features through embedding generation and feature extraction, combined with a DCN for deep contextual understanding, the method improves the detection and optimization capabilities of PoL smart contract liquidity defects.

[0091] In one embodiment, in order to evaluate the effectiveness of the smart contract liquidity defect detection method based on a large language model and a dynamic co-attention network in this application, 631 real-world smart contracts collected from Etherscan and Github are used as a verification set, and a comprehensive evaluation experiment is conducted on the detection results from the two levels of reliability and stability.

[0092] Among them, reliability evaluation includes five indicators: accuracy, precision, recall, specificity, and F1 index. Accuracy is an indicator for evaluating the accuracy of model predictions. It is calculated as all correct predictions (true positives TP and true negatives TN) divided by the total number of all predictions. It reflects the model's ability to correctly identify positive and negative examples. Precision is the proportion of positive examples predicted by the evaluation model that are actually positive. It is calculated as the sum of true positives TP divided by the sum of true positives TP and false positives FP. The higher the accuracy, the more reliable the model's judgment of predicted positive examples and the fewer false positives. Recall is the ability of the evaluation model to identify all actual positive examples. It is calculated as the sum of true positives TP divided by the sum of true positives TP and false negatives FN. The higher the recall rate, the fewer positive examples the model misses, reflecting the effectiveness of the model in finding all positive examples. The specific value is used to evaluate the model's ability to identify all actual negative examples. It is calculated as the number of true negative examples (TN) divided by the sum of the number of true negative examples (TN) and the number of false positive examples (FP). The higher the specific value, the stronger the model's ability to correctly exclude negative examples, reflecting the reliability of the model in identifying negative examples. The F1 index is the harmonic mean of precision and recall, which strikes a balance between the two. It is an indicator that comprehensively considers precision and recall, and is particularly suitable for evaluation when the positive and negative samples are unbalanced.

[0093] Stability assessment includes three metrics: the empty report rate, the abnormal label rate, and the multi-label rate. The empty report rate records the proportion of empty reports generated during the audit; the abnormal label rate records the proportion of reports generated during the audit that contain labels outside the problem space; and the multi-label rate records the proportion of reports generated during the audit that contain more than three defect labels.

[0094] The purpose of this experiment was to evaluate the quality of the results of a proposed smart contract liquidity defect detection method based on a large language model and a dynamic co-attention network on a real-world contract validation set. When using GPT-4o as the test model, the reliability and stability of the detection results are shown in Table 1 below:

[0095] Table 1 Reliability and stability evaluation indicators of three detection strategies

[0096]

[0097] Experimental results show that the three liquidity defect detection strategies provided by the smart contract liquidity defect detection method based on large language model and dynamic co-attention network all show their respective advantages.

[0098] For the strict precision strategy, its accuracy and precision reached 87% and 72% respectively, demonstrating that the strategy has strong detection capabilities in high-security scenarios and can effectively reduce false positives, making it suitable for high-security, low-tolerance liquidity pool contract detection.

[0099] For the balanced strategy, its precision and recall rates reach 85% and 88% respectively, which is suitable for common liquidity defect detection tasks and can maintain good accuracy while ensuring a high recall rate.

[0100] For the strict recall rate strategy, its accuracy and recall rates reached 81% and 93% respectively, showing its advantages in revealing potential defects. It is especially suitable for the R&D and testing stages, and can effectively discover medium and low-risk defects, ensuring that omissions are minimized during the detection process.

[0101] Therefore, the smart contract liquidity defect detection method based on a large language model and a dynamic co-attention network in this application has wide applicability in the PoL smart contract liquidity defect detection task and has significant advantages in defect coverage, and can provide smart contract managers and developers with detailed customized audit reports and optimization suggestions.

[0102] It should be understood that, although the various steps in the above flow chart are shown in sequence as indicated by the arrows, these steps are not necessarily performed in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and these steps can be performed in other orders. Moreover, at least a portion of the steps in the above flow chart may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily performed at the same time, but can be performed at different times, and the execution order of these sub-steps or stages is not necessarily to be performed in sequence, but can be performed in turn or alternately with other steps or at least a portion of the sub-steps or stages of other steps.

[0103] In one embodiment, Figure 5 As shown, a smart contract liquidity defect detection system based on a large language model and a dynamic co-attention network is provided, including: a liquidity defect type determination module 510, a feature extraction module 520, a similarity matching module 530, an identification module 540 and an audit module 550, wherein:

[0104] a liquidity defect type determination module 510 , configured to determine the liquidity defect type based on the smart contract vulnerability database and extract the smart contract code snippet based on the liquidity defect type;

[0105] Feature extraction module 520, for converting smart contract code snippets into embedding vectors, extracting feature vectors from the embedding vectors using a dynamic co-attention network, and building a defect knowledge base based on the embedding vectors and feature vectors;

[0106] Similarity matching module 530 is used to obtain the smart contract to be tested, generate a target embedding vector and a target feature vector based on the smart contract to be tested, and match them based on the defect knowledge base to obtain a list of suspected defects;

[0107] Identification module 540, used to input the suspected defect list into the large language model, identify and record the defect content in the suspected defect list, and output a response result;

[0108] The audit module 550 is used to identify the liquidity defect document, conduct a comprehensive analysis of the smart contract to be tested based on the response results, and generate an audit report; the audit report includes liquidity defects.

[0109] In one embodiment, the liquidity defect type determination module 510 is also used to analyze decentralized financial attack events, collect malicious transaction call flows based on the attack events, extract smart contracts in the malicious transaction call flows, and build a smart contract vulnerability database; analyze the smart contract vulnerability dataset, and find the contract requirements corresponding to the PoL smart contract to determine the liquidity defect type; screen and extract smart contract codes corresponding to the liquidity defect type from open source smart contract projects; and filter the smart contract codes to obtain smart contract code snippets.

[0110] In one embodiment, the feature extraction module 520 is also used to input the smart contract code snippet into the code embedding model, convert each smart contract code snippet into an embedding vector through the code embedding model; input the embedding vector into the dynamic co-attention network, dynamically adjust the embedding vector through the co-attention layer in the dynamic co-attention network, and extract the feature vector based on the adjusted embedding vector.

[0111] In one embodiment, the feature extraction module 520 is further used to map the embedded vector, feature vector and defect information, and construct a fluidity defect knowledge base index based on the mapping relationship; and construct a defect knowledge base based on the mapping relationship, defect information and fluidity defect knowledge base index.

[0112] In one embodiment, the similarity matching module 530 is also used to preprocess the data of the smart contract to be detected, and segment it according to its function to obtain the segmented smart contract fragments to be detected; convert the smart contract fragments to be detected into a target embedding vector, and use a dynamic co-attention network to extract a target feature vector from the target embedding vector.

[0113] In one embodiment, the similarity matching module 530 is further configured to use the Mahalanobis distance to match the target embedding and target feature vector with entries in the defect knowledge base to obtain a matching result; calculate the similarity between the smart contract fragment to be detected and the entry in the defect knowledge base based on the matching result; and filter and fuse the matching results based on the similarity to obtain a suspected defect list.

[0114] In one embodiment, the identification module 540 is also used to take the response results and the liquidity defect document as input, and review the defects in the defect suspicion list according to the defect content through a large language model to obtain a review result; determine the false positive entries in the defect suspicion list based on the review result, and generate a first preliminary report marked with whether it is a true defect.

[0115] In one embodiment, the audit module 550 is also used to take the smart contract to be tested and the liquidity defect document as input, perform a comprehensive analysis of the smart contract to be tested through a large language model, and obtain analysis results; search for potential defect types that are not stored in the defect suspicion list in the smart contract to be tested based on the analysis results; and output a second preliminary report containing the potential defect types.

[0116] In one embodiment, the audit module 550 is also used to integrate the first preliminary report and the second preliminary report to generate an audit report; wherein, if defects are confirmed in both the first preliminary report and the second preliminary report, the defects are included in the audit report; if there are no defects in the first preliminary report but there are defects in the second preliminary report, the new defects are marked in the audit report.

[0117] In one embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as follows: Figure 6 As shown. The computer device includes a processor, memory, network interface, display screen and input device connected via a system bus. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with an external terminal via a network connection. When the computer program is executed by the processor, a method for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network is implemented. The display screen of the computer device can be a liquid crystal display or an electronic ink display screen. The input device of the computer device can be a touch layer covering the display screen, or a key, trackball or touchpad provided on the computer device housing, or an external keyboard, touchpad or mouse.

[0118] Those skilled in the art will understand that Figure 6 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than shown in the figure, or combine certain components, or have a different component arrangement.

[0119] In one embodiment, a computer device is provided, including a memory and a processor, wherein a computer program is stored in the memory, and when the processor executes the computer program, the steps of a method for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network are implemented.

[0120] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps of a method for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network are implemented.

[0121] Those skilled in the art will appreciate that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).

[0122] The technical features of the above embodiments can be combined arbitrarily. In order to make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0123] The above-described embodiments merely represent several implementation methods of the present application. While the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present invention. It should be noted that a person skilled in the art could make various modifications and improvements without departing from the spirit of the present application, all of which fall within the scope of protection of the present application. Therefore, the scope of protection of the present patent application shall be determined by the appended claims.

Claims

1. A method for detecting liquidity defects in smart contracts based on a large language model and a dynamic co-attention network, characterized in that: The method comprises: Determine the type of liquidity defect based on the smart contract vulnerability database, and extract the smart contract code snippet based on the liquidity defect type; Converting the smart contract code snippet into an embedding vector and extracting a feature vector from the embedding vector using a dynamic co-attention network, including: inputting the smart contract code snippet into a code embedding model, converting each smart contract code snippet into an embedding vector using the code embedding model; inputting the embedding vector into a dynamic co-attention network, dynamically adjusting the embedding vector using a co-attention layer in the dynamic co-attention network, and extracting a feature vector based on the adjusted embedding vector; and constructing a defect knowledge base based on the embedding vector and the feature vector. Obtaining a smart contract to be tested, generating a target embedding vector and a target feature vector based on the smart contract to be tested, and matching them based on the defect knowledge base to obtain a suspected defect list; Inputting the suspected defect list into a large language model, identifying and recording the defect content in the suspected defect list, and outputting a response result; Determine the liquidity defect document, conduct a comprehensive analysis of the smart contract to be tested in combination with the response result, and generate an audit report, including: using the response result and the liquidity defect document as input, reviewing the defects in the suspected defect list based on the defect content using the large language model to obtain a review result; determine false positive entries in the suspected defect list based on the review result, and generate a first preliminary report marked with whether they are true defects; the audit report includes liquidity defects; The final audit report contains defect descriptions, remediation recommendations, and risk-level assessments; the liquidity defect documentation includes the causes, manifestations, and impacts of each defect type to provide contextual support during the detection process.

2. The method for detecting smart contract liquidity defects based on a large language model and a dynamic co-attention network according to claim 1 is characterized in that: Determine the liquidity defect type based on the smart contract vulnerability database, and extract the smart contract code snippet based on the liquidity defect type, including: Analyze decentralized finance attack events, collect malicious transaction call flows based on the attack events, extract smart contracts from the malicious transaction call flows, and build a smart contract vulnerability database; Analyze the smart contract vulnerability dataset, find the contract requirements corresponding to the PoL smart contract, and determine the type of liquidity defect; Screen and extract smart contract codes corresponding to the liquidity defect type from open source smart contract projects; The smart contract code is filtered to obtain a smart contract code fragment.

3. The method for detecting smart contract liquidity defects based on a large language model and a dynamic co-attention network according to claim 1 is characterized in that: Constructing a defect knowledge base based on the embedding vector and the feature vector includes: Mapping the embedding vector, the feature vector and the defect information, and constructing a liquidity defect knowledge base index based on the mapping relationship; A defect knowledge base is constructed according to the mapping relationship, defect information, and liquidity defect knowledge base index.

4. The method for detecting smart contract liquidity defects based on a large language model and a dynamic co-attention network according to claim 1 is characterized in that: Generating a target embedding vector and a target feature vector according to the smart contract to be detected, including: Performing data preprocessing on the smart contract to be tested and segmenting it according to its function to obtain segmented smart contract fragments to be tested; The smart contract fragment to be detected is converted into a target embedding vector, and a target feature vector is extracted from the target embedding vector using a dynamic co-attention network.

5. The method for detecting smart contract liquidity defects based on a large language model and a dynamic co-attention network according to claim 4 is characterized in that: Based on the matching in the defect knowledge base, a defect suspicion list is obtained, including: Matching the target embedding vector and the target feature vector with entries in the defect knowledge base using Mahalanobis distance to obtain a matching result; Calculating the similarity between the smart contract fragment to be detected and the entries in the defect knowledge base based on the matching result; The matching results are screened and integrated according to the similarity to obtain a suspected defect list.

6. The method for detecting smart contract liquidity defects based on a large language model and a dynamic co-attention network according to claim 1 is characterized in that: Identify the liquidity defect document, conduct a comprehensive analysis of the smart contract to be tested based on the response results, and generate an audit report, which also includes: Taking the smart contract to be tested and the liquidity defect document as input, comprehensively analyzing the smart contract to be tested using the large language model to obtain an analysis result; Searching, in the smart contract to be tested, for potential defect types that are not stored in the suspected defect list according to the analysis result; A second preliminary report including the potential defect type is output.

7. The method for detecting smart contract liquidity defects based on a large language model and a dynamic co-attention network according to claim 6 is characterized in that: The method further comprises: Integrate the first preliminary report and the second preliminary report to generate an audit report; Wherein, if defects are confirmed in both the first preliminary report and the second preliminary report, the audit report shall contain defects; If there are no deficiencies in the first preliminary report and there are deficiencies in the second preliminary report, new deficiencies are marked in the audit report.

8. A smart contract liquidity defect detection system based on a large language model and a dynamic co-attention network, characterized by: The system comprises: A liquidity defect type determination module, configured to determine the liquidity defect type based on the smart contract vulnerability database and extract smart contract code snippets based on the liquidity defect type; A feature extraction module is configured to convert the smart contract code snippets into embedding vectors and extract feature vectors from the embedding vectors using a dynamic co-attention network, comprising: inputting the smart contract code snippets into a code embedding model, and converting each smart contract code snippet into an embedding vector using the code embedding model; inputting the embedding vectors into a dynamic co-attention network, dynamically adjusting the embedding vectors using a co-attention layer in the dynamic co-attention network, and extracting feature vectors based on the adjusted embedding vectors; and constructing a defect knowledge base based on the embedding vectors and feature vectors. A similarity matching module is used to obtain a smart contract to be tested, generate a target embedding vector and a target feature vector based on the smart contract to be tested, and match them based on the defect knowledge base to obtain a suspected defect list; an identification module, configured to input the suspected defect list into a large language model, identify and record the defect content in the suspected defect list, and output a response result; An audit module is configured to determine the liquidity defect document, conduct a comprehensive analysis of the smart contract to be tested in combination with the response result, and generate an audit report, including: taking the response result and the liquidity defect document as input, reviewing the defects in the suspected defect list based on the defect content using the large language model to obtain a review result; determining false positive entries in the suspected defect list based on the review result, and generating a first preliminary report indicating whether the entries are true defects; the audit report includes liquidity defects; The final audit report contains defect descriptions, remediation recommendations, and risk-level assessments; the liquidity defect documentation includes the causes, manifestations, and impacts of each defect type to provide contextual support during the detection process.

Citation Information

Patent Citations

  • Ethereum intelligent contract vulnerability detection method, device and system and medium

    CN115033896A

  • Intelligent contract state defect detection method and system based on deep learning

    CN117992060A