How to test Ethereum smart contracts: audit best practices
Tags: blockchain, security, testing
Published: 2019-07-26 12:30:10

When developing an <a href="https://github.com/ethereum">Ethereum</a> blockchain-based project, the first thing to do is to ensure smart contract security and code quality. Otherwise, you may lose a huge amount of money. 

As you might have heard, the Parity Ethereum wallet lost $30 million because of having code vulnerabilities. For this reason, Satoshi Pie fond lost 32,4% of its assets. In 2016, the attacker hacked the DAO and took away $50 million.

These examples clearly show that even a slight defect or an error can result in bad consequences. Therefore, smart contract testing is a crucial part of creating smart contracts. 

The purpose of their audit is to find code errors, check the program logic, and ensure everything is okay. A high-quality audit will enable you to remove risks, anxiety, as well as save your nerves and resources. 
 

<h2>How to prepare for testing smart contracts</h2>

The preparation has an important role, helping auditors define the project objectives, faster define vulnerabilities, and improve the testing process. 

Also, keep in mind that preparation begins from the moment you start creating smart contracts. That's why it's important to know <a href="https://smartym.pro/blog/smart-contract-security-issues-smart-contract-vulnerabilities-and-how-to-protect">smart contract vulnerabilitites and ways to protect from them.</a>


<h3>I recommend adhering to the following tips:</h3>

 
<b>1. Provide a detailed specification</b>

<a href="https://smartym.pro/blog/how-to-write-software-requirements-document/">Providing a clear technical specification is always a good idea</a>, as it helps blockchain developers clearly understand smart contract goals, use cases, and working principles.

Although having it is important, many companies don’t create it. However, most engineers say that the document enables to clarify thinking and increase chances for project success.

In the spec, describe the intended behavior of a smart contract: how each function should work, how it shouldn’t, and how all of them should work together. Use diagrams as they also allow you to see possible alternatives and see how people can misinterpret your code. 

 
<b>2. Document the process of deploying smart contracts</b>

Though the code of your smart contract can be great, there are some important steps to make before it’s deployed. To avoid mishaps and save the team’s efforts, provide the documentation of the deployment process.

A well-documented process that includes the order in which contracts will be deployed, which compiler versions will be used for which contracts, etc., will help you prevent smart contract vulnerabilities.
 

<b>3. Clean the project repository</b>

A quality smart contract code without clutter will facilitate and automate the work of auditors. When preparing the project for the testing process, remove any unused and unnecessary files, contracts, or pieces of code.

