Distributed application intent consistency identification method, device, equipment and storage medium

By generating annotation prompts and code features, and using pre-trained embedding models and random forest classifiers to judge the intent consistency of distributed applications, the problems of poor universality and poor recognition effect in existing technologies are solved, and more efficient intent consistency detection is achieved.

CN119225708BActive Publication Date: 2025-10-03SUN YAT SEN UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411486017.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-10-23
Publication Date
2025-10-03
Estimated Expiration
2044-10-23

AI Technical Summary

Technical Problem

Existing distributed application intent consistency recognition methods have poor universality and poor recognition effect, and cannot effectively detect inconsistencies between code and comments in smart contracts.

Method used

By obtaining the source code and comments of distributed applications, generating comment prompts, using intermediate representation language to extract code features, and converting comments and code into embeddings through a pre-trained embedding model, the random forest classifier is used to determine intent consistency.

Benefits of technology

The universality and recognition effect of intent consistency recognition are improved, and inconsistencies between code and comments can be detected more accurately, reducing economic losses.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119225708B_ABST
    Figure CN119225708B_ABST
Patent Text Reader

Abstract

The present invention discloses a method, apparatus, device, and storage medium for identifying intent inconsistencies in distributed applications, aiming to address the technical issues of poor universality and recognition effectiveness of existing intent consistency identification methods. The method comprises the following steps: obtaining source code and annotations of a distributed application; generating annotation prompts for the annotations; extracting code features from the source code using an intermediate representation language; converting the annotation prompts into annotation embeddings and converting code features into code embeddings using a pre-trained embedding model; and determining intent consistency of the distributed application based on the annotation embeddings and code embeddings.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of consistency recognition technology, in particular to a distributed application Figure 1 A method, device, equipment and storage medium for consistency recognition. Background Art

[0002] Blockchain is a secure, shared data ledger that stores data distributed across multiple nodes in a network, forming a continuous, immutable chain of data, thereby enabling decentralized data management and transaction records. Blockchain technology has continuously evolved from Blockchain 1.0 to Blockchain 2.0 and then to Blockchain 3.0. Smart contracts, as a representative technology of Blockchain 2.0, play a key role. A smart contract is essentially a piece of code that automatically and irreversibly executes once pre-defined conditions are met. Smart contracts have been widely used to create distributed applications, including but not limited to gaming platforms, cryptocurrencies, and decentralized finance. With the development of blockchain technology and the proliferation of distributed applications, the number of smart contracts has increased, raising concerns about their security. In smart contracts, developers often use comments and other text to describe their intent and implement the corresponding code. However, due to developer negligence or inexperience, the actual code logic may not fully align with the developer's true intent. This inconsistency can hinder readers' understanding of the code and, in serious cases, can lead to financial losses. Unfortunately, this inconsistency often does not result in runtime errors and is easily overlooked.

[0003] Existing code intent inconsistency detection methods utilize different code intent descriptions. For example, DAppHunter first triggers DApp transactions by simulating front-end actions and infers expected behavior from the perspective of user intent. It then captures transaction data sent to blockchain wallets and extracts behavior from the perspective of transaction semantics. Simultaneously, it analyzes transaction execution logs to capture the actual behavior of smart contracts. If any two of these three behaviors do not match, an inconsistency is detected. Furthermore, prior art proposes DocCon, which detects inconsistencies between Solidity smart contract library API documentation and corresponding code. DocCon successfully detected multiple API documentation errors in smart contract libraries, including parameter mismatches, missing requirements, and outdated descriptions. Furthermore, prior art proposes SmartCoCo, which defines rule templates and extracts, correlates, and compares comment constraints and code facts through a series of propagation and binding strategies. This enables detection of three types of code comment inconsistencies in smart contracts. This is the only existing work on code comment inconsistency detection in the smart contract field. There is also work on intent inconsistency detection in non-smart contract languages ​​that uses comments as intent descriptions, primarily including rule-based and learning-based approaches. Rule-based methods use predefined rules, natural language processing, and static analysis techniques to detect inconsistent code comments; learning-based methods attempt to use machine learning techniques to detect inconsistent code comments.

[0004] However, in real scenarios, the code and its corresponding API documentation and views are usually not generated synchronously. In some cases, the code may not even have a corresponding API view, so the DApp view and API documentation are used to generate the code. Figure 1 Conformity testing is not always effective or feasible.

[0005] Existing rule-based methods usually focus on specific types of comments, making the formulated rules unsuitable for detecting inconsistent code comments of other types or in other languages. Moreover, formulating rules requires a lot of analysis work, which is time-consuming and labor-intensive.

