Skip to content

Commit 4f22957

Browse files
mergify[bot]srdtrkDimitrisJim
authored
feat(08-wasm): querier plugins implemented (backport #5345) (#5381)
* feat(08-wasm): querier plugins implemented (#5345) * 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 <carlos@interchain.io> (cherry picked from commit e2bcb77) # Conflicts: # modules/light-clients/08-wasm/internal/ibcwasm/wasm.go # modules/light-clients/08-wasm/keeper/keeper.go # modules/light-clients/08-wasm/keeper/keeper_test.go # modules/light-clients/08-wasm/keeper/migrations.go # modules/light-clients/08-wasm/testing/simapp/app.go # modules/light-clients/08-wasm/types/querier_test.go * fix conflicts. * lint issues --------- Co-authored-by: srdtrk <59252793+srdtrk@users.noreply.github.com> Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
1 parent 9ddc6f0 commit 4f22957

File tree

12 files changed

+574
-70
lines changed

12 files changed

+574
-70
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: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package ibcwasm
33
import (
44
"errors"
55

6-
wasmvm "github.com/CosmWasm/wasmvm"
76
storetypes "github.com/cosmos/cosmos-sdk/store/types"
87
)
98

109
var (
11-
vm WasmEngine
12-
querier wasmvm.Querier
10+
vm WasmEngine
11+
12+
queryRouter QueryRouter
13+
queryPlugins QueryPluginsI
1314

1415
// wasmStoreKey stores the storeKey for the 08-wasm module. Using a global storeKey is required since
1516
// the client state interface functions do not have access to the keeper.
@@ -40,17 +41,29 @@ func GetWasmStoreKey() storetypes.StoreKey {
4041
return wasmStoreKey
4142
}
4243

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

53-
// GetQuerier returns the custom wasm query handler for the 08-wasm module.
54-
func GetQuerier() wasmvm.Querier {
55-
return querier
66+
// GetQueryPlugins returns the current query plugins
67+
func GetQueryPlugins() QueryPluginsI {
68+
return queryPlugins
5669
}

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
wasmvm "github.com/CosmWasm/wasmvm"
1111

1212
errorsmod "cosmossdk.io/errors"
13-
storetypes "github.com/cosmos/cosmos-sdk/store/types"
1413

1514
"github.com/cosmos/cosmos-sdk/codec"
15+
storetypes "github.com/cosmos/cosmos-sdk/store/types"
1616
sdk "github.com/cosmos/cosmos-sdk/types"
1717

1818
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/internal/ibcwasm"
@@ -41,7 +41,8 @@ func NewKeeperWithVM(
4141
clientKeeper types.ClientKeeper,
4242
authority string,
4343
vm ibcwasm.WasmEngine,
44-
querier wasmvm.Querier,
44+
queryRouter ibcwasm.QueryRouter,
45+
opts ...Option,
4546
) Keeper {
4647
if clientKeeper == nil {
4748
panic(errors.New("client keeper must be not nil"))
@@ -55,15 +56,24 @@ func NewKeeperWithVM(
5556
panic(errors.New("authority must be non-empty"))
5657
}
5758

58-
ibcwasm.SetVM(vm)
59-
ibcwasm.SetQuerier(querier)
60-
ibcwasm.SetWasmStoreKey(storeKey)
61-
62-
return Keeper{
59+
keeper := &Keeper{
6360
cdc: cdc,
6461
clientKeeper: clientKeeper,
6562
authority: authority,
6663
}
64+
65+
// set query plugins to ensure there is a non-nil query plugin
66+
// regardless of what options the user provides
67+
ibcwasm.SetQueryPlugins(types.NewDefaultQueryPlugins())
68+
for _, opt := range opts {
69+
opt.apply(keeper)
70+
}
71+
72+
ibcwasm.SetVM(vm)
73+
ibcwasm.SetQueryRouter(queryRouter)
74+
ibcwasm.SetWasmStoreKey(storeKey)
75+
76+
return *keeper
6777
}
6878

6979
// NewKeeperWithConfig creates a new Keeper instance with the provided Wasm configuration.
@@ -75,14 +85,15 @@ func NewKeeperWithConfig(
7585
clientKeeper types.ClientKeeper,
7686
authority string,
7787
wasmConfig types.WasmConfig,
78-
querier wasmvm.Querier,
88+
queryRouter ibcwasm.QueryRouter,
89+
opts ...Option,
7990
) Keeper {
8091
vm, err := wasmvm.NewVM(wasmConfig.DataDir, wasmConfig.SupportedCapabilities, types.ContractMemoryLimit, wasmConfig.ContractDebugMode, types.MemoryCacheSize)
8192
if err != nil {
8293
panic(fmt.Errorf("failed to instantiate new Wasm VM instance: %v", err))
8394
}
8495

85-
return NewKeeperWithVM(cdc, storeKey, clientKeeper, authority, vm, querier)
96+
return NewKeeperWithVM(cdc, storeKey, clientKeeper, authority, vm, queryRouter, opts...)
8697
}
8798

8899
// GetAuthority returns the 08-wasm module's authority.

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
157157
GetSimApp(suite.chainA).IBCKeeper.ClientKeeper,
158158
GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(),
159159
ibcwasm.GetVM(),
160-
nil,
160+
GetSimApp(suite.chainA).GRPCQueryRouter(),
161161
)
162162
},
163163
true,
@@ -172,7 +172,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
172172
GetSimApp(suite.chainA).IBCKeeper.ClientKeeper,
173173
"", // authority
174174
ibcwasm.GetVM(),
175-
nil,
175+
GetSimApp(suite.chainA).GRPCQueryRouter(),
176176
)
177177
},
178178
false,
@@ -187,7 +187,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
187187
nil, // client keeper,
188188
GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(),
189189
ibcwasm.GetVM(),
190-
nil,
190+
GetSimApp(suite.chainA).GRPCQueryRouter(),
191191
)
192192
},
193193
false,
@@ -202,12 +202,27 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
202202
GetSimApp(suite.chainA).IBCKeeper.ClientKeeper,
203203
GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(),
204204
nil,
205-
nil,
205+
GetSimApp(suite.chainA).GRPCQueryRouter(),
206206
)
207207
},
208208
false,
209209
errors.New("wasm VM must be not nil"),
210210
},
211+
{
212+
"failure: nil query router",
213+
func() {
214+
keeper.NewKeeperWithVM(
215+
GetSimApp(suite.chainA).AppCodec(),
216+
GetSimApp(suite.chainA).GetKey(types.StoreKey),
217+
GetSimApp(suite.chainA).IBCKeeper.ClientKeeper,
218+
GetSimApp(suite.chainA).WasmClientKeeper.GetAuthority(),
219+
ibcwasm.GetVM(),
220+
nil,
221+
)
222+
},
223+
false,
224+
errors.New("query router must be not nil"),
225+
},
211226
}
212227

213228
for _, tc := range testCases {
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)