Skip to content

Commit

Permalink
fix: use aspect store service in aspect store v0
Browse files Browse the repository at this point in the history
  • Loading branch information
0xDevVoyager committed Nov 28, 2024
1 parent 525cd9d commit afd1a43
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 45 deletions.
4 changes: 4 additions & 0 deletions x/aspect/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 8 additions & 6 deletions x/aspect/provider/artela.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
}
}
2 changes: 1 addition & 1 deletion x/aspect/store/v0/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion x/aspect/store/v0/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion x/aspect/store/v0/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
39 changes: 24 additions & 15 deletions x/aspect/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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,
}
}

Expand Down
5 changes: 4 additions & 1 deletion x/evm/artela/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions x/evm/artela/contract/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
}
33 changes: 20 additions & 13 deletions x/evm/artela/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ const (
AspectModuleName = "aspect"
)

var cachedStoreService cstore.KVStoreService
var (
cachedStoreService cstore.KVStoreService
cachedAspectStoreService cstore.KVStoreService
)

type GetLastBlockHeight func() int64

Expand All @@ -56,28 +59,32 @@ 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
}

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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions x/evm/keeper/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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{
Expand Down
2 changes: 2 additions & 0 deletions x/evm/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -215,6 +216,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
in.BankKeeper,
in.StakingKeeper,
in.FeeKeeper,
in.AspectKeeper,
in.BlockGetter,
in.ChainIDGetter,
in.Logger,
Expand Down
5 changes: 5 additions & 0 deletions x/evm/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -61,3 +62,7 @@ type ParamSubspace interface {
Get(context.Context, []byte, interface{})
Set(context.Context, []byte, interface{})
}

type AspectKeeper interface {
GetStoreService() cstore.KVStoreService
}

0 comments on commit afd1a43

Please sign in to comment.