Skip to content

Commit

Permalink
Problem: native action don't support mem keys (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang authored Sep 9, 2023
1 parent 6a00c3a commit 7f03e19
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
12 changes: 11 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,22 @@ func NewEthermintApp(

// Set authority to x/gov module account to only expect the module account to update params
evmSs := app.GetSubspace(evmtypes.ModuleName)
allKeys := make(map[string]storetypes.StoreKey, len(keys)+len(tkeys)+len(memKeys))
for k, v := range keys {
allKeys[k] = v
}
for k, v := range tkeys {
allKeys[k] = v
}
for k, v := range memKeys {
allKeys[k] = v
}
app.EvmKeeper = evmkeeper.NewKeeper(
appCodec, keys[evmtypes.StoreKey], tkeys[evmtypes.TransientKey], authtypes.NewModuleAddress(govtypes.ModuleName),
app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.FeeMarketKeeper,
tracer,
evmSs, nil,
keys,
allKeys,
)

// Create IBC Keeper
Expand Down
2 changes: 1 addition & 1 deletion store/cachemulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewFromKVStore(

// NewStore creates a new Store object from parent rootmulti store, it branch out inner store of the specified keys.
func NewStore(
parent types.MultiStore, keys map[string]*types.KVStoreKey,
parent types.MultiStore, keys map[string]types.StoreKey,
) Store {
stores := make(map[types.StoreKey]types.KVStore, len(keys))
for _, key := range keys {
Expand Down
6 changes: 3 additions & 3 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type Keeper struct {

// a set of store keys that should cover all the precompile use cases,
// or ideally just pass the application's all stores.
keys map[string]*storetypes.KVStoreKey
keys map[string]storetypes.StoreKey
}

// NewKeeper generates new evm module keeper
Expand All @@ -91,7 +91,7 @@ func NewKeeper(
tracer string,
ss paramstypes.Subspace,
customContracts []precompiles.StatefulPrecompiledContract,
keys map[string]*storetypes.KVStoreKey,
keys map[string]storetypes.StoreKey,
) *Keeper {
// ensure evm module account is set
if addr := ak.GetModuleAddress(types.ModuleName); addr == nil {
Expand Down Expand Up @@ -120,7 +120,7 @@ func NewKeeper(
}
}

func (k Keeper) StoreKeys() map[string]*storetypes.KVStoreKey {
func (k Keeper) StoreKeys() map[string]storetypes.StoreKey {
return k.keys
}

Expand Down
2 changes: 1 addition & 1 deletion x/evm/statedb/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// Keeper provide underlying storage of StateDB
type Keeper interface {
// for cache store wrapping
StoreKeys() map[string]*storetypes.KVStoreKey
StoreKeys() map[string]storetypes.StoreKey

// Read methods
GetAccount(ctx sdk.Context, addr common.Address) *Account
Expand Down
8 changes: 4 additions & 4 deletions x/evm/statedb/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ type MockAcount struct {
type MockKeeper struct {
accounts map[common.Address]MockAcount
codes map[common.Hash][]byte
keys map[string]*storetypes.KVStoreKey
keys map[string]storetypes.StoreKey
}

func NewMockKeeperWithKeys(keys map[string]*storetypes.KVStoreKey) *MockKeeper {
func NewMockKeeperWithKeys(keys map[string]storetypes.StoreKey) *MockKeeper {
return &MockKeeper{
accounts: make(map[common.Address]MockAcount),
codes: make(map[common.Hash][]byte),
Expand All @@ -41,7 +41,7 @@ func NewMockKeeper() *MockKeeper {
return NewMockKeeperWithKeys(nil)
}

func (k MockKeeper) StoreKeys() map[string]*storetypes.KVStoreKey {
func (k MockKeeper) StoreKeys() map[string]storetypes.StoreKey {
return k.keys
}

Expand Down Expand Up @@ -121,7 +121,7 @@ func (k MockKeeper) Clone() *MockKeeper {
for k, v := range k.codes {
codes[k] = v
}
keys := make(map[string]*storetypes.KVStoreKey, len(k.keys))
keys := make(map[string]storetypes.StoreKey, len(k.keys))
for k, v := range k.keys {
keys[k] = v
}
Expand Down
11 changes: 10 additions & 1 deletion x/evm/statedb/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,10 +575,12 @@ func (suite *StateDBTestSuite) TestIterateStorage() {
func (suite *StateDBTestSuite) TestNativeAction() {
db := dbm.NewMemDB()
ms := rootmulti.NewStore(db, log.NewNopLogger())
keys := map[string]*storetypes.KVStoreKey{
keys := map[string]storetypes.StoreKey{
"storekey": storetypes.NewKVStoreKey("storekey"),
"mem": storetypes.NewMemoryStoreKey("mem"),
}
ms.MountStoreWithDB(keys["storekey"], storetypes.StoreTypeIAVL, nil)
ms.MountStoreWithDB(keys["mem"], storetypes.StoreTypeMemory, nil)
suite.Require().NoError(ms.LoadLatestVersion())
ctx := sdk.NewContext(ms, tmproto.Header{}, false, log.NewNopLogger())

Expand All @@ -589,12 +591,19 @@ func (suite *StateDBTestSuite) TestNativeAction() {
store := ctx.KVStore(keys["storekey"])
store.Set([]byte("success1"), []byte("value"))
ctx.EventManager().EmitEvent(sdk.NewEvent("success1"))

mem := ctx.KVStore(keys["mem"])
mem.Set([]byte("mem"), []byte("value"))

return nil
})
stateDB.ExecuteNativeAction(func(ctx sdk.Context) error {
store := ctx.KVStore(keys["storekey"])
store.Set([]byte("failure1"), []byte("value"))
ctx.EventManager().EmitEvent(sdk.NewEvent("failure1"))

mem := ctx.KVStore(keys["mem"])
suite.Require().Equal([]byte("value"), mem.Get([]byte("mem")))
return errors.New("failure")
})

Expand Down

0 comments on commit 7f03e19

Please sign in to comment.