A dual-channel smart contract defect detection method integrating multi-view features

Through the dual-channel smart contract defect detection method integrating multi-view features, combining syntax tree, execution logic, text, annotation and gas consumption value characteristics, the problem of a single detection angle of the existing detection method is solved, achieving more accurate and comprehensive defects Test.

CN118779236BActive Publication Date: 2025-02-11NANJING AUDIT UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202410929666.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-11
Publication Date
2025-02-11
Estimated Expiration
2044-07-11

AI Technical Summary

Technical Problem

The existing smart contract defect detection methods have a single detection angle, which can easily lead to missed or false alarms. In addition, deep learning technology mainly stays at the text semantic level and does not consider the cost of code execution.

Method used

Using a dual-channel smart contract defect detection method that incorporates multi-view features, it is possible to extract syntax tree features, execution logic features, text features, annotation features and gas consumption value features, and weighted fusion to establish a dual-channel analysis model and consider the logic of the code. Function description and execution consumption loss.

Benefits of technology

It realizes more accurate and comprehensive smart contract defect detection, avoids the problem of too single analysis angle, and improves the detection efficiency and accuracy of results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118779236B_ABST
    Figure CN118779236B_ABST
Patent Text Reader

Abstract

The present invention discloses a dual-channel smart contract defect detection method incorporating multi-view features, step 1, extracting syntax tree features, execution logic features, and text features from the smart contract code, and weighted fusion of the three extracted feature vectors to obtain code features. The present invention not only establishes a dual-channel analysis code from the text and visual perspectives by incorporating a dual-channel smart contract defect detection model with multi-view features, avoiding the problem of too single an analysis perspective, but also adds the annotation content and gas consumption value of the smart contract code to the perspective of code defect detection, which can consider the logical function description and execution consumption loss of the code. Defect detection is no longer just detecting code defects, but also considers the consumption of gas value and annotation description, so that the present application is novel and comprehensive in identifying smart contract codes, with high efficiency and more accurate detection results.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention specifically relates to a dual-channel smart contract defect detection method incorporating multi-view features. Background Art

[0002] Smart contracts are automated execution protocols designed in programming languages. They are decentralized, tamper-proof, and transparent. They are widely used in decentralized finance, supply chain management, copyright, and intellectual property, and have very good application prospects. Smart contract defect detection can discover potential vulnerabilities and security risks in contracts, reduce the risk of contracts being attacked or exploited, improve contract quality, and promote innovation and the safe application of smart contract technology. At present, smart contract defect detection mainly includes traditional non-deep learning detection methods and deep learning technology-based detection methods. Among them, traditional detection methods are mainly carried out from four perspectives: formal verification, static analysis, symbolic execution, and fuzz testing. The smart contract defect detection method based on deep learning technology treats the code as text and uses neural networks such as RNN and LSTM for defect detection;

[0003] However, the study found that although traditional detection methods can effectively detect defects, the evaluation angle is too single, which can easily lead to missed reports or false positives. The analysis angle of smart contract defect detection methods based on deep learning technology only stays at the text semantic level, and does not take into account the cost of code execution. When dealing with smart contract defect detection, the above detection methods are not comprehensive in detection angles, and the detection results are difficult to improve, which urgently needs to be improved.

[0004] Therefore, it is necessary to invent a dual-channel smart contract defect detection method that incorporates multi-view features to solve the above problems. Summary of the invention

[0005] The purpose of the present invention is to provide a dual-channel smart contract defect detection method that incorporates multi-view features. By integrating a dual-channel smart contract defect detection model that incorporates multi-view features, not only is a dual-channel analysis code established from a text and visual perspective, avoiding the problem of too single an analysis perspective, but the annotation content and gas consumption value of the smart contract code are also added to the perspective of code defect detection. The logical function description and execution consumption loss of the code can be considered, and defect detection is no longer just detecting code defects, so that the present application has a novel and comprehensive perspective when identifying smart contract codes, high efficiency, and more accurate detection results, so as to solve the above-mentioned deficiencies in the technology.

[0006] In order to achieve the above object, the present invention provides the following technical solution: a dual-channel smart contract defect detection method incorporating multi-view features, comprising the following steps:

[0007] Step 1: Perform syntax tree feature f on the smart contract code Set(i)ast , execute logical feature f elg , text features f code Extract and weight the three extracted feature vectors to obtain the code feature f codefeature ;

[0008] Step 2: Annotate the smart contract code Set(i) with feature f ann 、Gas consumption value characteristics f gas After extraction, the code feature f codefeature , annotation feature f ann 、Gas consumption value characteristics f gas Weighted fusion to obtain text semantic features f text_seman ;

[0009] Step 3: Perform visual syntax tree feature f on the smart contract code Set(i) visual_ast and visual execution logic features f visual_elg After extraction, all visual feature vectors are weighted and fused to obtain the visual semantic feature f visua_lseman ;

[0010] Step 4: Get the text semantic features f text_seman and visual semantic features f visua_lseman Weighted fusion to obtain a new feature vector f seman , the new feature vector f seman Modeling is performed to obtain a defect detection model;

[0011] Step 5: Obtain the smart contract data set Set, annotate the data, divide it into training set and test set, train the defect detection model, and obtain the defect detection result of the smart contract to be tested based on the trained defect detection model.

[0012] In the aforementioned dual-channel smart contract defect detection method incorporating multi-view features, in step 1, the syntax tree feature f is used to detect the smart contract code Set(i). ast , execute logical feature f elg , text features f code Extract and weight the three extracted feature vectors to obtain the code feature f codefeature , including the following steps:

[0013] 1.1 Syntax tree feature f ast Extraction of

[0014] 1.2. Execution logic feature f elg Extraction of

[0015] 1.3 Text features f code Extraction of

[0016] 1.4. Extracted syntax tree features f ast , execute logical feature f elg , and text features f code Perform linear concatenation and give weight ratios between features to obtain the code feature f codefeature , the specific formula is as follows:

[0017]

[0018] in, For vector concatenation,

[0019] w 1 is the syntax tree feature f ast The splicing weight,

[0020] w 2 To perform the logical feature f elg The splicing weight,

[0021] w 3 is the text feature f code The splicing weight,

[0022] b 1 It is an adjustable parameter.

[0023] In the aforementioned dual-channel smart contract defect detection method incorporating multi-view features, in step 1.1, the syntax tree feature f ast The specific steps for extraction are as follows:

[0024] 1.1.1. Construct smart contract tree structure data based on abstract syntax tree, use Word2Vec to embed the words from 1 to N nodes in the tree, and get the input feature x of each node k , input into the Tree-LSTM neural network;

[0025] Among them, the tree structure data is: TreeSet(node 1 , node 2 , ..., node k , ..., node N ),

[0026] Among them, 1≤k≤N, node 1 is the root node of the tree, node k For any node in the tree,

[0027] And node k The node's child node set is (node k,1 , node k,2 , ..., node k,n , ..., node k,M),

[0028] Among them, 1≤n≤M, node k,n For node k The nth child node of the kth node, (k,n) represents the nth child node of the kth node, and (k,M) represents the Mth child node of the kth node;

[0029] 1.1.2. Calculate the input gate i of each node k 、Forget gate k and output gate o k information;

[0030] Among them, the input gate i k The calculation formula is as follows:

[0031]

[0032] Among them, bi k is the bias top of the k-th node input gate,

[0033] Wi k is the weight of the kth node input gate,

