Smart contract code completion method based on deep neural network and global cache
Through deep neural network and global cache technology, using target identifier prediction model and multi-layer perceptron, combined with identifier name and type probability, the problem of IDE's inability to understand contract dependencies in smart contracts is solved, and the accuracy of code completion is improved.
Patent Information
- Application Number
- CN202410241249.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-03-04
- Publication Date
- 2025-10-03
- Estimated Expiration
- 2044-03-04
AI Technical Summary
Existing integrated development environments (IDEs) cannot fully understand the dependencies between multiple contracts when writing smart contracts, resulting in reduced code completion accuracy.
Using deep neural networks and global cache technology, code completion is performed through target identifier prediction models and multi-layer perceptrons, combined with identifier name and type probabilities.
Improved the accuracy of smart contract code completion, reduced syntax errors, and resolved the issue of poor prediction performance for long-distance and custom identifiers.
Smart Images

Figure CN118092889B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a smart contract code completion method based on deep neural network and global cache. Background Art
[0002] An integrated development environment (IDE) is a software application that provides an all-in-one development environment, including a code editor, debugger, compiler, and other tools for convenient software development. The goal of an IDE is to improve developer efficiency by providing a range of collaborative tools and features to simplify the process of writing, testing, and debugging code. Code completion is a key feature in an IDE, designed to improve developer efficiency by automatically popping up suggestions or completing code snippets as the developer types, based on the context and possible choices.
[0003] Solidity, one of the mainstream programming languages for writing smart contracts, often involves complex contexts, including state variables, functions, and events. In such contexts, intelligent code completion becomes even more complex, as the IDE must understand the entire contract structure and functionality. Furthermore, in large projects, multiple smart contracts may depend on each other. However, because code completion is typically performed within the context of the current file, the IDE cannot fully understand the dependencies between all contracts, resulting in reduced completion accuracy. Summary of the Invention
[0004] The purpose of this application is to solve at least one of the above-mentioned technical defects, especially the technical defect in the prior art that the IDE cannot fully understand the dependencies between all contracts, resulting in a decrease in the accuracy of code completion.
[0005] This application provides a code completion method, which includes:
[0006] Obtaining code data including a code to be completed, and determining a target identifier prediction model; wherein the code to be completed is composed of multiple identifiers;
[0007] Inputting the code data into the target identifier prediction model to obtain the predicted name probability and predicted type probability of the next identifier of the code to be completed output by the target identifier prediction model, and inputting the code data into a pre-trained target multi-layer perceptron to obtain the predicted position probability of the next identifier of the code to be completed output by the multi-layer perceptron;
[0008] Probabilistically integrating the predicted location probability and the predicted type probability, and weighting the integration result with the predicted name probability to obtain an adjusted result;
[0009] A next identifier of the code to be completed is determined based on the adjustment result, and code completion is performed on the code to be completed according to the next identifier.
[0010] Optionally, obtaining code data containing the code to be completed includes:
[0011] Get the code to be completed;
[0012] Using a lexical analyzer to convert each identifier in the code to be completed into an identifier name, and sorting each identifier name according to the order of the identifiers in the code to be completed to obtain an identifier name file;
[0013] Parsing the code to be completed to obtain an abstract syntax tree of the code to be completed, and replacing each identifier name in the identifier name file with a corresponding identifier type based on the abstract syntax tree to generate an identifier type file; wherein the abstract syntax tree records a mapping relationship between each identifier name and identifier type in the code to be completed;
[0014] The code data of the code to be completed is generated according to the code to be completed, the identifier name file and the identifier type file.
[0015] Optionally, determining a target identifier prediction model includes:
[0016] Constructing an initial identifier prediction model and obtaining training data for the initial identifier prediction model; the training data includes sample code data and real identifier results of the sample code data;
[0017] Inputting the training data into a preset initial identifier prediction model to obtain a prediction probability result output by the initial identifier prediction model;
[0018] Training the initial identifier prediction model with the goal of making the predicted probability result close to the true identifier result of the sample code data;
[0019] When the initial identifier prediction model meets the preset training conditions, the trained initial identifier prediction model is used as the target identifier prediction model.
[0020] Optionally, the constructing of an initial prediction identifier prediction model includes:
[0021] Obtain historical code data from the blockchain platform and determine the identifier name file and identifier type file corresponding to each source code in the historical code data;
[0022] Building a name vocabulary based on each identifier name file, and building a type vocabulary based on each identifier type file;
[0023] Storing the name vocabulary and the type vocabulary in a recurrent neural network respectively, and obtaining a name prediction sub-model corresponding to the name vocabulary and a type prediction sub-model corresponding to the type vocabulary;
[0024] The name prediction sub-model and the type prediction sub-model are cascaded to obtain an initial prediction identifier prediction model.
[0025] Optionally, the target identifier prediction model includes a name prediction sub-model and a type prediction sub-model;
[0026] Inputting the code data into the target identifier prediction model to obtain a predicted name probability and a predicted type probability of the next identifier of the code to be completed output by the target identifier prediction model includes:
[0027] Inputting the identifier name file into the name prediction sub-model to obtain the predicted name probability of the next identifier of the code to be completed output by the name prediction sub-model;
[0028] Furthermore, the identifier type file is input into the type prediction sub-model to obtain the predicted type probability of the next identifier of the code to be completed output by the type prediction sub-model.
[0029] Optionally, the training process of the target multi-layer perceptron includes:
[0030] Obtaining a sample code to be completed, and determining a feature vector corresponding to the sample code to be completed;
[0031] Inputting the feature vector into a preset initial multi-layer perceptron to obtain a predicted position probability output by the initial multi-layer perceptron;
[0032] Inputting the predicted position probability into the objective function of the initial multilayer perceptron to obtain a first value output by the objective function, and inputting the first output value into a back propagation algorithm of the initial multilayer perceptron to obtain a second value output by the back propagation algorithm;
[0033] The parameters of the initial multilayer perceptron are adjusted according to the second value until the initial multilayer perceptron meets the preset training conditions, and the trained initial multilayer perceptron is used as the target multilayer perceptron.
[0034] Optionally, determining the next identifier of the code to be completed based on the adjustment result includes:
[0035] Determining the probability of each identifier in the preset identifier table according to the adjustment result;
[0036] Sort each identifier according to each probability to obtain a sorting result;
[0037] The identifier with the highest probability in the sorting result is used as the next identifier of the code to be completed.
[0038] This application also provides a code completion device, including:
[0039] A data acquisition module, configured to acquire code data containing a code to be completed and determine a target identifier prediction model; wherein the code to be completed is composed of a plurality of identifiers;
[0040] a probability prediction module, configured to input the code data into the target identifier prediction model to obtain a predicted name probability and a predicted type probability of the next identifier of the code to be completed output by the target identifier prediction model, and to input the code data into a pre-trained target multi-layer perceptron to obtain a predicted position probability of the next identifier of the code to be completed output by the multi-layer perceptron;
[0041] a probability adjustment module, configured to perform probability integration on the predicted location probability and the predicted type probability, and perform weight adjustment on the integration result and the predicted name probability to obtain an adjustment result;
[0042] A code completion module is configured to determine a next identifier of the code to be completed based on the adjustment result, and perform code completion on the code to be completed according to the next identifier.
[0043] The present application also provides a storage medium, in which computer-readable instructions are stored. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the code completion method described in any one of the above embodiments.
[0044] The present application also provides a computer device, comprising: one or more processors, and a memory;
[0045] The memory stores computer-readable instructions, and when the computer-readable instructions are executed by the one or more processors, the steps of the code completion method described in any one of the above embodiments are performed.
[0046] It can be seen from the above technical solutions that the embodiments of the present application have the following advantages:
[0047] The smart contract code completion method based on deep neural network and global cache provided by the present application can, when completing the code to be completed, first obtain code data containing the code to be completed, and determine the target identifier prediction model, so as to obtain basic data for code completion; wherein, the code to be completed is composed of multiple identifiers; then the code data can be input into the target identifier prediction model to obtain the predicted name probability and predicted type probability of the next identifier of the code to be completed output by the target identifier prediction model, and the code data can be input into the target multi-layer perceptron trained in advance to obtain the predicted position probability of the next identifier of the code to be completed output by the multi-layer perceptron, and then the predicted position probability and the predicted type probability can be probabilistically integrated, and the integration result and the predicted name probability can be weighted to obtain an adjusted result, and finally, based on the adjustment result, the next identifier of the code to be completed is determined, and the code to be completed is completed according to the next identifier. This application can use a multi-layer perceptron to build a global cache when completing code, and combine it with code identifier types for prediction to increase prediction accuracy and reduce the probability of syntax errors. Finally, combined with the prediction of identifier names, it can solve the problem of poor prediction performance for long-distance identifiers and custom identifiers in the code to be completed, thereby understanding the dependencies between all contracts and improving the accuracy of code completion. BRIEF DESCRIPTION OF THE DRAWINGS
[0048] In order to more clearly illustrate the embodiments of the present application 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 application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0049] Figure 1 A flowchart of a code completion method provided in an embodiment of the present application;
[0050] Figure 2 A schematic diagram of the architecture of an identifier probability prediction method provided in an embodiment of the present application;
[0051] Figure 3 A schematic diagram of a flow chart of a code completion device provided in an embodiment of the present application;
[0052] Figure 4 A schematic diagram of the internal structure of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0053] The following will be combined with the drawings in the embodiments of this application to clearly and completely describe the technical solutions in the embodiments of this application. Obviously, the embodiments described are only part of the embodiments of this application, not all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.
[0054] Solidity, one of the mainstream programming languages for writing smart contracts, often involves complex contexts, including state variables, functions, and events. In such contexts, intelligent code completion becomes even more complex, as the IDE must understand the entire contract structure and functionality. Furthermore, in large projects, multiple smart contracts may depend on each other. However, because code completion is typically performed within the context of the current file, the IDE cannot fully understand the dependencies between all contracts, resulting in reduced completion accuracy.
[0055] Based on this, this application proposes the following technical solutions, please refer to the following for details:
[0056] In one embodiment, Figure 1 As shown, Figure 1 A flowchart of a code completion method provided in an embodiment of the present application is provided; the present application also provides a code completion method, which specifically includes the following:
[0057] S110: Obtain code data including the code to be completed, and determine a target identifier prediction model.
[0058] In this step, when the code completion system completes the code to be completed, it can first obtain the code data of the code to be completed, and determine the target identifier prediction model corresponding to the code to be completed, so that the code completion system can determine the next identifier of the code to be completed through the code data and the target identifier prediction model.
[0059] It is understandable that in computer programming, identifiers refer to the basic elements in a programming language, which may include keywords, identifiers, operators, and separators, etc. Therefore, the code to be completed in this application refers to a small section of source code composed of multiple identifiers, each of which represents a grammatical unit in the code to be completed, such as a variable name, a function name, an operator, a bracket, etc. In addition, programming languages have specific syntax and structure when they are written, and the code must be written according to its syntax and structure in order to run. Therefore, when the code completion system completes the code to be completed, it can identify the syntax and structure of the code to be completed through the code data of the code to be completed, and then generate the next identifier of the code to be completed to achieve code completion of the code to be completed.
[0060] Specifically, after obtaining the code data, the code completion system can first determine the code category of the code to be completed, and then determine the target identifier prediction model corresponding to the code to be completed based on the code type. The target identifier prediction model obtained in this way has a code completion mode specific to the code category, so it can focus on the characteristics of the code type, better meet the needs of the code category, and thus improve the model's understanding of the code category and the accuracy of code completion.
[0061] For example, in blockchain-based smart contracts, smart contracts can be divided into multiple categories based on their functions and application areas. Common smart contract code categories include token contracts, DeFi contracts, NFT contracts, etc. Each smart contract code category has its own specific conditions, rules, and execution logic. Therefore, this application can train a corresponding target identifier prediction model for each smart contract code category, thereby reducing the model's confusion with other code types.
[0062] S120: Input the code data into the target identifier prediction model to obtain the predicted name probability and predicted type probability of the next identifier of the code to be completed output by the target identifier prediction model, and input the code data into the target multi-layer perceptron trained in advance to obtain the predicted position probability of the next identifier of the code to be completed output by the multi-layer perceptron.
[0063] In this step, after obtaining the code data and the target identifier prediction model through step S110, the code completion system can input the code data into the target identifier prediction model to obtain the predicted name probability and predicted type probability of the next identifier of the code to be completed output by the target identifier prediction model. At the same time, the code completion system can also input the code data into the target multi-layer perceptron trained in advance to obtain the predicted position probability of the next identifier of the code to be completed output by the multi-layer perceptron.
[0064] It can be understood that a queue is maintained in the cache of the code completion system of the present application, which orderly stores information such as the type, name, line number and position in the line of multiple declared identifiers in the program. Therefore, when using the target identifier prediction model and the multi-layer perceptron to predict the next identifier of the code to be completed, all the identifier types, identifier names and other information in the cache can also be spliced into a feature vector and used as one of the input data of the target identifier prediction model and the multi-layer perceptron. Then, based on the output of the target identifier prediction model and the multi-layer perceptron, the probability prediction of each identifier in the cache being the next identifier of the code to be completed can be obtained.
[0065] Among them, the predicted name probability refers to the predicted probability that the next identifier name is each word in the name vocabulary preset in the model. The predicted type probability refers to the predicted probability that the next identifier name is each word in the type vocabulary preset in the model. The predicted position probability refers to the predicted probability that the next identifier is the identifier at the i-th position in the cache, which can be expressed as a vector consistent with the cache size. For example, when the cache size is 3, the predicted position probability can be 0.1, 0.5, and 0.4, respectively representing the predicted probabilities corresponding to the three positions in the cache, and the sum is 1. Among them, i is a positive integer greater than zero.
[0066] S130: Probabilistically integrating the predicted location probability and the predicted type probability, and weight-adjusting the integration result and the predicted name probability to obtain an adjusted result.
[0067] In this step, after obtaining the predicted name probability, predicted type probability and predicted position probability through step S120, the code completion system can probabilistically integrate the predicted position probability and the predicted type probability to obtain an integrated result, thereby reducing the probability of grammatical errors in the code completion result; then the code completion system can weight the integrated result and the predicted name probability to obtain an adjusted result of the prediction result of the next identifier of the code to be completed, thereby solving the problem of general prediction performance of long-distance identifiers and custom identifiers.
[0068] Specifically, the expression for the probability integration of the predicted position probability and the predicted type probability is as follows:
[0069]
[0070] Where p i Represents the integrated result of predicted location probability and predicted type probability; o i represents the predicted position probability; o it represents the probability of the predicted type; Represents the sum of the predicted probabilities for each identifier type in the cache.
[0071] Furthermore, the expression for weight adjustment of the integration result and the predicted name probability is as follows:
[0072] o token =λ×p i +(1-λ)×p l ,
[0073]
[0074] In the formula, o token represents the weighted result of the integration result and the predicted name probability; λ represents the adjustment weight; p irepresents the integration result; p l represents the predicted name probability; ∑o represents the sum of the predicted probabilities of all identifier names in the cache; p token represents the integrated results and the adjusted results of the predicted name probabilities.
[0075] S140: Determine the next identifier of the code to be completed based on the adjustment result, and perform code completion on the code to be completed according to the next identifier.
[0076] In this step, after obtaining the adjustment result in step S130, the code completion system may determine the next identifier of the code to be completed based on the adjustment result, and perform code completion on the code to be completed according to the next identifier.
[0077] It is understood that when the code completion system completes the code to be completed, it may add a single identifier or multiple consecutive identifiers to the code to be completed, without limitation. When multiple consecutive identifiers need to be added, the code completion system may, after determining the next identifier of the code to be completed, add the next identifier to the end of the last identifier in the code to be completed, thereby forming a new code to be completed, and then re-predict the next identifier until all identifiers in the code to be completed are completed.
[0078] In the above embodiment, when completing the code to be completed, the code data containing the code to be completed can be obtained first, and the target identifier prediction model can be determined to obtain the basic data for code completion; wherein, the code to be completed is composed of multiple identifiers; then the code data can be input into the target identifier prediction model to obtain the predicted name probability and predicted type probability of the next identifier of the code to be completed output by the target identifier prediction model, and the code data can be input into the target multi-layer perceptron trained in advance to obtain the predicted position probability of the next identifier of the code to be completed output by the multi-layer perceptron, and then the predicted position probability and the predicted type probability can be probabilistically integrated, and the integration result and the predicted name probability are weighted to obtain the adjusted result, and finally the next identifier of the code to be completed is determined based on the adjusted result, and the code to be completed is completed according to the next identifier. When completing the code, the present application can use the multi-layer perceptron to build a global cache, and combine the code identifier type for prediction to increase the accuracy of the prediction and reduce the probability of grammatical errors. Finally, combined with the prediction of the identifier name, it can solve the problem of general prediction performance of long-distance identifiers and custom identifiers in the code to be completed, and improve the accuracy of the completed code.
[0079] In one embodiment, obtaining code data including the code to be completed in step S110 may include:
[0080] S111: Obtain the code to be completed.
[0081] S112: using a lexical analyzer to convert each identifier in the code to be completed into an identifier name, and sorting each identifier name according to the order of the identifiers in the code to be completed to obtain an identifier name file.
[0082] S113: Parse the code to be completed to obtain an abstract syntax tree of the code to be completed, and replace each identifier name in the identifier name file with a corresponding identifier type based on the abstract syntax tree to generate an identifier type file.
[0083] S114: Generate code data of the code to be completed according to the code to be completed, the identifier name file, and the identifier type file.
[0084] In this embodiment, when obtaining code data, the code completion system can first obtain the code to be completed, and can use a lexical analyzer to convert each identifier in the code to be completed into an identifier name, and sort each identifier name according to the order of identifiers in the code to be completed to obtain an identifier name file; then the code completion system can parse the code to be completed to obtain an abstract syntax tree of the code to be completed, and replace each identifier name in the identifier name file with a corresponding identifier type based on the abstract syntax tree to generate an identifier type file; finally, the code completion system can generate code data for the code to be completed based on the code to be completed, the identifier name file and the identifier type file.
[0085] It is understood that the identifier name refers to the name of the identifier, which is used to represent the name of an entity such as a variable, function, or contract; while the identifier type refers to the type of the identifier, which is used to represent the type of an entity such as a variable, function, or contract. Therefore, this application completes the code by combining the identifier name and identifier type of the code to be completed. This can solve the problem of poor prediction performance for long-range identifiers and custom identifiers in the code to be completed, thereby understanding the dependencies between all contracts and improving the accuracy of the completed code.
[0086] For example, in Solidity source code, identifier names are typically strings consisting of letters, numbers, and underscores, and must begin with a letter or underscore. They can be used to identify and reference different entities in the code. Identifier types, on the other hand, are typically strings consisting of keywords or user-defined types, used to represent the semantics and function of the identifier. Therefore, identifier names and identifier types together constitute the syntax and semantics of Solidity.
[0087] Specifically, when determining the identifier names of each identifier in the code to be completed, the code completion system can first determine the lexical analyzer corresponding to the code type of the code to be completed, and use the lexical analyzer to scan the code to be completed, identifying identifiers such as keywords, identifiers, constants, operators, and separators therein. Then, a unique identifier name can be assigned to each identifier based on a pre-set name vocabulary. Furthermore, the present application can specify the grammatical rules and lexical rules corresponding to the code type of the code to be completed in tools such as lex, ANTLR, and JFlex, thereby generating a lexical analyzer. The specific process is not limited here.
[0088] In addition, after obtaining the identifier name file for the code to be completed, the code completion system can also use a syntax parser to parse the code to be completed into an abstract syntax tree. For example, for Solidity source code, the code completion system can use solidity-parser-antlr as the syntax parser. The abstract syntax tree records the mapping relationship between each identifier name and identifier type in the code to be completed. Based on the abstract syntax tree, the code completion system can replace each identifier name in the identifier name file with the corresponding identifier type, thereby generating an identifier type file.
[0089] In one embodiment, determining the target identifier prediction model in step S110 may include:
[0090] S115: Construct an initial identifier prediction model and obtain training data for the initial identifier prediction model; the training data includes sample code data and real identifier results of the sample code data.
[0091] S116: Input the training data into a preset initial identifier prediction model to obtain a prediction probability result output by the initial identifier prediction model.
[0092] S117: The initial identifier prediction model is trained with the goal of making the predicted probability result close to the true identifier result of the sample code data.
[0093] S118: When the initial identifier prediction model meets the preset training conditions, the trained initial identifier prediction model is used as the target identifier prediction model.
[0094] In this embodiment, when determining the target identifier prediction model, the code completion system of the present application can first construct an initial identifier prediction model and obtain training data for the initial identifier prediction model; wherein the training data includes sample code data and the real identifier result of the sample code data; then the code completion system can input the training data into the preset initial identifier prediction model to obtain the predicted probability result output by the initial identifier prediction model, and train the initial identifier prediction model with the goal of making the predicted probability result close to the real identifier result of the sample code data; when the initial identifier prediction model meets the preset training conditions, the trained initial identifier prediction model can be used as the target identifier prediction model.
[0095] It can be understood that the target identifier prediction model of the present application refers to a model that predicts the identifier probability of the code to be completed in the input sample code data and obtains the predicted probability result. When the target identifier prediction model is training the model, multiple different sample code data can be used as training samples, and each training sample is labeled with a sample label, that is, a real identifier result consisting of an identifier name and an identifier type. When all the training samples are labeled, the training samples with sample labels can be input into the pre-built initial identifier prediction model for forward propagation to train the model, and the model parameters can be tuned during the back propagation process of the model. When the model meets certain training conditions or parameter convergence conditions, such as the number of iterations reaches the set value, the training is considered to be completed. At this time, the trained model can be used as the final target identifier prediction model.
[0096] Furthermore, the present application divides the training data into multiple training data sets based on code type, with each training data set corresponding to a code type. This allows for the training of target identifier prediction models corresponding to multiple different code types. This allows for the customization of the code completion function within the code completion system to better meet the needs of specific contract categories and improve the quality of code completion. Once the target identifier prediction model training is complete, the code completion system can also store each target identifier prediction model so that the subsequent code completion system can directly call the corresponding target identifier prediction model to perform code completion operations on the code to be completed.
[0097] In one embodiment, building the initial prediction identifier prediction model in step S115 may include:
[0098] S1151: Obtain historical code data from the blockchain platform, and determine the identifier name file and identifier type file corresponding to each source code in the historical code data.
[0099] S1152: Construct a name vocabulary based on each identifier name file, and construct a type vocabulary based on each identifier type file.
[0100] S1153: The name vocabulary and the type vocabulary are stored in the recurrent neural network respectively, and a name prediction sub-model corresponding to the name vocabulary and a type prediction sub-model corresponding to the type vocabulary are obtained.
[0101] S1154: Cascade the name prediction sub-model and the type prediction sub-model to obtain an initial prediction identifier prediction model.
[0102] In this embodiment, when constructing the initial predictive identifier prediction model, the code completion system can obtain historical code data from the blockchain platform and determine the identifier name file and identifier type file corresponding to each source code in the historical code data. Then, a name vocabulary can be constructed based on each identifier name file, and a type vocabulary can be constructed based on each identifier type file, so that the code completion system can store the name vocabulary and the type vocabulary in the recurrent neural network respectively, and obtain a name prediction sub-model corresponding to the name vocabulary and a type prediction sub-model corresponding to the type vocabulary. Finally, the code completion system can cascade the name prediction sub-model with the type prediction sub-model to obtain the initial predictive identifier prediction model.
[0103] It is understood that a blockchain platform refers to a platform that provides blockchain technology support and services, including Ethereum, Hyperledger, EOS, Corda, and TRON. Blockchain platforms typically include components such as blockchain infrastructure, development tools, application programming interfaces (APIs), and smart contracts, helping developers quickly build and deploy blockchain applications. Therefore, this application can obtain publicly available source code from blockchains as historical code data.
[0104] For example, the code completion system can obtain publicly available smart contract source code data from the Ethereum blockchain platform and then use popular Ethereum security analysis tools such as Mythril, Securify, Oyente, and Slither to test this data and identify secure smart contracts. Mythril, an officially recommended smart contract security analysis tool from Ethereum, uses symbolic execution to detect various security vulnerabilities in smart contracts and is integrated into IDEs such as Remix and Truffle. Securify, a security analysis tool developed by the ChainSecurity team, supports detection of 38 different vulnerability types. These tools utilize novel context-sensitive static analysis methods to more comprehensively identify potential security risks and ensure the security of historical code data.
[0105] Furthermore, after acquiring smart contracts from the blockchain platform, the code completion system can categorize them by their functions and application areas, generating historical code data corresponding to various code types. When constructing the initial predictive identifier prediction model, the code completion system can first determine the model's build goal and then select historical code data corresponding to the corresponding code type based on that build goal.
[0106] Specifically, when constructing a name vocabulary, the code completion system can traverse the identifier names in each identifier name file and, after counting the number of occurrences of each identifier name, filter out identifier names that appear more than a preset number of times to form a name vocabulary. This method can effectively reduce dimensionality and sparsity and accelerate model prediction. The construction process of the type vocabulary is consistent with that of the name vocabulary and will not be repeated here.
[0107] In addition, the recurrent neural network selected by this application when constructing the initial prediction identifier prediction model can be LSTM (Long Short-Term Memory). LSTM is a deep learning model for processing sequence data. It is a variant of the recurrent neural network (RNN) and is designed to solve the long-term dependency problem in RNN, enabling it to better capture and remember information in long sequences. Therefore, the LSTM selected by this application can process the contextual correlation and data dependency between the identifier at the current position and the previous identifier in the code to be completed, thereby capturing deeper semantic associations to provide more targeted content.
[0108] In one embodiment, in step S120, the code data is input into the target identifier prediction model to obtain the predicted name probability and predicted type probability of the next identifier of the code to be completed output by the target identifier prediction model, which may include:
[0109] S121: Input the identifier name file into the name prediction sub-model to obtain the predicted name probability of the next identifier of the code to be completed output by the name prediction sub-model.
[0110] S122: Input the identifier type file into the type prediction sub-model to obtain the predicted type probability of the next identifier of the code to be completed output by the type prediction sub-model.
[0111] In this embodiment, since the target identifier prediction model is obtained by cascading the name prediction sub-model and the type prediction sub-model, the code completion system can input the identifier name file into the name prediction sub-model to obtain the predicted name probability of the next identifier in the code to be completed, as output by the name prediction sub-model; and input the identifier type file into the type prediction sub-model to obtain the predicted type probability of the next identifier in the code to be completed, as output by the type prediction sub-model.
[0112] It is understood that when training the target identifier prediction model, the present application can train the name prediction sub-model and the type prediction sub-model separately, and then cascade the trained name prediction sub-model and type prediction sub-model. Therefore, the name prediction sub-model and the type prediction sub-model are independent of each other and do not affect each other, so that when the code completion system uses the target identifier prediction model to predict the identifier probability of the code to be completed, it can reduce the error rate of the target identifier prediction model's prediction.
[0113] In one embodiment, the training process of the target multi-layer perceptron in step S120 may include:
[0114] S123: Obtain a sample code to be completed, and determine a feature vector corresponding to the sample code to be completed.
[0115] S124: Input the feature vector into a preset initial multi-layer perceptron to obtain the predicted position probability output by the initial multi-layer perceptron.
[0116] S125: Input the predicted position probability into the objective function of the initial multi-layer perceptron to obtain a first value of the output of the objective function, and input the first output value into the back propagation algorithm of the initial multi-layer perceptron to obtain a second value of the output of the back propagation algorithm.
[0117] S126: Adjust the parameters of the initial multilayer perceptron according to the second value until the initial multilayer perceptron meets the preset training conditions, and use the trained initial multilayer perceptron as the target multilayer perceptron.
[0118] In this embodiment, when the present application trains the target multi-layer perceptron, the code completion system can first obtain the sample code to be completed and determine the feature vector corresponding to the sample code to be completed. Then, the feature vector can be input into the preset initial multi-layer perceptron to obtain the predicted position probability output by the initial multi-layer perceptron. After that, the code completion system can input the predicted position probability into the objective function of the initial multi-layer perceptron to obtain the first numerical value of the output of the objective function, and input the first output value into the back propagation algorithm of the initial multi-layer perceptron to obtain the second numerical value output by the back propagation algorithm. Finally, the code completion system can adjust the parameters of the initial multi-layer perceptron according to the second numerical value until the initial multi-layer perceptron meets the preset training conditions, and the trained initial multi-layer perceptron is used as the target multi-layer perceptron.
[0119] The feature vector refers to a vector generated based on features such as the type, name, line number, and position of the sample code to be completed.
[0120] In addition, this application takes into account the general performance of neural network models in completing long-distance identifiers and custom identifiers when making predictions. In the code completion system, a target multi-layer perceptron is introduced. This multi-target multi-layer perceptron introduces one or more hidden layers based on a single-layer neural network. The hidden layer contains several hidden units, and the hidden layer and output layer in the perceptron are both fully connected layers. Therefore, this application can build a global cache through this target multi-layer perceptron to improve the accuracy of code completion.
[0121] In one embodiment, determining the next identifier of the code to be completed based on the adjustment result in step S140 may include:
[0122] S141: Determine the probability of each identifier in the preset identifier table according to the adjustment result.
[0123] S142: Sort the identifiers according to the probabilities to obtain a sorting result.
[0124] S143: The identifier with the highest probability in the sorting result is used as the next identifier of the code to be completed.
[0125] In this embodiment, when determining the next identifier of the code to be completed, the code completion system can first determine the probability of each identifier in the preset identifier table based on the adjustment result, and sort the identifiers in descending order according to the respective probabilities to obtain a sorting result. Then, the code completion system can use the identifier with the highest probability in the sorting result as the next identifier of the code to be completed.
[0126] In order to better explain the code completion method of this application, the following Figure 2To further illustrate, schematically, Figure 2 As shown, Figure 2 A schematic diagram of the architecture of an identifier probability prediction method provided in an embodiment of the present application.
[0127] Figure 2 In [1], after obtaining smart contracts from Ethereum, the code completion system can filter out safe smart contracts from the obtained smart contracts and divide the filtered smart contracts according to their functions or application scenarios to obtain the corresponding source code. The code completion system can then determine the Token name file (identifier name file) and Token type file (identifier type file) of the source code and input these two files into the corresponding LSTM models (name prediction sub-model and type prediction sub-model) respectively to obtain the probability prediction results corresponding to the Token name file and the probability prediction results corresponding to the Tokentype file. At the same time, the code completion system can also obtain the cache element probability distribution through the global cache (target multi-layer perceptron) of the source code, so that the code completion system can probabilistically integrate the probability prediction results corresponding to the Token type file and the cache element probability distribution, and weight the integration result with the probability prediction result corresponding to the Token name file. Finally, the adjusted result can be used as the weighted probability prediction of the next token in the source code. The probability prediction results obtained in this way can solve the problem of poor prediction performance of long-distance identifiers and custom identifiers in the source code, and improve the accuracy of the prediction results of the next identifier in the source code.
[0128] The following describes a code completion device provided in an embodiment of the present application. The code completion device described below and the code completion method described above can refer to each other.
[0129] In one embodiment, Figure 3 As shown, Figure 3 This is a flow chart of a code completion device provided in an embodiment of the present application. The present application also provides a code completion device, including a data acquisition module 210, a probability prediction module 220, a probability adjustment module 230, and a code completion module 240, specifically including the following:
[0130] The data acquisition module 210 is used to acquire code data including the code to be completed and determine a target identifier prediction model.
[0131] The probability prediction module 220 is used to input the code data into the target identifier prediction model to obtain the predicted name probability and predicted type probability of the next identifier of the code to be completed output by the target identifier prediction model, and to input the code data into the target multi-layer perceptron trained in advance to obtain the predicted position probability of the next identifier of the code to be completed output by the multi-layer perceptron.
[0132] The probability adjustment module 230 is used to perform probability integration on the predicted location probability and the predicted type probability, and perform weight adjustment on the integration result and the predicted name probability to obtain an adjustment result.
[0133] The code completion module 240 is configured to determine a next identifier of the code to be completed based on the adjustment result, and perform code completion on the code to be completed according to the next identifier.
[0134] In the above embodiment, when completing the code to be completed, the code data containing the code to be completed can be obtained first, and the target identifier prediction model can be determined to obtain the basic data for code completion; wherein, the code to be completed is composed of multiple identifiers; then the code data can be input into the target identifier prediction model to obtain the predicted name probability and predicted type probability of the next identifier of the code to be completed output by the target identifier prediction model, and the code data can be input into the target multi-layer perceptron trained in advance to obtain the predicted position probability of the next identifier of the code to be completed output by the multi-layer perceptron, and then the predicted position probability and the predicted type probability can be probabilistically integrated, and the integration result and the predicted name probability are weighted to obtain the adjusted result, and finally the next identifier of the code to be completed is determined based on the adjusted result, and the code to be completed is completed according to the next identifier. When completing the code, the present application can use the multi-layer perceptron to build a global cache, and combine the code identifier type for prediction to increase the accuracy of the prediction and reduce the probability of grammatical errors. Finally, combined with the prediction of the identifier name, it can solve the problem of general prediction performance of long-distance identifiers and custom identifiers in the code to be completed, and improve the accuracy of the completed code.
[0135] In one embodiment, the data acquisition module 210 may include:
[0136] Code acquisition submodule, used to obtain the code to be completed;
[0137] The first file generation submodule is used to convert each identifier in the code to be completed into an identifier name by using a lexical analyzer, and sort the identifier names according to the identifier order in the code to be completed to obtain an identifier name file.
[0138] The second file generation submodule is used to parse the code to be completed, obtain the abstract syntax tree of the code to be completed, replace each identifier name in the identifier name file with the corresponding identifier type based on the abstract syntax tree, and generate an identifier type file.
[0139] The data generation submodule is used to generate code data of the code to be completed according to the code to be completed, the identifier name file and the identifier type file.
[0140] In one embodiment, the data acquisition module 210 may further include:
[0141] The model building submodule is used to build an initial identifier prediction model and obtain training data for the initial identifier prediction model; the training data includes sample code data and the real identifier results of the sample code data.
[0142] The model prediction submodule is used to input the training data into the preset initial identifier prediction model to obtain the prediction probability result output by the initial identifier prediction model.
[0143] The model training submodule is used to train the initial identifier prediction model with the goal of making the predicted probability results close to the true identifier results of the sample code data.
[0144] The model generation submodule is used to use the trained initial identifier prediction model as the target identifier prediction model when the initial identifier prediction model meets the preset training conditions.
[0145] In one embodiment, the model building submodule may include:
[0146] The historical data acquisition unit is used to obtain historical code data from the blockchain platform and determine the identifier name file and identifier type file corresponding to each source code in the historical code data.
[0147] The vocabulary building unit is used to build a name vocabulary based on each identifier name file, and to build a type vocabulary based on each identifier type file.
[0148] The sub-model generation unit is used to store the name vocabulary and the type vocabulary in the recurrent neural network respectively, and obtain a name prediction sub-model corresponding to the name vocabulary and a type prediction sub-model corresponding to the type vocabulary.
[0149] The sub-model cascading unit is used to cascade the name prediction sub-model and the type prediction sub-model to obtain an initial prediction identifier prediction model.
[0150] In one embodiment, the probability prediction module 220 may include:
[0151] The first prediction submodule is used to input the identifier name file into the name prediction submodel to obtain the predicted name probability of the next identifier of the code to be completed output by the name prediction submodel.
[0152] The second prediction submodule is used to input the identifier type file into the type prediction submodel to obtain the predicted type probability of the next identifier of the code to be completed output by the type prediction submodel.
[0153] In one embodiment, the probability prediction module 220 may further include:
[0154] The vector determination submodule is used to obtain the sample code to be completed and determine the feature vector corresponding to the sample code to be completed.
[0155] The position prediction submodule is used to input the feature vector into a preset initial multi-layer perceptron to obtain the predicted position probability output by the initial multi-layer perceptron.
[0156] The vector calculation submodule is used to input the predicted position probability into the objective function of the initial multi-layer perceptron to obtain the first output value of the objective function, and input the first output value into the back propagation algorithm of the initial multi-layer perceptron to obtain the second output value of the back propagation algorithm.
[0157] The parameter adjustment submodule is used to adjust the parameters of the initial multilayer perceptron according to the second value until the initial multilayer perceptron meets the preset training conditions, and then use the trained initial multilayer perceptron as the target multilayer perceptron.
[0158] In one embodiment, the code completion module 240 may include:
[0159] The probability determination submodule is used to determine the probability of each identifier in the preset identifier table according to the adjustment result.
[0160] The identifier sorting submodule is used to sort the identifiers according to the probabilities to obtain a sorting result.
[0161] The identifier selection submodule is used to select the identifier with the highest probability in the sorting result as the next identifier of the code to be completed.
[0162] In one embodiment, the present application also provides a storage medium storing computer-readable instructions. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the code completion method described in any of the above embodiments.
[0163] In one embodiment, the present application further provides a computer device having computer-readable instructions stored therein. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the code completion method as described in any one of the above embodiments.
[0164] Schematically, as Figure 4 As shown, Figure 4 This is a schematic diagram of the internal structure of a computer device provided in an embodiment of the present application. The computer device 300 can be provided as a server. Figure 4 Computer device 300 includes a processing component 302, which further includes one or more processors, and a memory resource represented by memory 301 for storing instructions executable by processing component 302, such as an application. The application stored in memory 301 may include one or more modules, each corresponding to a set of instructions. In addition, processing component 302 is configured to execute the instructions to perform the code completion method of any of the above embodiments.
[0165] The computer device 300 may further include a power supply component 303 configured to perform power management of the computer device 300, a wired or wireless network interface 304 configured to connect the computer device 300 to a network, and an input / output (I / O) interface 305. The computer device 300 may operate based on an operating system stored in the memory 301, such as Windows Server™, Mac OS X™, Unix™, Linux™, Free BSD™, or the like.
[0166] Those skilled in the art will understand that Figure 4 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.
[0167] 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 device comprising 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 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 device comprising the element.
[0168] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The various embodiments can be combined as needed, and the same or similar parts can be referenced to each other.
[0169] The above description of the disclosed embodiments is intended to enable one skilled in the art to implement or use the present application. Various modifications to these embodiments will be readily apparent to one skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application is not limited to the embodiments shown herein, but is intended to conform to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A code completion method, characterized in that: The method comprises: Obtaining code data including a code to be completed, and determining a target identifier prediction model; wherein the code to be completed is composed of multiple identifiers; Inputting the code data into the target identifier prediction model to obtain the predicted name probability and predicted type probability of the next identifier of the code to be completed output by the target identifier prediction model, and inputting the code data into a pre-trained target multi-layer perceptron to obtain the predicted position probability of the next identifier of the code to be completed output by the multi-layer perceptron; Probabilistically integrating the predicted location probability and the predicted type probability, and weighting the integration result with the predicted name probability to obtain an adjusted result; Determining a next identifier of the code to be completed based on the adjustment result, and performing code completion on the code to be completed according to the next identifier; The expression for the probability integration of the predicted position probability and the predicted type probability includes the following: ; Where, Represents the integrated result of predicted location probability and predicted type probability; represents the predicted location probability; represents the probability of the predicted type; represents the sum of the predicted probabilities of each identifier type in the cache; The expression for weight adjustment of the integration result and the predicted name probability includes the following: ; Where, represents the weighted result of the integration result and the predicted name probability; Indicates adjustment weight; Indicates the integration result; represents the predicted name probability; represents the sum of the predicted probabilities of all identifier names in the cache; represents the integrated results and the adjusted results of the predicted name probabilities.
2. The code completion method according to claim 1, wherein: The obtaining of code data containing the code to be completed includes: Get the code to be completed; Using a lexical analyzer to convert each identifier in the code to be completed into an identifier name, and sorting each identifier name according to the order of the identifiers in the code to be completed to obtain an identifier name file; Parsing the code to be completed to obtain an abstract syntax tree of the code to be completed, and replacing each identifier name in the identifier name file with a corresponding identifier type based on the abstract syntax tree to generate an identifier type file; wherein the abstract syntax tree records a mapping relationship between each identifier name and identifier type in the code to be completed; The code data of the code to be completed is generated according to the code to be completed, the identifier name file and the identifier type file.
3. The code completion method according to claim 1, wherein: Determining a target identifier prediction model includes: Constructing an initial identifier prediction model and obtaining training data for the initial identifier prediction model; the training data includes sample code data and real identifier results of the sample code data; Inputting the training data into a preset initial identifier prediction model to obtain a prediction probability result output by the initial identifier prediction model; Training the initial identifier prediction model with the goal of making the predicted probability result close to the true identifier result of the sample code data; When the initial identifier prediction model meets the preset training conditions, the trained initial identifier prediction model is used as the target identifier prediction model.
4. The code completion method according to claim 3, wherein: The constructing of the initial identifier prediction model includes: Obtain historical code data from the blockchain platform and determine the identifier name file and identifier type file corresponding to each source code in the historical code data; Building a name vocabulary based on each identifier name file, and building a type vocabulary based on each identifier type file; Storing the name vocabulary and the type vocabulary in a recurrent neural network respectively, and obtaining a name prediction sub-model corresponding to the name vocabulary and a type prediction sub-model corresponding to the type vocabulary; The name prediction sub-model is cascaded with the type prediction sub-model to obtain an initial prediction identifier prediction model.
5. The code completion method according to claim 2, wherein: The target identifier prediction model includes a name prediction sub-model and a type prediction sub-model; Inputting the code data into the target identifier prediction model to obtain a predicted name probability and a predicted type probability of the next identifier of the code to be completed output by the target identifier prediction model includes: Inputting the identifier name file into the name prediction sub-model to obtain the predicted name probability of the next identifier of the code to be completed output by the name prediction sub-model; Furthermore, the identifier type file is input into the type prediction sub-model to obtain the predicted type probability of the next identifier of the code to be completed output by the type prediction sub-model.
6. The code completion method according to claim 1, wherein: The training process of the target multi-layer perceptron includes: Obtaining a sample code to be completed, and determining a feature vector corresponding to the sample code to be completed; Inputting the feature vector into a preset initial multi-layer perceptron to obtain a predicted position probability output by the initial multi-layer perceptron; Inputting the predicted position probability into the objective function of the initial multilayer perceptron to obtain a first value output by the objective function, and inputting the first value into a back propagation algorithm of the initial multilayer perceptron to obtain a second value output by the back propagation algorithm; The parameters of the initial multilayer perceptron are adjusted according to the second value until the initial multilayer perceptron meets the preset training conditions, and the trained initial multilayer perceptron is used as the target multilayer perceptron.
7. The code completion method according to claim 1, wherein: The determining the next identifier of the code to be completed based on the adjustment result includes: Determining the probability of each identifier in the preset identifier table according to the adjustment result; Sort each identifier according to each probability to obtain a sorting result; The identifier with the highest probability in the sorting result is used as the next identifier of the code to be completed.
8. A code completion device, characterized in that: include: A data acquisition module, configured to acquire code data containing a code to be completed and determine a target identifier prediction model; wherein the code to be completed is composed of a plurality of identifiers; a probability prediction module, configured to input the code data into the target identifier prediction model to obtain a predicted name probability and a predicted type probability of the next identifier of the code to be completed output by the target identifier prediction model, and to input the code data into a pre-trained target multi-layer perceptron to obtain a predicted position probability of the next identifier of the code to be completed output by the multi-layer perceptron; a probability adjustment module, configured to perform probability integration on the predicted location probability and the predicted type probability, and perform weight adjustment on the integration result and the predicted name probability to obtain an adjustment result; a code completion module, configured to determine a next identifier of the code to be completed based on the adjustment result, and perform code completion on the code to be completed according to the next identifier; The expression for probability integration of the predicted position probability and the predicted type probability in the probability adjustment module includes the following: ; Where, Represents the integrated result of predicted location probability and predicted type probability; represents the predicted location probability; represents the probability of the predicted type; represents the sum of the predicted probabilities of each identifier type in the cache; The expression for weight adjustment of the integration result and the predicted name probability in the probability adjustment module includes the following: ; Where, represents the weighted result of the integration result and the predicted name probability; Indicates adjustment weight; Indicates the integration result; represents the predicted name probability; represents the sum of the predicted probabilities of all identifier names in the cache; represents the integrated results and the adjusted results of the predicted name probabilities.
9. A storage medium, characterized in that: The storage medium stores computer-readable instructions, which, when executed by one or more processors, cause the one or more processors to perform the steps of the code completion method according to any one of claims 1 to 7.
10. A computer device, characterized in that: include: one or more processors, and memory; The memory stores computer-readable instructions, which, when executed by the one or more processors, perform the steps of the code completion method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Method and device for generating fine-grained code template based on deep learning
CN115495085A
Automatic program repairing method and system based on large language model and completion engine
CN117130645A