I am currently testing a contract that can evolve in different ways, depending on the interaction between parties. But all these possible scenarios share the same initialization.
So, the question I have is the following. When I am writing a test for this contract in Truffle/Ganache, is it possible to run a single chain until the decision point, then create several copies of the test blockchain state, one for each possible scenario?
I know that Ganache offers the evm_snapshot
and evm_revert
RPC calls. But it is not clear to me how to use them and how it interacts with Truffle and Mocha.
Also, can I use nested contract
directives, as we can nest describe
in Mocha?
Ideally I would like to do something like this:
var Contract = artifacts.require("./Contract.sol");
contract('Contract', function(accounts) {
beforeEach(async function() {
// launch contract
let contract = await Contract
.new({ from: accounts[2], gas: 2000000 });
// let users interact here
// ...
});
contract('Agreement', function(accounts) {
it('Payment is processed', async function() {
// test that payment was processed
});
});
contract('Disagreement, see who is right', function(accounts) {
contract('Alice right', function(accounts) {
it('Punish Bob', async function() {
// show that Alice is right and punish Bob
});
});
contract('Alice right', function(accounts) {
it('Punish Alice', async function() {
// show that Bob is right and punish Alice
});
});
});
}
You see that we are testing three different outcomes, but they share the same initialization. I would like to avoid starting three fresh chains, one for each test. Is it possible and how one does it?