[0006] The effectiveness of existing learning-based methods depends on a large amount of labeled data, but the lack of large-scale labeled data in smart contracts reduces the effectiveness of existing learning-based methods and leads to poor consistency recognition results. Summary of the Invention

[0007] The present invention provides a distributed application intention inconsistency identification method, device, equipment and storage medium, which are used to solve the existing intention Figure 1 The technical problems of poor universality and poor recognition effect of the consistent recognition method are discussed.

[0008] The present invention provides a distributed application Figure 1Consistency identification methods include:

[0009] Obtaining source code of a distributed application and annotations of the source code;

[0010] generating an annotation prompt for the annotation;

[0011] extracting code features of the source code through an intermediate representation language;

[0012] Converting the annotation prompts into annotation embeddings and converting the code features into code embeddings through a pre-trained embedding model;

[0013] Determine the meaning of the distributed application based on the annotation embedding and the code embedding Figure 1 Consistency.

[0014] Optionally, the step of generating the annotation prompt includes:

[0015] Get the function name of the function corresponding to the comment;

[0016] The function name and the comment are encapsulated as a comment prompt.

[0017] Optionally, the step of extracting code features of the source code using an intermediate representation language includes:

[0018] Converting the source code into an intermediate representation code and generating an abstract syntax tree of the intermediate representation code;

[0019] Obtaining a function call relationship of the intermediate representation code;

[0020] Expanding the abstract syntax tree using the function call relationship to obtain an extended abstract syntax tree, where the extended abstract syntax tree includes a plurality of nodes;

[0021] Extract the code element corresponding to each node at the position of the intermediate representation code;

[0022] Obtaining a built-in function of the intermediate representation function and generating an operation code of the built-in function;

[0023] Obtaining edges of nodes in the extended abstract syntax tree, and extracting node numbers at both ends of the edges and the sequence number of the edges;

[0024] A code signature of the source code is generated using the position, the operation code, and the sequence number.

[0025] Optionally, the meaning of the distributed application is determined based on the annotation embedding and the code embedding. Figure 1 Consistent steps include:

[0026] calculating a first similarity score between the comment embedding and the code embedding;

[0027] Concatenating the comment embedding, the code embedding, and the first similarity score to obtain a function feature;

[0028] The function features are input into a random forest-based classifier, and a judgment result of whether there is inconsistent intention is output.

[0029] Optionally, the training process of the pre-trained embedding model includes:

[0030] Obtain a code comment pair; the code comment pair includes a code sample and a comment sample;

[0031] Inputting the code sample into a first embedding model to be trained to obtain a code embedding result;

[0032] Inputting the annotation sample into a second embedding model to be trained to obtain an annotation embedding result;

[0033] The first embedding model to be trained and the second embedding model to be trained are adjusted using the code embedding result, the comment embedding result and a preset first loss function to obtain a first-stage pre-trained code embedding model and a first-stage pre-trained comment embedding model;

[0034] generating mutation data of the code sample;

[0035] Inputting the code sample and the variant data into the first-stage pre-trained code embedding model respectively, and training with a preset second loss function to obtain a second-stage pre-trained code embedding model;

[0036] The first-stage pre-trained annotation embedding model and the second-stage pre-trained code embedding model are fine-tuned by presetting a third loss function to obtain a pre-trained embedding model.

[0037] The present invention also provides a distributed application Figure 1 A consistency recognition device comprising:

[0038] A source code and annotation acquisition module, used to acquire the source code of the distributed application and the annotations of the source code;

[0039] An annotation prompt generating module, used for generating an annotation prompt for the annotation;

[0040] A code feature extraction module, configured to extract code features of the source code using an intermediate representation language;

[0041] An embedding conversion module, configured to convert the annotation prompts into annotation embeddings and convert code features into code embeddings using a pre-trained embedding model;

[0042] meaning Figure 1 A consistency judgment module is used to judge the consistency of the distributed application according to the annotation embedding and the code embedding. Figure 1 Consistency.

[0043] Optionally, the annotation prompt generation module includes:

[0044] Function name acquisition submodule, used to obtain the function name corresponding to the annotation;

[0045] The comment prompt encapsulation submodule is used to encapsulate the function name and the comment into a comment prompt.

[0046] Optionally, the code feature extraction module includes:

[0047] An abstract syntax tree generation submodule, configured to convert the source code into an intermediate representation code and generate an abstract syntax tree of the intermediate representation code;

[0048] A function call relationship acquisition submodule, used to acquire the function call relationship of the intermediate representation code;

[0049] An extension submodule, configured to extend the abstract syntax tree using the function call relationship to obtain an extended abstract syntax tree, wherein the extended abstract syntax tree includes a plurality of nodes;

[0050] A position extraction submodule, configured to extract the position of the code element corresponding to each node in the intermediate representation code;

