EVMuncher: Anti-Patterns in Smart Contracts finden

Von Jacek Varky
19. December 2019

In this article I introduce EVMuncher. EVMuncher is an open source tool that checks Smart Contracts for non-optimized code. It shows how much gas is wasted. To understand how EVMuncher works, I first explain the following points:

  • How does a Smart Contract work in detail?
  • What role does gas play?
  • What is an anti-pattern?

Then I describe how EVMuncher works.

How does a Smart Contract work?

There are many tutorials about writing Smart Contracts. Everyone can read such a tutorial and write a Smart Contract afterwards. But what happens behind the scenes is not described in most tutorials. To understand how EVMuncher works, this knowledge is necessary.

A Smart Contract is usually written in a high-level language for Smart Contracts. Thus it is far away from machine language in complexity and abstraction. This also means that the written commands are not understood by the machine to be executed at first; they first have to be converted into the respective machine language by a compiler. In the case of the Ethereum Blockchain, the machine to be executed is the Ethereum Virtual Machine, or EVM for short.

Let's look at an example.

On the left side we see the Solidity Code for a simple Smart Contract. On the right side we see the same Smart Contract in machine language. Each of the numbers represents either a command or a date. This is exactly what the EVM works with.

Now, when the get() function of the Smart Contract is executed, the EVM goes through the gibberish of numbers, jumps to the point where the get() function is described in machine language, and executes the corresponding code. The complete instruction set of the EVM can be found in the Yellow Paper (link).

Stack-based Architecture

To understand how the EVM processes the commands, we need to understand the architecture of the EVM.

The EVM is based on a stack-based architecture: Here, all data required for an instruction is populated by the stack, processed and the result is pushed back onto the stack. An example is shown in the following figure.

Here first the value 5 and then the value 2 is pushed onto the stack. In the next step the add command is executed. For this the previously pushed values are popped from the stack, added and the result, here the 7, is pushed onto the stack again.

Description of Gas

Each command executed by the EVM costs something. For example, if you want to execute the add command, you have to pay three units to the miner. These units are called gas in the Ethereum Blockchain.

If a user wants to execute a Smart Contract, the user must provide enough gas to allow the miner to execute the Smart Contract and store the new status of the Smart Contract in the block chain. If the user does not provide enough gas, the EVM is unable to execute all of the code; it aborts and the smart contract is reset to its old state.

What is an Anti-Pattern?

In software development, anti-patterns describe solutions with a poor programming style.

However, anti-patterns are not limited to the programming style; they also exist in the translation from high-level languages to machine languages.

Antipattern       Pattern
  add                   pop
  pop                   pop

On the left side is an Anti-Pattern, on the right side you can see the matching Pattern. The Anti-Pattern uses the add command. The command takes the top two values from the stack, adds these values and pushes the result onto the stack. Once the add command is executed, the pop command is executed, which removes the topmost value from the stack. This means that the result of the add command is discarded. We notice that when add and pop are combined, two values are removed from the stack at the end. We might as well use two pop commands.

Since the add command costs three gas and pop two gas, we can save one gas with the Pattern compared to the Anti-Pattern. Our tool EVMuncher reveals such savings.

How EVMuncher works

EVMuncher analyzes the machine code of a Smart Contract for anti-patterns. Chen identified the anti-patterns in the paper 'Towards Saving Money in Using Smart Contracts' (here online at IEEE after login). He implemented 24 anti-patterns in a tool called GasReducer. However, this tool is not available. We decided to write a tool ourselves and provide open source: That's how EVMuncher was born.

Smart Contract developers can enter a Smart Contract address at, EVMuncher gets the machine code of the Smart Contract from the Ethereum Blockchain. This machine code is then analyzed for anti-patterns. At the end of the analysis the tool shows the user the amount of gas wasted.

Gas-saving programming

Each time a smart contract is executed, gas is paid to the miner. If you look at the exchange rate between gas and the euro or dollar, it looks like negligible sums. Nevertheless, the Ethereum block chain has the ambition to process many more transactions in the future and to be the backbone of the web. With millions of transcations and smart contract executions daily, the amount of wasted gas adds up. That's why we want to use the tool to encourage developers to work in a resource-saving manner and to be economical with user input.

Add new comment

Public Comment form

  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd><p><h1><h2><h3>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

ME Landing Page Question