[0034] Di k,n is the weight of the path from the kth node to all child nodes in the input gate, (k,n) represents the n child nodes of the kth node,

[0035] h k,n is the hidden layer information of the n child nodes of the kth node,

[0036] σ is the Sigmoid function,

[0037] x k is the input feature of the current k-th node;

[0038] Among them, the forget gate f k The calculation formula is as follows:

[0039]

[0040] Among them, bf k is the bias top of the forget gate of the kth node,

[0041] W k is the weight of the forget gate of the kth node,

[0042] Df k,n is the weight of the path from the kth node to all child nodes in the forget gate, (k,n) represents the n child nodes of the kth node;

[0043] Among them, the output gate o k The calculation formula is as follows:

[0044]

[0045] Among them, bo k is the bias top of the k-th node output gate,

[0046] I k is the weight of the output gate of the kth node,

[0047] Do k,n is the weight of the path from the kth node to all children in the output gate, (k,n) represents the n child nodes of the kth node;

[0048] 1.1.3. According to the input gate i k 、Forget gate k , calculate the cell state information c of the node k , the specific formula is as follows:

[0049]

[0050] Among them, tanh(·) is the activation function,

[0051] bu k is the bias top of the cell state information of the kth node,

[0052] Wu k is the weight of the cell state information of the kth node,

[0053] Du k,n is the weight of the path from node k to all child nodes in the cell state information, u represents the cell state, and (k,n) represents the n child nodes of the kth node;

[0054] 1.1.4, according to the obtained output gate o k , and cell state information c k , calculate the hidden layer vector h of the kth node N , the specific formula is as follows:

[0055]

[0056] 1.1.5. From the leaf node in the tree N (k=N) Start recursive calculation layer by layer to get node N The input gate i N 、Forget gate N , output gate o N and the hidden layer vector h N Repeat steps 1.1.2-1.1.4 to calculate the input gate, forget gate, output gate and hidden layer vector of each node, and obtain the hidden layer vector h of each node N, the hidden layer vector representation of the root node is used as the syntax tree feature vector of the entire smart contract, that is, the syntax tree feature f ast , specifically expressed as:

[0057] f ast =h 1

[0058] Among them, h 1 is the hidden layer vector of the root node of the syntax tree;

[0059] In step 1.2, the logical feature f is executed elg The specific steps for extraction are as follows:

[0060] 1.2.1. Construct the smart contract execution logic structure diagram G = (V, E) based on the code execution process, where V is the set of vertices in the diagram and E is the relationship between the edges of the vertices;

[0061] 1.2.2. Concatenate the eigenvectors of n vertices to form an n×d-dimensional matrix H 0 , the relationship between the edges of the vertices builds an n×n dimensional adjacency matrix A;

[0062] 1.2.3、H 0 and A are input into the GCN neural network, and after iterative calculation, H is extracted (m) , the specific formula is as follows:

[0063]

[0064] Among them, the initial input is H 0 ,

[0065]

[0066] I is the identity matrix,

[0067] yes The degree matrix of

[0068] H is the feature of each layer,

[0069] σ is the ReLU function;

[0070] H (m) H 0 The vector obtained after performing m graph neural network calculations;

[0071] W (m-1) is the weight matrix calculated for the m-1th time;

[0072] 1.2.4. According to the calculation formula in step 1.2.3, after learning the l-layer graph convolutional neural network, the execution logic feature f can be obtained.elg , specifically expressed as:

[0073] f elg =H (l)

[0074] Where V = {v 1 ,v 2 ,...,v n}, its feature value is extracted by word2vec, and j is used i Represents the characteristics of the i-th node, which can be concatenated to obtain H 0 ,Right now

[0075] H 0 ={j 1 ,j 2 ,...,j n},

[0076] Where n is the number of vertices,

[0077] And E={<v i , v j >|v i , v j ∈V} is the set of relationships between each node, and the adjacency matrix A is a relationship matrix composed of 0s and 1s;

[0078] In step 1.3, the text feature f code Specifically, the code text corresponding to the smart contract code Set(i) is input into the Sentence-Bert model in units of lines as sentences, and the text features f of the smart contract code are extracted by the Sentence-Bert model. code .

[0079] In the aforementioned dual-channel smart contract defect detection method incorporating multi-view features, in step 2, the smart contract code Set(i) is annotated with feature f ann 、Gas consumption value characteristics f gas After extraction, the code feature f codefeature , annotation feature f ann 、Gas consumption value characteristics f gas Weighted fusion to obtain text semantic features f text_seman , including the following steps:

[0080] 2.1. Input the annotation text corresponding to the smart contract code Set(i) into the Sentence-Bert model, and use the Sentence-Bert model to extract the annotation features f of the smart contract ann ;

[0081] 2.2. Based on the gas consumption value of the smart contract code Set(i), direct coding is used to extract the gas consumption value feature f of the smart contract gas ;

[0082] 2.3. Extract the code features f codefeature , annotation feature f ann 、Gas consumption value characteristics f gas Weighted fusion, linear splicing, and giving weight ratios between features to obtain text semantic features f text_seman , the specific formula is as follows:

[0083]

[0084] Among them, λ 1 is the concatenation weight of the syntax tree feature,

[0085] λ 2 is the concatenation weight of the code feature,

[0086] λ 3 is the splicing weight of the annotation feature,

[0087] b 2 It is an adjustable parameter.

[0088] In the aforementioned dual-channel smart contract defect detection method incorporating multi-view features, in step 1.3 and step 2.1, the annotation text corresponding to the smart contract code Set(i) is input into the Sentence-Bert model to extract the text feature f code and annotation feature f ann Previously, the Sentence-Bert model was fine-tuned using the dataset. The specific steps were:

[0089] K1. Randomly select 2,000 smart contract code texts and comments. Each line of code is considered as a sentence, and the comments are divided into sentences according to periods.

[0090] K2, use cosine similarity calculation to determine the calculation result, and input the result as training data into the initial Sentence-BERT model for secondary fine-tuning;

[0091] K3. Divide each text into m sentences, and obtain the sentence vector Vec of each sentence by the Sentence-Bert model i , and then concatenate to get the feature vector f of the text text , specifically expressed as:

[0092] f text =[Vec 1 ,Vec 2 ,...,Vecm ].

[0093] In the aforementioned dual-channel smart contract defect detection method incorporating multi-view features, in step 3, the smart contract code Set(i) is subjected to visual syntax tree feature f visual_ast and visual execution logic features f visual_elg After extraction, all visual feature vectors are weighted and fused to obtain the visual semantic feature f visual_seman , including the following steps:

[0094] 3.1. Based on syntax tree feature f ast Get the syntax tree feature vector h for each code word i ;

[0095] 3.2. According to the visual image form of the tree structure, all the syntax tree feature vectors h i Splice and obtain the visual grammar tree image matrix visual_ast, divide the input visual grammar tree image matrix visual_ast into multiple groups, and input it into the Vision Transformers neural network to perform attention calculation to obtain multiple groups of attention mechanism vectors ast_head i , the specific formula is as follows:

[0096]

[0097] Among them, Q ast The key after linear transformation of the syntax tree feature input,

[0098] and

[0099] K ast The value after linear transformation of the syntax tree feature input.

[0100] and

[0101] V ast The query after linear transformation of the syntax tree feature input,

[0102] and

[0103] d k is the dimension of the word vector,

[0104] The visual syntax tree image matrix visual_ast is specifically:

[0105] [h 1 ,h 2 ,h 3 ,...,h N ]