[0051] An operation code generation submodule, configured to obtain a built-in function of the intermediate representation function and generate an operation code for the built-in function;

[0052] A sequence number extraction submodule, configured to obtain edges between nodes in the extended abstract syntax tree, and extract node numbers at both ends of the edges and the sequence numbers of the edges;

[0053] A code feature generation submodule is used to generate a code feature of the source code using the position, the operation code and the sequence number.

[0054] The present invention further provides an electronic device, comprising a processor and a memory:

[0055] The memory is used to store program code and transmit the program code to the processor;

[0056] The processor is configured to execute any of the above distributed application instructions according to the instructions in the program code. Figure 1 Consistency identification method.

[0057] The present invention also provides a computer-readable storage medium for storing program code, wherein the program code is used to execute the distributed application program described in any one of the above items. Figure 1 Consistency identification method.

[0058] It can be seen from the above technical solutions that the present invention has the following advantages: the present invention obtains the source code and source code comments of the distributed application; generates annotation prompts for the annotations; extracts code features of the source code through an intermediate representation language; converts the annotation prompts into annotation embeddings and the code features into code embeddings through a pre-trained embedding model; and judges the meaning of the distributed application based on the annotation embeddings and code embeddings. Figure 1 No need to consider the type of annotation, which improves the Figure 1 The universality and recognition effect of consistent recognition. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0060] Figure 1 A distributed application provided by the embodiment of the present invention Figure 1 Flowchart of the steps of the consistency identification method;

[0061] Figure 2 A flowchart of a method for identifying inconsistent distributed application intents provided by another embodiment of the present invention;

[0062] Figure 3 This is a diagram of Yul AST extension based on function calls;

[0063] Figure 4 This is a one-hot encoding structure diagram of built-in functions based on opcodes;

[0064] Figure 5 Schematic diagram of training the embedding model;

[0065] Figure 6 A distributed application provided by the embodiment of the present invention Figure 1 Structural block diagram of the consistency recognition device. DETAILED DESCRIPTION

[0066] The embodiment of the present invention provides a distributed application intention inconsistency identification method, device, equipment and storage medium, which is used to solve the existing intention Figure 1The technical problems of poor universality and poor recognition effect of the consistent recognition method are discussed.

[0067] In order to make the purpose, features, and advantages of the present invention more obvious and easy to understand, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the embodiments described below are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.

[0068] See also Figure 1 , Figure 1 A distributed application provided by the embodiment of the present invention Figure 1 Flowchart of the steps of the consistency identification method.

[0069] The present invention provides a distributed application Figure 1 The consistency identification method may specifically include the following steps:

[0070] Step 101: Obtain source code of a distributed application and annotations of the source code;

[0071] In smart contracts, developers often use comments and other text to describe their intent and implement the corresponding code. However, due to developer negligence or inexperience, the actual code logic may not fully match the developer's true intent. This inconsistency can hinder readers' understanding of the code and, in serious cases, lead to financial losses. Therefore, consistency analysis of source code and its corresponding comments is necessary.

[0072] Step 102, generating annotation prompts for the annotations;

[0073] In the embodiment of the present invention, a specific template can be used to encapsulate annotation information to obtain annotation prompts.

[0074] Step 103: extracting code features of the source code using an intermediate representation language;

[0075] In the embodiment of the present invention, code features of source code may be extracted using the intermediate representation language Yul.

[0076] Yul is an intermediate representation language designed specifically for the Ethereum Virtual Machine. It can be used directly to write smart contracts or as an intermediate representation for high-level programming languages ​​like Solidity. As an intermediate representation language, Yul supports a variety of smart contract programming languages, such as Solidity, Move, Flint, Logiken, and Fe.

[0077] Step 104: converting the annotation prompt into an annotation embedding and converting the code feature into a code embedding using a pre-trained embedding model;

[0078] In the embodiment of the present invention, the pre-trained embedding model may include a comment embedding model and a code embedding model; comment embeddings may be generated by inputting comment prompts into the comment embedding model, and code embeddings may be generated by inputting code features into the code embedding model.

[0079] In one example, the comment embedding model can be CodeBERT. CodeBERT demonstrates significant advantages in embedding natural language, such as comments. Firstly, CodeBERT leverages the Transformer architecture and a self-attention mechanism to capture contextual information in comments. Secondly, CodeBERT has been trained on a wide range of datasets and possesses strong semantic representation capabilities, enabling it to recognize and encode complex semantics in comments. Inputting comment prompts into the CodeBERT model yields high-dimensional comment embeddings rich in semantic information.

