A method for generating smart contract code annotations based on information retrieval

Through information retrieval-based methods, combined with semantic, lexical and grammatical similarities, the most similar code segments in the smart contract corpus are found and their annotations are reused, which solves the security vulnerability problem caused by code cloning in smart contracts and improves code readability and understandability, and reduces computing resource consumption.

CN113743062BActive Publication Date: 2024-09-03NANTONG UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110937182.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-08-16
Publication Date
2024-09-03
Estimated Expiration
2041-08-16

AI Technical Summary

Technical Problem

Security vulnerabilities caused by code cloning in smart contracts, especially missing or mismatched code comments, lead to poor code readability and understandability, and existing deep learning-based methods consume large computing resources and are difficult to implement simple and fast Comment generation.

Method used

Using an information retrieval-based method, through the fusion of semantic, lexical and syntactic similarity, CodeBert and BERT-whitening are used to extract code semantic features, combined with FAISS library and Jaccard similarity calculation, to find the most similar code segments in the corpus and reuse their comments. Simplified annotation generation process.

Benefits of technology

The readability and understandability of the smart contract code are improved, and the quality of the generated annotations exceeds the baseline method. The method is simple and fast, the consumption of computing resources is reduced, and the training speed can be increased by more than twice.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113743062B
    Figure CN113743062B
Patent Text Reader

Abstract

The present invention provides a method for generating smart contract code comments based on information retrieval, comprising the following steps: (1) collecting a smart contract corpus from Etherscan.io and performing a preprocessing operation on the corpus; (2) extracting code semantic features using CodeBert, performing key feature extraction and dimensionality reduction through BERT‑whitening, calculating semantic similarity, and selecting the top k most semantically similar code snippets as candidate codes; (3) regarding the code snippets as a set composed of word units, and calculating the lexical similarity between the sets through Jaccard similarity; (4) considering the abstract syntax tree information of the smart contract code, calculating the grammatical similarity between AST sequences through edit distance; (5) calculating a mixed score according to the ratio of lexical and grammatical similarity fusion, and finding the most similar code snippet from the candidate code for generating code comments. The beneficial effects of the present invention are: high-quality code comments can be generated, and the readability and comprehensibility of the smart contract code can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a method for generating smart contract code annotations based on information retrieval. Background Art

[0002] Smart contracts are programs that automatically process traditional contracts in the form of computer instructions. They can be automatically executed when certain predefined conditions are met and are widely used. Unlike traditional software development, smart contracts do not change once deployed. Therefore, errors in smart contracts can be difficult to fix and require great costs. To avoid these errors, smart contract developers are encouraged to reuse code snippets from reliable sources such as OpenZeppelin. As a result, code clones frequently appear in smart contracts and the proportion of clones continues to increase over time. However, previous studies have found that about 10% of security vulnerabilities in smart contracts are caused by code cloning, among which the abuse of uncommented code is the main reason.

[0003] High-quality code comments help improve the readability and understandability of smart contract code. Therefore, they play an important role in the development and maintenance of smart contracts. However, in the field of smart contracts, code comments are often missing or do not match the actual semantics of the code due to limited project development budgets, developers' lack of programming experience, or failure to synchronize comments when cloning code. Therefore, it is necessary to design an effective automatic code comment generation method for smart contracts.

[0004] Recent studies have modeled code comment generation as a neural machine translation problem, proposed deep learning-based methods and achieved good results. However, these methods usually require a lot of computing resources to train the model. Therefore, it is necessary to explore a simple, fast, effective and lightweight method to solve the problem of code comment generation for smart contracts.

[0005] How to solve the above technical problems becomes the subject faced by the present invention. Summary of the invention

[0006] The purpose of the present invention is to provide a method for generating smart contract code annotations based on information retrieval, which can generate corresponding high-quality code annotations based on smart contract code snippets.

[0007] The idea of ​​the present invention is: the present invention proposes a method for generating smart contract code annotations based on information retrieval, that is, by integrating semantic, lexical and grammatical similarities, the most similar code segment in the corpus is found for the target code segment based on information retrieval, and then the annotation of the most similar code segment is reused as the annotation of the target code segment. The method of the present invention achieves better performance than the benchmark method in the same corpus.

