计算机科学技术学报 ›› 2022,Vol. 37 ›› Issue (1): 67-82.doi: 10.1007/s11390-021-1674-4

所属专题: Software Systems

• • 上一篇    下一篇

定义和检测智能合约中低效率的Gas模式

  

  • 收稿日期:2021-06-02 修回日期:2021-11-01 接受日期:2021-12-03 出版日期:2022-01-28 发布日期:2022-01-28

Characterizing and Detecting Gas-Inefficient Patterns in Smart Contracts

Que-Ping Kong1 (孔雀屏), Member, CCF, Zi-Yan Wang1 (王子彦), Member, CCF, Yuan Huang2 (黄袁), Member, CCF, Xiang-Ping Chen3,* (陈湘萍), Member, CCF, IEEE, Xiao-Cong Zhou1 (周晓聪), Member, CCF, Zi-Bin Zheng1 (郑子彬), Senior Member, CCF, ACM, IEEE, and Gang Huang4 (黄罡), Distinguished Member, CCF, Member, ACM, IEEE        

  1. 1School of Computer Science and Engineering, Sun Yat-sen University, Guangzhou 510006, China
    2School of Software Engineering, Sun Yat-sen University, Zhuhai 519082, China
    3Guangdong Key Laboratory for Big Data Analysis and Simulation of Public Opinion, School of Communication and Design, Sun Yat-sen University, Guangzhou 510006, China
    4Peking University Shenzhen Graduate School, Shenzhen 518000, China
  • Received:2021-06-02 Revised:2021-11-01 Accepted:2021-12-03 Online:2022-01-28 Published:2022-01-28
  • Contact: Xiang-Ping Chen E-mail:chenxp8@mail.sysu.edu.cn
  • About author:Xiang-Ping Chen is an associate professor at the Sun Yat-sen University, Guangzhou. She got her Ph.D. degree in computer software and theory from the Peking University, Beijing, in 2010. Her research interests include software engineering and mining software repositories.
  • Supported by:
    This work was supported by the Key-Area Research and Development Program of Guangdong Province of China under Grant No.2020B010164002, and the National Natural Science Foundation of China under Grant No.62032025.

1、研究背景
区块链是一种新的中间件,智能合约是在区块链上运行的程序。由于智能合约具有去中心化和不可篡改的特性,各种数字资产可以被灵活地嵌入到智能合约来实现安全和高效的信息交换和价值转移,为供应链、智能家居、电子商务、资产管理等不同行业和领域提供了巨大的机遇。以太坊(第一个支持智能合约的区块链平台)为了避免用户过度消耗资源和确保合约终止,引入了Gas机制,规定合约每执行一个操作码都要消耗一定的Gas,Gas需要使用以太币购买。换句话说,合约执行越多操作码,合约消耗的金钱越多。然而,由于合约开发者对合约运行机制了解不够或者没有意识到一些编码模式会来带不必要的gas消耗,智能合约存在很多优化空间。然而,现有的Gas优化方法集中在字节码层级的窥孔优化,对于每一个基本块的字节码序列,寻找Gas消耗更少语义相同的字节码序列替代它,较少考虑合约开发者引入的低效率Gas模式。
2、目的
本文的研究目标是发现常见的低效率Gas模式,并提出一种自动去除合约中低效率Gas模式的方法,帮助智能合约使用者节约成本,同时提供给Solidity编译器开发者提供优化方案。
3、方法
在本文中,我们总结了6个常见的低效率Gas模式和提出了一个低效率Gas模式的检测和优化方法。具体地,首先从Ethereum Stack Exchange论坛的超过25,000的帖子中人工总结低效率Gas模式。然后,利用抽象语法树检测智能合约中的低效率Gas模式。最后,为这些低效率Gas模式设计了源代码到源代码的优化方法。
4、结果
将上述方法应用到以太坊上160,000个开源的真实智能合约,发现52.75%的合约至少具有一种低效率Gas模式。如果移除合约中所有低效率Gas模式,每个合约至少可以节省0.3美元。
5、结论
智能合约中存在大量低效率Gas模式,这些模式会引发大量的不必要Gas消耗,从而增加使用合约的手续费和合约执行时间。在将来的工作中,我们将从Solidity项目的提交信息中挖掘更多的低效率Gas模式,减少合约开发人员引入的不必要Gas消耗。

关键词: 智能合约, 反模式, 检测, 优化, 实证研究

