Skip to content

Commit

Permalink
Fix: Native action revert issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dudong2 committed Oct 17, 2024
1 parent 203f3e8 commit fcf207c
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 239 deletions.
5 changes: 1 addition & 4 deletions app/ante/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,7 @@ func (ctd CanTransferDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
BaseFee: baseFee,
}

stateDB, err := statedb.New(ctx, ctd.evmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash())))
if err != nil {
return ctx, errorsmod.Wrap(err, "failed to create new stateDB")
}
stateDB := statedb.New(ctx, ctd.evmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash())))
evm := ctd.evmKeeper.NewEVM(ctx, coreMsg, cfg, evmtypes.NewNoOpTracer(), stateDB)

// check that caller has enough balance to cover asset transfer for **topmost** call
Expand Down
4 changes: 1 addition & 3 deletions app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ type AnteTestSuite struct {
const TestGasLimit uint64 = 100000

func (suite *AnteTestSuite) StateDB() *statedb.StateDB {
db, err := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash())))
suite.Require().NoError(err)
return db
return statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash())))
}

func (suite *AnteTestSuite) SetupTest() {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ require (

replace (
cosmossdk.io/core => cosmossdk.io/core v0.11.0
cosmossdk.io/store => /Users/dudong2/workspace/github.com/b-harvest/cosmos-sdk2/store
cosmossdk.io/store => github.com/b-harvest/cosmos-sdk/store v0.0.0-20241015155842-364149db166b
// use cosmos keyring
github.com/99designs/keyring => github.com/cosmos/keyring v1.1.7-0.20210622111912-ef00f8ac3d76
github.com/cosmos/cosmos-sdk => /Users/dudong2/workspace/github.com/b-harvest/cosmos-sdk
github.com/cosmos/cosmos-sdk => github.com/b-harvest/cosmos-sdk v0.0.0-20241017100038-2b7fae21f59d
github.com/ethereum/go-ethereum => github.com/b-harvest/go-ethereum v0.0.0-20240925105515-5fa8b7772734
// Fix upstream GHSA-h395-qcrw-5vmq vulnerability.
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX
github.com/aws/aws-sdk-go v1.44.224 h1:09CiaaF35nRmxrzWZ2uRq5v6Ghg/d2RiPjZnSgtt+RQ=
github.com/aws/aws-sdk-go v1.44.224/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/b-harvest/cosmos-sdk v0.0.0-20241017100038-2b7fae21f59d h1:XG9XWbLOi/IhelbffMmNEicMSARLAgmsFVjTpM4U7VI=
github.com/b-harvest/cosmos-sdk v0.0.0-20241017100038-2b7fae21f59d/go.mod h1:i72MYwrjOF82mlXRK91O2h2MYHl3EG0+zjDeKJLZ6/g=
github.com/b-harvest/cosmos-sdk/store v0.0.0-20241015155842-364149db166b h1:V8FGjyQqNZ4qTWGEFJQsInCWORqevhSs9oAENzuLEEE=
github.com/b-harvest/cosmos-sdk/store v0.0.0-20241015155842-364149db166b/go.mod h1:oZfW/4Fc/zYqu3JmQcQdUJ3fqu5vnYTn3LZFFy8P8ng=
github.com/b-harvest/go-ethereum v0.0.0-20240925105515-5fa8b7772734 h1:7uYMhLMyZgyEuulnqAVaD5wWua66nbY3kS62fFQaoeE=
github.com/b-harvest/go-ethereum v0.0.0-20240925105515-5fa8b7772734/go.mod h1:EYFyF19u3ezGLD4RqOkLq+ZCXzYbLoNDdZlMt7kyKFg=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down
3 changes: 1 addition & 2 deletions tests/importer/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ func (suite *ImporterTestSuite) TestImportBlocks() {
suite.Require().NoError(err)
ctx := suite.app.NewContextLegacy(false, tmheader)
ctx = ctx.WithBlockHeight(tmheader.Height)
vmdb, err := statedb.New(ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash())))
suite.Require().NoError(err)
vmdb := statedb.New(ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash())))

if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 {
applyDAOHardFork(vmdb)
Expand Down
4 changes: 1 addition & 3 deletions x/evm/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ func (suite *EvmTestSuite) SignTx(tx *types.MsgEthereumTx) {
}

func (suite *EvmTestSuite) StateDB() *statedb.StateDB {
db, err := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash())))
suite.Require().NoError(err)
return db
return statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash())))
}

