CCGRA: Smart Contract Code Comment Generation with Retrieval-enhanced Approach

Zhiyong Feng, Zhenhua Zhang, Shizhan Chen, Guodong Fan, Guang Yang, Zhiyong Feng 0002

Abstract

Smart contracts are foundational to decentralized finance and supply chain management, yet their complexity often makes them difficult to audit and understand. While standard boilerplate code is well-documented, unique user-defined logic frequently lacks descriptive comments. This paper introduces CCGRA (Code Comment Generation with Retrieval-enhanced Approach), a framework that combines a hybrid retrieval module with a pre-trained CodeT5 model. By filtering out template noise and using retrieved comments as exemplars, CCGRA achieves state-of-the-art results in generating high-quality natural language summaries for Solidity smart contracts.

Introduction

The rapid growth of blockchain technology has placed smart contracts at the center of critical infrastructure. However, these contracts are often "black boxes" for traders and auditors due to a lack of clear documentation. A significant challenge in automated code summarization for smart contracts is the prevalence of "template" code (e.g., standard ERC-20 implementations). Existing AI models trained on raw datasets often become biased toward these repetitive patterns, failing to capture the nuances of custom, high-stakes business logic. CCGRA addresses this by employing a retrieval-enhanced strategy that prioritizes unique logic over boilerplate.

Methodology

The CCGRA framework consists of two primary stages: a retrieval module and a generation module.

1. Hybrid Retrieval Module

To find the most relevant context for a given code snippet, CCGRA calculates a "mixed score" based on three dimensions of similarity:

  • Semantic Similarity: Code is transformed into dense vectors using CodeBert and BERT-whitening to capture underlying meaning.
  • Syntactical Similarity: Abstract Syntax Trees (ASTs) are compared using edit distance to identify structural commonalities.
  • Lexical Similarity: Jaccard scores are used to compare the actual text and tokens of the code.

2. Generation Module

The system retrieves the most similar code-comment pair from a large corpus. This pair is then used to construct a prompt for a pre-trained CodeT5 model. Following a format similar to instruction-based models, the input is structured as: summarize Solidity: [retrieved_comment] + [input_code]. This "hint" guides the model to produce more accurate and context-aware summaries.

The CCGRA framework architecture, illustrating the retrieval of knowledge from a training corpus to prompt a pre-trained language model.

Dataset and Preprocessing

The researchers utilized a Solidity dataset from Etherscan.io, applying a rigorous cleaning pipeline to ensure quality:

  • Deduplication: Removal of identical code-comment pairs to prevent data leakage and overfitting.
  • Template Removal: Identification and stripping of common boilerplate code that does not contribute to learning unique logic.

The resulting high-quality corpus contains approximately 29,720 <code, comment> pairs, providing a robust foundation for training and evaluation.

Results and Evaluation

CCGRA was evaluated against eight state-of-the-art baselines, including CodeT5, UniXcoder, and BM25.

Automated Metrics

CCGRA demonstrated superior performance, achieving a BLEU-4 score of 22.20 and a ROUGE-L of 46.32. This represents an improvement over the standard CodeT5 model by approximately 8.4% and 1.0%, respectively.

Human Evaluation

A study involving five master's students assessed the models across three dimensions on a 0-4 scale:

  • Similarity: CCGRA (2.75) vs. CodeT5 (2.05)
  • Naturalness: CCGRA (3.44) vs. CodeT5 (3.15)
  • Informativeness: CCGRA (2.32) vs. CodeT5 (2.15)
Comparison of code comment generation results between CCGRA and baseline models against human-written ground truth.

Discussion and Future Work

The success of CCGRA highlights the importance of addressing the "Template Trap" in smart contract datasets. By cleaning the data and using a hybrid retrieval approach, the model effectively reduces semantic bias. Retrieval-based methods are particularly adept at maintaining language consistency, while pre-trained models excel at capturing semantic meaning; CCGRA successfully hybridizes these strengths.

Future research will focus on more advanced code representations beyond ASTs to capture deeper logic, scaling the dataset to include a wider variety of smart contract patterns, and exploring the application of this retrieval-enhanced logic to other boilerplate-heavy programming languages.

Summary updated

BibTeX
@inproceedings{feng2023ccgra,
  author = {Zhiyong Feng and Zhenhua Zhang and Shizhan Chen and Guodong Fan and Guang Yang and Zhiyong Feng},
  title = {{CCGRA: Smart Contract Code Comment Generation with Retrieval-enhanced Approach}},
  booktitle = {Seke},
  year = {2023},
  doi = {10.18293/seke2023-090},
}
Figure