[0080] In one example, the code embedding model can be a heterogeneous graph neural network model, UniMP. UniMP can process both node and edge features and has a large number of parameters, effectively extracting and learning information from graphs. UniMP learns node and edge features in the graph by propagating and aggregating features within the graph to generate code embeddings.

[0081] Step 105: Determine the meaning of the distributed application based on the annotation embedding and the code embedding. Figure 1 Consistency.

[0082] In the embodiment of the present invention, by analyzing the annotation embedding and the code embedding, it can be determined whether there is a problem of inconsistent intentions in the distributed application.

[0083] The present invention obtains the source code and source code annotations of the distributed application; generates annotation prompts for the annotations; extracts code features of the source code through an intermediate representation language; converts the annotation prompts into annotation embeddings through a pre-trained embedding model, and converts the code features into code embeddings; and determines the meaning of the distributed application based on the annotation embeddings and the code embeddings. Figure 1 No need to consider the type of annotation, which improves the Figure 1 The universality and recognition effect of consistent recognition.

[0084] See also Figure 2 , Figure 2 This is a flowchart of a method for identifying inconsistent distributed application intents provided by another embodiment of the present invention. Specifically, the method may include the following steps:

[0085] Step 201: Obtain source code of a distributed application and annotations of the source code;

[0086] Step 202, obtaining the function name of the function corresponding to the annotation;

[0087] Step 203: Encapsulate the function name and the comment into a comment prompt;

[0088] In a specific implementation, the function name of the function marked with the comment can be obtained, and the function name and the comment can be encapsulated into a comment prompt (Prompt).

[0089] The use of annotation prompts can effectively improve the performance of the model in natural language processing tasks and help better understand the relationship between functions and annotations.

[0090] Step 204: extracting code features of the source code using an intermediate representation language;

[0091] In this embodiment of the present invention, step 204 may include the following sub-steps:

[0092] S41, converting the source code into an intermediate representation code; and generating an abstract syntax tree of the intermediate representation code;

[0093] S42, obtaining a function call relationship of the intermediate representation code;

[0094] S43, using the function call relationship to expand the abstract syntax tree to obtain an extended abstract syntax tree, where the extended abstract syntax tree includes a plurality of nodes;

[0095] S44, extracting the position of the code element corresponding to each node in the intermediate representation code;

[0096] S45, obtaining a built-in function of the intermediate representation function, and generating an operation code of the built-in function;

[0097] S46, obtaining edges connecting nodes in the extended abstract syntax tree, and extracting node numbers at both ends of the edges and the sequence number of the edges;

[0098] S47: Generate a code feature of the source code using the position, the operation code, and the sequence number.

[0099] In a specific implementation, the step of extracting code features of the source code may include three steps, namely code conversion, AST expansion and feature extraction.

[0100] Code conversion involves first using a compiler to convert the source code into Yul code and its corresponding abstract syntax tree (AST). The Yul AST is a tree-like structure used to represent the grammatical structure and hierarchical relationships of Yul code. It decouples syntax and semantics, with each node representing a code structure independent of specific grammatical rules. This enhances the flexibility of code analysis and helps better understand and capture the underlying intent of the code. The Yul AST generated by the compiler contains different types of nodes, such as YulCode, YulObject, YulIf, YulFunctionCall, and YulFunctionDefinition. Each node corresponds to a code element of varying granularity and type within the Yul code.

[0101] AST extension: Considering that the function call in the code reflects the logic of the code, the embodiment of the present invention will extract the function-level AST and expand the function syntax tree according to the calling relationship between functions to obtain the extended abstract syntax tree Yul_AST+. The expansion diagram is shown in the following figure: Figure 3 Specifically, because called functions may also call other functions, unrestricted expansion may cause the AST+ to be too large. Therefore, the AST expansion is limited to M iterations. The specific value of M is determined by parameter experiments.

[0102] Feature extraction: The AST+ obtained by AST expansion contains multiple node types, each type of node corresponds to code elements of different granularity. The types of edges connecting different node types are also different. In this paper, the code features of AST+ are extracted:

[0103] For nodes, we extract the position of the code element corresponding to the node in the Yul code as a feature to reflect the relationship between AST+ and Yul code. In addition, considering that some functions in the Yul code are built-in functions, these built-in functions implement low-level operations and logical calculations, corresponding to the opcodes in the EVM. Different built-in functions are associated with different logics, so we use a one-hot encoding to describe the opcode corresponding to each function and use it as a feature. Figure 4 A one-hot encoding diagram is shown, indicating that this built-in function corresponds to the AND opcode.

[0104] For edges, we extract the node numbers at both ends of the edge and the sequence number of the edge starting from the parent node of the edge as features. These features reflect the relationship between different types of nodes and code elements.

[0105] Step 205: convert the annotation prompt into an annotation embedding and convert the code feature into a code embedding using a pre-trained embedding model;