func TestEvmTestSuite(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions x/evm/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,7 @@ func (suite *KeeperTestSuite) TestQueryTxLogs() {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset

vmdb, err := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewTxConfig(common.BytesToHash(suite.ctx.HeaderHash()), txHash, txIndex, logIndex))
suite.Require().NoError(err)
vmdb := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewTxConfig(common.BytesToHash(suite.ctx.HeaderHash()), txHash, txIndex, logIndex))
tc.malleate(vmdb)
suite.Require().NoError(vmdb.Commit())

Expand Down
3 changes: 1 addition & 2 deletions x/evm/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@ func (suite *KeeperTestSuite) TestEvmHooks() {
k := suite.app.EvmKeeper
ctx := suite.ctx
txHash := common.BigToHash(big.NewInt(1))
vmdb, err := statedb.New(ctx, k, statedb.NewTxConfig(
vmdb := statedb.New(ctx, k, statedb.NewTxConfig(
common.BytesToHash(ctx.HeaderHash()),
txHash,
0,
0,
))
suite.Require().NoError(err)

vmdb.AddLog(&ethtypes.Log{
Topics: []common.Hash{},
Expand Down
60 changes: 1 addition & 59 deletions x/evm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ 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"
Expand Down Expand Up @@ -255,9 +254,7 @@ func (suite *KeeperTestSuite) Commit() {
}

func (suite *KeeperTestSuite) StateDB() *statedb.StateDB {
db, err := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash())))
suite.Require().NoError(err)
return db
return statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash())))
}

// DeployTestContract deploy a test erc20 contract and returns the contract address
Expand Down Expand Up @@ -534,58 +531,3 @@ 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))
}
5 changes: 1 addition & 4 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,7 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context,
return nil, errorsmod.Wrap(types.ErrCallDisabled, "failed to call contract")
}

stateDB, err := statedb.New(ctx, k, txConfig)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to create new stateDB")
}
stateDB := statedb.New(ctx, k, txConfig)
evm := k.NewEVM(ctx, msg, cfg, tracer, stateDB)

leftoverGas := msg.Gas()
Expand Down
3 changes: 1 addition & 2 deletions x/evm/keeper/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,12 +715,11 @@ func (suite *KeeperTestSuite) TestAddLog() {
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
vmdb, err := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewTxConfig(
vmdb := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewTxConfig(
common.BytesToHash(suite.ctx.HeaderHash()),
tc.hash,
0, 0,
))
suite.Require().NoError(err)
tc.malleate(vmdb)

vmdb.AddLog(tc.log)
Expand Down
36 changes: 5 additions & 31 deletions x/evm/statedb/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import (
"bytes"
"sort"

storetypes "cosmossdk.io/store/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"cosmossdk.io/store/cachemulti"

"github.com/ethereum/go-ethereum/common"
)
Expand Down Expand Up @@ -137,14 +135,9 @@ type (
slot *common.Hash
}

precompileChange struct {
ms storetypes.MultiStore
es sdk.Events
}

nativeChange struct {
ms storetypes.MultiStore
es int
snapshot cachemulti.Store
events int // native events index
}
)

Expand Down Expand Up @@ -240,28 +233,9 @@ func (ch accessListAddSlotChange) Dirtied() *common.Address {
return nil
}

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()
}
}

func (ch precompileChange) Dirtied() *common.Address {
return nil
}

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

// Necessary to revert the sdk state
s.writeCache = func() {
s.nativeEvents = s.nativeEvents[:len(s.nativeEvents)-ch.es]
ch.ms.CacheMultiStore().Write()
}
s.cacheMS.Restore(ch.snapshot)
s.nativeEvents = s.nativeEvents[:len(s.nativeEvents)-ch.events]
}

func (ch nativeChange) Dirtied() *common.Address {
Expand Down
4 changes: 2 additions & 2 deletions x/evm/statedb/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (s *stateObject) Code() []byte {
if bytes.Equal(s.CodeHash(), emptyCodeHash) {
return nil
}
code := s.db.keeper.GetCode(s.db.ctx, common.BytesToHash(s.CodeHash()))
code := s.db.keeper.GetCode(s.db.cacheCtx, common.BytesToHash(s.CodeHash()))
s.code = code
return code
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func (s *stateObject) GetCommittedState(key common.Hash) common.Hash {
return value
}
// If no live objects are available, load it from keeper
value := s.db.keeper.GetState(s.db.ctx, s.Address(), key)
value := s.db.keeper.GetState(s.db.cacheCtx, s.Address(), key)
s.originStorage[key] = value
return value
}
Expand Down
Loading

0 comments on commit fcf207c

Please sign in to comment.