# Toward Vulnerability Detection for Ethereum Smart Contracts Using Graph-Matching Network
## 4 Methodology
### Graph Similarity Learning
#### 4.3.1 Contract Graph Embedding  
So far, we transformed a smart contract to its graph representation. In ASG, each node represents a statement of the source code, while each edge represents a control or data dependency in the code. In BASG, each node represents a basic block of the bytecode, while each edge represents a control dependency. Although such a contract graph is able to represent plentiful syntactic and semantic information of the program, it is usually in a high-dimension space and can hardly be processed by a deep neural network. Thus, it has to be further converted to a vector representation, which is formally called embedding.  
We use a gated graph neural network (GGNN) for graph embedding. Given a contract graph \(G=(V,E)\), \(\mathbf{x}_{i}\) and \(\mathbf{x}_{i,j}\) are feature vectors of node \(v_{i}\) and edge \(e_{i,j}\), respectively. Then, these features are encoded through separate multi-layer perceptrons (MLPs):  
\[\begin{split}\mathbf{h}_{i}^{(0)}&=\mathrm{MLP}_{ \mathrm{node}}(\mathbf{x}_{i}),\quad\forall i\in V\\ \mathbf{e}_{ij}&=\mathrm{MLP}_{\mathrm{edge}}( \mathbf{x}_{ij}),\quad\forall(i,j)\in E,\end{split} \tag{1}\]  
where \(\mathbf{h}_{i}^{(0)}\) is the embedding of node \(v_{i}\) in the initial state (state 0), and \(\mathbf{e}_{ij}\) is the embedding assigned to edge \(e_{i,j}\). The hidden state of nodes are updated in the message passing step by  
\[\begin{split}\mathbf{m}_{j\to i}&=f_{\mathrm{ message}}\Big{(}\mathbf{h}_{i}^{(t)},\mathbf{h}_{j}^{(t)},\mathbf{e}_{ij} \Big{)},\quad\forall(i,j)\in E\\ \mathbf{h}_{i}^{(t+1)}&=f_{\mathrm{update}}( \mathbf{h}_{i}^{(t)}.\sum_{j}\mathbf{m}_{j\to i}),\end{split} \tag{2}\]where \(f_{\text{message}}\) is a message function that collects messages from node \(v_{i}\)'s neighborhoods, and \(f_{\text{update}}\) is a node update function that combines these messages and the last hidden state of node embedding. In this paper, we use an MLP and a gated recurrent unit (GRU) for the two functions, respectively. At each iteration \(t\) of the GNN, \(\sum_{j}\mathbf{m}_{j\to i}\) aggregates the set of embeddings of \(v_{i}\)'s neighborhoods through a simple \(sum\) function. Based on that, \(f_{\text{update}}\) generates the updated embedding \(\mathbf{h}_{i}^{(t+1)}\) for each node. After \(T\) iterations of the GNN message passing, a set of embeddings of all nodes are obtained and denoted as \(\left\{\mathbf{h}_{i}^{(T)}\right\}_{i\in V}\). To compute the graph-level representation for a contract, we follow the readout function proposed in [33]:  
\[\mathbf{h}_{G}=f_{\text{G}}\Big{(}\left\{\mathbf{h}_{i}^{(T)}\right\}_{i\in V }\Big{)}=\text{MLP}_{\text{G}}\Bigg{(}\sum_{i\in V}\sigma\Big{(}\text{MLP}_{ \text{gate}}\Big{(}\mathbf{h}_{i}^{(T)}\Big{)}\Big{)}\bigodot\text{MLP}\Big{(} \mathbf{h}_{i}^{(T)}\Big{)}\Bigg{)}, \tag{3}\]  
which leverages the weighted sum with gating vectors to aggregate across nodes. Note that given the graph representations \(\mathbf{h}_{G_{1}}\) and \(\mathbf{h}_{G_{2}}\) for two graphs \(G_{1}\) and \(G_{2}\), their similarity can be calculated using metrics such as Euclidean, Hamming, or cosine similarities, and thus can already be used for vulnerability detection, which will be investigated in Section 5.5.