[0106] Among them, the visual syntax tree image matrix visual_ast is processed by the image preprocessing module before being input into the Vision Transformers neural network, thereby obtaining multiple sets of attention mechanism vectors ast_head of the visual syntax tree i ;

[0107] 3.3、Multiple sets of attention mechanism vectors ast_head i The multi-head attention mechanism vector is obtained by splicing, and the visual syntax tree feature f is calculated based on the multi-head attention mechanism vector visual_ast , the specific formula is as follows:

[0108] f visual_ast =LayerNorm(FFN(Mhead ast )+Sublayer(FFN(Mhead ast )))

[0109] Among them, Mhead ast is the multi-head attention mechanism vector corresponding to the visual syntax tree image,

[0110] And Mhead ast =[ast_head 1 ,ast_head 2 ,...,ast_head h ]×W ast 0 ,

[0111] is the linear transformation matrix of the visual grammar tree,

[0112] FFN is a feed-forward neural network calculation.

[0113] Sublayer is the layer normalization process.

[0114] LayerNorm is the residual connection processing;

[0115] 3.4. Based on the execution logic feature f elg Get the execution logic feature vector H of each code word i ;

[0116] 3.5. Based on the visual image form of the graph structure, perform logical feature vectors H on all iSplice to obtain the visual execution logic image matrix visual_elg, divide the visual execution logic image matrix visual_elg into multiple groups, and input it into the Vision Transformers neural network to perform attention calculation to obtain multiple groups of attention mechanism vectors elg_head i , the specific formula is as follows:

[0117]

[0118] Among them, Q elg The key after performing linear transformation on the logical feature input.

[0119] and

[0120] K elg The value after performing linear transformation on the logical feature input.

[0121] and

[0122] V elg To perform linear transformation on the query of logical feature input,

[0123] and

[0124] The visual execution logic image matrix visual_elg is specifically:

[0125] [H 1 ,H 2 ,H 3 ,...,H N ]

[0126] Among them, the visual execution logic image matrix visual_elg is processed by the image preprocessing module before being input into the Vision Transformers neural network, so as to obtain multiple sets of visual execution logic image matrices elg_head of visual execution logic features. i ;

[0127] 3.6、Multiple sets of visual execution logic image matrix elg_head i Splice to get the multi-head attention mechanism vector Mhead elg , the visual grammar tree feature f is calculated by the formula visual_elg , the specific formula is as follows:

[0128] f visual_elg =LayerNorm(FFN(Mhead elg )+Sublayer(FFN(Mhead elg)))

[0129] Among them, Mhead elg is the multi-head attention mechanism vector corresponding to the visual execution logic image, and

[0130] Perform a logistic linear transformation matrix for vision,

[0131] FFN is a feed-forward neural network calculation.

[0132] Sublayer is the layer normalization process.

[0133] LayerNorm is the residual connection processing;

[0134] 3.7. Visual grammar tree feature f visual_ast and visual execution logic features f visual_elg Perform linear splicing, give weight ratios between features, and obtain visual semantic features f visual_seman , the specific formula is as follows:

[0135] f visual_seman =λ 4 f visual_ast +λ 5 f visual_elg +c

[0136] Among them, λ 4 is the visual grammar tree feature f visual_ast The weight of

[0137] λ 5 Perform logical features f for vision visual_elg The weight of

[0138] c is an adjustable parameter.

[0139] In the aforementioned dual-channel smart contract defect detection method incorporating multi-view features, in step 3.2, the visual image acquisition method of the tree structure is:

[0140] Use the solidity-parser-antlr parser to convert each smart contract code in the dataset into an abstract syntax tree. Use the screenshot tool to capture the abstract syntax tree image of the code to obtain a visual image of the tree structure.

[0141] In step 3.5, the visual image acquisition method of the graph structure is:

[0142] When the smart contract code is running, the corresponding execution logic graph is constructed for each smart contract code in the dataset according to the execution process sequence of each code word in the code. By using the screenshot tool, the execution logic graph image of the code can be captured to obtain a visual image of the graph structure.

[0143] In the aforementioned dual-channel smart contract defect detection method incorporating multi-view features, in step 4, the obtained text semantic feature f text_seman and visual semantic features f visual_seman Weighted fusion to obtain a new feature vector f seman , the new feature vector f seman Modeling is performed to obtain a defect detection model, including the following steps:

[0144] 4.1. Text semantic features f text_seman and visual features f visual_seman They are concatenated linearly to obtain a new feature vector f seman , the specific formula is:

[0145] f seman =ρ 1 f text_seman +ρ 2 f visual_seman

[0146] Among them, ρ 1 is the semantic feature f text_seman The weight of

[0147] ρ 2 is the visual feature f visual_seman The weight of

[0148] 4.2. Use the fully connected layer to transform the new feature vector f seman After dimensionality reduction, the model is connected to a model consisting of a dense layer, a fully connected layer, and a dropout layer to construct a defect detection model.

[0149] The aforementioned dual-channel smart contract defect detection method incorporating multi-view features, in step 5, obtains a smart contract dataset Set, annotates the data, divides the data into a training set and a test set, trains a defect detection model, and obtains a defect detection result of the smart contract to be tested based on the trained defect detection model, including the following steps:

[0150] 5.1. All smart contracts in the smart contract dataset Set are marked according to whether they have defects;

[0151] 5.2. De-duplicate the smart contract code according to its content and delete duplicate smart contracts;

[0152] 5.3. Divide the data set into a training set and a test set, train the training set based on the defect detection model, and obtain the trained defect detection model;

[0153] 5.4. Input the smart contract code to be tested into the defect detection model, and the output is 0 or 1 to complete the detection.

[0154] In the aforementioned dual-channel smart contract defect detection method incorporating multi-view features, in step 5.1, the marking rule is: failure of the security detection tool, incomplete logic, incomplete code, and excessive gas consumption value are all regarded as defective codes;

[0155] In step 5.3, the ratio of training set to test set is 7:3.

[0156] In step 5.4, output 0 means that the detected smart contract code has no defects, and output 1 means that the detected smart contract code has defects;

[0157] The smart contract code input format is input(code, annotation, gas), where code is the code body content, annotation is the corresponding comment content, and gas is the gas consumption value of code execution.

[0158] Compared with the prior art, the present invention has the following beneficial effects:

[0159] The present invention integrates a dual-channel smart contract defect detection model with multi-view features, not only establishing a dual-channel analysis code from the text and visual perspectives, avoiding the problem of too single analysis perspective, but also adding the annotation content and gas consumption value of the smart contract code to the perspective of code defect detection, which can consider the logical function description and execution consumption loss of the code. Defect detection is no longer just detecting code defects, which makes the present application novel and comprehensive in identifying smart contract codes, with high efficiency and more accurate detection results. BRIEF DESCRIPTION OF THE DRAWINGS

[0160] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the present invention. For ordinary technicians in this field, other drawings can also be obtained based on these drawings.

[0161] Figure 1 It is a flow chart of the present invention. DETAILED DESCRIPTION

[0162] In order to enable those skilled in the art to better understand the technical solution of the present invention, the present invention will be further described in detail below with reference to the accompanying drawings.

[0163] The present invention provides Figure 1 A dual-channel smart contract defect detection method incorporating multi-view features is shown, comprising the following steps:

[0164] Step 1: Perform syntax tree feature f on the smart contract code Set(i) ast , execute logical feature f elg , text features f code Extract and weight the three extracted feature vectors to obtain the code feature f codefeature , including the following steps:

