State Machines in Solidity

Jules Goddard
Coinmonks

--

This article discusses using State Machines as a convenient way of enforcing the workflow in Solidity, the defacto smart contract language for the Ethereum blockchain.

Photo by Franck V. on Unsplash

A State Machine is only ever in one state and is moved between states by changing inputs or by input events.

In the case of a Smart Contract in Solidity, a message is sent from a different account (an external account or another contract) to a contract function to cause a change in state. If the input is valid for the current state, the State Machine will move to a new state.

Background

During the development and testing of Datona Labs’ Solidity Smart-Data-Access-Contract (S-DAC) templates, we often use state machines to encompass the workflow. In the example for this article, between two parties who ask questions and provide answers.

Roles and Actions in our example

UML State Machine Diagram

Here is the UML State Machine diagram for our example:

--

--

Jules Goddard
Coinmonks

Experienced high-integrity software engineer, crypto code compactor and Datona Labs founder — providing smart contracts to protect your digital information.