Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(08-wasm): querier plugins implemented #5345

Merged
merged 31 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b45b605
imp: moved gas to internal
srdtrk Dec 7, 2023
208e314
fix: fixed compiler complaints
srdtrk Dec 7, 2023
fbb076e
feat: added 'QueryRouter' interface
srdtrk Dec 7, 2023
e41b149
imp: passing the queryRouter to keeper
srdtrk Dec 7, 2023
f9ddfdb
Revert "fix: fixed compiler complaints"
srdtrk Dec 7, 2023
25f4cd3
Revert "imp: moved gas to internal"
srdtrk Dec 7, 2023
52439b7
fix(test): fixed keeper_test.go
srdtrk Dec 7, 2023
b4707ef
imp: initial querier template
srdtrk Dec 7, 2023
d18be76
imp: moved querier to types
srdtrk Dec 7, 2023
0dc9152
fix: compiler complaints
srdtrk Dec 7, 2023
6e34d19
imp: removed querier from keeper
srdtrk Dec 7, 2023
69513c1
feat: including default querier
srdtrk Dec 7, 2023
350b80b
imp: added options
srdtrk Dec 7, 2023
28da657
feat: querier implemented fully
srdtrk Dec 7, 2023
51bf845
docs: improved godocs
srdtrk Dec 8, 2023
94649b9
imp: improved the querier
srdtrk Dec 8, 2023
6f116f9
style: improved styling of querier
srdtrk Dec 8, 2023
e0bb1fb
fix: fixed options
srdtrk Dec 8, 2023
1691bd8
Merge branch 'main' into serdar/issue#5325-replace-querier
srdtrk Dec 8, 2023
ab155ce
fix: fixed options not being passed with config
srdtrk Dec 8, 2023
b445790
Merge branch 'main' into serdar/issue#5325-replace-querier
Dec 11, 2023
6995c10
style: renamed variables
srdtrk Dec 11, 2023
13cae40
imp: added review items
srdtrk Dec 11, 2023
1cccd74
imp: review items
srdtrk Dec 11, 2023
6d17d08
imp: set and get query plugins made private
srdtrk Dec 12, 2023
c3ef405
docs: added more godocs
srdtrk Dec 12, 2023
a200d19
Merge branch 'main' into serdar/issue#5325-replace-querier
srdtrk Dec 12, 2023
f51bfa4
fix: default plugin not set
srdtrk Dec 12, 2023
962ec46
imp: review items
srdtrk Dec 12, 2023
d61a67f
Merge branch 'main' into serdar/issue#5325-replace-querier
srdtrk Dec 12, 2023
f40e387
docs: added a godoc
srdtrk Dec 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package ibcwasm
import (
wasmvm "github.com/CosmWasm/wasmvm"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
)

var _ WasmEngine = (*wasmvm.VM)(nil)
Expand Down Expand Up @@ -115,3 +118,14 @@ type WasmEngine interface {
// Unpin is idempotent.
Unpin(checksum wasmvm.Checksum) error
}

type QueryRouter interface {
damiannolan marked this conversation as resolved.
Show resolved Hide resolved
// Route returns the GRPCQueryHandler for a given query route path or nil
// if not found
Route(path string) baseapp.GRPCQueryHandler
}

type QueryPluginsI interface {
// HandleQuery will route the query to the correct plugin and return the result
HandleQuery(ctx sdk.Context, caller string, request wasmvmtypes.QueryRequest) ([]byte, error)
}
17 changes: 0 additions & 17 deletions modules/light-clients/08-wasm/internal/ibcwasm/querier.go

This file was deleted.

37 changes: 24 additions & 13 deletions modules/light-clients/08-wasm/internal/ibcwasm/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package ibcwasm
import (
"errors"

wasmvm "github.com/CosmWasm/wasmvm"

"cosmossdk.io/collections"
storetypes "cosmossdk.io/core/store"
)