[0165] 1.1 Syntax tree feature f ast The specific steps for extraction are as follows:

[0166] 1.1.1. Construct smart contract tree structure data based on abstract syntax tree, use Word2Vec to embed words from 1 to N nodes in the tree, and get the input feature x of each node k , input into the Tree-LSTM neural network;

[0167] Among them, the tree structure data is: TreeSet(node 1 , node 2 , ..., node k , ..., node N ),

[0168] Among them, 1≤k≤N, node 1 is the root node of the tree, node k For any node in the tree,

[0169] And node k The node's child node set is (node k,1 , node k,2 , ..., node k,n , ..., node k,M ),

[0170] Among them, 1≤n≤M, node k,n For node k The nth child node of the kth node, (k,n) represents the nth child node of the kth node, and (k,M) represents the Mth child node of the kth node;

[0171] 1.1.2. Calculate the input gate i of each node k 、Forget gate k and output gate o k information;

[0172] Among them, the input gate ik The calculation formula is as follows:

[0173]

[0174] Among them, bi k is the bias top of the k-th node input gate,

[0175] Wi k is the weight of the kth node input gate,

[0176] Di k,n is the weight of the path from the kth node to all child nodes in the input gate, (k,n) represents the n child nodes of the kth node,

[0177] h k,n is the hidden layer information of the n child nodes of the kth node,

[0178] σ is the Sigmoid function,

[0179] x k is the input feature of the current k-th node;

[0180] The above formula is specifically explained as follows: for the hidden layer information of the child nodes 1 to M of the k node and the corresponding weight Di k,n Accumulate the sum, then add the k-node input feature x k and weight Wi k The product of , and finally add the bias term bi of the input gate k After that, the input gate information i of node k is obtained through the σ activation function operation. k ;

[0181] Among them, the forget gate f k The calculation formula is as follows:

[0182]

[0183] Among them, bf k is the bias top of the forget gate of the kth node,

[0184] W k is the weight of the forget gate of the kth node,

[0185] Df k,n is the weight of the path from the kth node to all child nodes in the forget gate, (k,n) represents the n child nodes of the kth node,

[0186] The above formula is specifically explained as follows: for the hidden layer information of the child nodes 1 to M of the k node and the corresponding weight Df k,n Accumulate the sum, then add k nodes input x k and weight Wf kThe product of , and finally add the output bias term bf k After that, the forget gate information f of the k node is obtained through the σ activation function operation. k ;

[0187] Among them, the output gate o k The calculation formula is as follows:

[0188]

[0189] Among them, bo k is the bias top of the k-th node output gate,

[0190] I k is the weight of the output gate of the kth node,

[0191] Do k,n is the weight of the path from the kth node to all children in the output gate, (k,n) represents the n child nodes of the kth node;

[0192] The above formula is specifically explained as follows: for the hidden layer information of the child nodes 1 to M of the k node and the corresponding weights Do k,n Cumulative sum, then add k node input x k and weight Wo k The product of , and finally add the output gate bias term bo k After that, the forget gate information o of the k node is obtained through the σ activation function operation. k ;

[0193] 1.1.3. According to the input gate i k 、Forget gate k , calculate the cell state information c of the node k , the specific formula is as follows:

[0194]

[0195] Among them, tanh(·) is the activation function,

[0196] bu k is the bias top of the cell state information of the kth node,

[0197] Wu k is the weight of the cell state information of the kth node,

[0198] Du k,n is the weight of the path from node k to all child nodes in the cell state information, u represents the cell state, and (k,n) represents the n child nodes of the kth node;

[0199] 1.1.4, according to the obtained output gate o k , and cell state information ck , calculate the hidden layer vector h of the kth node N , the specific formula is as follows:

[0200]

[0201] 1.1.5. From the leaf node in the tree N (k=N) Start recursive calculation layer by layer to get node N The input gate i N 、Forget gate N , output gate o N and the hidden layer vector h N Repeat steps 1.1.2-1.1.4 to calculate the input gate, forget gate, output gate and hidden layer vector of each node, and obtain the hidden layer vector h of each node N , the hidden layer vector representation of the root node is used as the syntax tree feature vector of the entire smart contract, that is, the syntax tree feature f ast , specifically expressed as:

[0202] f ast =h 1

[0203] Among them, h 1 is the hidden layer vector of the root node of the syntax tree;

[0204] 1.2. Execution logic feature f elg The specific steps for extraction are as follows:

[0205] 1.2.1. Construct the smart contract execution logic structure diagram G = (V, E) based on the code execution process, where V is the set of vertices in the diagram and E is the relationship between the edges of the vertices;

[0206] 1.2.2. Concatenate the eigenvectors of n vertices to form an n×d-dimensional matrix H 0 , the relationship between the edges of the vertices builds an n×n dimensional adjacency matrix A;

[0207] 1.2.3、H 0 and A are input into the GCN neural network, and after iterative calculation, H is extracted (m) , the specific formula is as follows:

[0208]

[0209] Among them, the initial input is H 0 ,

[0210]

[0211] I is the identity matrix,

[0212] yes The degree matrix of

[0213] H is the feature of each layer,

[0214] σ is the ReLU function;

[0215] H (m) H 0 The vector obtained after performing m graph neural network calculations;

[0216] W (m-1) is the weight matrix calculated for the m-1th time;

[0217] 1.2.4. According to the calculation formula in step 1.2.3, after learning the l-layer graph convolutional neural network, the execution logic feature f can be obtained. elg , specifically expressed as:

[0218] f elg =H (l)

[0219] In step 1.2, V = {v 1 ,v 2 ,...,v n}, its feature value is extracted by word2vec, and j is used i Represents the characteristics of the i-th node, which can be concatenated to obtain H 0 ,Right now

[0220] H 0 ={j 1 ,j 2 ,...,j n},

[0221] Where n is the number of vertices,

[0222] And E={<v i , v j >|v i , v j ∈V} is the set of relations between each node, if v j The node corresponding statement follows v i Node execution, then <v i ,v j >=1, otherwise, <v i ,v j >=0, so the adjacency matrix A is a relationship matrix composed of 0 and 1;

[0223] 1.3 Text features f codeSpecifically, the code text corresponding to the smart contract code Set(i) is input into the Sentence-Bert model in units of lines as sentences, and the text features f of the smart contract code are extracted by the Sentence-Bert model. code ;

[0224] 1.4. Extracted syntax tree features f ast , execute logical feature f elg , and text features f code Perform linear concatenation and give weight ratios between features to obtain the code feature f codefeature , the specific formula is as follows:

[0225]

[0226] in, For vector concatenation,

[0227] w 1 is the syntax tree feature f ast The splicing weight,

[0228] w 2 To perform the logical feature f elg The splicing weight,

[0229] w 3 is the text feature f code The splicing weight,

[0230] b 1 is an adjustable parameter;

[0231] In this step 1, when extracting syntax tree features, by taking the abstract syntax tree of the smart contract as input, Tree-LSTM, which is better at processing tree structures, is used to extract features, so that syntax tree features can be accurately extracted. For execution logic features, by constructing the corresponding execution logic graph and using GCN to extract execution logic features, Sentence-Ber is used to perform adaptive training of the data set of the present invention, which is closer to the data set of this article, and then a data structure and modeling method that are more suitable for this application are constructed, so as to more accurately extract these features.

