Skip to content

Commit

Permalink
test: Add revert test
Browse files Browse the repository at this point in the history
  • Loading branch information
dudong2 committed Oct 10, 2024
1 parent d40c19c commit 6cf77ec
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
61 changes: 57 additions & 4 deletions x/evm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import (
"github.com/evmos/ethermint/crypto/ethsecp256k1"
"github.com/evmos/ethermint/server/config"
"github.com/evmos/ethermint/tests"
"github.com/evmos/ethermint/testutil"
ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm/statedb"
"github.com/evmos/ethermint/x/evm/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand All @@ -50,8 +50,6 @@ import (
"github.com/cometbft/cometbft/version"
)

var testTokens = sdkmath.NewIntWithDecimal(1000, 18)

type KeeperTestSuite struct {
suite.Suite

Expand Down Expand Up @@ -225,7 +223,7 @@ func (suite *KeeperTestSuite) SetupAppWithT(checkTx bool, t require.TestingT) {
suite.clientCtx = client.Context{}.WithTxConfig(suite.app.TxConfig())
suite.ethSigner = ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
suite.appCodec = suite.app.AppCodec()
suite.denom = evmtypes.DefaultEVMDenom
suite.denom = types.DefaultEVMDenom
}

func (suite *KeeperTestSuite) EvmDenom() string {
Expand Down Expand Up @@ -535,3 +533,58 @@ func (suite *KeeperTestSuite) TestGetAccountOrEmpty() {
})
}
}

func (suite *KeeperTestSuite) TestRevertByPrecompileSnapshot() {
db := suite.StateDB()

// snapshot id for journal
rev := db.Snapshot()

ctx, err := db.GetCacheContext()
suite.NoError(err)

snapshotMultiStore, err := db.MultiStoreSnapshot()
suite.NoError(err)
snapshotEvents := suite.ctx.EventManager().Events()
db.AddPrecompileSnapshot(snapshotMultiStore, snapshotEvents)

// manipulate statedb(evm)
evmAddr, priv := tests.NewAddrKey()
key1 := common.BigToHash(big.NewInt(1))
value1 := common.BigToHash(big.NewInt(2))
key2 := common.BigToHash(big.NewInt(3))
value2 := common.BigToHash(big.NewInt(4))
db.SetState(evmAddr, key1, value1)
db.SetState(evmAddr, key2, value2)

suite.Equal(value1, db.GetState(evmAddr, key1))
suite.Equal(value2, db.GetState(evmAddr, key2))

// manipulate bank keeper(sdk)
addr1 := sdk.AccAddress(priv.PubKey().Address().Bytes())
denom := "testdenom"
testutil.FundAccount(suite.app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewCoin(denom, sdkmath.NewInt(1000000))))
addr2 := sdk.AccAddress(tests.GenerateAddress().Bytes())
suite.app.BankKeeper.SendCoins(ctx, addr1, addr2, sdk.NewCoins(sdk.NewCoin(denom, sdkmath.NewInt(1000))))

suite.Equal(sdk.NewCoin(denom, sdkmath.NewInt(999000)), suite.app.BankKeeper.GetBalance(ctx, addr1, denom))
suite.Equal(sdk.NewCoin(denom, sdkmath.NewInt(1000)), suite.app.BankKeeper.GetBalance(ctx, addr2, denom))

// revert to snapshot
db.RevertToSnapshot(rev)

suite.Equal(common.Hash{}, db.GetState(evmAddr, key1))
suite.Equal(common.Hash{}, db.GetState(evmAddr, key2))

suite.Equal(sdk.NewCoin(denom, sdkmath.NewInt(999000)), suite.app.BankKeeper.GetBalance(ctx, addr1, denom))
suite.Equal(sdk.NewCoin(denom, sdkmath.NewInt(1000)), suite.app.BankKeeper.GetBalance(ctx, addr2, denom))

// commit changes(revert sdk state)
db.Commit()

suite.Equal(common.Hash{}, db.GetState(evmAddr, key1))
suite.Equal(common.Hash{}, db.GetState(evmAddr, key2))

suite.Equal(sdk.NewCoin(denom, sdkmath.ZeroInt()), suite.app.BankKeeper.GetBalance(suite.ctx, addr1, denom))
suite.Equal(sdk.NewCoin(denom, sdkmath.ZeroInt()), suite.app.BankKeeper.GetBalance(suite.ctx, addr2, denom))
}
2 changes: 2 additions & 0 deletions x/evm/statedb/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ func (ch accessListAddSlotChange) Dirtied() *common.Address {

func (ch precompileChange) Revert(s *StateDB) {
s.cacheCtx = s.cacheCtx.WithMultiStore(ch.ms)

// Necessary to revert the sdk state
s.writeCache = func() {
s.cacheCtx.EventManager().EmitEvents(ch.es)
ch.ms.CacheMultiStore().Write()
Expand Down

0 comments on commit 6cf77ec

Please sign in to comment.