Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 58 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
58
Dung lượng
0,99 MB
Nội dung
HANOI UNIVERSITY OF SCIENCE AND TECHNOLOGY MASTER THESIS IN DATA SCIENCE AND ARTIFICIAL INTELLIGENCE Optimizing smart contracts on Ethereum NGUYEN QUANG THANG thang.nq202710m@sis.hust.edu.vn Supervisor: Dr Do Ba Lam Institution: —————————————- School of Information and Communication Technology HANOI, 04/2022 Declaration of Authorship and Topic Sentences Personal Information • Full name: Nguyen Quang Thang • Email: thang.nq202710m@sis.hust.edu.vn • Class: Data Science • Tel: 098 669 1647 • Program: Full-time program • This thesis is performed at: School of Information and Communication Technology • This thesis is performed: from 10/04/2021 to 10/04/2022 Goals of the Thesis • Proposing optimizing rules on smart contract source codes using Solidity language • Developing a tool named GasSaver, an automatic tool for detecting the wastinggas Solidity source codes and suggesting solutions • Conducting experiments and evaluating GasSaver Main Tasks of the Thesis • Study on the blockchain, smart contract, and optimizing smart contract gas fee on Ethereum • Collect and analyze the typical wasting gas issues on Solidity smart contract • Present seven rules for reducing the gas fee on the smart contract • Develop a tool name GasSaver, which implements the seven rules above to automatically detect the wasting-gas Solidity source codes and suggest solutions for these issues • Implement experiments and evaluation GasSaver efficiency • Conclude and outline future developments Declaration of Authorship I - Nguyen Quang Thang - hereby warrant that the work and presentation in this thesis are performed by myself under the supervision of Dr Do Ba Lam All results presented in this thesis are truthful and are not copied from any other works Attestation of the Supervisor on the Fulfillment of the Requirements for the Thesis: Supervisor Hanoi, 10th Apr 2022 Author Dr Do Ba Lam Nguyen Quang Thang Acknowledgements I want to acknowledge and give my warmest thanks to my supervisor, Dr Do Ba Lam He is an excellent blockchain researcher, gave me the golden opportunity to this research on optimizing gas fees on the smart contract, which also helped me a lot of research, and I came to know about so many new things Not only that, he is very kind and friendly and inspired me a lot in my research career path I would also like to give a special thanks to my girlfriend Nguyen Thi Tham and my family for their mental support during my thesis writing process Moreover, in the absence of my friends, Tong Van Vinh, Hong Ngoc, Xuan Thu, and Thanh Dat, I could hardly melt away all the tension from my work I also thank Thanks for always accompanying me through ups and downs Abstract Currently, the number of smart contracts using Solidity language in Ethereum is increasing at a breakneck speed In this context, optimizing gas fees in smart contracts becomes necessary to save costs for both developers and users who interact directly with these contracts To optimize gas, developers typically need to have deep knowledge of blockchain and programming languages, making it a significant challenge for new programmers This thesis, we introduce GasSaver – an open-source tool that analyzes Solidity-based contracts and suggests modifications to reduce transaction fees Our tool, which relies on seven rules for identifying non-optimized codes, shows that 6,333 in a total of 10,245 top most interactive contracts on Ethereum contain at least one optimization problem and the problems are more than 30,000 Hence, the use of this tool can reduce a considerable cost for the deployment and interaction of smart contracts Author Nguyen Quang Thang Table of Contents Declaration of Authorship and Topic Sentences Acknowledgement Abstract Lists of Figures Lists of Tables 10 Lists of Listing 11 Background and related work 1.1 Blockchain 1.2 Ethereum and smart contracts 1.2.1 Ethereum blockchain 1.2.2 Smart contracts on Ethereum 1.2.3 Ethereum Virtual Machine (EVM) 1.2.4 Gas and payment 1.2.5 Solidity - a high-level language for 1.3 Related work 1.3.1 Bytecode related work 1.3.2 High-level language related work 1.3.3 Evaluation developing smart contracts Gas-inefficient Programming Patterns 2.1 Data Representation 2.1.1 Pattern 1: Members Arrangement in Struct (MAS) 2.1.2 Pattern 2: Data Location and Function Visibility (DLFV) 2.1.3 Pattern 3: Constant State Variables (CSV) 2.1.4 Pattern 4: Immutable State Variables (ISV) 2.2 Logic Relation 2.2.1 Pattern 5: Operator Reduction in Expression (ORE) 2.2.2 Pattern 6: Repeated Computation in Loop (RCL) 2.2.3 Pattern 7: Loop Combination (LC) 2.3 Sumnary 15 15 17 17 18 19 20 21 23 23 26 26 27 27 27 29 31 32 34 34 35 37 38 Implementation 3.1 System Design 3.2 Rule Implementation 3.2.1 Members Arrangements in Struct 39 39 40 40 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 Data Location and Function Visibility Constant State Variables Immutable State Variables Operator Reduction in Expression Repeated Computation in Loop Loop Combination Experiments and Evaluations 4.0.1 Optimization Findings 4.0.2 Execution Time 4.0.3 Comparison 4.0.4 Evaluation References 42 43 43 44 44 46 48 48 49 49 51 56 List of Figures Opensea - the most prominent digital marketplace for crypto collectibles and non-fungible tokens [1] 13 1.1 1.2 Blockchain model networks Ethereum state machine mechanism 15 17 3.1 3.2 39 3.8 System Overview The example output of GasSaver with Members Arrangements in Struct issue The example output of GasSaver with Data Location and Function Visibility issue The example output of GasSaver with Constant State Variables issue The example output of GasSaver with Immutable State Variables issue The example output of GasSaver with Operator Reduction in Expression issue The example output of GasSaver with Repeated Computation in Loop issue The example output of GasSaver with Loop Combination issue 4.1 4.2 4.3 4.4 4.5 4.6 Distribution of Violations on the First Dataset Distribution of Violations on the Second Dataset Time Executions on the First Dataset Time Executions on the Second Dataset Violations on the First Dataset of GasSaver and Tamara’s tool Time comparison of GasSaver and Tamara’s tool 3.3 3.4 3.5 3.6 3.7 42 43 44 45 46 46 47 49 50 50 51 52 52 List of Tables 1.1 1.2 EVM operation gas cost overview EVM operation gas refund overview 22 23 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 Costs of deployment and function call in Listing 2.1 Costs of deployment and function Call in Listing 2.2 Costs of deployment and function call in Listing 2.3 The difference in assembly code in Listing 2.3 Costs of deployment and function call in Listing 2.4 Costs of deployment and function call in Listing 2.5 Costs of deployment and function call in Listing 2.6 Costs of deployment and function call in Listing 2.7 29 31 32 32 34 35 36 38 4.1 Costs of deployment and function call in SAC Contract 54 10 Figure 3.4: The example output of GasSaver with Constant State Variables issue • This state is not modified in the current contract and derived contracts • The initial state value is only assigned in the constructor or at the initial declaration The solution for this issue is adding the immutable keyword in the state declaration However, as mentioned in the examples, the constant state has more benefits than the immutable state in the contract deployment so the constant variables will take precedence over the immutable variables OlympusDAO: gOHM Token is a smart contract violated Immutable State Variables rule Figure 3.5 describes the output of GasSaver, mainly including wasting codes and solutions 3.2.5 Operator Reduction in Expression Currently, GasSaver only detects issues including (!a&&!b) and (!a||!b) to return !(a||b) or !(a&&b) respectively to minimize gas fees To detect these expressions, GasSaver finds binary operations with an operator of && or ||, having left as an operator ! and right as an operator ! when GasSaver is parsing source codes Then, it transfers the current representation to the equivalently optimized expressions The conditions to decide this expression are very simple, and they can be immediately detected in the parsing This detecting approach will reduce the overlapping of the parsing and analyzing NookToken is a smart contract violated Operator Reduction in Expression rule on line 349 Figure 3.6 describes the output of GasSaver, mainly including wasting codes and solutions 3.2.6 Repeated Computation in Loop To find violations of this rule, we need to identify statements in the loop that not change values when the remaining statements in this loop execute As a 44 Figure 3.5: The example output of GasSaver with Immutable State Variables issue result, these statements can be put outside the loop to minimize gas costs when interacting with the contract To this end, GasSaver filters statements for variable declaration in the loop body On each variable declaration statement, there are two cases as follows: • If the statement has no initial value, the statement can be immediately moved outside the loop • If an initial value is affected by the variables in the loop, it cannot be passed outside the loop The detecting does in the parsing code, the conditions to check only in the loop, reducing the overlapping of the parsing and analyzing The solution lets developers know to pass the variable declaration statements outside the loop body MusdV3 is a smart contract violated Repeated Computation in Loop rule Both of “uint256 upperMargin = 10001e14;” and “uint256 lowerMargin = 9999e14;” variable declaration statements can be moved outside the loop body to reduce the gas 45 Figure 3.6: The example output of GasSaver with Operator Reduction in Expression issue Figure 3.7: The example output of GasSaver with Repeated Computation in Loop issue fee Figure 3.7 describes the output of GasSaver, mainly include wasting codes and solutions 3.2.7 Loop Combination To identify issues related to loop combination, in the parsing Solidity source codes, when GasSaver parses a block, it will check whether consecutive statements are loop statements and have the same loop content If yes, they can be combined to reduce gas costs The solution lets the developers know the position of wasted-gas code, and they can combine it Flasher is a smart contract violated Loop Combination rule Figure 3.8 describes the output of GasSaver, mainly including wasting codes and solutions 46 Figure 3.8: The example output of GasSaver with Loop Combination issue 47 Chapter Experiments and Evaluations This section introduces experiment scenarios, obtains results, and gives evaluations In particular, our effort contains three extensional logic rules selected from existing research and four novel rules of data representation We experiment with two data sets, including: • The dataset provided by Tamara [15]: after filtering out unverified contracts, the number of contracts is 4,615 • The top 15,000 most interacted contracts on February 4th, 2022, according to Dune Analytics [19] After removing unverified contracts, the data set contains 10,245 contracts Firstly, we apply GasSaver with each dataset and introduce the results in Section 4.0.1 Next, we present the optimization time of GasSaver in Section 4.0.2 Then, we compare GasSaver with Tamara et al [15] ’s tool to demonstrate the efficacy of GasSaver in Section 4.0.3 Finally, an evaluation of our tool’s benefits is given in Section 4.0.4 Our experiments are executed on MacBook Pro 2019, 2.3 GHz 8-Core i9, 16 GB RAM, and 1024 GB SSD storage We used NodeJS version v16.10.0 and Solidity Parser library version 0.14.0 4.0.1 Optimization Findings Our experiments show that 2,133 in 4,615 contracts (occupying 46.2%) in the first dataset and 6,333 in 10,245 contracts (occupying 61.8%) in the second dataset contain at least one gas-wasting issue be optimized The distribution of errors are shown in Figures 4.1 and 4.2 The violations are mainly concentrated in the CSV with 1,331 contracts in the first dataset and DLFV with 861 contracts ISV violations are detected at 1,879 in 10,245 contracts in the second dataset These violations appear in Solidity from version 0.6.5, and these contracts not exist in the first data set These results heavily depend on developers’ behavior and the type of violation The violations are mainly concentrated in the Data Representation category because these rules are specifically introduced for Solidity source code, which is 48 close to the wasting-gas problem in the world In the Logic relation category, ORE violations are the most because the detecting condition is simple, and the occurrence of an expensive expression in a smart contract is the highest The violation number is small with RLC and LC patterns because they heavily depend on the particular use cases, so GasSaver only detects some common use cases and ignores the remaining This is the limit of GasSaver, and I will enhance it in the future, handling more particular use cases 5298 Number contracts Number Vulnerability 5000 4000 3261 3000 2000 1331 1000 861 297 201 139 70 MAS DLFV CSV ORE 12 42 25 RCL LC Figure 4.1: Distribution of Violations on the First Dataset 4.0.2 Execution Time Figure 4.3 and 4.4 show the execution time for smart contract optimization We can see that the processing time is 0–7,000 ms, but mainly in the 0–1.000 ms range Some contracts have a longer time due to the implementation of MAS violation detection, GasSaver must resolve the Bin Packing Problem For structs having large members (more than 10), the set of ways to arrange members in the struct is large, so it takes time to figure out how to sort and check the results, creating such time differences 4.0.3 Comparison This section presents the comparison results of GasSaver and Tamara’s tool However, when we performed through the top 15,000 most interacted contracts using Tamara’s tool, most of these contracts got the error because Tamara’s tool is outdated for the newer Solidity version So, we only performed Tamara’s tool through his dataset and optimized three same gas-wasting issue types (Operator Reduction 49 10907 Number contracts Number Vulnerability 10000 8000 7415 6000 4683 4000 3547 2879 1879 2000 MAS 691 604 253 201 DLFV CSV ISV ORE 72 60 RCL LC Figure 4.2: Distribution of Violations on the Second Dataset Time execution 7000 6000 Time (ms) 5000 4000 3000 2000 1000 0 500 1000 1500 Figure 4.3: Time Executions on the First Dataset 50 2000 Time execution 6000 5000 Time (ms) 4000 3000 2000 1000 0 1000 2000 3000 4000 5000 6000 Figure 4.4: Time Executions on the Second Dataset in Expression, Repeated Computation in Loop, Loop Combination) to make a fair comparison Figure 4.5 and 4.6 show the detected gas-wasting issue and execution time when detecting three gas-wasting types of GasSaver and Tamara’s tool, respectively We can see that GasSaver is more effective With Tamara’s tool, they use pythonsolidity-parser library to parse Then they analyze these results to detect gaswasting source code However, with GasSaver, we integrate the analysis in the parsing in solidity parser library, making the analysis easier, removing the overlapping of the parsing and analysis, and increasing the detected gas-wasting issues in the Solidity source code 4.0.4 Evaluation To evaluate the benefits of GasSaver, we select a specific contract at address of 0x984f7B398d577C0ADDE08293a53aE9D3B6b7a5c5 This contract has 35,054 transactions on February 4th, 2022, and is one of the top 50 most interactive contracts 51 139 140 GasSaver Tamara Number vulnerabilities 120 100 80 60 42 40 20 20 13 ORE 14 11 RCL LC Figure 4.5: Violations on the First Dataset of GasSaver and Tamara’s tool 7000 GasSaver Tamara 6000 Time (ms) 5000 4000 3000 2000 1000 0 10 15 20 25 30 35 Figure 4.6: Time comparison of GasSaver and Tamara’s tool 52 contract SAC is ERC721Enumerable, Ownable { // CSV violation address private wallet1 = 0x25eDb46cBB7744De5507ebe50b5086D236B63073; // CSV violation address private wallet2 = 0x827dfa08e6282961b6491851869001cB444b840F; // CSV violation 10 address private wallet3 = 11 0xDf958FE148633B49930775c1E0393594983a128B; 12 // CSV violation 13 address private wallet4 = 14 0x458f5AA4393035f4713bE4EB7176B89f8dD4A6F1; 15 // CSV violation 16 address private wallet5 = 17 0x5f09Bd6Ef7EBDB9CC7d090DC43f9047a2b7A2240; 18 // CSV violation 19 address private wallet6 = 20 0x0308539095797e6F562203C3d8F60Cae82566eBA; 21 // CSV violation 22 address private Authorized = 23 0x7a29d9a21A45E269F1bFFFa15a84c16BA0050E27; 24 // CSV violation 25 uint public SACWhitelistReserve = 3900; 26 // DLFV violation 27 function setBaseURI(string memory baseURI) public 28 onlyAuthorized { 29 baseTokenURI = baseURI; 30 } Listing 4.1: A Sample Contract contract SAC is ERC721Enumerable, Ownable { address private constant wallet1 = 0x25eDb46cBB7744De5507ebe50b5086D236B63073; address private constant wallet2 = 0x827dfa08e6282961b6491851869001cB444b840F; address private constant wallet3 = 0xDf958FE148633B49930775c1E0393594983a128B; address private constant wallet4 = 10 0x458f5AA4393035f4713bE4EB7176B89f8dD4A6F1; 11 address private constant wallet5 = 12 0x5f09Bd6Ef7EBDB9CC7d090DC43f9047a2b7A2240; 13 address private constant wallet6 = 14 0x0308539095797e6F562203C3d8F60Cae82566eBA; 15 address private constant Authorized = 16 0x7a29d9a21A45E269F1bFFFa15a84c16BA0050E27; 17 uint public constant SACWhitelistReserve = 3900; 18 53 19 20 21 function setBaseURI(string calldata baseURI) external onlyAuthorized { baseTokenURI = baseURI; } Listing 4.2: The Optimized Contract Listings 4.1 and 4.2 show an except of the original contract and the corresponding optimized contract given by GasSaver Table 4.1 depicts the gas cost for contract deployment and expenses for interacting with three functions, including setBaseURI, whitelistedMints (WM), and whitelistedVIPMints (WVM) which GasSaver modifies The number of function calls stored Table 4.1: Costs of deployment and function call in SAC Contract Cost UnOptimized Optimized Difference Call number Saving Amount (est) Deployment 5,741,260 5,484,296 256,964 32.89 USD setBaseURI 138,738 138,250 488 0.06 USD WM 194,422 189,522 4,900 4,133 2,592.23 USD WVM 305,017 302,817 2,200 110 28.16 USD in Call number column shows the number of actual interactions to these functions from the deployment time of the contract (i.e., January 29th, 2022) to the analysis time (i.e., February 4th, 2022) As the result, until the analysis time, 2,653.34 USD can be saved in this contract if the developer uses GasSaver to optimize the smart contract before deploying it This table also shows the difficulty of evaluating how much gas is saved before and after optimization This amount of gas depends on each context in the contract, for example, the frequency of function call 54 Conclusion and future plans Blockchain is a promising technology that can change the operations of companies, organizations, and governments over the world Smart contracts are an essential factor in making the success of blockchain Currently, smart contracts are used in a wide range of fields, including finance, arts, and games It is very easy to develop smart contracts because the programming language to create these is similar to the popular language such as Python, Javascript, etc., so gas optimization only occurs when users develop smart contracts be typically ignored With numerous smart contracts have deployed each day, the wasting fee is too much So, the research to automatically detect and suggest the resolves is necessary to help developers optimize smart contracts and save the wasted money This thesis presents seven patterns used to detect inefficient codes when users create or interact with smart contracts and suggest modifications to save gas consumption After that, we introduced an automatic tool named GasSaver, relying on the Solidity Parser library to analyze and optimize Solidity smart contracts Currently, GasSaver detects and finds out that 61.8 % top smart contracts contain wasted codes As a result, the use of GasSaver will bring significant benefits for developers and users In the future, we will enhance the detecting rules of Repeated Computation in Loop and Loop Combination patterns and extend more new patterns in GasSaver As a result, this tool can become more efficient and save more money It is also necessary to support more smart contract languages instead of Solidity and extend with more blockchain networks Finally, we aim to conduct a user survey to collect objective evaluations of the advantages and disadvantages of GasSaver 55 References [1] Blogtienao Opensea Available: https://blogtienao.com/ opensea-da-vuot-qua-mot-trieu-vi-dang-hoat-dong-tren-nen-tang/, 2022 [2] Zvonko Merkaˇs, Davor Perkov, and Vesna Bonin The significance of blockchain technology in digital transformation of logistics and transportation International Journal of E-Services and Mobile Applications (IJESMA), 12(1):1– 20, 2020 [3] Dune able Analytics at Contract deployments per month Avail- https://duneanalytics.com/embeds/329/515/ RqqCse8wDeW3MZKeZFOab2Ju8QA5Q8Itnr1cbLgk, 2021 [4] Dune by Analytics usage Ethereum Available at top contracts and projects https://dune.xyz/msilb7/ Ethereum-Top-Contracts-and-Projects-by-Usage, 2021 [5] Loi Luu, Duc-Hiep Chu, Hrishi Olickel, Prateek Saxena, and Aquinas Hobor Making smart contracts smarter In Proceedings of the 2016 ACM SIGSAC Conference on Computer and Communications Security, CCS ’16, page 254–269, New York, NY, USA, 2016 Association for Computing Machinery [6] Sukrit Kalra, Seep Goel, Mohan Dhawan, and Subodh Sharma Zeus: analyzing safety of smart contracts In Network and Distributed Systems Security (NDSS) Symposium, pages 112, 2018 [7] Christof Ferreira Torres, Julian Schăutte, and Radu State Osiris: Hunting for integer bugs in ethereum smart contracts In Proceedings of the 34th Annual Computer Security Applications Conference, pages 664–676, 2018 56 [8] Lexi Brent, Anton Jurisevic, Michael Kong, Eric Liu, Francois Gauthier, Vincent Gramoli, Ralph Holz, and Bernhard Scholz Vandal: A scalable security analysis framework for smart contracts, 2018 [9] Sergei Tikhomirov, Ekaterina Voskresenskaya, Ivan Ivanitskiy, Ramil Takhaviev, Evgeny Marchenko, and Yaroslav Alexandrov Smartcheck: Static analysis of ethereum smart contracts In 2018 IEEE/ACM 1st International Workshop on Emerging Trends in Software Engineering for Blockchain (WETSEB), pages 9–16, 2018 [10] Bo Jiang, Ye Liu, and W K Chan Contractfuzzer: Fuzzing smart contracts for vulnerability detection In Proceedings of the 33rd ACM/IEEE International Conference on Automated Software Engineering, ASE 2018, page 259–269, New York, NY, USA, 2018 Association for Computing Machinery [11] Imran Ashraf, Xiaoxue Ma, Bo Jiang, and W K Chan Gasfuzzer: Fuzzing ethereum smart contract binaries to expose gas-oriented exception security vulnerabilities IEEE Access, 8:99552–99564, 2020 [12] Ting Chen, Xiaoqi Li, Xiapu Luo, and Xiaosong Zhang Under-optimized smart contracts devour your money In 2017 IEEE 24th International Conference on Software Analysis, Evolution and Reengineering (SANER), pages 442–446, 2017 [13] Elvira Albert, Jes´us Correas, Pablo Gordillo, Guillermo Rom´an-D´ıez, and Albert Rubio Gasol: Gas analysis and optimization for ethereum smart contracts In Armin Biere and David Parker, editors, Tools and Algorithms for the Construction and Analysis of Systems, pages 118–125, Cham, 2020 Springer International Publishing [14] Ting Chen, Youzheng Feng, Zihao Li, Hao Zhou, Xiaopu Luo, Xiaoqi Li, Xiuzhuo Xiao, Jiachi Chen, and Xiaosong Zhang Gaschecker: Scalable analysis for discovering gas-inefficient smart contracts IEEE Transactions on Emerging Topics in Computing, 9(3):14331448, 2021 [15] Tamara Brandstăatter Optimization of solidity smart contracts, 2020 57 [16] Ethereum Layout of state variables in storage Available: https: //docs.soliditylang.org/en/v0.8.11/internals/layout_in_ storage.html, 2021 [17] Ethereum Solidity v0.5.0 breaking changes Available: https: //docs.soliditylang.org/en/v0.5.0/050-breaking-changes html#explicitness-requirements, 2021 [18] Solidity parser for javascript Available: https://www.npmjs.com/ package/@solidity-parser/parser, 2021 [19] Dune Analytics Most called addresses, last weeks, unlimited Available: https://dune.xyz/queries/396720, 2021 58 ... develop smart contracts on Ethereum, such as Solidity, Vyper, etc , and optimize the smart contracts by themselves Secondly, they can rely on third parties for optimizing smart contracts and using... takes a long time for developers to study and have deep knowledge about smart contracts language on Ethereum, especially with newbies The reason is that optimizing gas consumption in smart contracts. .. Section 1.1, then especially on Ethereum blockchain, smart contract knowledge on Ethereum and Solidity- the most popular programming language used to develop smart contracts on Ethereum at Section