Property Checking with Scribble and MythX
Intro
In this tutorial we will learn how to:
start using scribble
add a custom property to an ERC20 token contract
look for violations of our custom property with MythX
Without further ado...
Dependencies
Make sure that you have the following installed:
python3 and pip3
nodejs version 12.x and npm.
Setup
To get setup you first need to install scribble:
Next install mythx-cli:
Next you will need a MythX account. If you don't already have one, you can make a MythX account here.
Once you have an account you will need an API Key to authenticate to the mythx service. You can generate one by logging into the MythX dashboard, clicking on the "API Key" **menu entry and filling out the password field. (detailed instructions here). After you get your key, you can set it as the MYTHX_API_KEY
environment variable:
Now we are ready to begin!
Background
Before we begin, we should clarify what we mean by 'custom properties'. Custom properties in general can be any predicate you have in mind about your code. For example, a custom property can be:
Only the current owner of the contract may change the
owner
state variableMy contract will allow trades only after the
initialize()
function was calledThe number of minted tokens only increases
etc..
In order to be able to reason about these properties formally however, we need to translate their English statements into a formal language that automated tools understand. We will show how to do this using the Scribble spec language. Note that the language choice imposes certain limitations on what can be expressed. We are actively working on adding more expressive power to Scribble.
Our example
For this tutorial we will consider a simple ERC20 token, and annotate it with a custom property expressed in the Scribble language. Take the below code and save it as Token.sol
in a directory of your choice:
Token.sol
Custom Properties
Lets consider the transfer
function above.
There are several properties that could be specified over the transfer function:
If the transfer function succeeds then the recipient had sufficient balance at the start
If the transfer succeeds then the sender will have `_value` subtracted from it’s balance
If the transfer succeeds then the receiver will have `_value` added to it’s balance
If the transfer succeeds then the sum of the balances between the sender and receiver remains he same
For this example we’ll go with property #4;
💡 Try to write annotations for the other two properties after you’ve finished this getting started guide!
Formalizing the property
First things first, we need to dissect what the property actually entails. There are two main parts: if the transfer succeeds
and the sum of the sender and receiver balances is the same before and after the transaction
.
if the transfer succeeds
This specifies when you expect something to hold. In the scribble specification language we use if_succeeds
function annotations to check properties after successful termination of a function. Check out the specification docs for detailed information on the if_succeeds
keyword.
the sum of the sender and receiver balances is the same before and after the transaction
This is the boolean expression that we expect to hold. Note that this property introduces a statement that relates the initial and final state of the contract. That is, we want to know that the sum of the balances (_balances[sender] + _balances[receiver]
) doesn’t change relative to the state before the transaction. To allow you to express such properties the old()
is used. Check out the specification docs for more information on old
.
Using this keyword we can formulate the following expression:
Now we can combine the two to get to our specification:
Adding the annotation
if_succeeds
annotations are function specifications, which in the Scribble specification language are placed above the function definitions using a docstring comment.
🔍 Analysis
To see if the property holds we will move to the terminal, and use the MythX CLI to start fuzzing and symbolic exploration.
In the directory with the annotated contract, run:
☕ Relax for 2 minutes as the MythX robots do their work!
After about 2 minutes we get the following results:
Under the hood
The command you just ran actually did several steps. First mythx-cli
invoked scribble
to generate an instrumented version of the contract. That is, a version of the original contract with the mostly the same behavior, except it also converted the property you had in the docstring into a concrete check at the end of transfer
. For more information on how instrumentation works check out this section. If you want to see the instrumented code, you can run
Next Steps
📖 Read Specification Language Docs to learn more about the specification language
👓 Check out the repository of examples
Last updated