var (
vm WasmEngine

querier wasmvm.Querier
queryRouter QueryRouter
queryPlugins QueryPluginsI

// state management
Schema collections.Schema
Expand All @@ -36,19 +35,31 @@ func GetVM() WasmEngine {
return vm
}

// SetQuerier sets the custom wasm query handle for the 08-wasm module.
// If wasmQuerier is nil a default querier is used that return always an error for any query.
func SetQuerier(wasmQuerier wasmvm.Querier) {
if wasmQuerier == nil {
querier = &defaultQuerier{}
} else {
querier = wasmQuerier
// SetQueryRouter sets the custom wasm query router for the 08-wasm module.
// Panics if the queryRouter is nil.
func SetQueryRouter(router QueryRouter) {
if router == nil {
panic(errors.New("query router must be not nil"))
}
queryRouter = router
}

// GetQueryRouter returns the custom wasm query router for the 08-wasm module.
func GetQueryRouter() QueryRouter {
return queryRouter
}

// SetQueryPlugins sets the current query plugins
func SetQueryPlugins(plugins QueryPluginsI) {
srdtrk marked this conversation as resolved.
Show resolved Hide resolved
if plugins == nil {
panic(errors.New("query plugins must be not nil"))
}
queryPlugins = plugins
}

// GetQuerier returns the custom wasm query handler for the 08-wasm module.
func GetQuerier() wasmvm.Querier {
return querier
// GetQueryPlugins returns the current query plugins
func GetQueryPlugins() QueryPluginsI {
return queryPlugins
}

// SetupWasmStoreService sets up the 08-wasm module's collections.
Expand Down
34 changes: 19 additions & 15 deletions modules/light-clients/08-wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import (

storetypes "cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/internal/ibcwasm"
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
)

// Keeper defines the 08-wasm keeper
Expand All @@ -44,7 +42,8 @@ func NewKeeperWithVM(
clientKeeper types.ClientKeeper,
authority string,
vm ibcwasm.WasmEngine,
querier wasmvm.Querier,
queryRouter ibcwasm.QueryRouter,
opts ...Option,
) Keeper {
if clientKeeper == nil {
panic(errors.New("client keeper must be not nil"))
Expand All @@ -62,16 +61,25 @@ func NewKeeperWithVM(
panic(errors.New("authority must be non-empty"))
}

ibcwasm.SetVM(vm)
ibcwasm.SetQuerier(querier)
ibcwasm.SetupWasmStoreService(storeService)

return Keeper{
keeper := &Keeper{
cdc: cdc,
storeService: storeService,
clientKeeper: clientKeeper,
authority: authority,
}

// set query plugins to ensure there is a non-nil query plugin
// regardless of what options the user provides
ibcwasm.SetQueryPlugins(types.NewDefaultQueryPlugins())
for _, opt := range opts {
srdtrk marked this conversation as resolved.
Show resolved Hide resolved
opt.apply(keeper)
}

ibcwasm.SetVM(vm)
ibcwasm.SetQueryRouter(queryRouter)
ibcwasm.SetupWasmStoreService(storeService)

return *keeper
}

// NewKeeperWithConfig creates a new Keeper instance with the provided Wasm configuration.
Expand All @@ -83,26 +91,22 @@ func NewKeeperWithConfig(
clientKeeper types.ClientKeeper,
authority string,
wasmConfig types.WasmConfig,
querier wasmvm.Querier,
queryRouter ibcwasm.QueryRouter,
opts ...Option,
) Keeper {
vm, err := wasmvm.NewVM(wasmConfig.DataDir, wasmConfig.SupportedCapabilities, types.ContractMemoryLimit, wasmConfig.ContractDebugMode, types.MemoryCacheSize)
if err != nil {
panic(fmt.Errorf("failed to instantiate new Wasm VM instance: %v", err))
}

return NewKeeperWithVM(cdc, storeService, clientKeeper, authority, vm, querier)
return NewKeeperWithVM(cdc, storeService, clientKeeper, authority, vm, queryRouter, opts...)
}

// GetAuthority returns the 08-wasm module's authority.
func (k Keeper) GetAuthority() string {
return k.authority
}

// Logger returns a module-specific logger.
func (Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+exported.ModuleName+"-"+types.ModuleName)
}

func (Keeper) storeWasmCode(ctx sdk.Context, code []byte, storeFn func(code wasmvm.WasmCode) (wasmvm.Checksum, error)) ([]byte, error) {
var err error
if types.IsGzip(code) {
Expand Down
25 changes: 20 additions & 5 deletions modules/light-clients/08-wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
GetSimApp(suite.chainA).IBCKeeper.ClientKeeper,
GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(),
ibcwasm.GetVM(),
nil,
GetSimApp(suite.chainA).GRPCQueryRouter(),
)
},
true,
Expand All @@ -173,7 +173,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
GetSimApp(suite.chainA).IBCKeeper.ClientKeeper,
"", // authority
ibcwasm.GetVM(),
nil,
GetSimApp(suite.chainA).GRPCQueryRouter(),
)
},
false,
Expand All @@ -188,7 +188,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
nil, // client keeper,
GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(),
ibcwasm.GetVM(),
nil,
GetSimApp(suite.chainA).GRPCQueryRouter(),
)
},
false,
Expand All @@ -203,7 +203,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
GetSimApp(suite.chainA).IBCKeeper.ClientKeeper,
GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(),
nil,
nil,
GetSimApp(suite.chainA).GRPCQueryRouter(),
)
},
false,
Expand All @@ -218,12 +218,27 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
GetSimApp(suite.chainA).IBCKeeper.ClientKeeper,
GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(),
ibcwasm.GetVM(),
nil,
GetSimApp(suite.chainA).GRPCQueryRouter(),
)
},
false,
errors.New("store service must be not nil"),
},
{
"failure: nil query router",
func() {
keeper.NewKeeperWithVM(
GetSimApp(suite.chainA).AppCodec(),
runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)),
GetSimApp(suite.chainA).IBCKeeper.ClientKeeper,
GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(),
ibcwasm.GetVM(),
nil,
)
},
false,
errors.New("query router must be not nil"),
},
}

for _, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion modules/light-clients/08-wasm/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (m Migrator) MigrateChecksums(ctx sdk.Context) error {
return err
}

m.keeper.Logger(ctx).Info("successfully migrated Checksums to collections")
types.Logger(ctx).Info("successfully migrated Checksums to collections")
return nil
}

Expand Down
32 changes: 32 additions & 0 deletions modules/light-clients/08-wasm/keeper/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package keeper

import (
"errors"

"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/internal/ibcwasm"
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
)

// Option is an extension point to instantiate keeper with non default values
type Option interface {
apply(*Keeper)
}

type optsFn func(*Keeper)

func (f optsFn) apply(keeper *Keeper) {
f(keeper)
}

// WithQueryPlugins is an optional constructor parameter to pass custom query plugins for wasmVM requests.
// Missing fields will be filled with default queriers.
func WithQueryPlugins(plugins *types.QueryPlugins) Option {
return optsFn(func(_ *Keeper) {
currentPlugins, ok := ibcwasm.GetQueryPlugins().(*types.QueryPlugins)
if !ok {
panic(errors.New("invalid query plugins type"))
}
newPlugins := currentPlugins.Merge(plugins)
ibcwasm.SetQueryPlugins(&newPlugins)
})
}
Loading
Loading