Abstract
Smart contracts have recently attracted much attention from academia and industry. They are computerized transaction protocols built on top of the blockchain technology, which provides a platform for automatically executing contracts in an anonymous, distributed, and trusted way. In essence, smart contracts are computer programs which may contain vulnerabilities. The problem is magnified by the fact that smart contracts, unlike ordinary programs, cannot be patched easily once deployed. Thus, it is crucial to ensure the correctness of smart contracts before deployment. At the same time, users are charged with fees, a.k.a. gas in Ethereum, when they create, deploy or execute smart contracts. How we reduce such gas consumption without sacrificing the security of smart contracts also matters. In this thesis, we address three problems regarding to the security and cost of smart contracts and explain the motivations and approaches one by one. Firstly, we propose an alternative approach to find the vulnerabilities through automatically identifying critical program paths (with multiple function calls including inter-contract function calls) in a smart contract, rank the paths according to their criticalness, discard them if they are infeasible or otherwise present them with user friendly warnings for user inspection. To achieve a high assurance about the correctness of smart contracts, we further develop formal verifiers based on a novel combination of lazy annotation and automatic loop invariant learning techniques to verify the smart contracts at function level and contract level. A concept of call invariant (which maintains before and after the external function call) is proposed to facilitate the verification of smart contracts with inter-contract function calls. Patterns and features which are specific to smart contracts are used to facilitate invariant learning. Based on the above analyses, lastly, we present a tool to optimize the gas consumption automatically without compromising the functionality and security of smart contracts to minimize the cost by users.