Skip to content

Commit cffbd67

Browse files
mergify[bot]srdtrk
andauthored
feat(08-wasm): querier plugins implemented (#5345) (#5382)
* imp: moved gas to internal * fix: fixed compiler complaints * feat: added 'QueryRouter' interface * imp: passing the queryRouter to keeper * Revert "fix: fixed compiler complaints" This reverts commit 208e314. * Revert "imp: moved gas to internal" This reverts commit b45b605. * fix(test): fixed keeper_test.go * imp: initial querier template * imp: moved querier to types * fix: compiler complaints * imp: removed querier from keeper * feat: including default querier * imp: added options * feat: querier implemented fully * docs: improved godocs * imp: improved the querier * style: improved styling of querier * fix: fixed options * fix: fixed options not being passed with config * style: renamed variables * imp: added review items * imp: review items * imp: set and get query plugins made private * docs: added more godocs * fix: default plugin not set * imp: review items * docs: added a godoc --------- Co-authored-by: Carlos Rodriguez <[email protected]> (cherry picked from commit e2bcb77) Co-authored-by: srdtrk <[email protected]>
1 parent dc7a8b6 commit cffbd67

File tree

13 files changed

+574
-78
lines changed

13 files changed

+574
-78
lines changed

modules/light-clients/08-wasm/internal/ibcwasm/expected_interfaces.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package ibcwasm
33
import (
44
wasmvm "github.com/CosmWasm/wasmvm"
55
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
6+
7+
"github.com/cosmos/cosmos-sdk/baseapp"
8+
sdk "github.com/cosmos/cosmos-sdk/types"
69
)
710

811
var _ WasmEngine = (*wasmvm.VM)(nil)
@@ -115,3 +118,14 @@ type WasmEngine interface {
115118
// Unpin is idempotent.
116119
Unpin(checksum wasmvm.Checksum) error
117120
}
121+
122+
type QueryRouter interface {
123+
// Route returns the GRPCQueryHandler for a given query route path or nil
124+
// if not found
125+
Route(path string) baseapp.GRPCQueryHandler
126+
}
127+
128+
type QueryPluginsI interface {
129+
// HandleQuery will route the query to the correct plugin and return the result
130+
HandleQuery(ctx sdk.Context, caller string, request wasmvmtypes.QueryRequest) ([]byte, error)
131+
}

modules/light-clients/08-wasm/internal/ibcwasm/querier.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

modules/light-clients/08-wasm/internal/ibcwasm/wasm.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ package ibcwasm
33
import (
44
"errors"
55

6-
wasmvm "github.com/CosmWasm/wasmvm"
7-
86
"cosmossdk.io/collections"
97
storetypes "cosmossdk.io/core/store"
108
)
119

1210
var (
1311
vm WasmEngine
1412

15-
querier wasmvm.Querier
13+
queryRouter QueryRouter
14+
queryPlugins QueryPluginsI
1615

1716
// state management
1817
Schema collections.Schema
@@ -36,19 +35,31 @@ func GetVM() WasmEngine {
3635
return vm
3736
}
3837

39-
// SetQuerier sets the custom wasm query handle for the 08-wasm module.
40-
// If wasmQuerier is nil a default querier is used that return always an error for any query.
41-
func SetQuerier(wasmQuerier wasmvm.Querier) {
42-
if wasmQuerier == nil {
43-
querier = &defaultQuerier{}
44-
} else {
45-
querier = wasmQuerier
38+
// SetQueryRouter sets the custom wasm query router for the 08-wasm module.
39+
// Panics if the queryRouter is nil.
40+
func SetQueryRouter(router QueryRouter) {
41+
if router == nil {
42+
panic(errors.New("query router must be not nil"))
43+
}
44+
queryRouter = router
45+
}
46+
47+
// GetQueryRouter returns the custom wasm query router for the 08-wasm module.
48+
func GetQueryRouter() QueryRouter {
49+
return queryRouter
50+
}
51+
52+
// SetQueryPlugins sets the current query plugins
53+
func SetQueryPlugins(plugins QueryPluginsI) {
54+
if plugins == nil {
55+
panic(errors.New("query plugins must be not nil"))
4656
}
57+
queryPlugins = plugins
4758
}
4859

49-
// GetQuerier returns the custom wasm query handler for the 08-wasm module.
50-
func GetQuerier() wasmvm.Querier {
51-
return querier
60+
// GetQueryPlugins returns the current query plugins
61+
func GetQueryPlugins() QueryPluginsI {
62+
return queryPlugins
5263
}
5364

5465
// SetupWasmStoreService sets up the 08-wasm module's collections.

modules/light-clients/08-wasm/keeper/keeper.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ import (
1111

1212
storetypes "cosmossdk.io/core/store"
1313
errorsmod "cosmossdk.io/errors"
14-
"cosmossdk.io/log"
1514

1615
"github.com/cosmos/cosmos-sdk/codec"
1716
sdk "github.com/cosmos/cosmos-sdk/types"
1817

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

2523
// Keeper defines the 08-wasm keeper
@@ -44,7 +42,8 @@ func NewKeeperWithVM(
4442
clientKeeper types.ClientKeeper,
4543
authority string,
4644
vm ibcwasm.WasmEngine,
47-
querier wasmvm.Querier,
45+
queryRouter ibcwasm.QueryRouter,
46+
opts ...Option,
4847
) Keeper {
4948
if clientKeeper == nil {
5049
panic(errors.New("client keeper must be not nil"))
@@ -62,16 +61,25 @@ func NewKeeperWithVM(
6261
panic(errors.New("authority must be non-empty"))
6362
}
6463

65-
ibcwasm.SetVM(vm)
66-
ibcwasm.SetQuerier(querier)
67-
ibcwasm.SetupWasmStoreService(storeService)
68-
69-
return Keeper{
64+
keeper := &Keeper{
7065
cdc: cdc,
7166
storeService: storeService,
7267
clientKeeper: clientKeeper,
7368
authority: authority,
7469
}
70+
71+
// set query plugins to ensure there is a non-nil query plugin
72+
// regardless of what options the user provides
73+
ibcwasm.SetQueryPlugins(types.NewDefaultQueryPlugins())
74+
for _, opt := range opts {
75+
opt.apply(keeper)
76+
}
77+
78+
ibcwasm.SetVM(vm)
79+
ibcwasm.SetQueryRouter(queryRouter)
80+
ibcwasm.SetupWasmStoreService(storeService)
81+
82+
return *keeper
7583
}
7684

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

93-
return NewKeeperWithVM(cdc, storeService, clientKeeper, authority, vm, querier)
102+
return NewKeeperWithVM(cdc, storeService, clientKeeper, authority, vm, queryRouter, opts...)
94103
}
95104

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

101-
// Logger returns a module-specific logger.
102-
func (Keeper) Logger(ctx sdk.Context) log.Logger {
103-
return ctx.Logger().With("module", "x/"+exported.ModuleName+"-"+types.ModuleName)
104-
}
105-
106110
func (Keeper) storeWasmCode(ctx sdk.Context, code []byte, storeFn func(code wasmvm.WasmCode) (wasmvm.Checksum, error)) ([]byte, error) {
107111
var err error
108112
if types.IsGzip(code) {

modules/light-clients/08-wasm/keeper/keeper_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
158158
GetSimApp(suite.chainA).IBCKeeper.ClientKeeper,
159159
GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(),
160160
ibcwasm.GetVM(),
161-
nil,
161+
GetSimApp(suite.chainA).GRPCQueryRouter(),
162162
)
163163
},
164164
true,
@@ -173,7 +173,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
173173
GetSimApp(suite.chainA).IBCKeeper.ClientKeeper,
174174
"", // authority
175175
ibcwasm.GetVM(),
176-
nil,
176+
GetSimApp(suite.chainA).GRPCQueryRouter(),
177177
)
178178
},
179179
false,
@@ -188,7 +188,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
188188
nil, // client keeper,
189189
GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(),
190190
ibcwasm.GetVM(),
191-
nil,
191+
GetSimApp(suite.chainA).GRPCQueryRouter(),
192192
)
193193
},
194194
false,
@@ -203,7 +203,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
203203
GetSimApp(suite.chainA).IBCKeeper.ClientKeeper,
204204
GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(),
205205
nil,
206-
nil,
206+
GetSimApp(suite.chainA).GRPCQueryRouter(),
207207
)
208208
},
209209
false,
@@ -218,12 +218,27 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
218218
GetSimApp(suite.chainA).IBCKeeper.ClientKeeper,
219219
GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(),
220220
ibcwasm.GetVM(),
221-
nil,
221+
GetSimApp(suite.chainA).GRPCQueryRouter(),
222222
)
223223
},
224224
false,
225225
errors.New("store service must be not nil"),
226226
},
227+
{
228+
"failure: nil query router",
229+
func() {
230+
keeper.NewKeeperWithVM(
231+
GetSimApp(suite.chainA).AppCodec(),
232+
runtime.NewKVStoreService(GetSimApp(suite.chainA).GetKey(types.StoreKey)),
233+
GetSimApp(suite.chainA).IBCKeeper.ClientKeeper,
234+
GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(),
235+
ibcwasm.GetVM(),
236+
nil,
237+
)
238+
},
239+
false,
240+
errors.New("query router must be not nil"),
241+
},
227242
}
228243

