Graph-based model-based blockchain system high centrality smart contract identification method
By constructing a smart contract call graph and using a graph attention network to fuse feature vectors, the problem of traditional methods struggling to identify highly centralized smart contracts in dynamic scenarios is solved, achieving efficient and accurate identification and improving the reliability and security of the blockchain system.
Patent Information
- Application Number
- CN202411601713.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-11
- Publication Date
- 2025-11-21
- Estimated Expiration
- 2044-11-11
AI Technical Summary
Traditional blockchain testing methods struggle to quickly and accurately identify highly centralized smart contracts in blockchain systems under dynamic and complex scenarios.
A smart contract call graph is constructed based on a large graph model. By interpreting and vectorizing the source code of each node, and combining it with a graph attention network, the semantic feature vector of the code and the original description feature vector are fused to identify highly central smart contracts.
It improves the efficiency and accuracy of identifying highly centralized smart contracts, and enhances the stability and credibility of blockchain systems.
Smart Images

Figure CN119848825B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of blockchain technology, and in particular to a method for identifying highly centralized smart contracts in blockchain systems based on a graph-large model. Background Technology
[0002] Blockchain technology, as a decentralized distributed ledger technology, has been widely applied in finance, supply chain, the Internet of Things, and other fields in recent years. Smart contracts, as a core component of a blockchain system, directly affect the stability and trustworthiness of the entire system through their reliability and security. However, as blockchain systems continue to expand in scale, the number of smart contracts within them is increasing, and the calling relationships between smart contracts are becoming increasingly complex. Traditional blockchain testing methods only consider the static characteristics of smart contracts, making it difficult to quickly and accurately identify highly centralized smart contracts in dynamically changing and complex scenarios within a blockchain system. Summary of the Invention
[0003] This invention provides a method for identifying highly centralized smart contracts in blockchain systems based on a graph-large model. This method addresses the shortcomings of traditional blockchain testing methods that only consider the static characteristics of smart contracts and are unable to quickly and accurately identify highly centralized smart contracts in blockchain systems under dynamic and complex scenarios.
[0004] This invention provides a method for identifying highly centralized smart contracts in blockchain systems based on a graph-large model, comprising:
[0005] Using each smart contract in the blockchain system as a node and the real-time call relationship between the smart contracts as an edge, a smart contract call graph is constructed, and the adjacency matrix of the smart contract call graph is determined.
[0006] The source code of each node is interpreted to obtain the code interpretation text of each node. The code interpretation text of each node is vectorized to obtain the code semantic feature vector of each node. The original description text of each node is vectorized to obtain the original description feature vector of each node.
[0007] The code semantic feature vector and the original description feature vector are fused to obtain the first fused feature vector for each node;
[0008] The first fusion feature vector of each node and the adjacency matrix of the smart contract call graph are input into a pre-constructed graph attention network to obtain the embedding vector of each node output by the graph attention network. The graph attention network is trained based on the sample fusion feature vector and sample adjacency matrix of the sample nodes of the sample smart contract call graph, as well as the embedding vector label of the sample nodes.
[0009] Each node is evaluated based on its embedding vector and adjacency matrix to identify highly central smart contracts.
[0010] In some embodiments, interpreting the source code of each node to obtain the code interpretation text of each node includes:
[0011] Obtain the source code of each node;
[0012] The source code of each node is preprocessed to obtain the code of each node;
[0013] The code of each node is input into a pre-trained GPT-4 model to obtain the code explanation text of each node output by the GPT-4 model;
[0014] The GPT-4 model is trained using sample code of sample nodes in a sample smart contract call graph and text labels explaining the code of the sample nodes.
[0015] In some embodiments, the process of vectorizing the code explanation text of each node to obtain the code semantic feature vector of each node, and vectorizing the original description text of each node to obtain the original description feature vector of each node, includes:
[0016] Obtain the original description text for each node;
[0017] The code explanation text of each node is input into the pre-trained SentenceBERT model to obtain the code semantic feature vector of each node output by the SentenceBERT model.
[0018] The original description text of each node is input into the SentenceBERT model to obtain the original description feature vector of each node output by the SentenceBERT model.
[0019] The SentenceBERT model is trained using the sample code explanation text and original description text of the sample nodes in the sample smart contract call graph, as well as the code semantic feature vector label and original description feature vector label of the sample nodes.
[0020] In some embodiments, evaluating each node based on its embedding vector and adjacency matrix to identify highly central smart contracts includes:
[0021] Based on the adjacency matrix, the degree centrality, betweenness centrality, and eigenvector centrality of each node are determined.
[0022] The centrality of each node is scored based on its embedding vector, degree centrality, betweenness centrality, and eigenvector centrality, and the score of each node is determined.
[0023] The scores of each node are sorted to obtain a sorting result. Based on the sorting result, multiple high-centrality smart contracts are selected from the smart contracts.
[0024] In some embodiments, constructing a smart contract call graph using smart contracts in the blockchain system as nodes and the real-time call relationships between the smart contracts as edges includes:
[0025] Using each smart contract in the blockchain system as a node and the real-time call relationship between the smart contracts as an edge, a smart contract call text attribute graph is constructed.
[0026] The smart contract call text attribute graph includes text attribute information for each node, which includes source code and original description text.
[0027] In some embodiments, determining the adjacency matrix of the smart contract call graph includes:
[0028] The weight of each side is determined based on the real-time call frequency between the smart contracts.
[0029] The adjacency matrix of the smart contract call graph is determined based on each side and its weight.
[0030] In some embodiments, the graph attention network includes a feature adaptive fusion layer, a multi-head attention layer, and an output layer;
[0031] Correspondingly, the step of inputting the first fused feature vector of each node and the adjacency matrix of the smart contract call graph into a pre-constructed graph attention network to obtain the embedding vector of each node output by the graph attention network includes:
[0032] The first fused feature vector of each node and the adjacency matrix of the smart contract call graph are input into the feature adaptive fusion layer to obtain the second fused feature vector of each node output by the feature adaptive fusion layer.
[0033] The second fused feature vector of each node is input into the multi-head attention layer to obtain the attention feature vector of each node output by the multi-head attention layer;
[0034] The attention feature vector of each node is input into the output layer to obtain the embedding vector of each node output by the output layer.
[0035] In some embodiments, the training process of the graph attention network includes:
[0036] Using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between the sample smart contracts as a sample edge, a sample smart contract call graph is constructed, and the sample adjacency matrix of the sample smart contract call graph is determined.
[0037] The source code of each sample node is interpreted to obtain the sample code interpretation text of each sample node. The sample code interpretation text of each sample node is vectorized to obtain the sample code semantic feature vector of each sample node. The original sample description text of each sample node is vectorized to obtain the original sample description feature vector of each sample node.
[0038] The sample code semantic feature vector and the original sample description feature vector are fused to obtain the first sample fusion feature vector for each sample node;
[0039] Determine the embedding vector label for each sample node;
[0040] Using the first sample fusion feature vector of each sample node and the sample adjacency matrix of the sample smart contract call graph as training samples, and the embedding vector label of each sample node as sample label, an initial graph attention network is trained. After training, the graph attention network is obtained.
[0041] In some embodiments, the training process of the GPT-4 model includes:
[0042] Using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between the sample smart contracts as a sample edge, a sample smart contract call graph is constructed.
[0043] Obtain the sample source code for each sample node;
[0044] The sample source code of each sample node is preprocessed to obtain the sample code of each sample node.
[0045] Determine the code explanation text label for each sample node;
[0046] Using the sample code of each sample node as the training sample and the code explanation text label of each sample node as the sample label, an initial GPT-4 model is trained. After training, the GPT-4 model is obtained.
[0047] In some embodiments, the training process of the SentenceBERT model includes:
[0048] Using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between the sample smart contracts as a sample edge, a sample smart contract call graph is constructed.
[0049] Obtain the sample source code and the original sample description text of each sample node;
[0050] The source code of each sample node is interpreted to obtain the sample code interpretation text of each sample node;
[0051] Determine the code semantic feature vector label and the original description feature vector label of each sample node;
[0052] The initial SentenceBERT model is trained using the sample code explanation text of each sample node as the training sample and the code semantic feature vector label of each sample node as the sample label. The initial SentenceBERT model is trained using the original sample description text of each sample node as the training sample and the original description feature vector label of each sample node as the sample label. After training, the SentenceBERT model is obtained.
[0053] The present invention provides a method for identifying highly centralized smart contracts in blockchain systems based on a graph-based model. This method constructs a smart contract call graph based on the real-time call relationships of smart contracts within the blockchain system, determines the adjacency matrix of the call graph, interprets the source code of each node to obtain the code interpretation text, vectorizes the code interpretation text and the original description text of each node to obtain the code semantic feature vector and the original description feature vector of each node, fuses these two to obtain the first fused feature vector of each node, and uses a pre-trained graph attention network to fuse the first fused feature vector and the adjacency matrix of each node to obtain the embedding vector of each node. Based on the embedding vector and the adjacency matrix of each node, each node is evaluated. By comprehensively considering the static features and dynamic call relationships of smart contracts, this method identifies highly centralized smart contracts, improving the efficiency and accuracy of highly centralized smart contract identification. Attached Figure Description
[0054] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0055] Figure 1 This is a flowchart illustrating the method for identifying highly centralized smart contracts in a blockchain system based on a graph-large model, as provided in this embodiment of the invention.
[0056] Figure 2 This is a flowchart illustrating the process of interpreting the source code of each node, as provided in an embodiment of the present invention.
[0057] Figure 3 This is a schematic diagram of the process for vectorizing the code explanation text and original description text of each node, provided in an embodiment of the present invention.
[0058] Figure 4 This is a schematic diagram of the process for evaluating each node based on its embedding vector and adjacency matrix, provided in an embodiment of the present invention.
[0059] Figure 5 This is a schematic flowchart illustrating the training process of the graph attention network provided in an embodiment of the present invention. Detailed Implementation
[0060] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0061] The terms "first," "second," etc., used in this invention are used to distinguish similar objects and not to describe a specific order or sequence. It should be understood that such terms can be used interchangeably where appropriate so that embodiments of the invention can be implemented in orders other than those illustrated or described herein, and the objects distinguished by "first" and "second" are generally of the same class, without limiting the number of objects; for example, the first object can be one or more.
[0062] Currently, blockchain system testing mainly faces the following challenges:
[0063] 1) The number of smart contracts is huge: A mature blockchain system may contain hundreds or thousands of smart contracts, and it is time-consuming and labor-intensive to fully test all of them;
[0064] 2) Complex inter-contract call relationships: There are complex call relationships between smart contracts, and a problem in a single contract may affect the entire system;
[0065] 3) Difficulty in resource allocation: Limited testing resources are difficult to allocate reasonably, and it is impossible to determine which contracts require the most focused testing.
[0066] 4) Limitations of static analysis: Traditional static code analysis methods struggle to capture the dynamic characteristics of smart contracts during actual operation;
[0067] 5) Lack of systematic approach: Existing testing methods often lack systematicity and are difficult to fully assess the importance of a contract.
[0068] To address these issues, academia and industry have proposed several solutions. For example, formal verification methods can rigorously prove the correctness of smart contracts, but their application is limited and requires significant expertise. Fuzz testing methods can uncover some potential vulnerabilities in smart contracts, but they struggle to assess the overall importance of the contracts. Graph analysis methods have been introduced into smart contract analysis, constructing contract call graphs to study the relationships between contracts; however, traditional graph analysis methods struggle to handle large-scale, high-dimensional data.
[0069] To address this, this invention provides a method for identifying highly centralized smart contracts in blockchain systems based on a graph-based model. The method constructs a smart contract call graph based on the real-time call relationships of smart contracts within the blockchain system, determines the adjacency matrix of the call graph, interprets the source code of each node to obtain the code interpretation text, and vectorizes the code interpretation text and original description text of each node to obtain the code semantic feature vector and original description feature vector of each node. These two vectors are then fused to obtain the first fused feature vector of each node. A pre-trained graph attention network is used to fuse the first fused feature vector and the adjacency matrix of each node to obtain the embedding vector of each node. Based on the embedding vector and adjacency matrix of each node, each node is evaluated to identify highly centralized smart contracts. This invention utilizes a graph attention network for feature fusion, comprehensively considering both the static features and dynamic call relationships of smart contracts, thus improving the efficiency and accuracy of identifying highly centralized smart contracts.
[0070] Figure 1 This is a flowchart illustrating the method for identifying highly centralized smart contracts in a blockchain system based on a graph-large model, as provided in an embodiment of the present invention. Figure 1As shown, a method for identifying highly centralized smart contracts in a blockchain system based on a graph-large model is provided, including the following steps: steps 110, 120, 130, 140, and 150. These method steps are merely one possible implementation of the present invention.
[0071] Step 110: Using each smart contract in the blockchain system as a node and the real-time call relationship between each smart contract as an edge, construct a smart contract call graph and determine the adjacency matrix of the smart contract call graph.
[0072] A smart contract is a computer protocol that automatically executes contract terms when specific conditions are met. Smart contracts are programs that run on the blockchain, and their code and execution process are transparent to all participants.
[0073] In some embodiments, constructing a smart contract call graph, using smart contracts in a blockchain system as nodes and the real-time call relationships between smart contracts as edges, includes:
[0074] Using smart contracts in the blockchain system as nodes and the real-time call relationships between smart contracts as edges, construct a smart contract call text attribute graph;
[0075] The smart contract call text attribute graph includes text attribute information for each node, which includes source code and original description text.
[0076] Among them, the Text Attributed Graph (TAG) combines the structural information of the graph with the text attribute information of the nodes. In the TAG, each node not only represents an entity, but is also associated with the text attribute information related to that entity.
[0077] Optionally, the text attribute graph of smart contract calls is a weighted directed graph with tags, where the weights of the edges reflect the call frequency between smart contracts.
[0078] The original descriptive text includes at least descriptive text written by the developers, such as comments on the smart contract, docstrings, and interface specifications.
[0079] It is understandable that by using each smart contract in the blockchain system as a node and the real-time call relationship between smart contracts as an edge, a smart contract call text attribute graph can be constructed, which can effectively capture the interdependencies and interaction patterns between smart contracts.
[0080] In some embodiments, determining the adjacency matrix of the smart contract call graph includes:
[0081] The weight of each side is determined based on the real-time call frequency between smart contracts;
[0082] The adjacency matrix of the smart contract call graph is determined based on each side and its weight.
[0083] Optionally, monitoring tools can be used to obtain the real-time call frequency between smart contracts.
[0084] Optionally, determine the list of nodes, assign a unique index to each node, initialize the adjacency matrix, and fill the adjacency matrix according to the call frequency.
[0085] Step 120: Interpret the source code of each node to obtain the code interpretation text of each node; vectorize the code interpretation text of each node to obtain the code semantic feature vector of each node; vectorize the original description text of each node to obtain the original description feature vector of each node.
[0086] Among them, code explanation text refers to the natural language description text of the functional semantics of code.
[0087] Optionally, a code interpreter is used to interpret the source code of each node to obtain the code interpretation text of each node.
[0088] Figure 2 This is a flowchart illustrating the process of interpreting the source code of each node, as provided in an embodiment of the present invention. Figure 2 As shown, in some embodiments, step 120 interprets the source code of each node to obtain the code interpretation text of each node, including:
[0089] Step 121: Obtain the source code of each node;
[0090] Step 122: Preprocess the source code of each node to obtain the code for each node;
[0091] Step 123: Input the code of each node into the pre-trained GPT-4 model to obtain the code explanation text of each node output by the GPT-4 model;
[0092] The GPT-4 model is trained using sample code from sample nodes in the sample smart contract call graph, along with text labels explaining the code of the sample nodes.
[0093] It should be noted that the GPT-4 model is a deep learning model based on the Transformer architecture. The GPT-4 model uses a self-attention mechanism to capture global dependencies in the input sequence, which can generate coherent and context-sensitive text, and is widely used in natural language processing tasks.
[0094] Optionally, the source code of each node is preprocessed, using regular expressions to remove comments and consecutive whitespace characters from the source code.
[0095] Optionally, the length of the input code is limited to 8000 tokens. If the length exceeds this limit, it will be truncated, retaining 4000 tokens at the beginning and 4000 tokens at the end.
[0096] Optionally, use the following fixed prompt template:
[0097] Please analyze the specified smart contract code and generate a structured description, including:
[0098] 1) The main functions of the contract (within 100 words);
[0099] 2) Description of core methods (no more than 50 words for each method);
[0100] 3) Description of key state variables (no more than 30 words for each variable);
[0101] 4) External contract call relationship (within 100 words).
[0102] Understandably, by employing a pre-trained GPT-4 model to gain a deep understanding of the code at each node, it is possible to generate code explanation text containing semantics of code functionality, which helps in subsequent evaluation of the core functionality of smart contracts.
[0103] Figure 3 This is a schematic diagram illustrating the process of vectorizing the code explanation text and original description text of each node, as provided in an embodiment of the present invention. Figure 3 As shown, in some embodiments, step 120 involves vectorizing the code explanation text of each node to obtain the code semantic feature vector of each node, and vectorizing the original description text of each node to obtain the original description feature vector of each node, including:
[0104] Step 124: Obtain the original description text for each node;
[0105] Step 125: Input the code explanation text of each node into the pre-trained SentenceBERT model to obtain the code semantic feature vector of each node output by the SentenceBERT model.
[0106] Step 126: Input the original description text of each node into the SentenceBERT model to obtain the original description feature vector of each node output by the SentenceBERT model.
[0107] The SentenceBERT model is trained using the sample code explanation text and original description text of the sample nodes in the sample smart contract call graph, as well as the code semantic feature vector labels and original description feature vector labels of the sample nodes.
[0108] It should be noted that the SentenceBERT model is a variant of the BERT model for sentence-level tasks. By introducing sentence-level contrastive learning during the pre-training stage, the SentenceBERT model can better capture the semantic relationships between sentences.
[0109] Optionally, the code explanation text of each node is divided into paragraphs with a maximum length of 256 tokens. Each paragraph is independently encoded to obtain a vector for each paragraph. The vectors of all paragraphs are merged using average pooling to obtain the final pooled vector. L2 regularization is used to standardize the final vector to obtain the code semantic feature vector of each node.
[0110] Optionally, the dimension of the code semantic feature vector of each node and the original descriptive feature vector of each node output by the SentenceBERT model is fixed at 768 dimensions.
[0111] Step 130: Fuse the code semantic feature vector and the original description feature vector to obtain the first fused feature vector for each node.
[0112] Optionally, the weights of the code semantic feature vector and the original description feature vector of each node are determined, and the code semantic feature vector and the original description feature vector of each node are fused by weighted averaging to obtain the fused feature vector. The fused feature vector is then L2 regularized to finally generate a 768-dimensional text feature representation.
[0113] Optionally, the weight of the code semantic feature vector is 0.7, and the weight of the original description feature vector is 0.3.
[0114] Step 140: Input the first fusion feature vector of each node and the adjacency matrix of the smart contract call graph into the pre-constructed graph attention network to obtain the embedding vector of each node output by the graph attention network. The graph attention network is trained based on the sample fusion feature vector and sample adjacency matrix of the sample nodes of the sample smart contract call graph, as well as the embedding vector label of the sample nodes.
[0115] In this context, the embedding vector of each node integrates the semantic and structural features of that node.
[0116] It should be noted that Graph Attention Networks (GAT) is a model based on Graph Neural Networks (GNN). GAT improves traditional Graph Convolutional Networks (GCN) by introducing an attention mechanism, enabling each node to dynamically adjust its weights based on the features of its neighboring nodes. This improves the model's ability to identify the importance of different neighboring nodes, and this mechanism helps to capture more complex relationships and patterns in the graph.
[0117] Optionally, graph attention networks have the following characteristics:
[0118] A 4-head attention mechanism is adopted, with each head having an output dimension of 256, resulting in a total of 1024-dimensional features;
[0119] Capable of adaptive weight learning: using the LeakyReLU activation function with a negative slope of 0.2;
[0120] A three-layer residual connection is used: the output of each layer is directly added to the input;
[0121] A feature gating mechanism is introduced through a Gated Recurrent Unit (GRU), with a hidden state dimension of 1024.
[0122] Step 150: Evaluate each node based on its embedding vector and adjacency matrix to identify highly central smart contracts.
[0123] Optionally, a multi-dimensional importance assessment can be performed on each node, such as semantic importance, invocation importance, and structural importance.
[0124] Optionally, based on the embedding vector of each node, the coreness of the function of each node is evaluated, and the semantic importance of each node is determined.
[0125] Optionally, the importance of each node's call can be determined based on its position and connection pattern in the smart contract call graph.
[0126] Optionally, the traditional centrality index data of each node is determined based on the adjacency matrix, and the structural importance of each node is determined based on the traditional centrality index data of each node.
[0127] Understandably, by evaluating the importance of each node in multiple dimensions based on its embedding vector and adjacency matrix, highly central smart contracts can be accurately identified, which helps to allocate test resources reasonably and improve test efficiency.
[0128] Figure 4 This is a schematic diagram illustrating the process of evaluating each node based on its embedding vector and adjacency matrix, as provided in an embodiment of the present invention. Figure 4 As shown, in some embodiments, step 150 evaluates each node based on its embedding vector and adjacency matrix to identify highly central smart contracts, including the following steps:
[0129] Step 151: Based on the adjacency matrix, determine the degree centrality, betweenness centrality, and eigenvector centrality of each node;
[0130] Step 152: Based on the embedding vector of each node and the degree centrality, betweenness centrality and eigenvector centrality of each node, score the centrality of each node and determine the score of each node.
[0131] Step 153: Sort the scores of each node to obtain the sorting results. Based on the sorting results, select multiple high-centrality smart contracts from the smart contracts.
[0132] Degree centrality is a metric that measures the number of nodes directly connected to a node in the smart contract call graph. It represents the number of other nodes directly connected to a node and reflects the total number of times the node is directly called, or the total number of times the node calls other nodes.
[0133] Betweenness centrality refers to the number of times a node acts as an intermediary in the shortest call path between other node pairs in the smart contract call graph, reflecting the important role of the node in the call chain.
[0134] Eigenvector centrality measures a node's centrality based on the importance of its neighboring nodes. A node has a higher eigenvector centrality if it is connected to multiple important nodes.
[0135] Optionally, the weights of the embedding vector, degree centrality, betweenness centrality, and eigenvector centrality are determined, and the total score of the node is calculated based on the weights of the embedding vector, degree centrality, betweenness centrality, and eigenvector centrality, as well as the scores of the embedding vector, degree centrality, betweenness centrality, and eigenvector centrality.
[0136] Optionally, the calculation formula for the node's scoring function is as follows:
[0137] ;
[0138] in, This represents the total score of node i. The score represents the embedding vector of node i. This represents the degree centrality score of node i. This represents the betweenness centrality score of node i. The eigenvector centrality score of node i is represented by .
[0139] Optionally, the scores of each node are sorted in descending order, and the top 10% of nodes are identified as highly central smart contracts.
[0140] In this embodiment of the invention, a smart contract call graph is constructed based on the real-time call relationships of each smart contract in the blockchain system. The adjacency matrix of the smart contract call graph is determined, and the source code of each node is interpreted to obtain the code interpretation text of each node. The code interpretation text and the original description text of each node are vectorized to obtain the code semantic feature vector and the original description feature vector of each node. These two vectors are then fused to obtain the first fused feature vector of each node. A pre-trained graph attention network is used to fuse the first fused feature vector and the adjacency matrix of each node to obtain the embedding vector of each node. Based on the embedding vector and the adjacency matrix of each node, each node is evaluated. By comprehensively considering the static features and dynamic call relationships of smart contracts, highly centralized smart contracts are identified, improving the efficiency and accuracy of highly centralized smart contract identification. This helps to enhance the reliability and security of smart contracts, thereby strengthening the stability and credibility of the entire blockchain system.
[0141] In some embodiments, the graph attention network includes a feature adaptive fusion layer, a multi-head attention layer, and an output layer.
[0142] Correspondingly, the first fused feature vector of each node and the adjacency matrix of the smart contract call graph are input into the pre-constructed graph attention network to obtain the embedding vector of each node output by the graph attention network, including:
[0143] The first fused feature vector of each node and the adjacency matrix of the smart contract call graph are input into the feature adaptive fusion layer to obtain the second fused feature vector of each node output by the feature adaptive fusion layer.
[0144] The second fused feature vector of each node is input into the multi-head attention layer to obtain the attention feature vector of each node output by the multi-head attention layer;
[0145] The attention feature vector of each node is input into the output layer to obtain the embedding vector of each node output by the output layer.
[0146] Optionally, the feature adaptive fusion layer is used to perform a linear transformation on the first fused feature vector (768-dimensional) of each node and the adjacency matrix (3-dimensional) of the smart contract call graph, mapping them to the same dimension (256-dimensional), calculating the attention weights of both, and dynamically adjusting the attention weights of features from different sources to highlight key features. This can balance high-dimensional semantic features and low-dimensional structural features, effectively fusing semantic and structural features.
[0147] Optionally, the formula for calculating attention weights is as follows:
[0148] ;
[0149] in, Indicates attention weights. For querying the matrix, It is the transpose of the key matrix.
[0150] Optionally, the multi-head attention layer includes four attention heads, each focused on capturing the interaction patterns of different types of smart contracts. Each attention head independently calculates an attention score, enabling it to simultaneously focus on multiple dimensions such as call frequency, semantic similarity, and structural importance.
[0151] Optionally, the graph attention network also includes residual connection layers and feature gating layers. By employing residual connection layers and feature gating layers, the attenuation of high-dimensional semantic features during multi-layer transmission can be avoided.
[0152] Optionally, a feature-gating layer is used to control the information flow, and the update rule is as follows:
[0153] ;
[0154] ;
[0155] ;
[0156] in, This represents the hidden state at time t-1. This represents the feature of the input at time t. This represents the weight matrix of the reset gate. This represents the weight matrix of the updated gate. The weight matrix representing the candidate hidden state. This indicates the reset gate, used to control the retention of hidden state information at time t-1. This represents the update gate, used to control the proportion of new information that is updated. This represents the hidden state at time t.
[0157] Optionally, the output layer is used to perform feature dimensionality reduction using a two-stage dimensionality reduction mechanism. First, it merges the multi-head outputs through attention-weighted pooling, and then it compresses the 1024-dimensional features to 256 dimensions through nonlinear transformation. While preserving key semantic information, it provides a data foundation for subsequent evaluation of the importance of smart contracts.
[0158] Figure 5 This is a flowchart illustrating the training process of a graph attention network provided in an embodiment of the present invention. In some embodiments, the training process of the graph attention network includes:
[0159] Step 510: Using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between each sample smart contract as a sample edge, construct a sample smart contract call graph and determine the sample adjacency matrix of the sample smart contract call graph.
[0160] Step 520: Interpret the sample source code of each sample node to obtain the sample code interpretation text of each sample node; perform vectorization processing on the sample code interpretation text of each sample node to obtain the sample code semantic feature vector of each sample node; and perform vectorization processing on the original sample description text of each sample node to obtain the original sample description feature vector of each sample node.
[0161] Step 530: Fuse the sample code semantic feature vector and the original sample description feature vector to obtain the first sample fused feature vector for each sample node;
[0162] Step 540: Determine the embedding vector label for each sample node;
[0163] Step 550: Using the first sample fusion feature vector of each sample node and the sample adjacency matrix of the sample smart contract call graph as training samples, and the embedding vector label of each sample node as sample label, train the initial graph attention network. After training, the graph attention network is obtained.
[0164] Optionally, the first sample fusion feature vector of each sample node and the sample adjacency matrix of the sample smart contract call graph are input into the initial graph attention network to obtain the predicted embedding vector of each sample node output by the initial graph attention network.
[0165] Optionally, the loss function value is calculated based on the predicted embedding vector and the embedding vector label of each sample node.
[0166] Optionally, the parameters of the initial graph attention network can be iteratively optimized based on the loss function value.
[0167] Optionally, the sample source code of each sample node is interpreted to obtain the sample code interpretation text of each sample node, including:
[0168] Obtain the sample source code for each sample node;
[0169] The sample source code of each sample node is preprocessed to obtain the sample code of each sample node.
[0170] The sample code of each sample node is input into the pre-trained GPT-4 model to obtain the sample code explanation text of each sample node output by the GPT-4 model.
[0171] Optionally, the sample code explanation text of each sample node is vectorized to obtain the sample code semantic feature vector of each sample node, and the original sample description text of each sample node is vectorized to obtain the original sample description feature vector of each sample node, including:
[0172] Obtain the original description text of each sample node;
[0173] The sample code explanation text of each sample node is input into the pre-trained SentenceBERT model to obtain the sample code semantic feature vector of each sample node output by the SentenceBERT model.
[0174] The original description text of each sample node is input into the SentenceBERT model to obtain the original description feature vector of each sample node output by the SentenceBERT model.
[0175] Optionally, a sample smart contract call graph is constructed using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between each sample smart contract as a sample edge:
[0176] Using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between each sample smart contract as a sample edge, a sample smart contract call text attribute graph is constructed.
[0177] The sample smart contract call text attribute graph includes sample text attribute information for each sample node, which includes sample source code and sample original description text.
[0178] In some embodiments, the training process of the GPT-4 model includes:
[0179] Using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between each sample smart contract as a sample edge, a sample smart contract call graph is constructed.
[0180] Obtain the sample source code for each sample node;
[0181] The sample source code of each sample node is preprocessed to obtain the sample code of each sample node.
[0182] Determine the code explanation text label for each sample node;
[0183] The initial GPT-4 model is trained using the sample code of each sample node as the training sample and the code explanation text label of each sample node as the sample label. After training, the GPT-4 model is obtained.
[0184] Optionally, a sample smart contract call graph is constructed using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between each sample smart contract as a sample edge:
[0185] Using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between each sample smart contract as a sample edge, a sample smart contract call text attribute graph is constructed.
[0186] The sample smart contract call text attribute graph includes sample text attribute information for each sample node, which includes sample source code and sample original description text.
[0187] In some embodiments, the training process of the SentenceBERT model includes:
[0188] Using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between each sample smart contract as a sample edge, a sample smart contract call graph is constructed.
[0189] Obtain the sample source code and the original sample description text for each sample node;
[0190] The source code of each sample node is interpreted to obtain the sample code interpretation text of each sample node.
[0191] Determine the code semantic feature vector label and the original descriptive feature vector label for each sample node;
[0192] The initial SentenceBERT model is trained using the sample code explanation text of each sample node as the training sample and the code semantic feature vector label of each sample node as the sample label. The initial SentenceBERT model is trained using the original sample description text of each sample node as the training sample and the original description feature vector label of each sample node as the sample label. After training, the SentenceBERT model is obtained.
[0193] Optionally, a sample smart contract call graph is constructed using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between each sample smart contract as a sample edge:
[0194] Using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between each sample smart contract as a sample edge, a sample smart contract call text attribute graph is constructed.
[0195] The sample smart contract call text attribute graph includes sample text attribute information for each sample node, which includes sample source code and sample original description text.
[0196] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0197] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for identifying highly centralized smart contracts in a blockchain system based on a graph-large model, characterized in that, include: Using each smart contract in the blockchain system as a node and the real-time call relationship between the smart contracts as an edge, a smart contract call graph is constructed, and the adjacency matrix of the smart contract call graph is determined. The source code of each node is interpreted to obtain the code interpretation text of each node. The code interpretation text of each node is vectorized to obtain the code semantic feature vector of each node. The original description text of each node is vectorized to obtain the original description feature vector of each node. The code semantic feature vector and the original description feature vector are fused to obtain the first fused feature vector for each node; The first fusion feature vector of each node and the adjacency matrix of the smart contract call graph are input into a pre-constructed graph attention network to obtain the embedding vector of each node output by the graph attention network. The graph attention network is trained based on the sample fusion feature vector and sample adjacency matrix of the sample nodes of the sample smart contract call graph, as well as the embedding vector label of the sample nodes. Each node is evaluated based on its embedding vector and adjacency matrix to identify highly central smart contracts. Determining the adjacency matrix of the smart contract call graph includes: The weight of each side is determined based on the real-time call frequency between the smart contracts. The adjacency matrix of the smart contract call graph is determined based on each side and its weight.
2. The method for identifying highly centralized smart contracts in a blockchain system based on a graph-large model according to claim 1, characterized in that, The process of interpreting the source code of each node to obtain the code interpretation text of each node includes: Obtain the source code of each node; The source code of each node is preprocessed to obtain the code of each node; The code of each node is input into a pre-trained GPT-4 model to obtain the code explanation text of each node output by the GPT-4 model; The GPT-4 model is trained using sample code of sample nodes in a sample smart contract call graph and text labels explaining the code of the sample nodes.
3. The method for identifying highly centralized smart contracts in a blockchain system based on a graph-large model according to claim 1, characterized in that, The process of vectorizing the code explanation text of each node to obtain the code semantic feature vector of each node, and vectorizing the original description text of each node to obtain the original description feature vector of each node, includes: Obtain the original description text for each node; The code explanation text of each node is input into the pre-trained Sentence BERT model to obtain the code semantic feature vector of each node output by the Sentence BERT model. The original description text of each node is input into the Sentence BERT model to obtain the original description feature vector of each node output by the Sentence BERT model. The Sentence BERT model is trained using the sample code explanation text and original description text of the sample nodes in the sample smart contract call graph, as well as the code semantic feature vector labels and original description feature vector labels of the sample nodes.
4. The method for identifying highly centralized smart contracts in a blockchain system based on a graph-large model according to claim 1, characterized in that, The evaluation of each node based on its embedding vector and adjacency matrix to identify highly central smart contracts includes: Based on the adjacency matrix, the degree centrality, betweenness centrality, and eigenvector centrality of each node are determined. The centrality of each node is scored based on its embedding vector, degree centrality, betweenness centrality, and eigenvector centrality, and the score of each node is determined. The scores of each node are sorted to obtain a sorting result. Based on the sorting result, multiple high-centrality smart contracts are selected from the smart contracts.
5. The method for identifying highly centralized smart contracts in a blockchain system based on a graph-large model according to any one of claims 2-4, characterized in that, The construction of a smart contract call graph, using smart contracts in the blockchain system as nodes and the real-time call relationships between these smart contracts as edges, includes: Using each smart contract in the blockchain system as a node and the real-time call relationship between the smart contracts as an edge, a smart contract call text attribute graph is constructed. The smart contract call text attribute graph includes text attribute information for each node, which includes source code and original description text.
6. The method for identifying highly centralized smart contracts in a blockchain system based on a graph-large model according to claim 1, characterized in that, The graph attention network includes a feature adaptive fusion layer, a multi-head attention layer, and an output layer; Correspondingly, the step of inputting the first fused feature vector of each node and the adjacency matrix of the smart contract call graph into a pre-constructed graph attention network to obtain the embedding vector of each node output by the graph attention network includes: The first fused feature vector of each node and the adjacency matrix of the smart contract call graph are input into the feature adaptive fusion layer to obtain the second fused feature vector of each node output by the feature adaptive fusion layer. The second fused feature vector of each node is input into the multi-head attention layer to obtain the attention feature vector of each node output by the multi-head attention layer; The attention feature vector of each node is input into the output layer to obtain the embedding vector of each node output by the output layer.
7. The method for identifying highly centralized smart contracts in a blockchain system based on a graph-large model according to claim 1, characterized in that, The training process of the graph attention network includes: Using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between the sample smart contracts as a sample edge, a sample smart contract call graph is constructed, and the sample adjacency matrix of the sample smart contract call graph is determined. The source code of each sample node is interpreted to obtain the sample code interpretation text of each sample node. The sample code interpretation text of each sample node is vectorized to obtain the sample code semantic feature vector of each sample node. The original sample description text of each sample node is vectorized to obtain the original sample description feature vector of each sample node. The sample code semantic feature vector and the original sample description feature vector are fused to obtain the first sample fusion feature vector for each sample node; Determine the embedding vector label for each sample node; Using the first sample fusion feature vector of each sample node and the sample adjacency matrix of the sample smart contract call graph as training samples, and the embedding vector label of each sample node as sample label, an initial graph attention network is trained. After training, the graph attention network is obtained.
8. The method for identifying highly centralized smart contracts in a blockchain system based on a graph-large model according to claim 2, characterized in that, The training process of the GPT-4 model includes: Using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between the sample smart contracts as a sample edge, a sample smart contract call graph is constructed. Obtain the sample source code for each sample node; The sample source code of each sample node is preprocessed to obtain the sample code of each sample node. Determine the code explanation text label for each sample node; Using the sample code of each sample node as the training sample and the code explanation text label of each sample node as the sample label, an initial GPT-4 model is trained. After training, the GPT-4 model is obtained.
9. The method for identifying highly centralized smart contracts in a blockchain system based on a graph-large model according to claim 3, characterized in that, The training process of the Sentence BERT model includes: Using each sample smart contract in the sample blockchain system as a sample node and the sample call relationship between the sample smart contracts as a sample edge, a sample smart contract call graph is constructed. Obtain the sample source code and the original sample description text of each sample node; The source code of each sample node is interpreted to obtain the sample code interpretation text of each sample node; Determine the code semantic feature vector label and the original description feature vector label of each sample node; The initial Sentence BERT model is trained using the sample code explanation text of each sample node as the training sample and the code semantic feature vector label of each sample node as the sample label. The initial Sentence BERT model is also trained using the original sample description text of each sample node as the training sample and the original description feature vector label of each sample node as the sample label. After training, the Sentence BERT model is obtained.
Citation Information
Patent Citations
Intelligent contract vulnerability detection method based on contract semantic graph and deep feature fusion
CN116561771A
Intelligent contract vulnerability detection method and system based on graph attention network
CN117574385A