[0232] These three types of features are currently relatively mature research and have become the basic dimensions of the code, so these features cannot be ignored. Many inventions consider the features of smart contracts from a single perspective, such as being limited to syntax trees or text features, etc., but these features are currently relatively mature research areas, so the present invention considers these features more comprehensively and comprehensively, and adds all features to the text semantic features. The perspective of the present invention is more comprehensive, and the feature extraction method is more appropriate for the data set of this article;

[0233] Among them, the Tree-LSTM neural network, the GCN neural network, and the Sentence-Bert model are all existing technologies, which can be used to better operate the smart contract code Set(i). Specifically: the Tree-LSTM neural network is a neural network model that extends the structure of the long short-term memory network (LSTM) to a tree topology, which is used to capture hierarchical information in the data and is suitable for processing tree structure data;

[0234] The GCN neural network is a deep learning model specifically designed for processing graph structure data. It extracts features by performing convolution operations on the nodes of the graph, and is applicable to the data structure and modeling method of this step.

[0235] The Sentence-Bert model is a pre-trained model based on BERT, which is used to generate sentence embedding vectors, which can be used for natural language processing tasks such as text classification, similarity calculation, and clustering. It can perform data set adaptation training in this step and is closer to the data set in this article.

[0236] Step 2: Annotate the smart contract code Set(i) with feature f ann 、Gas consumption value characteristics f gas After extraction, the code feature f codefeature , annotation feature f ann 、Gas consumption value characteristics f gas Weighted fusion to obtain text semantic features f text_seman , comprising the following steps;

[0237] 2.1. Input the annotation text corresponding to the smart contract code Set(i) into the Sentence-Bert model, and use the Sentence-Bert model to extract the annotation features f of the smart contract ann ;

[0238] 2.2. Based on the gas consumption value of the smart contract code Set(i), direct coding is used to extract the gas consumption value feature f of the smart contract gas ;

[0239] 2.3. Extract the code features f codefeature , annotation feature f ann 、Gas consumption value characteristics f gas Weighted fusion, linear splicing, and giving weight ratios between features to obtain text semantic features f text_seman , the specific formula is as follows:

[0240]

[0241] Among them, λ 1is the concatenation weight of the syntax tree feature,

[0242] λ 2 is the concatenation weight of the code feature,

[0243] λ 3 is the splicing weight of the annotation feature,

[0244] b 2 is an adjustable parameter;

[0245] In step 1.3 and step 2.1, the annotation text corresponding to the smart contract code Set(i) is input into the Sentence-Bert model to extract the text feature f code and annotation feature f ann Previously, the Sentence-Bert model was fine-tuned using the dataset. The specific steps were:

[0246] K1. Randomly select 2,000 smart contract code texts and comments. Each line of code is considered as a sentence, and the comments are divided into sentences according to periods.

[0247] K2, use cosine similarity calculation to determine the calculation result, and input the result as training data into the initial Sentence-BERT model for secondary fine-tuning;

[0248] K3. Divide each text into m sentences, and obtain the sentence vector Vec of each sentence by the Sentence-Bert model i , and then concatenate to get the feature vector f of the text text , specifically expressed as:

[0249] f text =[Vec 1 ,Vec 2 ,...,Vec m ]

[0250] Among them, the feature vector f of the text text Analyzing smart contract codes from the perspective of ordinary text is the most primitive analysis perspective. Therefore, the text features of smart contracts are basic dimensional features, representing the features of smart contracts from a simple text perspective, and are also the most primitive features. From this basic dimensional analysis, syntax tree features, execution logic features, including the novel visual semantic features proposed in this invention, can be derived later.

[0251] In this step 2, by using the Sentence-Bert pre-training model to perform adaptive fine-tuning of the data set, the training cost can be greatly reduced, the efficiency and effect can be improved, and at the same time, the smart contract text feature extraction task in this application can be better understood and adapted to reduce the risk of overfitting.

[0252] Step 3: Perform visual syntax tree feature f on the smart contract code Set(i) visual_ast and visual execution logic features f visual_elg After extraction, all visual feature vectors are weighted and fused to obtain the visual semantic feature f visua_lseman , comprising the following steps;

[0253] 3.1. Based on syntax tree feature f ast Get the syntax tree feature vector h for each code word i ;

[0254] 3.2. According to the visual image form of the tree structure, all the syntax tree feature vectors h i Splice and obtain the visual grammar tree image matrix visual_ast, divide the input visual grammar tree image matrix visual_ast into multiple groups, and input it into the Vision Transformers neural network to perform attention calculation to obtain multiple groups of attention mechanism vectors ast_head i , the specific formula is as follows:

[0255]

[0256] Among them, Q ast The key after linear transformation of the syntax tree feature input,

[0257] and

[0258] K ast The value after linear transformation of the syntax tree feature input.

[0259] and

[0260] V ast The query after linear transformation of the syntax tree feature input,

[0261] and

[0262] d k is the dimension of the word vector,

[0263] The visual syntax tree image matrix visual_ast is specifically:

[0264] [h 1 ,h 2 ,h 3 ,...,h N ]

[0265] Among them, the visual syntax tree image matrix visual_ast is processed by the image preprocessing module before being input into the Vision Transformers neural network, thereby obtaining multiple sets of attention mechanism vectors ast_head of the visual syntax tree i ;

[0266] 3.3、Multiple sets of attention mechanism vectors ast_head i The multi-head attention mechanism vector is obtained by splicing, and the visual syntax tree feature f is calculated based on the multi-head attention mechanism vector visual_ast , the specific formula is as follows:

[0267] f visual_ast =LayerNorm(FFN(Mhead ast )+Sublayer(FFN(Mhead ast )))

[0268] Among them, Mhead ast is the multi-head attention mechanism vector corresponding to the visual syntax tree image,

[0269] And Mhead ast =[ast_head 1 ,ast_head 2 ,...,ast_head h ]×W ast 0 ,

[0270] is the linear transformation matrix of the visual grammar tree,

[0271] FFN is a feed-forward neural network calculation.

[0272] Sublayer is the layer normalization process.

[0273] LayerNorm is the residual connection processing;

[0274] 3.4. Based on the execution logic feature f elg Get the execution logic feature vector H of each code word i ;

[0275] 3.5. Based on the visual image form of the graph structure, perform logical feature vectors H on all i Splice to obtain the visual execution logic image matrix visual_elg, divide the input visual execution logic image matrix visual_elg into multiple groups, and input it into the Vision Transformers neural network to perform attention calculation to obtain multiple groups of attention mechanism vectors elg_headi , the specific formula is as follows:

[0276]

[0277] Among them, Q elg The key after performing linear transformation on the logical feature input.

[0278] and

[0279] K elg The value after performing linear transformation on the logical feature input.

[0280] and

[0281] V elg To perform linear transformation on the query of logical feature input,

[0282] and

[0283] The visual execution logic image matrix visual_elg is specifically:

[0284] [H 1 ,H 2 ,H 3 ,...,H N ]

[0285] Among them, the visual execution logic image matrix visual_elg is processed by the image preprocessing module before being input into the Vision Transformers neural network, so as to obtain multiple sets of visual execution logic image matrices elg_head of visual execution logic features. i ;

[0286] 3.6、Multiple sets of visual execution logic image matrix elg_head i Splice to get the multi-head attention mechanism vector Mhead elg , the visual grammar tree feature f is calculated by the formula visual_elg , the specific formula is as follows:

[0287] f visual_elg =LayerNorm(FFN(Mhead elg )+Sublayer(FFN(Mhead elg )))

[0288] Among them, Mhead elg is the multi-head attention mechanism vector corresponding to the visual execution logic image, and

