Skip to main content
QUICK REVIEW

[Paper Review] Semantic Code Search for Smart Contracts

Chaochen Shi, Yong Xiang|arXiv (Cornell University)|Nov 28, 2021
Software Engineering ResearchComputer Science26 references5 citations
TL;DR

This paper proposes MM-SCS, a multi-modal semantic code search model for smart contracts that integrates a Contract Elements Dependency Graph (CEDG) to capture control- and data-flow semantics, uses multi-head attention for contextual code embedding, and leverages a fine-tuned ALBERT model for query encoding. MM-SCS achieves an MRR of 0.572 on a 470K (code, docstring) dataset, outperforming state-of-the-art models by up to 59.3%, while maintaining fast inference at 0.34s/query.

ABSTRACT

Semantic code search technology allows searching for existing code snippets through natural language, which can greatly improve programming efficiency. Smart contracts, programs that run on the blockchain, have a code reuse rate of more than 90%, which means developers have a great demand for semantic code search tools. However, the existing code search models still have a semantic gap between code and query, and perform poorly on specialized queries of smart contracts. In this paper, we propose a Multi-Modal Smart contract Code Search (MM-SCS) model. Specifically, we construct a Contract Elements Dependency Graph (CEDG) for MM-SCS as an additional modality to capture the data-flow and control-flow information of the code. To make the model more focused on the key contextual information, we use a multi-head attention network to generate embeddings for code features. In addition, we use a fine-tuned pretrained model to ensure the model's effectiveness when the training data is small. We compared MM-SCS with four state-of-the-art models on a dataset with 470K (code, docstring) pairs collected from Github and Etherscan. Experimental results show that MM-SCS achieves an MRR (Mean Reciprocal Rank) of 0.572, outperforming four state-of-the-art models UNIF, DeepCS, CARLCS-CNN, and TAB-CS by 34.2%, 59.3%, 36.8%, and 14.1%, respectively. Additionally, the search speed of MM-SCS is second only to UNIF, reaching 0.34s/query.

Motivation & Objective

  • To address the semantic gap in existing code search models for smart contracts, especially for specialized blockchain queries.
  • To improve code search accuracy by capturing deep structural dependencies in Solidity code, such as control-flow and data-flow.
  • To enhance model performance under low-data regimes by leveraging a fine-tuned pretrained language model for query encoding.
  • To introduce a novel graph-based modality, the Contract Elements Dependency Graph (CEDG), to represent semantic relationships between code elements.
  • To balance high search accuracy with efficient inference speed in a real-world smart contract code search setting.

Proposed method

  • The model uses a multi-head self-attention network to generate context-aware embeddings for code features, focusing on semantically important elements.
  • A novel Contract Elements Dependency Graph (CEDG) is constructed to encode control-flow and data-flow dependencies between code elements, serving as an additional modality.
  • The query encoder employs a fine-tuned ALBERT model to generate query embeddings, improving semantic understanding even with limited training data.
  • The model fuses multiple modalities—textual tokens (T), abstract syntax tree (AST), function call sequences (F), and CEDG—into a unified representation for similarity computation.
  • Semantic similarity between code and query is computed via cosine distance in a shared vector space, optimized end-to-end.
  • The architecture is trained on a custom dataset of 470K (code, docstring) pairs collected from GitHub and Etherscan.

Experimental results

Research questions

  • RQ1How does the inclusion of a CEDG modality improve semantic code search performance for smart contracts?
  • RQ2To what extent does multi-head attention enhance the model’s ability to focus on key code semantics?
  • RQ3How effective is the fine-tuned ALBERT model in generating query embeddings when training data is limited?
  • RQ4How does MM-SCS compare in accuracy and speed to state-of-the-art models on smart contract code search?
  • RQ5What is the contribution of each modality (T, A, F, AST, CEDG) to the overall performance of the model?

Key findings

  • MM-SCS achieves an MRR of 0.572, significantly outperforming UNIF (MRR 0.426), DeepCS (0.359), CARLCS-CNN (0.418), and TAB-CS (0.501).
  • The model improves by 59.3% over DeepCS in MRR when using the full modality set (T+A+F+CEDG), demonstrating strong performance gains.
  • MM-SCS achieves a search speed of 0.34 seconds per query, second only to UNIF, indicating efficient inference despite high accuracy.
  • The addition of CEDG as a modality increases MRR by 4.9 percentage points over the T+A+F baseline, showing its significant contribution to semantic understanding.
  • In a case study, MM-SCS correctly retrieves a 'burn' function for the query 'destroy tokens of a certain address', while baselines fail due to lack of semantic understanding of synonymous blockchain terms.
  • The model’s performance is robust across different input modalities, with consistent improvements over baselines across all evaluation metrics (SR@1, SR@5, SR@10, MRR).

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.