[0106] In the embodiment of the present invention, the pre-trained embedding model may include a comment embedding model and a code embedding model; comment embeddings may be generated by inputting comment prompts into the comment embedding model, and code embeddings may be generated by inputting code features into the code embedding model.

[0107] In one example, further model training is required to enable the embedding model to better learn and capture the semantics in the code and comments and generate higher quality data embedding. In this paper, considering the inconsistent characteristics of code comments in smart contracts, a two-stage pre-training is set up, including contrastive learning and variation analysis. After pre-training, the model is fine-tuned using labeled data. The schematic diagram of model training is shown in Figure 2. Figure 5 The following describes the two-stage pre-training and model fine-tuning process. Specifically, it can include the following steps:

[0108] S51, obtaining a code comment pair; the code comment pair includes a code sample and a comment sample;

[0109] S52, inputting the code sample into a first embedding model to be trained to obtain a code embedding result;

[0110] S53, inputting the annotation sample into a second embedding model to be trained to obtain an annotation embedding result;

[0111] S54, using the code embedding result, the comment embedding result and a preset first loss function to adjust the first embedding model to be trained and the second embedding model to be trained to obtain a first-stage pre-trained code embedding model and a first-stage pre-trained comment embedding model;

[0112] In a specific implementation, the two-stage pre-training can include contrastive learning and variation analysis. Contrastive learning is widely used in the field of deep learning, which refers to achieving effective representation learning by comparing the relative relationships between samples. Specifically, in each batch, code-annotation pairs are used for learning and training. code comment pairs, which we denote as ( ,in Represents the comment in the code comment pair, Indicates the code in the code comment pair. For each comment , and its matching positive sample is , and the remaining code is considered as a negative sample. The goal of this invention is to make As low as possible. To achieve this goal, we introduce a first loss function called The loss function is used for pre-training of contrastive learning. The formula of the first loss function is as follows:

[0113]

[0114] in, and is the index of the code or comment in the code-comment pair, and No. Normalized embeddings of code and comments for code-comment pairs, is the total number of code-comment pairs.

[0115] S55, generating variation data of the code sample;

[0116] S56, inputting the code sample and the variant data into the first-stage pre-trained code embedding model respectively, and training with a preset second loss function to obtain a second-stage pre-trained code embedding model;

[0117] In an embodiment of the present invention, variation analysis is performed on the code comment pairs to optimize the first-stage pre-trained embedding model to obtain a second embedding model.

[0118] The concept of mutation analysis originates from mutation testing, a technique commonly used in software testing. Mutation testing is a technique used to evaluate the quality of a test suite by making small changes (mutations) to a software program. Given that the code elements that lead to inconsistent code comments can be very small, a model that detects inconsistent intent needs to be able to detect these small elements. Therefore, this paper designs a mutation analysis task that drives the model to capture subtle changes in the code. It includes two types of mutation operators: operator mutation and key code statement deletion mutation, corresponding to the operator level and statement level, respectively.

[0119] Operator mutation refers to changing a built-in function related to an operation to one with the opposite logic, such as changing "add" to "sub" or "lt" to "gt." Operators include both arithmetic operators and logical operators. Arithmetic operators are key to implementing smart contract services. For example, in smart contracts, transactions involve increasing or decreasing the balances of different accounts, while token burns involve reducing the number of tokens. Logical operators are often used in judgment statements, and changes to them directly affect the results of logical judgments.

[0120] Another type of mutation is the key code statement deletion mutation, which refers to deleting some key code statements. The logic contained in the key code statements usually directly corresponds to a part of the annotation. The absence of key code statements is likely to lead to inconsistent code annotations. Therefore, the present invention trains a key statement recognition model based on machine learning to complete the recognition of key statements, randomly delete key statements, and realize the key code statement deletion mutation. Specifically, the key statements in a large number of functions are marked, and the annotations, codes and corresponding labels are input into the classification model based on random forest. The classification model learns to fit the input data to realize the automatic classification of key statements.

[0121] The code data before mutation and the mutated code data Submit it to the first stage pre-trained code embedding model for learning. The normalized embedding of the original data is represented as , the normalized embedding of the variant data is expressed as The optimization goal of the mutation analysis phase is to widen the gap between the original code and the mutation code. The preset second loss function is used. for:

[0122]

[0123] in represents the number of variants, represents the probability obtained by applying the softmax function to the similarity score between the original code embedding and itself, Is to calculate the The similarity scores of the variant code embeddings and the original code embeddings are applied The probability obtained by the function represents the The probability that the variant code data is consistent with the original code data.

[0124] The optimization goal is As high as possible, and As low as possible. Calculated The sum of the negative log-likelihoods of By minimizing , which can effectively separate the code before mutation And the mutated code The embedding distance between them enables the model to pay attention to small element changes.

[0125] S57: Fine-tune the first-stage pre-trained annotation embedding model and the second-stage pre-trained code embedding model by presetting a third loss function to obtain a pre-trained embedding model.

[0126] Model fine-tuning refers to the process of further training a pre-trained model using a new dataset to adapt it to a specific task or domain. During the fine-tuning process, some parameters of the embedded model are fixed so that the model maintains a certain stability during the fine-tuning process. Subsequently, inconsistency detection is performed based on the fine-tuning dataset, and the detection results are compared with the labels. In the fine-tuning stage, the third loss function is used. as follows:

[0127]

[0128] in, is the total amount of data, It is The label of the function (0 or 1), The model predicts The probability that the function is a positive sample.

[0129] It measures the distance between the predicted probability distribution and the true distribution of the label, and optimizes , it is possible to adjust the model parameters and improve its performance in the code comment inconsistency detection task.

[0130] Step 206: Determine the meaning of the distributed application based on the annotation embedding and the code embedding. Figure 1 Consistency.

[0131] In this embodiment of the present invention, step 206 may include the following sub-steps:

[0132] S61, calculating a first similarity score between the comment embedding and the code embedding;

[0133] Compared to consistent code comment pairs, the difference between the comment embedding and the code embedding is greater in inconsistent code comments, which is manifested as a larger distance between the embeddings. Therefore, the similarity score SimScore between the comment embedding and the code embedding can be calculated using the following formula.

[0134]

[0135] in, and represents the embedding of code and comments, and Corresponding representations of the normalized embeddings of code and comments.

[0136] The value range of is -1 to 1. The closer the result is to 1, the closer the direction of the code and the comment in the feature space is. A smaller value indicates that the code and comments differ significantly in the feature space, and in this case, it is considered that there is an inconsistency in intent. In this invention, a threshold is set (obtained through parameter experiments). When the code-comment similarity score of a function is less than the threshold, the function is considered to have a code-comment inconsistency, and vice versa.

[0137] S62, concatenating the comment embedding, the code embedding, and the first similarity score to obtain a function feature;

[0138] S63: Input the function feature into a random forest-based classifier, and output a judgment result of whether there is intention inconsistency.

[0139] In an embodiment of the present invention, a classifier based on random forest can be designed, which takes the function features obtained by splicing code embedding, comment embedding, and similarity scores as input, and takes the judgment result of whether there is inconsistency as output, so as to determine whether there is an intention inconsistency problem in the distributed application.

[0140] The present invention obtains the source code and source code annotations of the distributed application; generates annotation prompts for the annotations; extracts code features of the source code through an intermediate representation language; converts the annotation prompts into annotation embeddings through a pre-trained embedding model, and converts the code features into code embeddings; and determines the meaning of the distributed application based on the annotation embeddings and the code embeddings. Figure 1 No need to consider the type of annotation, which improves the Figure 1 The universality and recognition effect of consistent recognition.

[0141] See also Figure 6 , Figure 6 A distributed application provided by the embodiment of the present invention Figure 1 Structural block diagram of the consistency recognition device.

[0142] The embodiment of the present invention provides a distributed application Figure 1 A consistency recognition device, comprising:

[0143] Source code and annotation acquisition module 601, used to acquire the source code of the distributed application and the annotations of the source code;

[0144] An annotation prompt generating module 602, used to generate an annotation prompt for the annotation;

[0145] A code feature extraction module 603 is used to extract code features of the source code using an intermediate representation language;

[0146] An embedding conversion module 604 is configured to convert the annotation prompt into an annotation embedding and convert the code feature into a code embedding using a pre-trained embedding model;

[0147] meaning Figure 1 The consistency judgment module 605 is used to judge the consistency of the distributed application according to the annotation embedding and the code embedding. Figure 1 Consistency.

[0148] In this embodiment of the present invention, the annotation prompt generating module 602 includes:

[0149] Function name acquisition submodule, used to obtain the function name corresponding to the annotation;

[0150] The comment prompt encapsulation submodule is used to encapsulate the function name and the comment into a comment prompt.

[0151] In this embodiment of the present invention, the code feature extraction module 603 includes:

[0152] An abstract syntax tree generation submodule, configured to convert the source code into an intermediate representation code and generate an abstract syntax tree of the intermediate representation code;

[0153] A function call relationship acquisition submodule, used to acquire the function call relationship of the intermediate representation code;

[0154] An extension submodule, configured to extend the abstract syntax tree using the function call relationship to obtain an extended abstract syntax tree, wherein the extended abstract syntax tree includes a plurality of nodes;

[0155] A position extraction submodule, configured to extract the position of the code element corresponding to each node in the intermediate representation code;

[0156] An operation code generation submodule, configured to obtain a built-in function of the intermediate representation function and generate an operation code for the built-in function;

[0157] A sequence number extraction submodule, configured to obtain edges between nodes in the extended abstract syntax tree, and extract node numbers at both ends of the edges and the sequence numbers of the edges;

[0158] A code feature generation submodule is used to generate a code feature of the source code using the position, the operation code and the sequence number.

[0159] In the embodiment of the present invention, Figure 1 The consistency judgment module 605 includes:

[0160] a first similarity score calculation submodule, configured to calculate a first similarity score between the comment embedding and the code embedding;

[0161] a function feature generation submodule, configured to concatenate the comment embedding, the code embedding, and the first similarity score to obtain a function feature;

[0162] The judgment submodule is used to input the function features into a random forest-based classifier and output a judgment result on whether there is an intention inconsistency.

[0163] In an embodiment of the present invention, the training process of the pre-trained embedding model includes:

[0164] A code comment pair acquisition module is used to acquire a code comment pair; the code comment pair includes a code sample and a comment sample;

[0165] A code embedding result generating module, configured to input the code sample into a first embedding model to be trained to obtain a code embedding result;

[0166] An annotation embedding result generating module, configured to input the annotation sample into a second embedding model to be trained to obtain an annotation embedding result;

[0167] A first-stage pre-trained embedding model generation module is configured to adjust the first embedding model to be trained and the second embedding model to be trained using the code embedding result, the comment embedding result, and a preset first loss function to obtain a first-stage pre-trained code embedding model and a first-stage pre-trained comment embedding model;

[0168] A variation data generating module, configured to generate variation data of the code sample;

[0169] A second-stage pre-trained code embedding model generation module is configured to input the code sample and the variant data into the first-stage pre-trained code embedding model respectively, and obtain the second-stage pre-trained code embedding model by training with a preset second loss function;

[0170] A fine-tuning module is used to fine-tune the first-stage pre-trained annotation embedding model and the second-stage pre-trained code embedding model by presetting a third loss function to obtain a pre-trained embedding model.

[0171] An embodiment of the present invention further provides an electronic device, comprising a processor and a memory.

[0172] The memory is used to store program code and transmit the program code to the processor;

[0173] The processor is configured to execute the distributed application program according to the instructions in the program code. Figure 1 Consistency identification method.

[0174] The embodiment of the present invention further provides a computer-readable storage medium for storing program code, wherein the program code is used to execute the distributed application program described in the embodiment of the present invention. Figure 1 Consistency identification method.

[0175] Those skilled in the art will clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0176] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.

