Domain Specific Code Smells in Smart Contracts

Jiachi Chen, Xin Xia, David Lo, John Grundy, Daniel Xiapu Luo, Ting Chen

Abstract

Smart contracts are programs running on a blockchain. They are immutable to change, and hence can not be patched for bugs once deployed. Thus it is critical to ensure they are bug-free and well-designed before deployment. A Contract defect is an error, flaw or fault in a smart contract that causes it to produce an incorrect or unexpected result, or to behave in unintended ways. The detection of contract defects is a method to avoid potential bugs and improve the design of existing code. Since smart contracts contain numerous distinctive features, such as the gas system, it is important to find smart contract specified defects. To fill this gap, we collected smart-contract-related posts from Ethereum StackExchange, as well as real-world smart contracts. We manually analyzed these posts and contracts; using them to define 20 kinds of contract defects. We categorized them into indicating potential security, availability, performance, maintainability and reusability problems. To validate if practitioners consider these contract as harmful, we created an online survey and received 138 responses from 32 different countries. Feedback showed these contract defects are harmful and removing them would improve the quality and robustness of smart contracts. We manually identified our defined contract defects in 587 real world smart contract and publicly released our dataset. Finally, we summarized 5 impacts caused by contract defects. These help developers better understand the symptoms of the defects and removal priority.

Introduction

Smart contracts are "one-and-done" programs. Once deployed to a blockchain like Ethereum, they are immutable; you cannot simply push a patch if a bug is found. While previous research has focused heavily on high-profile security exploits (like the DAO attack), there has been a lack of systematic study into the broader range of "defects" — errors or design flaws that might not lead to a hack but could cause excessive "gas" (transaction) costs, make code impossible to reuse, or leave a contract "greedy" (able to receive funds but unable to release them). This research establishes a comprehensive taxonomy of 20 distinct smart contract defects on Ethereum, moving beyond simple security vulnerabilities to include performance, maintainability, and reusability issues.

Methodology

The researchers employed a multi-step empirical approach to identify and validate these defects:

  1. Data Mining: They crawled 17,128 posts from Ethereum StackExchange to identify common pain points and errors reported by developers.
  2. Qualitative Analysis: Using Open Card Sorting, they manually analyzed 393 relevant posts to define and categorize 16 defects. They added 4 more by manually inspecting real-world code, resulting in a final list of 20 defect types.
  3. Practitioner Validation: They surveyed 138 developers from 32 countries to confirm if these defects are actually considered harmful in industry.
  4. Large-Scale Labeling: They manually inspected 587 real-world smart contracts (comprising over 231,000 lines of code) to see how prevalent these defects are in the wild.
Example of a StackExchange post used for defect identification, showing the title, description, and comments sections.

A Taxonomy of Smart Contract Defects

The authors define 20 kinds of contract defects, categorized into five pillars: Security, Availability, Performance, Maintainability, and Reusability. This highlights that a "correct" contract isn't just one that isn't hacked, but one that is also cost-efficient and readable.

The complete taxonomy of 20 contract defects and their definitions.

Key insights from this taxonomy include:

  • The "Gas" Factor: Unlike traditional software, performance defects in Solidity have direct financial consequences. For example, using the wrong data type (e.g., byte[] vs bytes) or an inefficient function type can significantly increase the "gas" cost paid by the user.
  • The "Greedy" Contract: A particularly interesting defect is the "Greedy Contract" — a contract that can receive Ether but lacks any function to withdraw or transfer it, effectively locking the funds forever.
  • The Interrupter Paradox: Developers identified "Missing Interrupter" (a way to pause or kill a contract in an emergency) as a major defect. However, this creates a tension with the "decentralized" ideal, as it essentially builds a backdoor for the contract owner.

Key Results

  • Practitioner Agreement: Developers gave the identified defects an average importance score of 4.22/5, confirming that the taxonomy aligns with real-world concerns.
  • Ubiquity of Defects: 99.82% of the 587 analyzed contracts contained at least one defect.
  • High-Risk Flaws: Roughly 32% of contracts contained high-severity defects that could lead to crashes, unauthorized control by attackers, or loss of funds.
  • Common Culprits: The most frequent defects were "Unspecified Compiler Version" (90.6%) and "Missing Interrupter" (89.1%). While these seem minor, they significantly hinder the long-term maintainability and safety of the Ethereum ecosystem.

Conclusion

The authors acknowledge that their manual labeling process, while rigorous, is subject to human error and is difficult to scale. Furthermore, the study focuses exclusively on Solidity and the Ethereum Virtual Machine (EVM). Future work involves developing automated detection tools based on this taxonomy. Currently, existing tools only detect about 7 of the 20 defects identified here, leaving a significant gap for new developer tools to fill.

Summary updated

BibTeX
@article{chen2019domain,
  author = {Jiachi Chen and Xin Xia and David Lo and John Grundy and Daniel Xiapu Luo and Ting Chen},
  title = {{Domain Specific Code Smells in Smart Contracts}},
  journal = {IEEE Trans. Software Eng.},
  year = {2019},
  url = {https://openreview.net/forum?id=VUi3bu8wW-e},
}
Figure