229244
for _, tc := range testCases {

modules/light-clients/08-wasm/keeper/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (m Migrator) MigrateChecksums(ctx sdk.Context) error {
4242
return err
4343
}
4444

45-
m.keeper.Logger(ctx).Info("successfully migrated Checksums to collections")
45+
types.Logger(ctx).Info("successfully migrated Checksums to collections")
4646
return nil
4747
}
4848

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package keeper
2+
3+
import (
4+
"errors"
5+
6+
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/internal/ibcwasm"
7+
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
8+
)
9+
10+
// Option is an extension point to instantiate keeper with non default values
11+
type Option interface {
12+
apply(*Keeper)
13+
}
14+
15+
type optsFn func(*Keeper)
16+
17+
func (f optsFn) apply(keeper *Keeper) {
18+
f(keeper)
19+
}
20+
21+
// WithQueryPlugins is an optional constructor parameter to pass custom query plugins for wasmVM requests.
22+
// Missing fields will be filled with default queriers.
23+
func WithQueryPlugins(plugins *types.QueryPlugins) Option {
24+
return optsFn(func(_ *Keeper) {
25+
currentPlugins, ok := ibcwasm.GetQueryPlugins().(*types.QueryPlugins)
26+
if !ok {
27+
panic(errors.New("invalid query plugins type"))
28+
}
29+
newPlugins := currentPlugins.Merge(plugins)
30+
ibcwasm.SetQueryPlugins(&newPlugins)
31+
})
32+
}

0 commit comments

Comments
 (0)