diff --git a/x/aspect/keeper/keeper.go b/x/aspect/keeper/keeper.go index 388db60..cbb9517 100644 --- a/x/aspect/keeper/keeper.go +++ b/x/aspect/keeper/keeper.go @@ -51,3 +51,7 @@ func (k Keeper) GetAuthority() string { func (k Keeper) Logger() log.Logger { return k.logger.With("module", fmt.Sprintf("x/%s", types.ModuleName)) } + +func (k Keeper) GetStoreService() store.KVStoreService { + return k.storeService +} diff --git a/x/aspect/provider/artela.go b/x/aspect/provider/artela.go index 15e25d4..9d58185 100644 --- a/x/aspect/provider/artela.go +++ b/x/aspect/provider/artela.go @@ -19,16 +19,18 @@ var _ asptypes.AspectProvider = (*ArtelaProvider)(nil) type ArtelaProvider struct { getBlockHeight types.GetLastBlockHeight - storeService cstore.KVStoreService + evmStoreService cstore.KVStoreService + storeService cstore.KVStoreService } func NewArtelaProvider( - storeService cstore.KVStoreService, + evmStoreService, storeService cstore.KVStoreService, getBlockHeight types.GetLastBlockHeight, ) *ArtelaProvider { return &ArtelaProvider{ - storeService: storeService, - getBlockHeight: getBlockHeight, + evmStoreService: evmStoreService, + storeService: storeService, + getBlockHeight: getBlockHeight, } } @@ -114,14 +116,14 @@ func (j *ArtelaProvider) getCodes(ctx context.Context, address common.Address, p func (j *ArtelaProvider) buildAspectStoreCtx(ctx *types.AspectRuntimeContext, aspectID common.Address) *aspectmoduletypes.AspectStoreContext { return &aspectmoduletypes.AspectStoreContext{ - StoreContext: aspectmoduletypes.NewGasFreeStoreContext(ctx.CosmosContext(), j.storeService), + StoreContext: aspectmoduletypes.NewGasFreeStoreContext(ctx.CosmosContext(), j.evmStoreService, j.storeService), AspectID: aspectID, } } func (j *ArtelaProvider) buildAccountStoreCtx(ctx *types.AspectRuntimeContext, account common.Address) *aspectmoduletypes.AccountStoreContext { return &aspectmoduletypes.AccountStoreContext{ - StoreContext: aspectmoduletypes.NewGasFreeStoreContext(ctx.CosmosContext(), j.storeService), + StoreContext: aspectmoduletypes.NewGasFreeStoreContext(ctx.CosmosContext(), j.evmStoreService, j.storeService), Account: account, } } diff --git a/x/aspect/store/v0/account.go b/x/aspect/store/v0/account.go index 48ede57..4706ecc 100644 --- a/x/aspect/store/v0/account.go +++ b/x/aspect/store/v0/account.go @@ -35,7 +35,7 @@ func NewAccountStore(ctx *types.AccountStoreContext) store.AccountStore { meter = NewNoOpGasMeter(ctx) } - store := runtime.KVStoreAdapter(ctx.StoreService().OpenKVStore(ctx.CosmosContext())) + store := runtime.KVStoreAdapter(ctx.EVMStoreService().OpenKVStore(ctx.CosmosContext())) return &accountStore{ BaseStore: NewBaseStore(meter, store), diff --git a/x/aspect/store/v0/meta.go b/x/aspect/store/v0/meta.go index dd6054a..8f986fe 100644 --- a/x/aspect/store/v0/meta.go +++ b/x/aspect/store/v0/meta.go @@ -44,7 +44,7 @@ func NewAspectMetaStore(ctx *types.AspectStoreContext, _ []byte) store.AspectMet meter = NewNoOpGasMeter(ctx) } - store := runtime.KVStoreAdapter(ctx.StoreService().OpenKVStore(ctx.CosmosContext())) + store := runtime.KVStoreAdapter(ctx.EVMStoreService().OpenKVStore(ctx.CosmosContext())) return &metaStore{ BaseStore: NewBaseStore(meter, store), diff --git a/x/aspect/store/v0/state.go b/x/aspect/store/v0/state.go index 6aefc50..147ea3f 100644 --- a/x/aspect/store/v0/state.go +++ b/x/aspect/store/v0/state.go @@ -22,7 +22,7 @@ type stateStore struct { func NewStateStore(ctx *types.AspectStoreContext) store.AspectStateStore { // for state Store, we have already charged gas in host api, // so no need to charge it again in the Store - store := runtime.KVStoreAdapter(ctx.StoreService().OpenKVStore(ctx.CosmosContext())) + store := runtime.KVStoreAdapter(ctx.EVMStoreService().OpenKVStore(ctx.CosmosContext())) return &stateStore{ BaseStore: NewBaseStore(NewNoOpGasMeter(ctx), store), diff --git a/x/aspect/types/context.go b/x/aspect/types/context.go index 40d6ca2..164845d 100644 --- a/x/aspect/types/context.go +++ b/x/aspect/types/context.go @@ -9,18 +9,20 @@ import ( ) type storeContext struct { - cosmosCtx sdk.Context - storeService cstore.KVStoreService - gas uint64 + cosmosCtx sdk.Context + evmStoreService cstore.KVStoreService + storeService cstore.KVStoreService + gas uint64 chargeGas bool } func (s *storeContext) clone() StoreContext { return &storeContext{ - cosmosCtx: s.cosmosCtx, - storeService: s.storeService, - gas: s.gas, + cosmosCtx: s.cosmosCtx, + evmStoreService: s.evmStoreService, + storeService: s.storeService, + gas: s.gas, } } @@ -32,6 +34,10 @@ func (s *storeContext) CosmosContext() sdk.Context { return s.cosmosCtx } +func (s *storeContext) EVMStoreService() cstore.KVStoreService { + return s.evmStoreService +} + func (s *storeContext) StoreService() cstore.KVStoreService { return s.storeService } @@ -60,6 +66,7 @@ func (s *storeContext) ConsumeGas(gas uint64) error { type StoreContext interface { CosmosContext() sdk.Context StoreService() cstore.KVStoreService + EVMStoreService() cstore.KVStoreService Gas() uint64 ConsumeGas(gas uint64) error UpdateGas(gas uint64) @@ -69,20 +76,22 @@ type StoreContext interface { clone() StoreContext } -func NewStoreContext(ctx sdk.Context, storeService cstore.KVStoreService, gas uint64) StoreContext { +func NewStoreContext(ctx sdk.Context, evmStoreService, storeService cstore.KVStoreService, gas uint64) StoreContext { return &storeContext{ - cosmosCtx: ctx, - storeService: storeService, - gas: gas, - chargeGas: true, + cosmosCtx: ctx, + evmStoreService: evmStoreService, + storeService: storeService, + gas: gas, + chargeGas: true, } } -func NewGasFreeStoreContext(ctx sdk.Context, storeService cstore.KVStoreService) StoreContext { +func NewGasFreeStoreContext(ctx sdk.Context, evmStoreService, storeService cstore.KVStoreService) StoreContext { return &storeContext{ - cosmosCtx: ctx, - storeService: storeService, - chargeGas: false, + cosmosCtx: ctx, + evmStoreService: evmStoreService, + storeService: storeService, + chargeGas: false, } } diff --git a/x/evm/artela/contract/contract.go b/x/evm/artela/contract/contract.go index ad032ce..1825b70 100644 --- a/x/evm/artela/contract/contract.go +++ b/x/evm/artela/contract/contract.go @@ -23,11 +23,13 @@ type AspectNativeContract struct { logger log.Logger handlers map[string]Handler - storeService store.KVStoreService + storeService store.KVStoreService + aspectStoreService store.KVStoreService } func NewAspectNativeContract( storeService store.KVStoreService, + aspectStoreService store.KVStoreService, evm *vm.EVM, evmState *states.StateDB, logger log.Logger, @@ -82,6 +84,7 @@ func (c *AspectNativeContract) applyMsg(ctx sdk.Context, msg *core.Message, gas c.evm, method, c.storeService, + c.aspectStoreService, msg.Data, msg.Nonce, msg.GasLimit, diff --git a/x/evm/artela/contract/handlers.go b/x/evm/artela/contract/handlers.go index a7859be..b5c37ac 100644 --- a/x/evm/artela/contract/handlers.go +++ b/x/evm/artela/contract/handlers.go @@ -46,7 +46,8 @@ type HandlerContext struct { evm *vm.EVM abi *abi.Method - storeService cstore.KVStoreService + storeService cstore.KVStoreService + aspectStoreService cstore.KVStoreService rawInput []byte nonce uint64 @@ -979,14 +980,14 @@ func mustGetAspectContext(ctx sdk.Context) *types.AspectRuntimeContext { func buildAspectStoreCtx(ctx *HandlerContext, aspectID common.Address, gas uint64) *aspectmoduletypes.AspectStoreContext { return &aspectmoduletypes.AspectStoreContext{ - StoreContext: aspectmoduletypes.NewStoreContext(ctx.cosmosCtx, ctx.storeService, gas), + StoreContext: aspectmoduletypes.NewStoreContext(ctx.cosmosCtx, ctx.storeService, ctx.aspectStoreService, gas), AspectID: aspectID, } } func buildAccountStoreCtx(ctx *HandlerContext, account common.Address, gas uint64) *aspectmoduletypes.AccountStoreContext { return &aspectmoduletypes.AccountStoreContext{ - StoreContext: aspectmoduletypes.NewStoreContext(ctx.cosmosCtx, ctx.storeService, gas), + StoreContext: aspectmoduletypes.NewStoreContext(ctx.cosmosCtx, ctx.storeService, ctx.aspectStoreService, gas), Account: account, } } diff --git a/x/evm/artela/types/context.go b/x/evm/artela/types/context.go index f989ff3..6b790a4 100644 --- a/x/evm/artela/types/context.go +++ b/x/evm/artela/types/context.go @@ -30,7 +30,10 @@ const ( AspectModuleName = "aspect" ) -var cachedStoreService cstore.KVStoreService +var ( + cachedStoreService cstore.KVStoreService + cachedAspectStoreService cstore.KVStoreService +) type GetLastBlockHeight func() int64 @@ -56,12 +59,13 @@ type GetLastBlockHeight func() int64 type AspectRuntimeContext struct { baseCtx context.Context - ethTxContext *EthTxContext - aspectContext *AspectContext - ethBlockContext *EthBlockContext - aspectState *AspectState - cosmosCtx *cosmos.Context - storeService cstore.KVStoreService + ethTxContext *EthTxContext + aspectContext *AspectContext + ethBlockContext *EthBlockContext + aspectState *AspectState + cosmosCtx *cosmos.Context + storeService cstore.KVStoreService + aspectStoreService cstore.KVStoreService logger log.Logger jitManager *inherent.Manager @@ -69,15 +73,18 @@ type AspectRuntimeContext struct { func NewAspectRuntimeContext() *AspectRuntimeContext { return &AspectRuntimeContext{ - aspectContext: NewAspectContext(), - storeService: cachedStoreService, - logger: log.NewNopLogger(), + aspectContext: NewAspectContext(), + storeService: cachedStoreService, + aspectStoreService: cachedAspectStoreService, + logger: log.NewNopLogger(), } } -func (c *AspectRuntimeContext) Init(storeService cstore.KVStoreService) { +func (c *AspectRuntimeContext) Init(storeService, aspectStoreService cstore.KVStoreService) { cachedStoreService = storeService + cachedAspectStoreService = aspectStoreService c.storeService = storeService + c.aspectStoreService = aspectStoreService } func (c *AspectRuntimeContext) WithCosmosContext(newTxCtx cosmos.Context) { @@ -156,7 +163,7 @@ func (c *AspectRuntimeContext) CreateStateObject() { func (c *AspectRuntimeContext) GetAspectProperty(ctx *artelatypes.RunnerContext, version uint64, key string) []byte { metaStore, _, err := store.GetAspectMetaStore(&types.AspectStoreContext{ - StoreContext: types.NewGasFreeStoreContext(*c.cosmosCtx, cachedStoreService), + StoreContext: types.NewGasFreeStoreContext(*c.cosmosCtx, cachedStoreService, cachedAspectStoreService), AspectID: ctx.AspectId, }) if err != nil { @@ -456,7 +463,7 @@ func (k *AspectState) newStoreFromCache(aspectID common.Address) store.AspectSta if !ok { var err error s, err = store.GetAspectStateStore(&types.AspectStoreContext{ - StoreContext: types.NewGasFreeStoreContext(k.ctx, cachedStoreService), + StoreContext: types.NewGasFreeStoreContext(k.ctx, cachedStoreService, cachedAspectStoreService), AspectID: aspectID, }) if err != nil { diff --git a/x/evm/keeper/evm.go b/x/evm/keeper/evm.go index 999d09c..a3a4252 100644 --- a/x/evm/keeper/evm.go +++ b/x/evm/keeper/evm.go @@ -384,8 +384,10 @@ func (k *Keeper) ApplyMessageWithConfig(ctx cosmos.Context, lastHeight := uint64(ctx.BlockHeight()) // if transaction is Aspect operational, short the circuit and skip the processes if isAspectOpTx := asptypes.IsAspectContractAddr(msg.To); isAspectOpTx { - nativeContract := contract.NewAspectNativeContract(k.storeService, evm, - stateDB, k.logger) + nativeContract := contract.NewAspectNativeContract( + k.storeService, + k.aspectKeeper.GetStoreService(), + evm, stateDB, k.logger) nativeContract.Init() ret, leftoverGas, vmErr = nativeContract.ApplyMessage(ctx, msg, leftoverGas, commit) } else if contractCreation { diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index 4b302b3..64668c1 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -45,6 +45,7 @@ type ( bankKeeper types.BankKeeper stakingKeeper types.StakingKeeper feeKeeper types.FeeKeeper + aspectKeeper types.AspectKeeper subSpace types.ParamSubspace blockGetter types.BlockGetter logger log.Logger @@ -84,6 +85,7 @@ func NewKeeper( bankKeeper types.BankKeeper, stakingKeeper types.StakingKeeper, feeKeeper types.FeeKeeper, + aspectKeeper types.AspectKeeper, blockGetter types.BlockGetter, chainIDGetter types.ChainIDGetter, logger log.Logger, @@ -99,10 +101,10 @@ func NewKeeper( } // init aspect - aspect := provider.NewArtelaProvider(storeService, artvmtype.GetLastBlockHeight(blockGetter)) + aspect := provider.NewArtelaProvider(storeService, aspectKeeper.GetStoreService(), artvmtype.GetLastBlockHeight(blockGetter)) // new Aspect Runtime Context aspectRuntimeContext := artvmtype.NewAspectRuntimeContext() - aspectRuntimeContext.Init(storeService) + aspectRuntimeContext.Init(storeService, aspectKeeper.GetStoreService()) // pass in the parameter space to the CommitStateDB in order to use custom denominations for the EVM operations k := Keeper{ diff --git a/x/evm/module/module.go b/x/evm/module/module.go index 174a3e4..4cbf6e1 100644 --- a/x/evm/module/module.go +++ b/x/evm/module/module.go @@ -188,6 +188,7 @@ type ModuleInputs struct { BankKeeper types.BankKeeper StakingKeeper types.StakingKeeper FeeKeeper types.FeeKeeper + AspectKeeper types.AspectKeeper BlockGetter types.BlockGetter ChainIDGetter types.ChainIDGetter @@ -215,6 +216,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { in.BankKeeper, in.StakingKeeper, in.FeeKeeper, + in.AspectKeeper, in.BlockGetter, in.ChainIDGetter, in.Logger, diff --git a/x/evm/types/expected_keepers.go b/x/evm/types/expected_keepers.go index 059d337..a5a9102 100644 --- a/x/evm/types/expected_keepers.go +++ b/x/evm/types/expected_keepers.go @@ -5,6 +5,7 @@ import ( "math/big" "cosmossdk.io/core/address" + cstore "cosmossdk.io/core/store" sdk "github.com/cosmos/cosmos-sdk/types" authmodule "github.com/cosmos/cosmos-sdk/x/auth/types" stakingmodule "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -61,3 +62,7 @@ type ParamSubspace interface { Get(context.Context, []byte, interface{}) Set(context.Context, []byte, interface{}) } + +type AspectKeeper interface { + GetStoreService() cstore.KVStoreService +}