# 以太坊生态中的智能合约设计模式（一）

Abstract—The idea to digitally facilitate contract law and business practices through computer programs has led to the notion of smart contracts. Today’s most prominent smart contract ecosystem is Ethereum, a blockchain based distributed computing platform. Due to the inherent nature of blockchain based contract execution, missing low level programming abstractions, and the constant evolution of platform features and security considerations, writing correct and secure smart contracts for Ethereum is a difficult task. Based on a Multivocal Literature Research and an analysis of the gathered data based on qualitative research methods, we mined a number of design patterns providing design guidelines. We describe those patterns in detail and provide exemplary code for better illustration. Our research shows that the patterns are widely used to address application requirements and common problems. We expect generalizability of some or all of the patterns for other smart contract ecosystems, but this is outside of the scope of this study, which studied only smart contract patterns in Ethereum.

#### I. INTRODUCTION

Bitcoin, which is the most popular cryptocurrency, records transactions in a decentralized data structure called blockchain and supports the feature to encode rules or simple scripts for processing transactions. This feature has evolved to the concept of smart contracts, self-executing computer programs that run on a blockchain to stipulate and enforce the negotiation and execution of (legal) contracts. The blockchain, or more precisely its decentralized nature, assures that contract initiated transactions are autonomously and truthfully executed. Today’s most prominent smart contract ecosystem is Ethereum, a blockchain based distributed computing platform, allowing anyone to write smart contracts with arbitrary rules in the platform’s leading language Solidity.

#### I. 介绍

Despite the increasing popularity of smart contracts, their implementation involves a number of problems. First, rather unconventional programming paradigms are required, because of the inherent characteristics of blockchain-based program execution. For example, programmers have to consider the lack of execution control and the immutable character of smart contracts once they are deployed. Second, due to missing low-level programming abstractions the developer is responsible for the internal organization and manipulation of data at a deeper level. Third, the rapid transformation of platform features and security considerations requires continuous awareness of platform capabilities and potential security risks. Furthermore, smart contracts handle considerable financial values, therefore it is crucial that their implementation is correct and secure against attacks. Given these points, it is beneficial to have a solid foundation of established design and coding guidelines that promote the creation of correct and secure smart contracts, for example in the form of design patterns. Design patterns [1, 2] are a commonly used technique to encode design guidelines or best practices. They express an abstract or conceptual solution to a concrete, complex, and reoccurring problem. So far, design patterns have not received a lot of attention in Ethereum research and information on Solidity design and coding guidelines is scattered among different sources. In previous work [3] we have gathered security related design patterns. In this work, we focus on general design patterns for smart contracts in Ethereum. Our research aims to answer the following two research questions (RQs):


RQ1 Which design patterns commonly appear in the Ethereum ecosystem? RQ2 How do these design patterns map to Solidity coding practices? In order to answer these questions, we followed the Multivocal Literature Research method by Garousi et al. [4] to incorporate practitioners’ experience and applied an analysis of the gathered data based on qualitative research methods (namely Grounded Theory [5] techniques to synthesize the patterns). Our research identified several patterns that pinpoint common issues during the implementation of smart contracts and provide guidance to resolve them. The paper is organised in the following way: First, we discuss the research study design in Section II, before we present design patterns for Solidity in Section III, and discuss our findings in Section IV. Finally, we present related work in Section V, and draw a conclusion in Section VI.


RQ1哪些设计模式经常出现在以太坊生态系统？ RQ2这些设计模式是如何映射到 Solidity 编码实践的？ 为了回答这些问题，我们遵循了 Garousi 等人的“多声文学研究”方法， [4]结合从业者的经验，并基于定性研究方法（即基于扎根理论[5]的综合模式技术）对收集的数据进行了分析。 我们的研究确定了几种模式，这些模式可以在智能合约的实施过程中指出常见问题，并为解决这些问题提供指导。 本文的组织方式如下：首先，我们在第二节中讨论研究设计，然后在第三节中介绍 Solidity 的设计模式，然后在第四节中讨论我们的发现。 最后，我们在第五节介绍相关工作，并在第六节得出结论。

[1] E. Gamma, R. Helm, R. Johnson, and J. Vlissides, Design Patterns: Elements of Reusable Object-oriented Software. Boston, MA, USA: Addison-Wesley Longman Publishing Co., Inc., 1995.

[2] D. C. Schmidt, M. Stal, H. Rohnert, and F. Buschmann, PatternOriented Software Architecture: Patterns for Concurrent and Networked Objects, 2nd ed. New York, NY, USA: John Wiley & Sons, Inc., 2000.

[3] M. Wohrer and U. Zdun, “Smart contracts: Security patterns ¨ in the ethereum ecosystem and solidity,” in 1st International Workshop on Blockchain Oriented Software Engineering @ SANER 2018, March 2018. [Online]. Available: http: //eprints.cs.univie.ac.at/5433/

[4] V. Garousi, M. Felderer, and M. V. Mantyl ¨ a, “The need for ¨ multivocal literature reviews in software engineering: complementing systematic literature reviews with grey literature,” in Proceedings of the 20th International Conference on Evaluation and Assessment in Software Engineering. ACM, 2016.