[0177] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, apparatuses, or computer program products. Thus, embodiments of the present invention may take the form of entirely hardware embodiments, entirely software embodiments, or embodiments combining software and hardware aspects. Furthermore, embodiments of the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROMs, optical storage, etc.) containing computer-usable program code.

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

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

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

[0181] Although the preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they become aware of the basic creative concepts. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present invention.

[0182] Finally, it should be noted that, in this document, relational terms such as first and second, etc., are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the terms "comprises," "comprising," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or terminal device that includes a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or terminal device. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of additional identical elements in the process, method, article, or terminal device that includes the element.

[0183] As described above, the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that the technical solutions described in the above embodiments can still be modified, or some of the technical features thereof can be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A distributed application intent consistency identification method, characterized in that: include: Obtaining source code of a distributed application and annotations of the source code; generating an annotation prompt for the annotation; extracting code features of the source code through an intermediate representation language; Converting the annotation prompts into annotation embeddings and converting the code features into code embeddings through a pre-trained embedding model; Determining the intent consistency of the distributed application based on the annotation embedding and the code embedding; The step of generating the annotation prompt includes: Get the function name of the function corresponding to the comment; Encapsulate the function name and the comment into a comment prompt; The step of extracting code features of the source code using an intermediate representation language includes: Converting the source code into an intermediate representation code and generating an abstract syntax tree of the intermediate representation code; Obtaining a function call relationship of the intermediate representation code; Expanding the abstract syntax tree using the function call relationship to obtain an extended abstract syntax tree, where the extended abstract syntax tree includes a plurality of nodes; Extract the code element corresponding to each node at the position of the intermediate representation code; Obtaining a built-in function of the intermediate representation code and generating an operation code of the built-in function; Obtaining edges of nodes in the extended abstract syntax tree, and extracting node numbers at both ends of the edges and the sequence number of the edges; generating a code signature of the source code using the position, the operation code, and the sequence number; The training process of the pre-trained embedding model includes two stages of pre-training, including contrastive learning and variation analysis. After pre-training, the model is fine-tuned using labeled data, specifically including: Obtain a code comment pair; the code comment pair includes a code sample and a comment sample; Inputting the code sample into a first embedding model to be trained to obtain a code embedding result; Inputting the annotation sample into a second embedding model to be trained to obtain an annotation embedding result; The first embedding model to be trained and the second embedding model to be trained are adjusted using the code embedding result, the comment embedding result and a preset first loss function to obtain a first-stage pre-trained code embedding model and a first-stage pre-trained comment embedding model; generating mutation data of the code sample; Inputting the code sample and the variant data into the first-stage pre-trained code embedding model respectively, and training with a preset second loss function to obtain a second-stage pre-trained code embedding model; The first-stage pre-trained annotation embedding model and the second-stage pre-trained code embedding model are fine-tuned by presetting a third loss function to obtain a pre-trained embedding model.

