Large graph model-based block chain system high-centrality intelligent contract identification method
By building a smart contract call graph and using the graph attention network to fusion feature vectors and adjacency matrix, the problem that traditional testing methods are difficult to identify high-center smart contracts is solved, achieving more efficient and accurate identification, and improving the stability and credibility of the blockchain system.
Patent Information
- Application Number
- CN202411601713.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-11
- Publication Date
- 2025-04-18
- Estimated Expiration
- 2044-11-11
AI Technical Summary
Traditional blockchain testing methods only consider the static characteristics of smart contracts, and it is difficult to quickly and accurately identify high-center smart contracts in blockchain systems in complex scenarios of dynamic change.
By constructing a smart contract call graph, the adjacency matrix of the smart contract call graph is determined, the source code of each node is interpreted and vectorized, the code semantic features and original description features are fused, the feature vector and adjacency matrix are fused using a pre-trained graph attention network, the embedding vector is generated, and each node is evaluated based on the embedding vector and adjacency matrix to identify high-centered smart contracts.
It improves the efficiency and accuracy of high-center smart contract identification, and can comprehensively consider the static characteristics and dynamic call relationships of smart contracts, enhancing the stability and credibility of the entire blockchain system.
Smart Images

Figure CN119848825A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of blockchain technology, and in particular to a method for identifying high-centrality smart contracts in a blockchain system based on a large graph model. Background Art
[0002] As a decentralized distributed ledger technology, blockchain technology has been widely used in finance, supply chain, Internet of Things and other fields in recent years. As the core component of the blockchain system, the reliability and security of smart contracts directly affect the stability and credibility of the entire system. However, with the continuous expansion of the scale of blockchain systems, the number of smart contracts in the system is becoming larger and larger, and the calling relationship between smart contracts is becoming increasingly complex. Traditional blockchain testing methods only consider the static characteristics of smart contracts, and it is difficult to quickly and accurately identify high-centrality smart contracts in blockchain systems in dynamically changing complex scenarios. Summary of the invention
[0003] The present invention provides a method for identifying high-centrality smart contracts in a blockchain system based on a large graph model, so as to solve the defect that the traditional blockchain testing method in the prior art only considers the static characteristics of the smart contract and is difficult to quickly and accurately identify high-centrality smart contracts in the blockchain system in dynamically changing complex scenarios.
[0004] The present invention provides a method for identifying high-centrality smart contracts in a blockchain system based on a large graph model, comprising: Taking 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 an adjacency matrix of the smart contract call graph is determined; Interpreting the source code of each node to obtain the code interpretation text of each node, vectorizing the code interpretation 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; Fusing the code semantic feature vector and the original description feature vector to obtain a first fused feature vector for each node; Inputting the first fused feature vector of each node and the adjacency matrix of the smart contract call graph into a pre-built graph attention network to obtain an embedding vector of each node output by the graph attention network, wherein the graph attention network is trained based on the sample fused feature vectors and sample adjacency matrix of the sample nodes of the sample smart contract call graph and the embedding vector labels of the sample nodes; Based on the embedding vector of each node and the adjacency matrix, each node is evaluated to identify high centrality smart contracts.
[0005] In some embodiments, the source code of each node is interpreted to obtain the code interpretation text of each node, including: Obtaining source code of each node; Preprocessing the source code of each node to obtain the code of each node; Input the code of each node into the pre-trained GPT-4 model to obtain the code interpretation text of each node output by the GPT-4 model; The GPT-4 model is trained based on sample codes of sample nodes of a sample smart contract call graph and code explanation text labels of the sample nodes.
[0006] In some embodiments, the vectorizing the code interpretation text of each node to obtain the code semantic feature vector of each node, and the vectorizing the original description text of each node to obtain the original description feature vector of each node include: Obtaining the original description text of each node; Inputting the code interpretation text of each node into a pre-trained SentenceBERT model to obtain a code semantic feature vector of each node output by the SentenceBERT model; Inputting 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; The SentenceBERT model is trained based on the sample code interpretation text and sample original description text of the sample nodes of 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.
[0007] In some embodiments, the step of evaluating each node based on the embedding vector of each node and the adjacency matrix to identify a high-centrality smart contract includes: Determining the degree centrality, betweenness centrality, and eigenvector centrality of each node based on the adjacency matrix; Scoring the centrality of each node according to the embedding vector of each node and the degree centrality, betweenness centrality and eigenvector centrality of each node to determine the score of each node; The scores of each node are sorted to obtain a sorting result, and a plurality of high-centrality smart contracts are screened out from the smart contracts according to the sorting result.
[0008] In some embodiments, the construction of a smart contract call graph using each smart contract in the blockchain system as a node and the real-time call relationship between the smart contracts as an edge includes: Taking 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 of each node, and the text attribute information includes source code and original description text.
[0009] In some embodiments, determining the adjacency matrix of the smart contract call graph includes: Determine the weight of each side based on the real-time call frequency between the smart contracts; Determine an adjacency matrix of the smart contract call graph according to each edge and the weight of each edge.
[0010] In some embodiments, the graph attention network includes a feature adaptive fusion layer, a multi-head attention layer, and an output layer; Correspondingly, the first fused feature vector of each node and the adjacency matrix of the smart contract call graph are input into a pre-built graph attention network to obtain the embedding vector of each node output by the graph attention network, including: Inputting the first fused feature vector of each node and the adjacency matrix of the smart contract call graph into the feature adaptive fusion layer, and obtaining the second fused feature vector of each node output by the feature adaptive fusion layer; Inputting the second fused feature vector of each node 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.
[0011] In some embodiments, the training process of the graph attention network includes: Taking 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 a sample adjacency matrix of the sample smart contract call graph is determined; The sample 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, and the sample original description text of each sample node is vectorized to obtain the sample original description feature vector of each sample node; Fusing the sample code semantic feature vector and the sample original description feature vector to obtain a first sample fusion feature vector of each sample node; Determine the embedding vector label of each sample node; The first sample fusion feature vector of each sample node and the sample adjacency matrix of the sample smart contract call graph are used as training samples, and the embedding vector label of each sample node is used as the sample label to train the initial graph attention network. After the training is completed, the graph attention network is obtained.
[0012] In some embodiments, the training process of the GPT-4 model includes: Taking 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; Get the sample source code of each sample node; Preprocessing the sample source code of each sample node to obtain the sample code of each sample node; Determine a code interpretation text label for each sample node; The sample code of each sample node is used as a training sample, and the code explanation text label of each sample node is used as a sample label to train the initial GPT-4 model. After the training is completed, the GPT-4 model is obtained.
[0013] In some embodiments, the training process of the SentenceBERT model includes: Taking 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; Obtaining a sample source code of each sample node and a sample original description text of each sample node; Explain the sample source code of each sample node to obtain the sample code explanation text of each sample node; Determine a code semantic feature vector label of each sample node and an original description feature vector label of each sample node; The sample code interpretation text of each sample node is used as a training sample, and the code semantic feature vector label of each sample node is used as a sample label to train the initial SentenceBERT model. The sample original description text of each sample node is used as a training sample, and the original description feature vector label of each sample node is used as a sample label to train the initial SentenceBERT model. After the training is completed, the SentenceBERT model is obtained.
[0014] The method for identifying high-centrality smart contracts in a blockchain system based on a graph macro model provided by the present invention constructs a smart contract call graph according to the real-time calling relationship of each smart contract in the blockchain system, determines the adjacency matrix of the smart contract call graph, interprets the source code of each node to obtain the code interpretation text of each node, 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 the two to obtain the first fused feature vector of each node, uses a pre-trained graph attention network to fuse the first fused feature vector of each node with the adjacency matrix to obtain the embedding vector of each node, evaluates each node based on the embedding vector and the adjacency matrix of each node, comprehensively considers the static features and dynamic calling relationship of the smart contract, identifies the high-centrality smart contract, and improves the efficiency and accuracy of identifying the high-centrality smart contract. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0016] Figure 1 It is a flow chart of a method for identifying high-centrality smart contracts in a blockchain system based on a large graph model provided by an embodiment of the present invention; Figure 2 is a schematic diagram of a flow chart of interpreting the source code of each node provided by an embodiment of the present invention; Figure 3 It is a schematic diagram of a flow chart of vectorizing the code interpretation text and the original description text of each node provided by an embodiment of the present invention; Figure 4 is a schematic diagram of a process of evaluating each node based on the embedding vector and adjacency matrix of each node provided by an embodiment of the present invention; Figure 5 It is a flowchart of the training process of the graph attention network provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0017] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0018] The terms "first", "second", etc. in the present invention are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the terms used in this way can be interchangeable under appropriate circumstances, so that the embodiments of the present invention can be implemented in an order other than those illustrated or described herein, and the objects distinguished by "first" and "second" are generally of the same type, and the number of objects is not limited. For example, the first object can be one or more.
[0019] At present, blockchain system testing mainly faces the following challenges: 1) A large number of smart contracts: A mature blockchain system may contain hundreds or thousands of smart contracts, and comprehensive testing of all contracts is time-consuming and labor-intensive; 2) Complex calling relationships between contracts: There are complex calling relationships between smart contracts, and problems with a single contract may affect the entire system; 3) Difficulty in resource allocation: Limited testing resources are difficult to allocate reasonably, and it is impossible to determine which contracts need the most focused testing; 4) Static analysis limitations: Traditional static code analysis methods are difficult to capture the dynamic characteristics of smart contracts in actual operation; 5) Lack of systematic approach: Existing testing methods often lack systematicity and make it difficult to comprehensively assess the importance of contracts.
[0020] In response to these problems, academia and industry have proposed some solutions. For example, methods based on formal verification can strictly prove the correctness of smart contracts, but their application scope is limited and requires a lot of expertise. Fuzz testing-based methods can find some potential vulnerabilities in smart contracts, but it is difficult to evaluate the overall importance of the contract. Graph analysis methods have been introduced into smart contract analysis to study the relationship between contracts by constructing a contract call graph, but traditional graph analysis methods have difficulty processing large-scale, high-dimensional data.
[0021] To this end, an embodiment of the present invention provides a method for identifying high-centrality smart contracts in a blockchain system based on a graph-based model, by constructing a smart contract call graph according to the real-time call relationship of each smart contract in the blockchain system, determining the adjacency matrix of the smart contract call graph, interpreting the source code of each node, obtaining the code interpretation text of each node, vectorizing the code interpretation text and the original description text of each node, obtaining the code semantic feature vector and the original description feature vector of each node, fusing the two, obtaining the first fused feature vector of each node, using a pre-trained graph attention network to fuse the first fused feature vector of each node with the adjacency matrix, obtaining the embedding vector of each node, and evaluating each node based on the embedding vector and the adjacency matrix of each node to identify high-centrality smart contracts. The present invention can use the graph attention network for feature fusion, comprehensively consider the static features and dynamic call relationships of smart contracts, and improve the efficiency and accuracy of high-centrality smart contract identification.
[0022] Figure 1 The flowchart of the method for identifying high-centrality smart contracts in a blockchain system based on a large graph model provided by an embodiment of the present invention is as follows. Figure 1 As shown, a method for identifying high-centrality smart contracts in a blockchain system based on a graph model is provided, comprising the following steps: step 110, step 120, step 130, step 140, and step 150. The method flow steps are only used as a possible implementation of the present invention.
[0023] Step 110: construct a smart contract call graph with each smart contract in the blockchain system as a node and the real-time call relationship between each smart contract as an edge, and determine the adjacency matrix of the smart contract call graph.
[0024] Among them, a smart contract is a computer protocol that automatically executes contract terms when specific conditions are met. A smart contract is a program that runs on the blockchain, and its code and execution process are transparent to all participants.
[0025] In some embodiments, taking each smart contract in the blockchain system as a node and the real-time call relationship between each smart contract as an edge, constructing a smart contract call graph includes: Taking 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 is constructed; Among them, the smart contract call text attribute graph includes the text attribute information of each node, and the text attribute information includes source code and original description text.
[0026] Among them, the Text Attributed Graph (TAG) combines the structural information of the graph with the text attribute information of the nodes. In TAG, each node not only represents an entity, but also is associated with the text attribute information related to the entity.
[0027] Optionally, the smart contract call text attribute graph is a TAG weighted directed graph, and the weights of the edges of the graph reflect the call frequency between smart contracts.
[0028] The original description text includes at least the descriptive text written by the developer, such as the smart contract's comments, documentation strings (docstring) and interface descriptions.
[0029] It can be understood that by constructing a smart contract call text attribute graph with each smart contract in the blockchain system as a node and the real-time call relationship between smart contracts as an edge, the interdependence and interaction patterns between smart contracts can be effectively captured.
[0030] In some embodiments, determining an adjacency matrix of a smart contract call graph includes: Determine the weight of each side based on the real-time call frequency between smart contracts; Based on each edge and the weight of each edge, determine the adjacency matrix of the smart contract call graph.
[0031] Optionally, a monitoring tool is used to obtain the real-time calling frequency between smart contracts.
[0032] Optionally, determine a list of nodes, assign a unique index to each node, initialize an adjacency matrix, and fill the adjacency matrix according to the calling frequency.
[0033] 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.
[0034] Among them, code interpretation text refers to the natural language description text of the code functional semantics.
[0035] Optionally, a code interpreter is used to interpret the source code of each node to obtain a code interpretation text of each node.
[0036] Figure 2 A schematic diagram of a flow chart of interpreting the source code of each node provided by an embodiment of the present invention. Figure 2 As shown, in some embodiments, the source code of each node is interpreted in step 120 to obtain the code interpretation text of each node, including: Step 121, obtaining the source code of each node; Step 122, preprocessing the source code of each node to obtain the code of each node; 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; Among them, the GPT-4 model is trained based on the sample code of the sample nodes of the sample smart contract call graph and the code explanation text labels of the sample nodes.
[0037] It should be noted that the GPT-4 model is a deep learning model based on the Transformer architecture. The GPT-4 model captures global dependencies in the input sequence through the self-attention mechanism, can generate coherent and contextually relevant text, and is widely used in natural language processing tasks.
[0038] Optionally, the source code of each node is preprocessed by using a regular expression to remove comments and consecutive blank characters in the source code.
[0039] Optionally, the input code length is limited to 8000 tokens. If the length exceeds this limit, the code is truncated to retain 4000 tokens at the beginning and end.
[0040] Optionally, use the following fixed prompt template: Please analyze the specified smart contract code and generate a structured description, including: 1) The main functions of the contract (within 100 words); 2) Description of core methods (50 words or less for each method); 3) Description of key state variables (30 words or less for each variable); 4) External contract call relationship (within 100 words).
[0041] It is understandable that by using the pre-trained GPT-4 model to deeply understand the code of each node, a code explanation text containing the functional semantics of the code can be generated, which will help to subsequently evaluate the core degree of the functionality of the smart contract.
[0042] Figure 3 A schematic diagram of a flow chart for vectorizing the code interpretation text and original description text of each node provided by an embodiment of the present invention. Figure 3 As shown, in some embodiments, in step 120, the code interpretation text of each node is vectorized to obtain the code semantic feature vector of each node, and the original description text of each node is vectorized to obtain the original description feature vector of each node, including: Step 124, obtaining the original description text of each node; Step 125: input the code interpretation 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; 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; Among them, the SentenceBERT model is trained based on the sample code interpretation text and sample original description text of the sample nodes of 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.
[0043] It should be noted that the SentenceBERT model is a variant of the BERT model for sentence-level tasks. The SentenceBERT model can better capture the semantic relationship between sentences by introducing sentence-level contrastive learning in the pre-training stage.
[0044] 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 the average pooling method to obtain a final pooled vector, and the final vector is normalized using L2 regularization to obtain a code semantic feature vector for each node.
[0045] Optionally, the dimensions of the code semantic feature vector of each node and the original description feature vector of each node output by the SentenceBERT model are fixed to 768 dimensions.
[0046] Step 130: Fuse the code semantic feature vector and the original description feature vector to obtain a first fused feature vector for each node.
[0047] Optionally, determine the respective weights of the code semantic feature vector and the original description feature vector of each node, fuse the code semantic feature vector and the original description feature vector of each node by weighted averaging to obtain a fused feature vector, perform L2 regularization on the fused feature vector, and finally generate a 768-dimensional text feature representation.
[0048] Optionally, the weight of the code semantic feature vector is 0.7, and the weight of the original description feature vector is 0.3.
[0049] Step 140: Input the first fused feature vector of each node and the adjacency matrix of the smart contract call graph into a pre-built 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 fused feature vectors and sample adjacency matrix of the sample nodes of the sample smart contract call graph, as well as the embedding vector labels of the sample nodes.
[0050] Among them, the embedding vector of each node combines the semantic features and structural features of each node.
[0051] It should be noted that Graph Attention Networks (GAT) is a model based on Graph Neural Network (GNN). GAT improves traditional Graph Convolutional Networks (GCN) by introducing the attention mechanism, so that each node can dynamically adjust the weight according to the characteristics of its neighboring nodes, thereby improving the model's ability to recognize the importance of different neighboring nodes. This mechanism helps to capture more complex relationships and patterns in the graph.
[0052] Optionally, the graph attention network has the following features: A 4-head attention mechanism is used, with the output dimension of each head being 256, generating a total of 1024-dimensional features; Capable of adaptive weight learning: using LeakyReLU activation function with a negative slope of 0.2; Use 3 layers of residual connections: the output of each layer is directly added to the input; The feature gating mechanism is introduced through the Gated Recurrent Unit (GRU), and the hidden state dimension is 1024.
[0053] Step 150: Evaluate each node based on the embedding vector and adjacency matrix of each node to identify high centrality smart contracts.
[0054] Optionally, a multi-dimensional importance evaluation is performed on each node, such as semantic importance, call importance, and structural importance.
[0055] Optionally, based on the embedding vector of each node, the core degree of the function of each node is evaluated to determine the semantic importance of each node.
[0056] Optionally, the call importance of each node is determined based on the position and connection pattern of each node in the smart contract call graph.
[0057] Optionally, traditional centrality indicator data of each node is determined according to the adjacency matrix, and the structural importance of each node is determined according to the traditional centrality indicator data of each node.
[0058] It can be understood that by performing a multi-dimensional importance assessment on each node based on the embedding vector and adjacency matrix of each node, high-centrality smart contracts can be accurately identified, which helps to reasonably allocate test resources and improve test efficiency.
[0059] Figure 4 A schematic diagram of a process for evaluating each node based on the embedding vector and adjacency matrix of each node provided in an embodiment of the present invention. Figure 4 As shown, in some embodiments, in step 150, each node is evaluated based on the embedding vector and adjacency matrix of each node to identify high centrality smart contracts, including the following steps: Step 151, based on the adjacency matrix, determine the degree centrality, betweenness centrality and eigenvector centrality of each node; Step 152: Score the centrality of each node according to the embedding vector of each node and the degree centrality, betweenness centrality and eigenvector centrality of each node to determine the score of each node; Step 153: sort the scores of each node to obtain a sorting result, and select multiple high-centrality smart contracts from each smart contract based on the sorting result.
[0060] Among them, degree centrality is an indicator that measures the number of nodes directly connected to a node in the smart contract call graph, indicating the number of other nodes directly connected to the node, reflecting the total number of times the node is directly called, or the total number of times the node calls other nodes.
[0061] Among them, 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 transmission role of the node in the call chain.
[0062] Among them, the eigenvector centrality measures the centrality of a node based on the importance of its neighboring nodes. If a node is connected to multiple important nodes, its eigenvector centrality is higher.
[0063] Optionally, the respective 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 respective weights of the embedding vector, degree centrality, betweenness centrality and eigenvector centrality, and the respective scores of the embedding vector, degree centrality, betweenness centrality and eigenvector centrality.
[0064] Optionally, the calculation formula of the node's scoring function is as follows: ; in, represents the total score of node i, represents the score of the embedding vector of node i, represents the degree centrality score of node i, represents the betweenness centrality score of node i, Represents the eigenvector centrality score of node i.
[0065] Optionally, the score of each node is sorted from high to low, and the top 10% of nodes are determined to be high-centrality smart contracts.
[0066] In an embodiment of the present invention, a smart contract call graph is constructed according to the real-time call relationship of each smart contract in the blockchain system, an 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 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, the two are fused to obtain the first fused feature vector of each node, the first fused feature vector and the adjacency matrix of each node are fused using a pre-trained graph attention network to obtain the embedding vector of each node, each node is evaluated based on the embedding vector and the adjacency matrix of each node, the static features and dynamic call relationship of the smart contract are comprehensively considered, and the high-centrality smart contract is identified, which improves the efficiency and accuracy of the identification of high-centrality smart contracts, helps to improve the reliability and security of smart contracts, and thus enhances the stability and credibility of the entire blockchain system.
[0067] In some embodiments, the graph attention network includes a feature adaptive fusion layer, a multi-head attention layer, and an output layer.
[0068] Correspondingly, the first fused feature vector of each node and the adjacency matrix of the smart contract call graph are input into the pre-built graph attention network to obtain the embedding vector of each node output by the graph attention network, including: Input the first fused feature vector of each node and the adjacency matrix of the smart contract call graph into the feature adaptive fusion layer to obtain the second fused feature vector of each node output by the feature adaptive fusion layer; Input the second fused feature vector of each node 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.
[0069] Optionally, the feature adaptive fusion layer is used to perform a linear transformation on the first fused feature vector (768 dimensions) of each node and the adjacency matrix (3 dimensions) of the smart contract call graph, map the two to the same dimension (256 dimensions), calculate the attention weights of the two, and dynamically adjust the attention weights of features from different sources to highlight key features. It can balance high-dimensional semantic features and low-dimensional structural features, and effectively fuse semantic features and structural features.
[0070] Optionally, the attention weight is calculated as follows: ; in, represents the attention weight, is the query matrix, is the transposed matrix of the key-value matrix.
[0071] Optionally, the multi-head attention layer includes 4 attention heads, each of which focuses on capturing the interaction patterns of different types of smart contracts. Each attention head independently calculates the attention score and can simultaneously focus on multiple dimensions such as call frequency, semantic similarity, and structural importance.
[0072] Optionally, the graph attention network also includes a residual connection layer and a feature gating layer. By adopting the residual connection layer and the feature gating layer, the attenuation of high-dimensional semantic features in multi-layer transmission can be avoided.
[0073] Optionally, a feature gating layer is used to control the information flow, and the update rule is: ; ; ; in, represents the hidden state at time t-1, represents the input feature at time t, represents the weight matrix of the reset gate, represents the weight matrix of the update gate, The weight matrix representing the candidate hidden states, Represents the reset gate, which is used to control the information retention of the hidden state at time t-1. represents the update gate, which is used to control the update ratio of new information. represents the hidden state at time t.
[0074] Optionally, the output layer is used to perform feature dimensionality reduction using a two-stage dimensionality reduction mechanism. Multi-head outputs are first merged through attention weighted pooling, and then the 1024-dimensional features are compressed to 256 dimensions through nonlinear transformation. While retaining key semantic information, it provides a data basis for subsequent importance assessment of smart contracts.
[0075] Figure 5 A flowchart of a 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: Step 510: Taking 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, and a sample adjacency matrix of the sample smart contract call graph is determined; Step 520: interpret the sample source code of each sample node to obtain the sample code interpretation text of each sample node, vectorize the sample code interpretation text of each sample node to obtain the sample code semantic feature vector of each sample node, and vectorize the sample original description text of each sample node to obtain the sample original description feature vector of each sample node; Step 530: Fuse the sample code semantic feature vector and the sample original description feature vector to obtain a first sample fusion feature vector of each sample node; Step 540: Determine the embedding vector label of each sample node; Step 550: Use 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 use the embedded vector label of each sample node as the sample label to train the initial graph attention network. After the training is completed, a graph attention network is obtained.
[0076] 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; Optionally, a loss function value is calculated based on the predicted embedding vector of each sample node and the embedding vector label of each sample node.
[0077] Optionally, the parameters of the initial graph attention network are iteratively optimized according to the loss function value.
[0078] Optionally, the sample source code of each sample node is interpreted to obtain a sample code interpretation text of each sample node, including: Get the sample source code of each sample node; Preprocessing the sample source code of each sample node to obtain the sample code of each sample node; 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.
[0079] Optionally, vectorizing the sample code interpretation text of each sample node to obtain the sample code semantic feature vector of each sample node, and vectorizing the sample original description text of each sample node to obtain the sample original description feature vector of each sample node, including: Obtain the sample original description text of each sample node; Input the sample code interpretation text of each sample node into the pre-trained SentenceBERT model to obtain the sample code semantic feature vector of each sample node output by the SentenceBERT model; The sample original description text of each sample node is input into the SentenceBERT model to obtain the sample original description feature vector of each sample node output by the SentenceBERT model.
[0080] Optionally, the sample smart contracts in the sample blockchain system are taken as sample nodes, and the sample call relationships between the sample smart contracts are taken as sample edges to construct a sample smart contract call graph: Taking 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; Among them, the sample smart contract call text attribute graph includes the sample text attribute information of each sample node, and the sample text attribute information includes the sample source code and the sample original description text.
[0081] In some embodiments, the training process of the GPT-4 model includes: Taking 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; Get the sample source code of each sample node; Preprocessing the sample source code of each sample node to obtain the sample code of each sample node; Determine the code explanation text label for each sample node; The sample code of each sample node is used as the training sample, and the code explanation text label of each sample node is used as the sample label to train the initial GPT-4 model. After the training is completed, the GPT-4 model is obtained.
[0082] Optionally, the sample smart contracts in the sample blockchain system are taken as sample nodes, and the sample call relationships between the sample smart contracts are taken as sample edges to construct a sample smart contract call graph: Taking 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; Among them, the sample smart contract call text attribute graph includes the sample text attribute information of each sample node, and the sample text attribute information includes the sample source code and the sample original description text.
[0083] In some embodiments, the training process of the SentenceBERT model includes: Taking 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; Obtaining the sample source code of each sample node and the sample original description text of each sample node; Explain the sample source code of each sample node to obtain the sample code explanation text of each sample node; Determine a code semantic feature vector label of each sample node and an original description feature vector label of each sample node; The sample code explanation text of each sample node is used as the training sample, and the code semantic feature vector label of each sample node is used as the sample label to train the initial SentenceBERT model. The sample original description text of each sample node is used as the training sample, and the original description feature vector label of each sample node is used as the sample label to train the initial SentenceBERT model. After the training is completed, the SentenceBERT model is obtained.
[0084] Optionally, the sample smart contracts in the sample blockchain system are taken as sample nodes, and the sample call relationships between the sample smart contracts are taken as sample edges to construct a sample smart contract call graph: Taking 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; Among them, the sample smart contract call text attribute graph includes the sample text attribute information of each sample node, and the sample text attribute information includes the sample source code and the sample original description text.
[0085] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0086] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for identifying high-centrality smart contracts in a blockchain system based on a large graph model, characterized in that: include: Taking 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 an adjacency matrix of the smart contract call graph is determined; Interpreting the source code of each node to obtain the code interpretation text of each node, vectorizing the code interpretation 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; Fusing the code semantic feature vector and the original description feature vector to obtain a first fused feature vector for each node; Inputting the first fused feature vector of each node and the adjacency matrix of the smart contract call graph into a pre-built graph attention network to obtain an embedding vector of each node output by the graph attention network, wherein the graph attention network is trained based on the sample fused feature vectors and sample adjacency matrix of the sample nodes of the sample smart contract call graph and the embedding vector labels of the sample nodes; Based on the embedding vector of each node and the adjacency matrix, each node is evaluated to identify high centrality smart contracts.
2. According to claim 1, the method for identifying high-centrality smart contracts in a blockchain system based on a large graph model is characterized in that: The source code of each node is interpreted to obtain the code interpretation text of each node, including: Obtaining source code of each node; Preprocessing the source code of each node to obtain the code of each node; Input the code of each node into the pre-trained GPT-4 model to obtain the code interpretation text of each node output by the GPT-4 model; The GPT-4 model is trained based on sample codes of sample nodes of a sample smart contract call graph and code explanation text labels of the sample nodes.
3. According to claim 1, the method for identifying high-centrality smart contracts in a blockchain system based on a large graph model is characterized in that: The vectorizing of the code interpretation text of each node to obtain the code semantic feature vector of each node, and the vectorizing of the original description text of each node to obtain the original description feature vector of each node include: Obtaining the original description text of each node; Inputting the code interpretation text of each node into a pre-trained SentenceBERT model to obtain a code semantic feature vector of each node output by the SentenceBERT model; Inputting 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; The SentenceBERT model is trained based on the sample code interpretation text and sample original description text of the sample nodes of 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. According to claim 1, the method for identifying high-centrality smart contracts in a blockchain system based on a large graph model is characterized in that: The step of evaluating each node based on the embedding vector of each node and the adjacency matrix to identify a high centrality smart contract includes: Determining the degree centrality, betweenness centrality, and eigenvector centrality of each node based on the adjacency matrix; Scoring the centrality of each node according to the embedding vector of each node and the degree centrality, betweenness centrality and eigenvector centrality of each node to determine the score of each node; The scores of each node are sorted to obtain a sorting result, and a plurality of high-centrality smart contracts are screened out from the smart contracts according to the sorting result.
5. The method for identifying high-centrality smart contracts in a blockchain system based on a graph-based model according to any one of claims 2 to 4, characterized in that: The construction of a smart contract call graph using each smart contract in the blockchain system as a node and the real-time call relationship between the smart contracts as an edge includes: Taking 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 of each node, and the text attribute information includes source code and original description text.
6. The method for identifying high-centrality smart contracts in a blockchain system based on a graph-based model according to claim 1 is characterized in that: Determining the adjacency matrix of the smart contract call graph includes: Determine the weight of each side based on the real-time call frequency between the smart contracts; Determine an adjacency matrix of the smart contract call graph according to each edge and the weight of each edge.
7. The method for identifying high-centrality smart contracts in a blockchain system based on a graph-based model according to claim 1 is characterized in that: The graph attention network includes a feature adaptive fusion layer, a multi-head attention layer and an output layer; Correspondingly, the first fused feature vector of each node and the adjacency matrix of the smart contract call graph are input into a pre-built graph attention network to obtain the embedding vector of each node output by the graph attention network, including: Inputting the first fused feature vector of each node and the adjacency matrix of the smart contract call graph into the feature adaptive fusion layer, and obtaining the second fused feature vector of each node output by the feature adaptive fusion layer; Inputting the second fused feature vector of each node 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.
8. The method for identifying high-centrality smart contracts in a blockchain system based on a graph-based model according to claim 1 is characterized in that: The training process of the graph attention network includes: Taking 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 a sample adjacency matrix of the sample smart contract call graph is determined; The sample 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, and the sample original description text of each sample node is vectorized to obtain the sample original description feature vector of each sample node; Fusing the sample code semantic feature vector and the sample original description feature vector to obtain a first sample fusion feature vector of each sample node; Determine the embedding vector label of each sample node; The first sample fusion feature vector of each sample node and the sample adjacency matrix of the sample smart contract call graph are used as training samples, and the embedding vector label of each sample node is used as the sample label to train the initial graph attention network. After the training is completed, the graph attention network is obtained.
9. The method for identifying high-centrality smart contracts in a blockchain system based on a graph-based model according to claim 2 is characterized in that: The training process of the GPT-4 model includes: Taking 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; Get the sample source code of each sample node; Preprocessing the sample source code of each sample node to obtain the sample code of each sample node; Determine a code interpretation text label for each sample node; The sample code of each sample node is used as a training sample, and the code explanation text label of each sample node is used as a sample label to train the initial GPT-4 model. After the training is completed, the GPT-4 model is obtained.
10. The method for identifying high-centrality smart contracts in a blockchain system based on a graph-based model according to claim 3 is characterized in that: The training process of the SentenceBERT model includes: Taking 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; Obtaining a sample source code of each sample node and a sample original description text of each sample node; Explain the sample source code of each sample node to obtain the sample code explanation text of each sample node; Determine a code semantic feature vector label of each sample node and an original description feature vector label of each sample node; The sample code interpretation text of each sample node is used as a training sample, and the code semantic feature vector label of each sample node is used as a sample label to train the initial SentenceBERT model. The sample original description text of each sample node is used as a training sample, and the original description feature vector label of each sample node is used as a sample label to train the initial SentenceBERT model. After the training is completed, the SentenceBERT model is obtained.
Citation Information
Patent Citations
Contract-based privacy evidence storage method and device
CN114172667A
Intelligent contract vulnerability detection method based on contract semantic graph and deep feature fusion
CN116561771A
Block chain core network construction method and device
CN116668452A
Intelligent contract vulnerability detection method and system based on graph attention network
CN117574385A
Vulnerability detection method and device for smart contract, equipment and medium
CN118536119A