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

Upgrade wasm bindings #740

Merged
merged 2 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions app/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
simappparams "cosmossdk.io/simapp/params"
"github.com/cosmos/cosmos-sdk/std"

"github.com/cybercongress/go-cyber/v4/app/params"
)

Expand Down
7 changes: 5 additions & 2 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,12 @@ func NewAppKeepers(
appKeepers.RankKeeper,
appKeepers.GraphKeeper,
appKeepers.DmnKeeper,
appKeepers.GridKeeper,
&appKeepers.GridKeeper,
appKeepers.BandwidthMeter,
appKeepers.LiquidityKeeper,
&appKeepers.ResourcesKeeper,
appKeepers.IndexKeeper,
&appKeepers.AccountKeeper,
appKeepers.CyberbankKeeper,
)
wasmOpts = append(wasmOpts, cyberOpts...)

Expand Down
3 changes: 2 additions & 1 deletion app/upgrades/v4/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package v3

import (
"fmt"
liquiditytypes "github.com/cybercongress/go-cyber/v4/x/liquidity/types"
"time"

liquiditytypes "github.com/cybercongress/go-cyber/v4/x/liquidity/types"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/baseapp"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down
3 changes: 2 additions & 1 deletion cmd/cyber/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd

import (
"cosmossdk.io/simapp/params"
"errors"
"io"
"os"

"cosmossdk.io/simapp/params"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
tmcfg "github.com/cometbft/cometbft/config"
Expand Down
32 changes: 30 additions & 2 deletions docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
- [QueryThoughtParamsRequest](#cyber.dmn.v1beta1.QueryThoughtParamsRequest)
- [QueryThoughtResponse](#cyber.dmn.v1beta1.QueryThoughtResponse)
- [QueryThoughtStatsResponse](#cyber.dmn.v1beta1.QueryThoughtStatsResponse)
- [QueryThoughtsFeesRequest](#cyber.dmn.v1beta1.QueryThoughtsFeesRequest)
- [QueryThoughtsFeesResponse](#cyber.dmn.v1beta1.QueryThoughtsFeesResponse)
- [QueryThoughtsRequest](#cyber.dmn.v1beta1.QueryThoughtsRequest)
- [QueryThoughtsResponse](#cyber.dmn.v1beta1.QueryThoughtsResponse)
- [QueryThoughtsStatsRequest](#cyber.dmn.v1beta1.QueryThoughtsStatsRequest)
Expand Down Expand Up @@ -366,7 +368,7 @@

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `load` | [cosmos.base.v1beta1.DecProto](#cosmos.base.v1beta1.DecProto) | | |
| `load` | [string](#string) | | |



Expand Down Expand Up @@ -446,7 +448,7 @@

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `price` | [cosmos.base.v1beta1.DecProto](#cosmos.base.v1beta1.DecProto) | | |
| `price` | [string](#string) | | |



Expand Down Expand Up @@ -766,6 +768,31 @@



<a name="cyber.dmn.v1beta1.QueryThoughtsFeesRequest"></a>

### QueryThoughtsFeesRequest







<a name="cyber.dmn.v1beta1.QueryThoughtsFeesResponse"></a>

### QueryThoughtsFeesResponse



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `fees` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | repeated | |






<a name="cyber.dmn.v1beta1.QueryThoughtsRequest"></a>

### QueryThoughtsRequest
Expand Down Expand Up @@ -834,6 +861,7 @@
| `ThoughtStats` | [QueryThoughtParamsRequest](#cyber.dmn.v1beta1.QueryThoughtParamsRequest) | [QueryThoughtStatsResponse](#cyber.dmn.v1beta1.QueryThoughtStatsResponse) | | GET|/cyber/dmn/v1beta1/dmn/thought_stats|
| `Thoughts` | [QueryThoughtsRequest](#cyber.dmn.v1beta1.QueryThoughtsRequest) | [QueryThoughtsResponse](#cyber.dmn.v1beta1.QueryThoughtsResponse) | | GET|/cyber/dmn/v1beta1/dmn/thoughts|
| `ThoughtsStats` | [QueryThoughtsStatsRequest](#cyber.dmn.v1beta1.QueryThoughtsStatsRequest) | [QueryThoughtsStatsResponse](#cyber.dmn.v1beta1.QueryThoughtsStatsResponse) | | GET|/cyber/dmn/v1beta1/dmn/thoughts_stats|
| `ThoughtsFees` | [QueryThoughtsFeesRequest](#cyber.dmn.v1beta1.QueryThoughtsFeesRequest) | [QueryThoughtsFeesResponse](#cyber.dmn.v1beta1.QueryThoughtsFeesResponse) | | GET|/cyber/dmn/v1beta1/dmn/thoughts_fees|

<!-- end services -->

Expand Down
60 changes: 0 additions & 60 deletions plugins/msg_binding.go

This file was deleted.

61 changes: 0 additions & 61 deletions plugins/query_binding.go

This file was deleted.

75 changes: 75 additions & 0 deletions plugins/types/custom_querier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package types

import (
"encoding/json"
"errors"
bandwidthkeeper "github.com/cybercongress/go-cyber/v4/x/bandwidth/keeper"
dmnkeeper "github.com/cybercongress/go-cyber/v4/x/dmn/keeper"
graphkeeper "github.com/cybercongress/go-cyber/v4/x/graph/keeper"
gridkeeper "github.com/cybercongress/go-cyber/v4/x/grid/keeper"
rankkeeper "github.com/cybercongress/go-cyber/v4/x/rank/keeper"

errorsmod "cosmossdk.io/errors"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type ModuleQuerier interface {
HandleQuery(ctx sdk.Context, query CyberQuery) ([]byte, error)
}

var ErrHandleQuery = errors.New("error handle query")

type QueryPlugin struct {
moduleQueriers []ModuleQuerier
rankKeeper *rankkeeper.StateKeeper
graphKeeper *graphkeeper.GraphKeeper
dmnKeeper *dmnkeeper.Keeper
gridKeeper *gridkeeper.Keeper
bandwidthMeter *bandwidthkeeper.BandwidthMeter
}

func NewQueryPlugin(
moduleQueriers []ModuleQuerier,
rank *rankkeeper.StateKeeper,
graph *graphkeeper.GraphKeeper,
dmn *dmnkeeper.Keeper,
grid *gridkeeper.Keeper,
bandwidth *bandwidthkeeper.BandwidthMeter,
) *QueryPlugin {
return &QueryPlugin{
moduleQueriers: moduleQueriers,
rankKeeper: rank,
graphKeeper: graph,
dmnKeeper: dmn,
gridKeeper: grid,
bandwidthMeter: bandwidth,
}
}

func CustomQuerier(qp *QueryPlugin) func(ctx sdk.Context, request json.RawMessage) ([]byte, error) {
return func(ctx sdk.Context, request json.RawMessage) ([]byte, error) {
var contractQuery CyberQuery
if err := json.Unmarshal(request, &contractQuery); err != nil {
return nil, errorsmod.Wrap(err, "cyber query error")
}

// Iterate over the module queriers and dispatch to the appropriate one
for _, querier := range qp.moduleQueriers {
resp, err := querier.HandleQuery(ctx, contractQuery)
if err != nil {
if err == ErrHandleQuery {
// This querier cannot handle the query, try the next one
continue
}
// Some other error occurred, return it
return nil, err
}
// Query was handled successfully, return the response
return resp, nil
}

// If no querier could handle the query, return an error
return nil, wasmvmtypes.UnsupportedRequest{Kind: "unknown cyber query variant"}
}
}
80 changes: 80 additions & 0 deletions plugins/types/dispatch_msg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package types

import (
"encoding/json"
"errors"
dmnkeeper "github.com/cybercongress/go-cyber/v4/x/dmn/keeper"
graphkeeper "github.com/cybercongress/go-cyber/v4/x/graph/keeper"
gridkeeper "github.com/cybercongress/go-cyber/v4/x/grid/keeper"
resourceskeeper "github.com/cybercongress/go-cyber/v4/x/resources/keeper"

errorsmod "cosmossdk.io/errors"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

var _ wasmkeeper.Messenger = (*CustomMessenger)(nil)

var ErrHandleMsg = errors.New("error handle message")

type ModuleMessenger interface {
HandleMsg(ctx sdk.Context, contractAddr sdk.AccAddress, contractIBCPortID string, msg CyberMsg) ([]sdk.Event, [][]byte, error)
}

type CustomMessenger struct {
wrapped wasmkeeper.Messenger
moduleMessengers []ModuleMessenger
graphKeeper *graphkeeper.GraphKeeper
dmnKeeper *dmnkeeper.Keeper
gridKeeper *gridkeeper.Keeper
resourcesKeeper *resourceskeeper.Keeper
}

func CustomMessageDecorator(
moduleMessengers []ModuleMessenger,
graph *graphkeeper.GraphKeeper,
dmn *dmnkeeper.Keeper,
grid *gridkeeper.Keeper,
resources *resourceskeeper.Keeper,
) func(wasmkeeper.Messenger) wasmkeeper.Messenger {
return func(old wasmkeeper.Messenger) wasmkeeper.Messenger {
return &CustomMessenger{
wrapped: old,
moduleMessengers: moduleMessengers,
graphKeeper: graph,
dmnKeeper: dmn,
gridKeeper: grid,
resourcesKeeper: resources,
}
}
}

func (m *CustomMessenger) DispatchMsg(ctx sdk.Context, contractAddr sdk.AccAddress, contractIBCPortID string, msg wasmvmtypes.CosmosMsg) ([]sdk.Event, [][]byte, error) {
if msg.Custom != nil {
var contractMsg CyberMsg
if err := json.Unmarshal(msg.Custom, &contractMsg); err != nil {
return nil, nil, errorsmod.Wrap(err, "cyber msg error")
}

// Iterate over the module message handlers and dispatch to the appropriate one
for _, handler := range m.moduleMessengers {
event, resp, err := handler.HandleMsg(ctx, contractAddr, contractIBCPortID, contractMsg)
if err != nil {
if err == ErrHandleMsg {
// This handler cannot handle the message, try the next one
continue
}
// Some other error occurred, return it
return nil, nil, err
}
// Message was handled successfully, return the result
return event, resp, nil
}

// If no handler could handle the message, return an error
return nil, nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "unknow cyber message type")
}
return m.wrapped.DispatchMsg(ctx, contractAddr, contractIBCPortID, msg)
}
Loading