2. The method according to claim 1, characterized in that The step of determining the consistency of the intention of the distributed application according to the annotation embedding and the code embedding includes: calculating a first similarity score between the comment embedding and the code embedding; Concatenating the comment embedding, the code embedding, and the first similarity score to obtain a function feature; The function features are input into a random forest-based classifier, and a judgment result of whether there is inconsistent intention is output.

3. A distributed application intent consistency identification device, characterized in that: include: A source code and annotation acquisition module, used to acquire the source code of the distributed application and the annotations of the source code; An annotation prompt generating module, used for generating an annotation prompt for the annotation; A code feature extraction module, configured to extract code features of the source code using an intermediate representation language; An embedding conversion module, configured to convert the annotation prompts into annotation embeddings and convert code features into code embeddings using a pre-trained embedding model; an intention consistency judgment module, configured to judge the intention consistency of the distributed application based on the annotation embedding and the code embedding; The annotation prompt generation module includes: Function name acquisition submodule, used to obtain the function name corresponding to the annotation; An annotation prompt encapsulation submodule, used for encapsulating the function name and the annotation into an annotation prompt; Wherein, the code feature extraction module includes: An abstract syntax tree generation submodule, configured to convert the source code into an intermediate representation code and generate an abstract syntax tree of the intermediate representation code; A function call relationship acquisition submodule, used to acquire the function call relationship of the intermediate representation code; An extension submodule, configured to extend the abstract syntax tree using the function call relationship to obtain an extended abstract syntax tree, wherein the extended abstract syntax tree includes a plurality of nodes; A position extraction submodule, configured to extract the position of the code element corresponding to each node in the intermediate representation code; An opcode generation submodule, configured to obtain a built-in function of the intermediate representation code and generate an opcode for the built-in function; A sequence number extraction submodule, configured to obtain edges between nodes in the extended abstract syntax tree, and extract node numbers at both ends of the edges and the sequence numbers of the edges; a code feature generating submodule, configured to generate a code feature of the source code using the position, the operation code, and the sequence number; The training process of the pre-trained embedding model includes two stages of pre-training, including contrastive learning and variation analysis. After pre-training, the model is fine-tuned using labeled data, specifically including: A code comment pair acquisition module is used to acquire a code comment pair; the code comment pair includes a code sample and a comment sample; A code embedding result generating module, configured to input the code sample into a first embedding model to be trained to obtain a code embedding result; An annotation embedding result generating module, configured to input the annotation sample into a second embedding model to be trained to obtain an annotation embedding result; A first-stage pre-trained embedding model generation module is configured to adjust the first embedding model to be trained and the second embedding model to be trained using the code embedding result, the comment embedding result, and a preset first loss function to obtain a first-stage pre-trained code embedding model and a first-stage pre-trained comment embedding model; A variation data generating module, configured to generate variation data of the code sample; A second-stage pre-trained code embedding model generation module is configured to input the code sample and the variant data into the first-stage pre-trained code embedding model respectively, and obtain the second-stage pre-trained code embedding model by training with a preset second loss function; A fine-tuning module is used to fine-tune the first-stage pre-trained annotation embedding model and the second-stage pre-trained code embedding model by presetting a third loss function to obtain a pre-trained embedding model.

4. An electronic device, characterized in that: The device includes a processor and a memory: The memory is used to store program code and transmit the program code to the processor; The processor is used to execute the distributed application intention consistency identification method described in any one of claims 1-2 according to the instructions in the program code.

5. A computer-readable storage medium, characterized in that The computer-readable storage medium is used to store program code, and the program code is used to execute the distributed application intent consistency identification method described in any one of claims 1-2.

Citation Information

Patent Citations

  • Code segment annotation generation method and system and readable storage medium

    CN113065322A

  • Automated test script generation from voice or speech in natural language based on code comments

    IN201841002384A