Abstract: Ethereum blockchain is a new internetware with tens of millions of smart contracts running on it. Different from general programs, smart contracts are decentralized, tamper-resistant and permanently running. Moreover, to avoid resource abuse, Ethereum charges users for deploying and invoking smart contracts according to the size of contract and the operations executed by contracts. It is necessary to optimize smart contracts to save money. However, since developers are not familiar with the operating environment of smart contracts (i.e., Ethereum virtual machine) or do not pay attention to resource consumption during development, there are many optimization opportunities for smart contracts. To fill this gap, this paper defines six gas-inefficient patterns from more than 25,000 posts and proposes an optimization approach at the source code level to let users know clearly where the contract is optimized. To evaluate the prevalence and economic benefits of gas-inefficient patterns, this paper conducts an empirical study on more than 160,000 real smart contracts. The promising experimental results demonstrate that 52.75% of contracts contain at least one gas-inefficient pattern proposed in this paper. If these patterns are removed from the contract, at least 0.30 can be saved per contract.

Key words: smart contract, anti-pattern, detection, optimization, empirical study

[1] Zheng Z, Xie S, Dai H N, Chen W, Chen X, Weng J, Imran M. An overview on smart contracts: Challenges, advances and platforms. Future Generation Computer Systems, 2020, 105: 475-491. DOI: 10.1016/j.future.2019.12.019.
[2] Zheng P, Zheng Z, Wu J, Dai H N. XBlock-ETH: Extracting and exploring blockchain data from Ethereum. IEEE Open Journal of the Computer Society, 2020, 1: 95-106. DOI: 10.1109/OJCS.2020.2990458.
[3] Albert E, Gordillo P, Rubio A, Schett M A. Synthesis of super-optimized smart contracts using Max-SMT. In Proc. the 32nd International Conference on Computer Aided Verification, Jul. 2020, pp.177-200. DOI: 10.1007/978-3-030-53288-8-10.
[4] Nagele J, Schett M A. Blockchain superoptimizer. arXiv:2005.05912, 2020. https://arxiv.org/abs/2005.05912, May 2021.
[5] Chen T, Li Z, Zhou H, Chen J, Luo X, Li X, Zhang X. Towards saving money in using smart contracts. In Proc. the 40th IEEE/ACM International Conference on Software Engineering: New Ideas and Emerging Technologies Results, May 27-Jun. 3, 2018, pp.81-84. DOI: 10.1145/3183399.3183420.
[6] Chen T, Feng Y, Li Z, Zhou H, Luo X, Li X, Xiao X, Chen J, Zhang X. GasChecker: Scalable analysis for discovering gas-inefficient smart contracts. IEEE Transactions on Emerging Topics in Computing, 2020, 9(3): 1433-1448. DOI: 10.1109/TETC.2020.2979019.
[7] Oliva G A, Hassan A E, Jiang Z M. An exploratory study of smart contracts in the Ethereum blockchain platform. Empirical Software Engineering, 2020, 25(3): 1864-1904. DOI: 10.1007/s10664-019-09796-5.
[8] Durieux T, Ferreira J F, Abreu R, Cruz P. Empirical review of automated analysis tools on 47,587 Ethereum smart contracts. In Proc. the 42nd IEEE/ACM International Conference on Software Engineering, Oct. 2020, pp.530-541. DOI: 10.1145/3377811.3380364.
[9] Chen J, Xia X, Lo D, Grundy J, Luo X, Chen T. Defining smart contract defects on Ethereum. IEEE Transactions on Software Engineering, 2022, 48(1): 327-345. DOI: 10.1109/TSE.2020.2989002.
[10] Jiang B, Liu Y, Chan W. ContractFuzzer: Fuzzing smart contracts for vulnerability detection. In Proc. the 33rd IEEE/ACM International Conference on Automated Software Engineering, Sept. 2018, pp.259-269. DOI: 10.1145/3238147.3238177.
[11] Grech N, Kong M, Jurisevic A, Brent L, Scholz B, Smaragdakis Y. MadMax: Surviving out-of-gas conditions in Ethereum smart contracts. Proceedings of the ACM on Programming Languages, 2018, 2(OOPSLA): Article No. 116. DOI: 10.1145/3276486.
[12] Liu C, Liu H, Cao Z, Chen Z, Chen B, Roscoe B. ReGuard: Finding reentrancy bugs in smart contracts. In Proc. the 40th IEEE/ACM International Conference on Software Engineering: Companion, May 27-June 3, 2018, pp.65-68. DOI: 10.1145/3183440.3183495.
[13] Li Z, Wu H, Xu J, Wang X, Zhang L, Chen Z. MuSC: A tool for mutation testing of Ethereum smart contract. In Proc. the 34th IEEE/ACM International Conference on Automated Software Engineering, Nov. 2019, pp.1198-1201. DOI: 10.1109/ASE.2019.00136.
[14] Wang X, Wu H, Sun W, Zhao Y. Towards generating cost-effective test-suite for Ethereum smart contract. In Proc. the 26th IEEE International Conference on Software Analysis, Evolution and Reengineering, Feb. 2019, pp.549-553. DOI: 10.1109/SANER.2019.8668020.
[15] Grech N, Brent L, Scholz B, Smaragdakis Y. Gigahorse: Thorough, declarative decompilation of smart contracts. In Proc. the 41st IEEE/ACM International Conference on Software Engineering, May 2019, pp.1176-1186. DOI: 10.1109/ICSE.2019.00120.
[16] Chen T, Li X, Luo X, Zhang X. Under-optimized smart contracts devour your money. In Proc. the 24th IEEE International Conference on Software Analysis, Evolution and Reengineering, Feb. 2017, pp.442-446. DOI: 10.1109/SANER.2017.7884650.
[17] Tikhomirov S, Voskresenskaya E, Ivanitskiy I, Takhaviev R, Marchenko E, Alexandrov Y. SmartCheck: Static analysis of Ethereum smart contracts. In Proc. the 1st International Workshop on Emerging Trends in Software Engineering for Blockchain, May 27-June 3, 2018, pp.9-16. DOI: 10.1145/3194113.3194115.
[18] Zhang P, Xiao F, Luo X. SolidityCheck: Quickly detecting smart contract problems through regular s. arXiv:1911.09425, 2019. https://arxiv.org/abs/1911.09425, Nov. 2021.
[19] Correas J, Gordillo P, Román-Díez G. Static profiling and optimization of Ethereum smart contracts using resource analysis. IEEE Access, 2021, 9: 25495-25507. DOI: 10.1109/ACCESS.2021.3057565.
[20] Li Z, Chen T H, Yang J, Shang W. DLFinder: Characterizing and detecting duplicate logging code smells. In Proc. the 41st IEEE/ACM International Conference on Software Engineering, May 2019, pp.152-163. DOI: 10.1109/ICSE.2019.00032.
[21] Vassallo C, Proksch S, Gall H C, Di Penta M. Automated reporting of anti-patterns and decay in continuous integration. In Proc. the 41st IEEE/ACM International Conference on Software Engineering, May 2019, pp.105-115. DOI: 10.1109/ICSE.2019.00028.
[22] Afjehei S S, Chen T H, Tsantalis N. iPerfDetector: Characterizing and detecting performance antipatterns in iOS applications. Empirical Software Engineering, 2019, 24(6): 3484-3513. DOI: 10.1007/s10664-019-09703-y.
[23] Dintyala P, Narechania A, Arulraj J. SQLCheck: Automated detection and diagnosis of SQL anti-patterns. In Proc. the 2020 ACM SIGMOD International Conference on Management of Data, Jun. 2020, pp.2331-2345. DOI: 10.1145/3318464.3389754.
[1] 陶琰, 张翼腾, 陈雪锦. 基于立面组件布局关联的建筑立面解析网络[J]. 计算机科学技术学报, 2022, 37(3): 652-665.
[2] 赵泽林, 黄頔, 马晓星. 软件动态更新中对象转换函数的自动测试[J]. 计算机科学技术学报, 2022, 37(1): 50-66.
[3] Ling-Yun Situ, Zhi-Qiang Zuo, Le Guan, Lin-Zhang Wang, Xuan-Dong Li, Jin Shi, Peng Liu. 漏洞区域感知的灰盒模糊测试[J]. 计算机科学技术学报, 2021, 36(5): 1212-1228.
[4] Yu-Wei Wu, Qing-Gang Wang, Long Zheng, Xiao-Fei Liao, Hai Jin, Wen-Bin Jiang, Ran Zheng, Kan Hu. FDGLib:在数据中心中支持高效大规模图计算加速的通信库[J]. 计算机科学技术学报, 2021, 36(5): 1051-1070.
[5] Xiao-Jing Zha, Yin-Shui Xia, Shang-Luan Xie, Zhu-Fei Chu. 面向时延优化的CMOL电路容错映射[J]. 计算机科学技术学报, 2021, 36(5): 1118-1132.
[6] Hui-Ming Tian, Zhu-Fei Chu. 基于具有互补属性器件的反相器优化策略[J]. 计算机科学技术学报, 2021, 36(5): 1145-1154.
[7] Dong-Hui Yang, Zhen-Yu Li, Xiao-Hui Wang, Kavé Salamatian, Gao-Gang Xie. 基于网络搜索欺诈关键词社区结构的网络搜索欺诈检测[J]. 计算机科学技术学报, 2021, 36(5): 1167-1183.
[8] Wen-Jun Yang, Bei-Ji Zou, Kai-Wen Li, Shu Liu. 面向多方向场景文字检测的字符流框架[J]. 计算机科学技术学报, 2021, 36(3): 465-477.
[9] Yu-Jie Yuan, Yukun Lai, Tong Wu, Lin Gao, Li-Gang Liu. 回顾形状编辑技术:从几何角度到神经网络方法[J]. 计算机科学技术学报, 2021, 36(3): 520-554.
[10] Jun Gao, Paul Liu, Guang-Di Liu, Le Zhang. 基于深度学习与波束偏转的穿刺针定位与增强算法[J]. 计算机科学技术学报, 2021, 36(2): 334-346.
[11] Xia-An Bi, Zhao-Xu Xing, Rui-Hui Xu, Xi Hu. 基于影像遗传学数据的发现帕金森症的危险基因和异常脑区的有效WRF框架[J]. 计算机科学技术学报, 2021, 36(2): 361-374.
[12] Zeynep Banu Ozger, Nurgul Yuzbasioglu Uslu. 基于三元组重新排序的有效离散人工蜂群SPARQL查询路径优化[J]. 计算机科学技术学报, 2021, 36(2): 445-462.
[13] Zhi-Xing Li, Yue Yu, Tao Wang, Gang Yin, Xin-Jun Mao, Huai-Min Wang. 基于文本和变更相似度的重复性合并请求检测技术[J]. 计算机科学技术学报, 2021, 36(1): 191-206.
[14] Jason Liu, Pedro Espina, Xian-He Sun. 关于储存系统建模和优化的综述[J]. 计算机科学技术学报, 2021, 36(1): 71-89.
[15] Mohammad Y. Mhawish, Manjari Gupta. 使用机器学习技术和软件度量的代码异味预测与预测分析[J]. 计算机科学技术学报, 2020, 35(6): 1428-1445.
Viewed
Full text