[0008] The present invention is implemented by the following measures: a method for generating smart contract code annotations based on information retrieval, which includes the following steps:

[0009] (1) Collect the smart contract corpus from Etherscan.io, perform preprocessing on the corpus, and obtain dataset D. The format of dataset D is set to <method, comment>. The dataset contains 57,676 <method, comment> pairs from 40,932 smart contracts. The specific preprocessing operation includes the following steps:

[0010] (1-1) First consider the regular function methods and modifiers, and delete the methods without comments;

[0011] (1-2) Use the first sentence of the comment as the actual comment, as this usually describes the function of the method;

[0012] (1-3) Remove <method, comment> pairs with less than 4 words, because these <method, comment> pairs are not suitable for the subsequent BLEU-4 score evaluation;

[0013] (1-4) Remove duplicate pairs in the corpus;

[0014] (2) CodeBert and BERT-whitening operations are used to extract code semantic information and calculate the semantic similarity between the target code segment and the code segments in the training set. The specific steps include the following:

[0015] (2-1) For a given smart contract code segment, first split it according to the camel case naming rules to obtain the input sequence;

[0016] (2-2) Input the sequence into CodeBert, extract the hidden states of the first and last layers in the output, and average them to obtain the semantic feature vector of the code segment;

[0017] (2-3) Use BERT-whitening to process semantic vectors, extract key features and reduce dimensionality through linear transformation;

[0018] (2-4) The semantic similarity between the code segments is derived by calculating the L2 distance between the semantic vectors of the target code segment and the code segments in the training set;

[0019] (3) Using the FAISS library as a vector retrieval tool, the candidate similar codes ranked in the top k in the training set are selected based on semantic similarity;

[0020] (4) Calculate the lexical similarity between code segments. Consider the code segments as a set of tokens. Then, calculate the similarity between the two sets using a statistical method. Specifically, the steps are as follows:

[0021] (4-1) First, split the code sequence according to the camel case naming rule;

[0022] (4-2) performing a deduplication operation on the code sequence to obtain a word set of the code fragment;

[0023] (4-3) Calculate the similarity between sets using Jaccard similarity;

[0024] (5) Calculate the syntactic similarity between code segments, taking into account the abstract syntax tree (AST) information of the smart contract code, which specifically includes the following steps:

[0025] (5-1) Use solidity-parser-antlr to convert the smart contract code into the corresponding abstract syntax tree (ASTs);

[0026] (5-2) Use the SimSBT method to traverse the type nodes of the AST tree to obtain an ordered sequence;

[0027] (5-3) Calculate the edit distance between ordered sequences to obtain their similarity;

[0028] (6) After calculating the mixed score based on the ratio of lexical and grammatical similarity fusion, select the most similar code from the candidate codes generated in the previous step (3) and reuse its comments as the corresponding comments of the new code.

[0029] As a further optimization scheme of the method for generating smart contract code annotations based on information retrieval provided by the present invention, the step (2) uses BERT-whitening in machine learning to further process the semantic vector, that is, the semantic vector extracted by CodeBert is subjected to key feature extraction and dimensionality reduction through linear transformation, thereby reducing the storage space, improving the model retrieval speed, and the training speed can be increased by more than two times.

[0030] The parameter settings of this information retrieval-based smart contract code annotation generation method are as follows:

[0031] Dimension K of BERT-whitening operation: 256;

[0032] Mixing fraction coefficient λ: 0.7.

[0033] Compared with the prior art, the beneficial effects of the present invention are as follows: a smart contract code comment generation method based on information retrieval proposed in the present invention takes into account the three aspects of semantic similarity, lexical similarity and grammatical similarity of the code. Through the information retrieval method, the most similar code segments in the code library are found, and their comments are reused. Compared with the previous deep learning-based and information retrieval-based code comment generation methods, this method can achieve better performance and generate higher quality code comments, which helps to improve the readability and comprehensibility of the smart contract code and facilitate the development and maintenance of smart contracts. Moreover, due to the reuse of code comments in smart contracts, the lightweight method based on information retrieval proposed in the present invention is simpler, faster and more effective than the code comment generation method based on deep learning. BRIEF DESCRIPTION OF THE DRAWINGS

[0034] The accompanying drawings are used to provide further understanding of the present invention and constitute a part of the specification. They are used to explain the present invention together with the embodiments of the present invention and do not constitute a limitation of the present invention.

[0035] Figure 1 A system framework diagram of a smart contract code annotation generation method based on information retrieval provided by the present invention. DETAILED DESCRIPTION

[0036] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. Of course, the specific embodiments described here are only used to explain the present invention and are not used to limit the present invention.

[0037] Example 1

[0038] See also Figure 1 As shown, the present invention provides a method for generating smart contract code annotations based on information retrieval, which specifically includes the following contents:

[0039] (1) We collected the smart contract corpus from Etherscan.io, which contains 40,932 Ethereum smart contracts written in the Solidity programming language, with a total of 93,146 regular methods, 73,533 modifiers, and 12,482 fallback methods.

[0040] (2) The corpus is preprocessed, including removing methods without comments and deleting duplicate pairs. The final corpus contains 57,676 <method, comment> pairs from 40,932 smart contracts. Tables 1, 2, and 3 show the detailed statistics of the corpus code length, AST length, and comment length, respectively.

[0041] Table 1

[0042]

[0043] Table 2

[0044]

[0045] Table 3

[0046]

[0047] (3) To ensure a fair comparison with the baseline method, 1,159 pairs of data were extracted from the corpus as the test set, and the rest of the corpus was used as the training set, based on the data partitioning method of previous studies.

[0048] (4) Calculate code semantic similarity: Use CodeBert to extract code semantic features, and use BERT-whitening to extract key features and reduce dimensionality, calculate semantic similarity, and select the top k most semantically similar code snippets as candidate codes.

[0049] (4-1) Split the code segment according to the camel case naming rules to obtain the input sequence Where M is the length of the code sequence. Then it is input into CodeBert, the hidden states of the first and last layers in the output are extracted, and the average is taken to obtain the code X∈R d The semantic feature vector of CodeBert is , where d is the hidden size of CodeBert. Finally, the set of smart contract code line vectors in the training set is obtained. Where N is the size of the training set;

[0050] (4-2) We further use BERT-whitening in machine learning to process semantic vectors. Linear transformation is used to extract key features and reduce dimensionality. The semantic similarity between code segments is derived by calculating the L2 distance between the semantic vectors of the target code segment and the code segment in the training set;

[0051] (4-3) Use the FAISS library as a vector retrieval tool to select the top k candidate similar codes in the training set based on semantic similarity;

[0052] (5) Calculate lexical similarity: The code snippet is regarded as a set of word units, and the similarity between the sets is calculated using Jaccard similarity.

[0053] (5-1) First, split the code sequence according to the camel case naming rule;

[0054] (5-2) performing a deduplication operation on the code sequence to obtain a word-unit set of the code fragment;

[0055] (5-3) Calculate the similarity between sets using Jaccard similarity;

[0056] (6) Calculate syntax similarity: Consider the abstract syntax tree (AST) information of the smart contract code and calculate the similarity between AST sequences through edit distance.

[0057] (6-1) Use solidity-parser-antlr to convert the smart contract code into the corresponding abstract syntax tree (ASTs);

[0058] (6-2) Use the SimSBT method to traverse the type nodes of the AST tree to obtain an ordered sequence;

[0059] (6-3) Calculate the edit distance between ordered sequences to obtain their similarity;

[0060] (7) After calculating the mixed score based on the weighted sum of the lexical and grammatical similarities, find the most similar code snippet from the candidate codes and reuse its code comments.

[0061] (8) The optimal parameter settings of this method are as follows:

[0062] Dimension K of BERT-whitening operation: 256;

[0063] Coefficient λ for mixing lexical and grammatical similarity scores: 0.7.

[0064] (9) The proposed method and existing code comment generation methods are evaluated on the same corpus, and three performance indicators (i.e., BLEU, METEOR, and ROUGE-L) from the field of neural machine translation are used to automatically evaluate the quality of the generated comments:

[0065] Table 4 Comparison of results of the method of the present invention and other methods

[0066]

[0067]

[0068] Experiments have shown that the method for generating smart contract code annotations based on information retrieval proposed in the present invention can generate higher quality code annotations compared to 8 advanced benchmark methods based on deep learning and information retrieval. Specifically, the method of the present invention integrates the semantic, lexical and grammatical information of the code, and can surpass these most advanced benchmark methods. Among them, for BLEU-1 / 2 / 3 / 4, the method of the present invention can at least improve the performance by 6.18%, 9.86%, 12.76% and 14.85% respectively; for METEOR, the method of the present invention improves the performance by at least 8.20%; for ROUGE-L, the method of the present invention can at least improve the performance by 4.87%. These results show the competitiveness of the method proposed in the present invention.

[0069] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims

1. A method for generating smart contract code annotations based on information retrieval, characterized in that: The following steps are involved: (1) Collect the smart contract corpus from Etherscan.io, perform preprocessing on the corpus, and obtain dataset D. The format of dataset D is set to <method, comment>. The dataset contains 57,676 <method, comment> pairs from 40,932 smart contracts; (2) CodeBert is used to extract code semantic features, and BERT-whitening is used to extract key features and reduce dimensionality, calculate semantic similarity, and select the top k most semantically similar code snippets as candidate codes; (3) The code snippets are regarded as a set of tokens, and the lexical similarity between the sets is calculated using Jaccard similarity; (4) Considering the abstract syntax tree information of the smart contract code, the syntax similarity between AST sequences is calculated by editing distance; (5) After calculating the mixed score based on the weighted sum of the lexical and grammatical similarities, find the most similar code snippet from the candidate codes and reuse its code comments.

2. The method for generating smart contract code annotations based on information retrieval according to claim 1, characterized in that: In the step (2), the code semantic information is extracted by CodeBert and BERT-whitening operations, which specifically includes the following steps: (2-1) For a given smart contract code segment, first split it according to the camel case naming rules to obtain the input sequence; (2-2) Input the sequence into CodeBert, extract the hidden states of the first and last layers in the output, and average them to obtain the semantic feature vector of the code segment; (2-3) Use BERT-whitening to process semantic vectors, extract key features and reduce dimension through linear transformation; (2-4) The semantic similarity between the code segments is derived by calculating the L2 distance between the semantic vectors of the target code segment and the code segments in the training set; (2-5) Use the FAISS library as a vector retrieval tool and select the top k candidate similar codes in the training set based on semantic similarity.

3. The method for generating smart contract code annotations based on information retrieval according to claim 1, characterized in that: The step (3) of calculating the lexical similarity between the code snippets specifically includes the following steps: (3-1) First, split the code sequence according to the camel case naming rule; (3-2) performing a deduplication operation on the code sequence to obtain a word set of the code fragment; (3-3) The similarity between sets is calculated using Jaccard similarity.

4. The method for generating smart contract code annotations based on information retrieval according to claim 1, characterized in that: The step (4) of calculating the grammatical similarity between the code snippets specifically includes the following steps: (4-1) Use solidity-parser-antlr to convert the smart contract code into the corresponding abstract syntax tree (ASTs); (4-2) Use the SimSBT method to traverse the type nodes of the AST tree to obtain an ordered sequence; (4-3) Calculate the edit distance between ordered sequences to obtain their similarity.

5. The method for generating smart contract code annotations based on information retrieval according to claim 1, characterized in that: The step (5) calculates a mixed score based on a weighted sum of the proportions of the lexical and grammatical similarities, finds the most similar code snippet from the candidate codes, and reuses its code comments; The optimal parameter settings of the information retrieval-based smart contract code comment generation method are as follows: Dimension K of BERT-whitening operation: 256; Coefficient λ for mixing lexical and grammatical similarity scores: 0.7.