ethereum/py-evm

Undo transactions

nicedinner opened this issue · 2 comments

I'm trying to test smart contract security issues with py-evm. Suppose i execute a set of transactions, check out the contract state after the transactions and find no loophole triggered. The problem is how can i undo the latest one or two executed transactions (for further test)? Or, how can i set the chain state to the precise state where several designated transactions are executed?

Hope to know, thanks a lot!

fjarri commented

Looking for a similar functionality, basically saving/restoring snapshots of the chain state. eth-tester is doing it via some undocumented API:

# revert the state to `block_hash`
block = chain.get_block_by_hash(block_hash)
chaindb = chain.chaindb

chaindb._set_as_canonical_chain_head(chaindb.db, block.header, eth.constants.GENESIS_PARENT_HASH)
if block.number > 0:
    chain.import_block(block)
else:
    chain = chain.from_genesis_header(chaindb.db, block.header)

This works, but it doesn't restore the state of pending transactions (if any), and it uses a private method, so it can break at any moment, and cannot be typechecked. I wonder if there's an official way of doing snapshots?

In case anyone bumps into this issue, I've pivoted to just copy the chain via eth.tools.builder.chain.copy(). Copies the pending transactions as well. Hopefully it's supposed to be public API and is not going away.