4

I know about tools like ganache-time-traveler that let you advance the block number.

But is it possible to go back to a lower value?

3 Answers 3

3

The only option to do this is to use the takeSnapshot and revertToSnapshot functions in that same repo.

When you are at a state you know you want to return to, call takeSnapshot in order to get a snapshot ID. When you want to revert to that state, simply run revertToSnapshot with that specific ID.

As a note, the snapshotting function in Ganache was finicky the last time I used it (Feb 2019). There were certain limitations, such as not being able to take multiple snapshots before reverting to one. I do not know the current status of this.


Edit

The issues I was having with this feature are addressed in the "Custom Methods" section of the ganache-cli repo:

evm_snapshot : Snapshot the state of the blockchain at the current block. Takes no parameters. Returns the integer id of the snapshot created. A snapshot can only be used once. After a successful evm_revert, the same snapshot id cannot be used again. Consider creating a new snapshot after each evm_revert if you need to revert to the same point multiple times.

evm_revert : Revert the state of the blockchain to a previous snapshot. Takes a single parameter, which is the snapshot id to revert to. This deletes the given snapshot, as well as any snapshots taken after (Ex: reverting to id 0x1 will delete snapshots with ids 0x1, 0x2, etc... If no snapshot id is passed it will revert to the latest snapshot. Returns true.

2
  • 1
    I used this, but it doesn't seem to lower the block number. It just resets the timestamp. Commented Sep 19, 2019 at 16:38
  • I wonder if that is a ganache-time-traveler specific thing? I do not see anything about it in the actual ganache-cli repo Commented Sep 19, 2019 at 17:19
1

When using ganache-cli you can specify the block as a parameter of the fork option by adding @BLOCK_NUMBER postfix, eg. to set to block 1599200:

ganache-cli --fork http://localhost:8545@1599200
0

You can use non-standard evm_revert JSON-RPC call to revert to the last evm_snapshot https://docs.nethereum.com/en/latest/ethereum-and-clients/ganache-cli/

You can also use chain.undo() to undo the most recent tx from the Ganache-cli console, see https://eth-brownie.readthedocs.io/en/v1.13.4/core-chain.html for examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.