Abstract

Cited

  Shared   
  Discussed   
[1] 周笛;. A Recovery Technique for Distributed Communicating Process Systems[J]. , 1986, 1(2): 34 -43 .
[2] 陈肇雄; 高庆狮;. A Substitution Based Model for the Implementation of PROLOG——The Design and Implementation of LPROLOG[J]. , 1986, 1(4): 17 -26 .
[3] 唐同诰; 招兆铿;. Stack Method in Program Semantics[J]. , 1987, 2(1): 51 -63 .
[4] 包丰;. On the Condition for FSM Being a Scrambler[J]. , 1988, 3(1): 70 -74 .
[5] 陈其明;. Extending the Object-Oriented Paradigm for Supporting Complex Objects[J]. , 1988, 3(2): 113 -130 .
[6] 范植华;. Vectorization for Loops with Three-Forked Jumps[J]. , 1988, 3(3): 186 -202 .
[7] 王能斌; 刘小青; 刘光富;. A Software Tool for Constructing Traditional Chinese Medical Expert Systems[J]. , 1988, 3(3): 214 -220 .
[8] 洪家荣; Carl Uhrik;. The ALFALFA Entomology Pest Identification System[J]. , 1988, 3(4): 251 -262 .
[9] 薛行; 孙钟秀; 周建强; 徐希豪;. A Message-Based Distributed Kernel for a Full Heterogeneous Environment[J]. , 1990, 5(1): 47 -56 .
[10] 徐洁; 李庆南; 黄世泽; 徐江峰;. DFTSNA:A Distributed Fault-Tolerant Shipboard System[J]. , 1990, 5(2): 109 -116 .
版权所有 © 《计算机科学技术学报》编辑部
本系统由北京玛格泰克科技发展有限公司设计开发 技术支持:support@magtech.com.cn
总访问量: