Skip to content

Commit

Permalink
the smartest way
Browse files Browse the repository at this point in the history
  • Loading branch information
quasisamurai committed Sep 27, 2022
1 parent a313108 commit 928d8ef
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 73 deletions.
30 changes: 30 additions & 0 deletions cmd/neutrond/genwasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"

wasmcli "github.com/CosmWasm/wasmd/x/wasm/client/cli"
)

func addGenesisWasmMsgCmd(defaultNodeHome string) *cobra.Command {
txCmd := &cobra.Command{
Use: "add-wasm-message",
Short: "Wasm genesis subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

genesisIO := wasmcli.NewDefaultGenesisIO()

txCmd.AddCommand(
wasmcli.GenesisStoreCodeCmd(defaultNodeHome, genesisIO),
wasmcli.GenesisInstantiateContractCmd(defaultNodeHome, genesisIO),
wasmcli.GenesisExecuteContractCmd(defaultNodeHome, genesisIO),
wasmcli.GenesisListContractsCmd(defaultNodeHome, genesisIO),
wasmcli.GenesisListCodesCmd(defaultNodeHome, genesisIO),
)

return txCmd
}
6 changes: 5 additions & 1 deletion cmd/neutrond/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
genutilcli.GenTxCmd(app.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
genutilcli.ValidateGenesisCmd(app.ModuleBasics),
AddGenesisAccountCmd(app.DefaultNodeHome),
addGenesisWasmMsgCmd(app.DefaultNodeHome),
tmcli.NewCompletionCmd(rootCmd, true),
// testnetCmd(app.ModuleBasics, banktypes.GenesisBalancesIterator{}),
debug.Cmd(),
Expand Down Expand Up @@ -208,11 +209,14 @@ func (ac appCreator) newApp(
wasmOpts = append(wasmOpts, wasmkeeper.WithVMCacheMetrics(prometheus.DefaultRegisterer))
}

enabledWasmProposals := app.GetEnabledProposals()
logger.Info("Enabled wasm proposals", "proposals", enabledWasmProposals)

return app.New(logger, db, traceStore, true, skipUpgradeHeights,
cast.ToString(appOpts.Get(flags.FlagHome)),
cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)),
ac.encCfg,
app.GetEnabledProposals(),
enabledWasmProposals,
appOpts,
wasmOpts,
baseapp.SetPruning(pruningOpts),
Expand Down
37 changes: 33 additions & 4 deletions wasmbinding/bindings/msg.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bindings

import (
types "github.com/neutron-org/neutron/x/adminmodule/types"
icqtypes "github.com/neutron-org/neutron/x/interchainqueries/types"
)

Expand Down Expand Up @@ -67,11 +66,41 @@ type AddAdminResponse struct {
}

type SubmitProposal struct {
TextProposal types.TextProposal
ParamChangeProposal types.ParamChangeProposal
Proposals Proposals
Proposer string
}

type SubmitProposalResponse struct {
type Proposals struct {
TextProposal *TextProposal
ParamChangeProposal *ParamChangeProposal
CommunityPoolSpendProposal *CommunityPoolSpendProposal
}

type TextProposal struct {
Title string
Description string
}

type ParamChangeProposal struct {
Title string
Description string
Changes []ParamChange
}

type CommunityPoolSpendProposal struct {
}

type SoftwareUpgradeProposal struct {
}
type CancelSoftwareUpgradeProposal struct {
}

type ClientUpdateProposal struct {
}
type ParamChange struct {
Subspace string
Key string
Value string
}

// RegisterInterchainQueryResponse holds response for RegisterInterchainQuery
Expand Down
27 changes: 20 additions & 7 deletions wasmbinding/message_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/gogo/protobuf/proto"

"github.com/neutron-org/neutron/wasmbinding/bindings"
adminkeeper "github.com/neutron-org/neutron/x/adminmodule/keeper"
Expand Down Expand Up @@ -232,19 +234,30 @@ func (m *CustomMessenger) submitProposal(ctx sdk.Context, contractAddr sdk.AccAd
}

func (m *CustomMessenger) PerformSubmitProposal(ctx sdk.Context, contractAddr sdk.AccAddress, submitProposal *bindings.SubmitProposal) (*admintypes.MsgSubmitProposalResponse, error) {
msg := admintypes.MsgSubmitProposal{Proposer: contractAddr.String()}

if submitProposal.Proposals.TextProposal != nil {

prop := govtypes.TextProposal{
Title: submitProposal.Proposals.TextProposal.Title,
Description: submitProposal.Proposals.TextProposal.Description,
}
cont, err := proto.Marshal(&prop)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to marshall incoming SubmitProposal message")
}
msg.Content = &types.Any{
TypeUrl: "/cosmos.gov.v1beta1.TextProposal",
Value: cont,
}

prop := admintypes.MsgSubmitProposal{
Proposer: contractAddr.String(),
Proposals: admintypes.Proposals{
TextProposal: &submitProposal.TextProposal,
ParamChangeProposal: &submitProposal.ParamChangeProposal},
}

if err := prop.ValidateBasic(); err != nil {
if err := msg.ValidateBasic(); err != nil {
return nil, sdkerrors.Wrap(err, "failed to validate incoming SubmitProposal message")
}

response, err := m.Adminserver.SubmitProposal(sdk.WrapSDKContext(ctx), &prop)
response, err := m.Adminserver.SubmitProposal(sdk.WrapSDKContext(ctx), &msg)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to submit proposal")
}
Expand Down
2 changes: 1 addition & 1 deletion x/adminmodule/keeper/msg_server_submit_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (k msgServer) SubmitProposal(goCtx context.Context, msg *types.MsgSubmitPro
return nil, fmt.Errorf("proposer %s must be admin to submit proposals to admin-module", msg.Proposer)
}

proposal, err := k.Keeper.SubmitProposal(ctx, msg.GetProposal())
proposal, err := k.Keeper.SubmitProposal(ctx, msg.GetContent())
if err != nil {
return nil, err
}
Expand Down
123 changes: 101 additions & 22 deletions x/adminmodule/types/message_submit_proposal.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,111 @@
package types

import (
"fmt"

"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
distrTypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
paramChange "github.com/cosmos/cosmos-sdk/x/params/types/proposal"
upgradeTypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
clientUpdate "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
"github.com/gogo/protobuf/proto"
"gopkg.in/yaml.v2"
)

func (m *MsgSubmitProposal) GetProposal() govtypes.Content {
if m.Proposals.TextProposal != nil {
textProposal := govtypes.TextProposal{
Title: m.Proposals.TextProposal.Title,
Description: m.Proposals.TextProposal.Description,
}
return &textProposal
var _ sdk.Msg = &MsgSubmitProposal{}

func NewMsgSubmitProposal(content govtypes.Content, proposer sdk.AccAddress) (*MsgSubmitProposal, error) {
m := &MsgSubmitProposal{
Proposer: proposer.String(),
}

if m.Proposals.TextProposal != nil {
prop := m.Proposals.ParamChangeProposal
var paramsChange []paramChange.ParamChange
for _, param := range prop.Changes {
parameterChange := paramChange.ParamChange{
Subspace: param.Subspace,
Key: param.Key,
Value: param.Value,
err := m.SetContent(content)
if err != nil {
return nil, err
}
return m, nil
}

//func (m *MsgSubmitProposal) GetContent() govtypes.Content { // TODO m.Content.GetCachedValue() returns nil!
// content, ok := m.Content.GetCachedValue().(govtypes.Content)
// if !ok {
// return nil
// }
// return content
//}

func (m *MsgSubmitProposal) GetContent() govtypes.Content {
var err error
switch m.Content.TypeUrl {
case "/cosmos.gov.v1beta1.TextProposal":
{
var textProposal govtypes.TextProposal
err = proto.Unmarshal(m.Content.Value, &textProposal)
if err == nil {
return &textProposal
}
}
case "/cosmos.params.v1beta1.ParameterChangeProposal":
{
var paramChangeProposal paramChange.ParameterChangeProposal
err = proto.Unmarshal(m.Content.Value, &paramChangeProposal)
if err == nil {
return &paramChangeProposal
}
}
case "/cosmos.distribution.v1beta1.CommunityPoolSpendProposal":
{
var comPoolSpendProposal distrTypes.CommunityPoolSpendProposal
err = proto.Unmarshal(m.Content.Value, &comPoolSpendProposal)
if err == nil {
return &comPoolSpendProposal
}
}
case "/cosmos.upgrade.v1beta1.SoftwareUpgradeProposal":
{
var upgradeProposal upgradeTypes.SoftwareUpgradeProposal
err = proto.Unmarshal(m.Content.Value, &upgradeProposal)
if err == nil {
return &upgradeProposal
}
paramsChange = append(paramsChange, parameterChange)
}
paramsProposal := paramChange.ParameterChangeProposal{
Title: prop.Title,
Description: prop.Description,
Changes: paramsChange,
case "/cosmos.upgrade.v1beta1.CancelSoftwareUpgradeProposal":
{
var cancelUpgradeProposal upgradeTypes.CancelSoftwareUpgradeProposal
err = proto.Unmarshal(m.Content.Value, &cancelUpgradeProposal)
if err == nil {
return &cancelUpgradeProposal
}
}
case "/ibc.core.client.v1.ClientUpdateProposal":
{
var clientUpdateProposal clientUpdate.ClientUpdateProposal
err = proto.Unmarshal(m.Content.Value, &clientUpdateProposal)
if err == nil {
return &clientUpdateProposal
}
}
return &paramsProposal
}

return nil
}

func (m *MsgSubmitProposal) SetContent(content govtypes.Content) error {
msg, ok := content.(proto.Message)
if !ok {
return fmt.Errorf("can't proto marshal %T", msg)
}
any, err := types.NewAnyWithValue(msg)
if err != nil {
return err
}
m.Content = any
return nil
}

func (m *MsgSubmitProposal) Route() string {
return RouterKey
}
Expand All @@ -54,13 +122,24 @@ func (m *MsgSubmitProposal) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{proposer}
}

func (m *MsgSubmitProposal) GetSignBytes() []byte {
bz := govtypes.ModuleCdc.MustMarshalJSON(m)
return sdk.MustSortJSON(bz)
}

// String implements the Stringer interface
func (m *MsgSubmitProposal) String() string {
out, _ := yaml.Marshal(m)
return string(out)
}

// ValidateBasic implements Msg
func (m *MsgSubmitProposal) ValidateBasic() error {
if m.Proposer == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, m.Proposer)
}

content := m.GetProposal()
content := m.GetContent()
if content == nil {
return sdkerrors.Wrap(govtypes.ErrInvalidProposalContent, "missing content")
}
Expand Down
38 changes: 38 additions & 0 deletions x/adminmodule/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 928d8ef

Please sign in to comment.