[0289] Perform a logistic linear transformation matrix for vision,

[0290] FFN is a feed-forward neural network calculation.

[0291] Sublayer is the layer normalization process.

[0292] LayerNorm is the residual connection processing;

[0293] 3.7. Visual grammar tree feature f visual_ast and visual execution logic features f visual_elg Perform linear splicing, give weight ratios between features, and obtain visual semantic features f visual_seman , the specific formula is as follows:

[0294] f visual_seman =λ 4 f visual_ast +λ 5 f visual_elg +c

[0295] Among them, λ 4 is the visual grammar tree feature f visual_ast The weight of

[0296] λ 5 Perform logical features f for vision visual_elg The weight of

[0297] c is an adjustable parameter;

[0298] And in step 3.2, the method for obtaining the visual image of the tree structure is:

[0299] Use the solidity-parser-antlr parser to convert each smart contract code in the dataset into an abstract syntax tree. Use the screenshot tool to capture the abstract syntax tree image of the code to obtain a visual image of the tree structure.

[0300] In step 3.5, the visual image acquisition method of the graph structure is:

[0301] When the smart contract code is running, the corresponding execution logic graph is constructed for each smart contract code in the data set according to the execution process sequence of each code word in the code. The execution logic graph of the code is captured using a screenshot tool to obtain a visual image of the graph structure.

[0302] In this step 3, the proposed visual semantic features include visual syntax tree features and visual execution logic features, which solves the problem of single detection angle and insufficient comprehensiveness in current code defect detection methods, and provides a new semantic analysis angle for defect detection, which has more room for improvement and expansion in the future.

[0303] At the same time, this step 3 analyzes the semantic relationship between nodes in the code more simply and intuitively from a visual perspective. It not only takes into account the various grammatical structural relationships of the nodes in the code itself, but also combines the dependency relationship of the nodes from a visual perspective, and integrates multiple textual and visual information, so that the defect detection model can achieve a comprehensive understanding of the smart contract;

[0304] And through the visual syntax tree and visual execution logic diagram, you can intuitively understand information such as the code execution process and syntax structure, help discover logical and structural loopholes and errors, help capture complex patterns and features, and improve the richness and effectiveness of feature representation.

[0305] Step 4: Get the text semantic features f text_seman and visual semantic features f visual_seman Weighted fusion to obtain a new feature vector f seman , the new feature vector f seman Modeling is performed to obtain a defect detection model, including the following steps:

[0306] 4.1. Text semantic features f text_seman and visual features f visual_seman They are concatenated linearly to obtain a new feature vector f seman , the specific formula is:

[0307] f seman =ρ 1 f text_seman +ρ 2 f visual_seman

[0308] Among them, ρ 1 is the semantic feature f text_seman The weight of

[0309] ρ 2 is the visual feature f visual_seman The weight of

[0310] 4.2. Use the fully connected layer to transform the new feature vector f seman Perform dimensionality reduction and connect it to a model consisting of dense layers, fully connected layers, and dropout layers to build a defect detection model;

[0311] In this step 4, during the model training process, this part can obtain accurate semantic features by learning the weights of text semantic features and visual semantic features to adjust the ratio of the two features, and the fully connected layer can reduce the dimension of the input feature vector through linear transformation to reduce the number of features, which helps to reduce the computational complexity. The dense layer and the Dropout layer randomly discard some neurons during the training process, which helps to prevent the model from overfitting and improve the generalization ability of the defect detection model.

[0312] Step 5: Obtain the smart contract data set Set, annotate the data, divide it into training set and test set, train the defect detection model, and obtain the defect detection result of the smart contract to be tested based on the trained defect detection model, including the following steps:

[0313] 5.1. All smart contracts in the smart contract dataset Set are marked according to whether they have defects;

[0314] 5.2. De-duplicate the smart contract code according to its content and delete duplicate smart contracts;

[0315] 5.3. Divide the data set into a training set and a test set, train the training set based on the defect detection model, and obtain the trained defect detection model;

[0316] 5.4. Input the smart contract code to be tested into the defect detection model, and the output is 0 or 1 to complete the detection;

[0317] In step 5.1, the marking rule is: failure of the security detection tool, incomplete logic, incomplete code, and excessive gas consumption are all considered defective code;

[0318] In step 5.3, the ratio of training set to test set is 7:3.

[0319] In step 5.4, output 0 means that the detected smart contract code has no defects, and output 1 means that the detected smart contract code has defects;

[0320] The smart contract code input format is input(code, annotation, gas), where code is the code body content, annotation is the corresponding comment content, and gas is the gas consumption value of code execution;

[0321] In step 5.2, removing duplicate smart contract codes can reduce data redundancy, avoid overfitting during model training, and improve the generalization ability and performance of the defect detection model;

[0322] The training set and test set are divided in a ratio of 7:3, which helps the model to be trained on enough data while retaining a certain proportion of data to verify the performance of the model, thus ensuring the effectiveness and reliability of the model.

[0323] In step 5, by marking rules, smart contracts that fail the security detection tool, have incomplete logic, incomplete code, and have too high gas consumption values ​​are regarded as defective codes, which can ensure the high quality of training data and thus improve the accuracy of the defect detection model in detecting defects.

[0324] Verification experiment

[0325] Smart contract defect detection is essentially a binary classification task in deep learning, so the accuracy, recall and F1 score are used to verify the effectiveness of the present invention, and a comparative experiment is conducted with the existing smart contract defect research method to verify the effect of the present invention. The experimental data are recorded in Table 1:

[0326] Table 1 Experimental scores of this application compared with other models

[0327]

[0328]

[0329] From the analysis of Table 1, it can be seen that compared with the traditional neural network (LSTM, GRU, BLSTM) model, the accuracy (Accuracy), precision (Precision) and F1 score (F1) of this application are significantly improved, indicating that the application uses Tree-LSTM, GCN, Sentence-Bert model for text semantic feature extraction with better effect, and is more suitable for modeling and analysis of code;

[0330] At the same time, a comparison is made between the smart contract defect detection model based on multi-feature fusion and the smart contract source code vulnerability detection model based on feature modulation graph neural network. This application improves the accuracy, recall rate and F1 score by 5%, 2% and 5.5% on average;

[0331] Therefore, it can be concluded that this method has obvious improvements in the three indicators of accuracy, recall rate and F1 score, and considering the gas consumption value of the smart contract and the description dimension of the annotation information, the perspective is more comprehensive.

[0332] To summarize, the present invention integrates a dual-channel smart contract defect detection model with multi-view features, not only establishing a dual-channel analysis code from text and visual perspectives, avoiding the problem of too single analysis perspective, but also adding the annotation content and gas consumption value of the smart contract code to the perspective of code defect detection, which can consider the logical function description and execution consumption loss of the code. Defect detection is no longer just detecting code defects, which makes the present application novel and comprehensive in identifying smart contract codes, with high efficiency and more accurate detection results.

[0333] The above description is only by way of illustration of certain exemplary embodiments of the present invention. It is undoubted that those skilled in the art can modify the described embodiments in various ways without departing from the spirit and scope of the present invention. Therefore, the above drawings and descriptions are illustrative in nature and should not be construed as limiting the scope of protection of the claims of the present invention.

Claims