In addition, you should specify which contracts are utilized for audit only (meaning that they aren't part of the end system) and which of them are reused from the tested code or inherit from the validated one.
 

<h2>How to test Ethereum smart contracts</h2>

The process of smart contract audit is similar to testing of any other code: a set of standard method calls is created in the predefined environment, and statements are written for their results. 


<b>The audit is a process that consists of:

- Test development
- Testing of smart contract state changes
- Event testing
- Error testing
- Checking a message sender


To audit your project, it's convenient to use Behavior Driven Development (BDD) practices, that along with the tests enable you to create documentation and use cases. Now there is a wide range of frameworks, libraries, and other tools for auditing smart contracts.

Although the test development in Solidity is limited to the capabilities of this programming language, you can use JavaScript, Truffle framework, Parity, and other reliable technologies.
 

<h2>Auditing smart contracts with Truffle</h2>

Today <a href="hhttps://github.com/trufflesuite/truffle">Truffle</a> is the most popular framework for Ethereum. Truffle is a Node.js framework used to compile, link, and deploy smart contracts. The framework is written in a completely modular way enabling engineers to choose the functionality they need.

While in Truffle v.2 tests are created on JavaScript, in Truffle v.3 developers added the ability to write tests on Solidity, whose syntax is similar to that of JavaScript.

Truffle offers plenty of useful features that include <a href="https://en.wikipedia.org/wiki/Binary_repository_manager">binary management</a>, library linking, custom deployment support, migrations framework, scriptable deployment, access to hundreds of external packages, external script runner, and automated contract audit with Mocha and Chai – everything to avoid failures is smart contracts.  

In speaking of the fact that blockchain systems generally don’t work very fast, auditors use blockchain test clients, for instance, TestRPC that almost completely emulates the work of the JSON RPC API of Ethereum clients.

Besides standard methods, TestRPC also implements a number of additional ones, such as evm_increaseTime and evm_mine. A good alternative to applying TestRPC is to use one of the standard clients, for instance, Parity, that runs in dev mode and has transactions instantly confirmed.
 

To start working with Truffle framework install it via npm:

<i>npm install -g truffle</i>
 

Then, perform the truffle init command in order to create the project structure:

<i>$ mkdir solidity-test-example
$ cd solidity-test-example/
$ truffle init</i>

 
Contracts must be located in the contracts/ directory and tests – in the test/ directory. When you compile contracts, Truffle expects that each contract is placed in a separate file, and the contract name is equal to the file name.
 

<h2>Test development with Truffle framework</h2>

Tests use JavaScript objects that represent abstractions for working with contracts, making the mapping between operations on objects and calls of JSON RPC methods of the Ethereum client. These objects are created automatically when you compile the source code for * .sol files.

The calls of all methods are asynchronous and return Promise, thus removing the worry about tracking transaction confirmations. To avoid large amounts of unreadable code, use async/await for writing tests.

Also, to simplify the work, it’s better not to mix migrations and the creation of test instances. Instead, create them in the test by using the code that creates a new contract instance before calling each test function. To this end, you can use the beforeEach async function.
 
<i>const congressInitialParams = {</i>
<i>minimumQuorumForProposals: 3,</i>
<i>minutesForDebate: 5,</i>
<i>marginOfVotesForMajority: 1,</i>
<i>congressLeader: accounts[0]</i>
<i>};</i>
<i>let congress;</i>
<i>beforeEach(async function() {</i>
  <i>congress = await Congress.new(…Object.values(congressInitialParams));</i>
<i>});</i>

 
<h2>How to test events</h2>

Events in Ethereum can be used for the following purposes:

- To return values from methods that change the contract state
- To create a history of changing the state of smart contracts
- To be used as a cheap and convenient information repository


Let’s take the following example. You need to check that when you call the new proposal method, adding a sentence to the contract, an event record Proposal Added is created.

For that, first, create a member of <a href="https://en.wikipedia.org/wiki/The_DAO_(organization)">DAO</a> in the test and create a proposal on its behalf. Then create a subscriber for the ProposalAdded event and check that after the newProposal method was called, the event occurred and its attributes correspond to the transmitted data. 
 

<i>A newProposalfunction code:</i>
<i>/* Function to create a new proposal */</i>
<i>function newProposal(</i>
   <i>address beneficiary,
      uint etherAmount,
      string JobDescription,
      bytes transactionBytecode</i>
<i>)</i>
  <i> onlyMembers
      returns (uint proposalID)</i>
 

<h2>How to test changes of the smart contract state</h2>

In Ethereum, the addMember method is responsible for changing the state of smart contracts. To test this method, check if it records the information about the DAO participant to the array of members' structures.
 

<i>/*make member*/
function addMember(address targetMember, string memberName) onlyOwner {
    uint id;</i>
    <i>if (memberId[targetMember] == 0) {
       memberId[targetMember] = members.length;
       id = members.length++;</i>
    <i>members[id] = Member({member: targetMember, memberSince: now, name: memberName});</i>
  <i>   } else {
         id = memberId[targetMember];
         Member m = members[id];
   }</i>
<i>MembershipChanged(targetMember, true);</i>
<i>}</i>
<i>function removeMember(address targetMember) onlyOwner {</i>
<i>if (memberId[targetMember] == 0) throw;</i>
<i>for (uint i = memberId[targetMember]; i<members.length-1; i++){</i>
   <i>    members[i] = members[i+1];</i>
 <i> }</i>
<i>  delete members[members.length-1];</i>
<i>  Members.length–;</i>
<i>}</i>
 

Using the array with test accounts, add participants in the contract in the test. Then check that the member's function returns the input data. 

It should be noted that every time the addMembermethod is called, a transaction is created and the Blockchain state is changed, that is the information is recorded in the distributed ledger.
 

<h2>How to test errors and checking of the message sender</h2>

A general way of stopping the work of the contract method is the exceptions that can be created using the throw instruction. You may need an exception if your task is to restrict access to the method.

To make it implement a modifier that would check an account address which has called a method: if it doesn’t correspond to the conditions (it’s not the contract owner who calls the method), an exception is created.
 

<h2>Some tips to avoid smart contract mistakes</h2>
 

<b>1. Use Solidity test coverage</b>

For smart contract audit, use Solidity-coverage. It will help you measure test coverage and define code pieces that haven’t been tested yet or need more attention. Surely, 100% test coverage isn’t a panacea, so it’s also important to write a quality test code from the beginning. 
 

<b>2. Use linters</b>

Linters enable Ethereum developers to enhance the code quality by making the code easier to read and review. There are many linters you can use. For example, Solcheck is a JavaScript linter for Solidity code.

Solhint is a linter that helps engineers avoid errors and vulnerabilities in Solidity smart contracts. Solhint provides Security and Style Guide validations.
 

<b>3. Make static analysis</b>

In smart contract testing, static analysis allows finding code vulnerabilities and insecurities. 

To make the code analysis, you can use such tools as Oyente that analyzes the code and detects common vulnerabilities, Manticore, that allows a dynamic binary analysis with the EVM support, and Solgraph that enables to visualize smart contract function control flow and identify potential insecurities.
 

<b>4. Prepare for failure</b>

Despite accurate testing, a smart contract may still have some errors or vulnerabilities, you should always be prepared for failures. So, make sure your code is able to respond to smart contract bugs.

To this end, create an effective upgrade plan for bugfixing and code enhancements. Manage the amount of money at risk by establishing rate limitation and maximum usage. In addition, if something goes wrong, pause your contract.