Add stubs for handling IBC QueryRequests from contract
This commit is contained in:
@@ -268,10 +268,7 @@ func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.A
|
||||
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
|
||||
|
||||
// prepare querier
|
||||
querier := QueryHandler{
|
||||
Ctx: ctx,
|
||||
Plugins: k.queryPlugins,
|
||||
}
|
||||
querier := NewQueryHandler(ctx, k.queryPlugins, contractAddress)
|
||||
|
||||
// instantiate wasm contract
|
||||
gas := gasForContract(ctx)
|
||||
@@ -340,10 +337,7 @@ func (k Keeper) Execute(ctx sdk.Context, contractAddress sdk.AccAddress, caller
|
||||
info := types.NewInfo(caller, coins)
|
||||
|
||||
// prepare querier
|
||||
querier := QueryHandler{
|
||||
Ctx: ctx,
|
||||
Plugins: k.queryPlugins,
|
||||
}
|
||||
querier := NewQueryHandler(ctx, k.queryPlugins, contractAddress)
|
||||
gas := gasForContract(ctx)
|
||||
res, gasUsed, execErr := k.wasmer.Execute(codeInfo.CodeHash, env, info, msg, prefixStore, cosmwasmAPI, querier, gasMeter(ctx), gas)
|
||||
consumeGas(ctx, gasUsed)
|
||||
@@ -405,10 +399,7 @@ func (k Keeper) migrate(ctx sdk.Context, contractAddress sdk.AccAddress, caller
|
||||
env := types.NewEnv(ctx, contractAddress)
|
||||
|
||||
// prepare querier
|
||||
querier := QueryHandler{
|
||||
Ctx: ctx,
|
||||
Plugins: k.queryPlugins,
|
||||
}
|
||||
querier := NewQueryHandler(ctx, k.queryPlugins, contractAddress)
|
||||
|
||||
prefixStoreKey := types.GetContractStorePrefix(contractAddress)
|
||||
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
|
||||
@@ -454,10 +445,7 @@ func (k Keeper) Sudo(ctx sdk.Context, contractAddress sdk.AccAddress, msg []byte
|
||||
env := types.NewEnv(ctx, contractAddress)
|
||||
|
||||
// prepare querier
|
||||
querier := QueryHandler{
|
||||
Ctx: ctx,
|
||||
Plugins: k.queryPlugins,
|
||||
}
|
||||
querier := NewQueryHandler(ctx, k.queryPlugins, contractAddress)
|
||||
gas := gasForContract(ctx)
|
||||
res, gasUsed, execErr := k.wasmer.Sudo(codeInfo.CodeHash, env, msg, prefixStore, cosmwasmAPI, querier, gasMeter(ctx), gas)
|
||||
consumeGas(ctx, gasUsed)
|
||||
@@ -543,10 +531,7 @@ func (k Keeper) QuerySmart(ctx sdk.Context, contractAddr sdk.AccAddress, req []b
|
||||
return nil, err
|
||||
}
|
||||
// prepare querier
|
||||
querier := QueryHandler{
|
||||
Ctx: ctx,
|
||||
Plugins: k.queryPlugins,
|
||||
}
|
||||
querier := NewQueryHandler(ctx, k.queryPlugins, contractAddr)
|
||||
|
||||
env := types.NewEnv(ctx, contractAddr)
|
||||
queryResult, gasUsed, qErr := k.wasmer.Query(codeInfo.CodeHash, env, req, prefixStore, cosmwasmAPI, querier, gasMeter(ctx), gasForContract(ctx))
|
||||
|
||||
@@ -18,6 +18,15 @@ import (
|
||||
type QueryHandler struct {
|
||||
Ctx sdk.Context
|
||||
Plugins QueryPlugins
|
||||
Caller sdk.AccAddress
|
||||
}
|
||||
|
||||
func NewQueryHandler(ctx sdk.Context, plugins QueryPlugins, caller sdk.AccAddress) QueryHandler {
|
||||
return QueryHandler{
|
||||
Ctx: ctx,
|
||||
Plugins: plugins,
|
||||
Caller: caller,
|
||||
}
|
||||
}
|
||||
|
||||
// -- interfaces from baseapp - so we can use the GPRQueryRouter --
|
||||
@@ -51,6 +60,9 @@ func (q QueryHandler) Query(request wasmvmtypes.QueryRequest, gasLimit uint64) (
|
||||
if request.Custom != nil {
|
||||
return q.Plugins.Custom(subctx, request.Custom)
|
||||
}
|
||||
if request.IBC != nil {
|
||||
return q.Plugins.IBC(subctx, q.Caller, request.IBC)
|
||||
}
|
||||
if request.Staking != nil {
|
||||
return q.Plugins.Staking(subctx, request.Staking)
|
||||
}
|
||||
@@ -72,6 +84,7 @@ type CustomQuerier func(ctx sdk.Context, request json.RawMessage) ([]byte, error
|
||||
type QueryPlugins struct {
|
||||
Bank func(ctx sdk.Context, request *wasmvmtypes.BankQuery) ([]byte, error)
|
||||
Custom CustomQuerier
|
||||
IBC func(ctx sdk.Context, caller sdk.AccAddress, request *wasmvmtypes.IBCQuery) ([]byte, error)
|
||||
Staking func(ctx sdk.Context, request *wasmvmtypes.StakingQuery) ([]byte, error)
|
||||
Stargate func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error)
|
||||
Wasm func(ctx sdk.Context, request *wasmvmtypes.WasmQuery) ([]byte, error)
|
||||
@@ -81,6 +94,7 @@ func DefaultQueryPlugins(bank bankkeeper.ViewKeeper, staking stakingkeeper.Keepe
|
||||
return QueryPlugins{
|
||||
Bank: BankQuerier(bank),
|
||||
Custom: NoCustomQuerier,
|
||||
IBC: IBCQuerier(wasm),
|
||||
Staking: StakingQuerier(staking, distKeeper),
|
||||
Stargate: StargateQuerier(queryRouter),
|
||||
Wasm: WasmQuerier(wasm),
|
||||
@@ -98,6 +112,9 @@ func (e QueryPlugins) Merge(o *QueryPlugins) QueryPlugins {
|
||||
if o.Custom != nil {
|
||||
e.Custom = o.Custom
|
||||
}
|
||||
if o.IBC != nil {
|
||||
e.IBC = o.IBC
|
||||
}
|
||||
if o.Staking != nil {
|
||||
e.Staking = o.Staking
|
||||
}
|
||||
@@ -146,6 +163,53 @@ func NoCustomQuerier(sdk.Context, json.RawMessage) ([]byte, error) {
|
||||
return nil, wasmvmtypes.UnsupportedRequest{Kind: "custom"}
|
||||
}
|
||||
|
||||
func IBCQuerier(wasm *Keeper) func(ctx sdk.Context, caller sdk.AccAddress, request *wasmvmtypes.IBCQuery) ([]byte, error) {
|
||||
return func(ctx sdk.Context, caller sdk.AccAddress, request *wasmvmtypes.IBCQuery) ([]byte, error) {
|
||||
if request.PortID != nil {
|
||||
contractInfo := wasm.GetContractInfo(ctx, caller)
|
||||
res := wasmvmtypes.PortIDResponse{
|
||||
PortID: contractInfo.IBCPortID,
|
||||
}
|
||||
return json.Marshal(res)
|
||||
}
|
||||
if request.ListChannels != nil {
|
||||
portID := request.ListChannels.PortID
|
||||
if portID == "" {
|
||||
contractInfo := wasm.GetContractInfo(ctx, caller)
|
||||
portID = contractInfo.IBCPortID
|
||||
}
|
||||
// TODO: query the channels for this port
|
||||
res := wasmvmtypes.ListChannelsResponse{
|
||||
Channels: wasmvmtypes.IBCEndpoints{},
|
||||
}
|
||||
return json.Marshal(res)
|
||||
}
|
||||
if request.Channel != nil {
|
||||
channelID := request.Channel.ChannelID
|
||||
_ = channelID
|
||||
portID := request.Channel.PortID
|
||||
if portID == "" {
|
||||
contractInfo := wasm.GetContractInfo(ctx, caller)
|
||||
portID = contractInfo.IBCPortID
|
||||
}
|
||||
// TODO: query the (port, channel) info
|
||||
channel := &wasmvmtypes.IBCChannel{
|
||||
Endpoint: wasmvmtypes.IBCEndpoint{},
|
||||
CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{},
|
||||
Order: "",
|
||||
Version: "",
|
||||
CounterpartyVersion: "",
|
||||
ConnectionID: "",
|
||||
}
|
||||
res := wasmvmtypes.ChannelResponse{
|
||||
Channel: channel,
|
||||
}
|
||||
return json.Marshal(res)
|
||||
}
|
||||
return nil, wasmvmtypes.UnsupportedRequest{Kind: "unknown IBCQuery variant"}
|
||||
}
|
||||
}
|
||||
|
||||
func StargateQuerier(queryRouter GRPCQueryRouter) func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error) {
|
||||
return func(ctx sdk.Context, msg *wasmvmtypes.StargateQuery) ([]byte, error) {
|
||||
route := queryRouter.Route(msg.Path)
|
||||
|
||||
@@ -23,10 +23,7 @@ func (k Keeper) OnOpenChannel(
|
||||
}
|
||||
|
||||
env := types.NewEnv(ctx, contractAddr)
|
||||
querier := QueryHandler{
|
||||
Ctx: ctx,
|
||||
Plugins: k.queryPlugins,
|
||||
}
|
||||
querier := NewQueryHandler(ctx, k.queryPlugins, contractAddr)
|
||||
|
||||
gas := gasForContract(ctx)
|
||||
gasUsed, execErr := k.wasmer.IBCChannelOpen(codeInfo.CodeHash, env, channel, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
|
||||
@@ -56,10 +53,7 @@ func (k Keeper) OnConnectChannel(
|
||||
}
|
||||
|
||||
env := types.NewEnv(ctx, contractAddr)
|
||||
querier := QueryHandler{
|
||||
Ctx: ctx,
|
||||
Plugins: k.queryPlugins,
|
||||
}
|
||||
querier := NewQueryHandler(ctx, k.queryPlugins, contractAddr)
|
||||
|
||||
gas := gasForContract(ctx)
|
||||
res, gasUsed, execErr := k.wasmer.IBCChannelConnect(codeInfo.CodeHash, env, channel, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
|
||||
@@ -95,10 +89,7 @@ func (k Keeper) OnCloseChannel(
|
||||
}
|
||||
|
||||
params := types.NewEnv(ctx, contractAddr)
|
||||
querier := QueryHandler{
|
||||
Ctx: ctx,
|
||||
Plugins: k.queryPlugins,
|
||||
}
|
||||
querier := NewQueryHandler(ctx, k.queryPlugins, contractAddr)
|
||||
|
||||
gas := gasForContract(ctx)
|
||||
res, gasUsed, execErr := k.wasmer.IBCChannelClose(codeInfo.CodeHash, params, channel, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
|
||||
@@ -134,10 +125,7 @@ func (k Keeper) OnRecvPacket(
|
||||
}
|
||||
|
||||
env := types.NewEnv(ctx, contractAddr)
|
||||
querier := QueryHandler{
|
||||
Ctx: ctx,
|
||||
Plugins: k.queryPlugins,
|
||||
}
|
||||
querier := NewQueryHandler(ctx, k.queryPlugins, contractAddr)
|
||||
|
||||
gas := gasForContract(ctx)
|
||||
res, gasUsed, execErr := k.wasmer.IBCPacketReceive(codeInfo.CodeHash, env, packet, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
|
||||
@@ -174,10 +162,7 @@ func (k Keeper) OnAckPacket(
|
||||
}
|
||||
|
||||
env := types.NewEnv(ctx, contractAddr)
|
||||
querier := QueryHandler{
|
||||
Ctx: ctx,
|
||||
Plugins: k.queryPlugins,
|
||||
}
|
||||
querier := NewQueryHandler(ctx, k.queryPlugins, contractAddr)
|
||||
|
||||
gas := gasForContract(ctx)
|
||||
res, gasUsed, execErr := k.wasmer.IBCPacketAck(codeInfo.CodeHash, env, acknowledgement, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
|
||||
@@ -210,10 +195,7 @@ func (k Keeper) OnTimeoutPacket(
|
||||
}
|
||||
|
||||
env := types.NewEnv(ctx, contractAddr)
|
||||
querier := QueryHandler{
|
||||
Ctx: ctx,
|
||||
Plugins: k.queryPlugins,
|
||||
}
|
||||
querier := NewQueryHandler(ctx, k.queryPlugins, contractAddr)
|
||||
|
||||
gas := gasForContract(ctx)
|
||||
res, gasUsed, execErr := k.wasmer.IBCPacketTimeout(codeInfo.CodeHash, env, packet, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas)
|
||||
|
||||
Reference in New Issue
Block a user