1. A dual-channel smart contract defect detection method incorporating multi-view features, characterized in that: The following steps are involved: Step 1: Perform syntax tree feature f on the smart contract code Set(i) ast , execute logical feature f elg , text features f code Extract and weight the three extracted feature vectors to obtain the code feature f codefeature ; Step 2: Annotate the smart contract code Set(i) with feature f ann 、Gas consumption value characteristics f gas After extraction, the code feature f codefeature , annotation feature f ann 、Gas consumption value characteristics f gas Weighted fusion to obtain text semantic features f text_seman ; Step 3: Perform visual syntax tree feature f on the smart contract code Set(i) visual_ast and visual execution logic features f visual_elg After extraction, all visual feature vectors are weighted and fused to obtain the visual semantic feature f visua_lseman ; Step 4: Get the text semantic features f text_seman and visual semantic features f visua_lseman Weighted fusion to obtain a new feature vector f seman , the new feature vector f seman Modeling is performed to obtain a defect detection model; Step 5: Obtain the smart contract data set Set, annotate the data, divide it into training set and test set, train the defect detection model, and obtain the defect detection result of the smart contract to be tested based on the trained defect detection model; In step 1, the syntax tree feature f is performed on the smart contract code Set(i) ast , execute logical feature f elg , text features f code Extract and weight the three extracted feature vectors to obtain the code feature f codefeature , including the following steps: 1.1 Syntax tree feature f ast Extraction of 1.

2. Execution logic feature f elg Extraction of 1.3 Text features f code Extraction of 1.

4. Extracted syntax tree features f ast , execute logical feature f elg , and text features f code Perform linear concatenation and give weight ratios between features to obtain code feature f codefeature , the specific formula is as follows: in, For vector concatenation, w1 is the syntax tree feature f ast The splicing weight, w2 is the execution logic feature f elg The splicing weight, w3 is the text feature f code The splicing weight, b1 is an adjustable parameter; In step 3, the smart contract code Set(i) is subjected to visual syntax tree feature f visual_ast and visual execution logic features f visual_elg After extraction, all visual feature vectors are weighted and fused to obtain the visual semantic feature f visual_seman , including the following steps: 3.

1. Based on syntax tree feature f ast Get the syntax tree feature vector h for each code word i ; 3.

2. According to the visual image form of the tree structure, all the syntax tree feature vectors h i Splice and obtain the visual grammar tree image matrix visual_ast, divide the input visual grammar tree image matrix visual_ast into multiple groups, and input it into the Vision Transformers neural network to perform attention calculation to obtain multiple groups of attention mechanism vectors ast_head i , the specific formula is as follows: Among them, Q ast The key after linear transformation of the syntax tree feature input, and K ast The value after linear transformation of the syntax tree feature input. and V ast The query after linear transformation of the syntax tree feature input, and d k is the dimension of the word vector, The visual syntax tree image matrix visual_ast is specifically: <h2 style=";text-align:left;direction:ltr">[h1,h2,h3,...,h<h2 style=";text-align:left;direction:ltr"> N <h2 style=";text-align:left;direction:ltr"> ] Among them, the visual syntax tree image matrix visual_ast is processed by the image preprocessing module before being input into the Vision Transformers neural network, thereby obtaining multiple sets of attention mechanism vectors ast_head of the visual syntax tree i ; 3.3、Multiple sets of attention mechanism vectors ast_head i The multi-head attention mechanism vector is obtained by splicing, and the visual syntax tree feature f is calculated based on the multi-head attention mechanism vector visual_ast , the specific formula is as follows: f visual_ast =LayerNorm(FFN(Size ast )+Sublayer(FFN(Size ast ))) Among them, Mhead ast is the multi-head attention mechanism vector corresponding to the visual syntax tree image, and Mhead ast = [ast_head1, ast_head2,..., ast_head h × W ast 0 , is the linear transformation matrix of the visual grammar tree, FFN is a feed-forward neural network calculation. Sublayer is the layer normalization process. LayerNorm is the residual connection processing; 3.

4. Based on the execution logic feature f elg Get the execution logic feature vector H of each code word i ; 3.

5. Based on the visual image form of the graph structure, perform logical feature vectors H on all i Splice to obtain the visual execution logic image matrix visual_elg, divide the input visual execution logic image matrix visual_elg into multiple groups, and input it into the Vision Transformers neural network to perform attention calculation to obtain multiple groups of attention mechanism vectors elg_head i , the specific formula is as follows: Among them, Q elg The key after performing linear transformation on the logical feature input. and K elg The value after performing linear transformation on the logical feature input. and V el g is the query after performing linear transformation on the logical feature input. and The visual execution logic image matrix visual_elg is specifically: [H 1 ,H 2 ,H 3 ,...,H N ] Among them, the visual execution logic image matrix visual_elg is processed by the image preprocessing module before being input into the Vision Transformers neural network, so as to obtain multiple sets of visual execution logic image matrices elg_head of visual execution logic features. i ; 3.6、Multiple sets of visual execution logic image matrix elg_head i Splice to get the multi-head attention mechanism vector Mhead elg , the visual grammar tree feature f is calculated by the formula visual_elg , the specific formula is as follows: f visual_elg =LayerNorm(FFN(Size elg )+Sublayer(FFN(Size elg ))) Among them, Mhead elg The multi-head attention mechanism vector corresponding to the visual execution logic image, and Perform a logistic linear transformation matrix for vision, FFN is a feed-forward neural network calculation. Sublayer is the layer normalization process. LayerNorm is the residual connection processing; 3.

7. Visual grammar tree feature f visual_ast and visual execution logic features f visual_elg Perform linear splicing, give weight ratios between features, and obtain visual semantic features f visual_seman , the specific formula is as follows: f visual_seman =λ4f visual_ast +λ5f visual_elg +c Among them, λ4 is the visual grammar tree feature f visual_ast The weight of λ5 is the visual execution logic feature f visual_elg The weight of c is an adjustable parameter; In step 3.2, the tree structure visual image acquisition method is: Use the solidity-parser-antlr parser to convert each smart contract code in the dataset into an abstract syntax tree. Use the screenshot tool to capture the abstract syntax tree image of the code to obtain a visual image of the tree structure. In step 3.5, the visual image acquisition method of the graph structure is: When the smart contract code is running, the corresponding execution logic graph is constructed for each smart contract code in the data set according to the execution process sequence of each code word in the code. The execution logic graph of the code is captured using a screenshot tool to obtain a visual image of the graph structure. In step 4, the obtained text semantic feature f text_seman and visual semantic features f visual_seman Weighted fusion to obtain a new feature vector f seman , the new feature vector f seman Modeling is performed to obtain a defect detection model, including the following steps: 4.

1. Text semantic features f text_seman and visual features f visual_seman They are concatenated linearly to obtain a new feature vector f seman , the specific formula is: f seman =ρ1f text_seman +p2f visual_seman Among them, ρ1 is the semantic feature f text_seman The weight of ρ2 is the visual feature f visual_seman The weight of 4.

2. Use the fully connected layer to transform the new feature vector f seman After dimensionality reduction, the model is connected to a model consisting of a dense layer, a fully connected layer, and a dropout layer to construct a defect detection model.

2. According to claim 1, a dual-channel smart contract defect detection method incorporating multi-view features is characterized in that: In step 1.1, the syntax tree feature f ast The specific steps for extraction are as follows: 1.1.

1. Construct smart contract tree structure data based on abstract syntax tree, use Word2Vec to embed words from 1 to N nodes in the tree, and get the input feature x of each node k , input into the Tree-LSTM neural network; The tree structure data is: TreeSet(node1, node2, ..., node k , ..., node N ), Among them, 1≤k≤N, node1 is the root node of the tree, node k For any node in the tree, And node k The node's child node set is (node k,1 , node k,2 , ..., node k,n , ..., node k,M ), Among them, 1≤n≤M, node k,n For node k The nth child node of the kth node, (k,n) represents the nth child node of the kth node, and (k,M) represents the Mth child node of the kth node; 1.1.

2. Calculate the input gate i of each node k 、Forget gate k and output gate o k information; Among them, the input gate i k The calculation formula is as follows: Among them, bi k is the bias top of the k-th node input gate, Wi k is the weight of the kth node input gate, Di k,n is the weight of the path from the kth node to all child nodes in the input gate, (k,n) represents the n child nodes of the kth node, h k,n is the hidden layer information of the n child nodes of the kth node, σ is the Sigmoid function, x k is the input feature of the current k-th node; Among them, the forget gate f k The calculation formula is as follows: Among them, bf k is the bias top of the forget gate of the kth node, W k is the weight of the forget gate of the kth node, Df k,n is the weight of the path from the kth node to all child nodes in the forget gate, (k,n) represents the n child nodes of the kth node; Among them, the output gate o k The calculation formula is as follows: Among them, bo k is the bias top of the k-th node output gate, I k is the weight of the output gate of the kth node, Do k,n is the weight of the path from the kth node to all children in the output gate, (k,n) represents the n child nodes of the kth node; 1.1.

3. According to the input gate i k 、Forget gate k , calculate the cell state information c of the node k , the specific formula is as follows: Among them, tanh(·) is the activation function, bu k is the bias top of the cell state information of the kth node, Wu k is the weight of the cell state information of the kth node, Du k,n is the weight of the path from node k to all child nodes in the cell state information, u represents the cell state, and (k,n) represents the n child nodes of the kth node; 1.1.

4. According to the obtained output gate o k , and cell state information c k , calculate the hidden layer vector h of the kth node N , the specific formula is as follows: 1.1.

5. From the leaf node in the tree N (k=N) Start recursive calculation layer by layer to get node N The input gate i N 、Forget gate N , output gate o N and the hidden layer vector h N Repeat steps 1.1.2-1.1.4 to calculate the input gate, forget gate, output gate and hidden layer vector of each node, and obtain the hidden layer vector h of each node N , the hidden layer vector representation of the root node is used as the syntax tree feature vector of the entire smart contract, that is, the syntax tree feature f ast , specifically expressed as: in ast =h1 Among them, h1 is the hidden layer vector of the root node of the syntax tree; In step 1.2, the logical feature f is executed elg The specific steps for extraction are as follows: 1.2.

1. Construct the smart contract execution logic structure diagram G = (V, E) based on the code execution process, where V is the set of vertices in the diagram and E is the relationship between the edges of the vertices; 1.2.

2. Concatenate the eigenvectors of n vertices to form an n×d-dimensional matrix H 0 , the relationship between the edges of the vertices builds an n×n dimensional adjacency matrix A; 1.2.3、H 0 and A are input into the GCN neural network, and after iterative calculation, H is extracted (m) , the specific formula is as follows: Among them, the initial input is H 0 , I is the identity matrix, yes The degree matrix of H is the feature of each layer, σ is the ReLU function; H (m) H 0 The vector obtained after performing m graph neural network calculations; W (m-1) is the weight matrix calculated for the m-1th time; 1.2.

4. According to the calculation formula in step 1.2.3, after learning the l-layer graph convolutional neural network, the execution logic feature f can be obtained. elg , specifically expressed as: f elg =H (l) Where V = {v1,v2,...,v n }, its feature value is extracted by word2vec, and j is used i Represents the characteristics of the i-th node, which can be concatenated to obtain H 0 ,Right now H 0 ={j1,j2,...,j n }, Where n is the number of vertices, And E={<v i , v j >|v i , v j ∈V} is the set of relationships between each node, and the adjacency matrix A is a relationship matrix composed of 0s and 1s; In step 1.3, the text feature f code Specifically, the code text corresponding to the smart contract code Set(i) is input into the Sentence-Bert model in units of lines as sentences, and the Sentence-Bert model extracts the text features f of the smart contract code. code .

3. According to claim 2, a dual-channel smart contract defect detection method incorporating multi-view features is characterized in that: In step 2, the smart contract code Set(i) is annotated with feature f ann 、Gas consumption value characteristics f gas After extraction, the code feature f codefeature , annotation feature f ann 、Gas consumption value characteristics f gas Weighted fusion to obtain text semantic features f text _seman , including the following steps: 2.

1. Input the annotation text corresponding to the smart contract code Set(i) into the Sentence-Bert model, and use the Sentence-Bert model to extract the annotation features f of the smart contract ann ; 2.

2. Based on the gas consumption value of the smart contract code Set(i), direct coding is used to extract the gas consumption value feature f of the smart contract gas ; 2.

3. Extract the code features f codefeature , annotation feature f ann 、Gas consumption value characteristics f gas Weighted fusion, linear splicing, and giving weight ratios between features to obtain text semantic features f text_seman , the specific formula is as follows: Among them, λ1 is the concatenation weight of the syntax tree feature, λ2 is the concatenation weight of the code feature, λ3 is the concatenation weight of the annotation feature, b2 is an adjustable parameter.

4. According to claim 3, a dual-channel smart contract defect detection method incorporating multi-view features is characterized in that: In step 1.3 and step 2.1, the annotation text corresponding to the smart contract code Set(i) is input into the Sentence-Bert model to extract the text feature f code and annotation feature f ann Previously, the Sentence-Bert model was fine-tuned using the dataset. The specific steps were: K1. Randomly select 2,000 smart contract code texts and comments. Each line of code is considered as a sentence, and the comments are divided into sentences according to periods. K2, use cosine similarity calculation to determine the calculation result, and input the result as training data into the initial Sentence-BERT model for secondary fine-tuning; K3. Divide each text into m sentences, and obtain the sentence vector Vec of each sentence by the Sentence-Bert model i , and then concatenate to get the feature vector f of the text text , specifically expressed as: f text =[Thing1,Thing2,...,Thing m ]。 5. According to claim 1, a dual-channel smart contract defect detection method incorporating multi-view features is characterized in that: In step 5, the smart contract data set Set is obtained, the data is labeled, divided into a training set and a test set, a defect detection model is trained, and the defect detection result of the smart contract to be tested is obtained based on the trained defect detection model, including the following steps: 5.

1. All smart contracts in the smart contract dataset Set are marked according to whether they have defects; 5.

2. De-duplicate the smart contract code according to its content and delete duplicate smart contracts; 5.

3. Divide the data set into a training set and a test set, train the training set based on the defect detection model, and obtain the trained defect detection model; 5.

4. Input the smart contract code to be tested into the defect detection model, and the output is 0 or 1 to complete the detection.

6. According to claim 5, a dual-channel smart contract defect detection method incorporating multi-view features is characterized in that: In step 5.1, the marking rule is: failure of the security detection tool, incomplete logic, incomplete code, and excessive gas consumption are all considered defective code; In step 5.3, the ratio of training set to test set is 7:

3. In step 5.4, output 0 means that the detected smart contract code has no defects, and output 1 means that the detected smart contract code has defects; The smart contract code input format is input(code, annotation, gas), where code is the code body content, annotation is the corresponding comment content, and gas is the gas consumption value of code execution.

Citation Information

Patent Citations

  • Contract defect detection method based on intelligent contract knowledge graph

    CN112860263A

  • On-chain smart contract code defect automatic monitoring method and system

    CN116401169A