Skip to content

Commit

Permalink
feat: add draft-proposal for x/group (cosmos#13353)
Browse files Browse the repository at this point in the history
* feat: add `draft-proposal` for x/group

* add changelog

* extract useful function

* add `GetMsgFromTypeURL` tests
  • Loading branch information
julienrbrt authored Sep 21, 2022
1 parent fc32ef1 commit 7eb259f
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 58 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* (cli) [#13304](https://github.com/cosmos/cosmos-sdk/pull/13304) Add `tx gov draft-proposal` command for generating proposal JSONs.
* (cli) [#13353](https://github.com/cosmos/cosmos-sdk/pull/13353) Add `tx group draft-proposal` command for generating group proposal JSONs (skeleton).
* (cli) [#13304](https://github.com/cosmos/cosmos-sdk/pull/13304) Add `tx gov draft-proposal` command for generating proposal JSONs (skeleton).
* (cli) [#13207](https://github.com/cosmos/cosmos-sdk/pull/13207) Reduce user's password prompts when calling keyring `List()` function
* (x/authz) [#12648](https://github.com/cosmos/cosmos-sdk/pull/12648) Add an allow list, an optional list of addresses allowed to receive bank assets via authz MsgSend grant.
* (sdk.Coins) [#12627](https://github.com/cosmos/cosmos-sdk/pull/12627) Make a Denoms method on sdk.Coins.
Expand Down Expand Up @@ -487,7 +488,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/params) [#12724](https://github.com/cosmos/cosmos-sdk/pull/12724) Add `GetParamSetIfExists` function to params `Subspace` to prevent panics on breaking changes.
* [#12668](https://github.com/cosmos/cosmos-sdk/pull/12668) Add `authz_msg_index` event attribute to message events emitted when executing via `MsgExec` through `x/authz`.
* [#12697](https://github.com/cosmos/cosmos-sdk/pull/12697) Upgrade IAVL to v0.19.0 with fast index and error propagation. NOTE: first start will take a while to propagate into new model.
- Note: after upgrading to this version it may take up to 15 minutes to migrate from 0.17 to 0.19. This time is used to create the fast cache introduced into IAVL for performance
* Note: after upgrading to this version it may take up to 15 minutes to migrate from 0.17 to 0.19. This time is used to create the fast cache introduced into IAVL for performance
* [#12784](https://github.com/cosmos/cosmos-sdk/pull/12784) Upgrade Tendermint to 0.34.20.
* (x/bank) [#12674](https://github.com/cosmos/cosmos-sdk/pull/12674) Add convenience function `CreatePrefixedAccountStoreKey()` to construct key to access account's balance for a given denom.

Expand Down
23 changes: 23 additions & 0 deletions types/tx_msg.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package types

import (
"encoding/json"
fmt "fmt"

"github.com/cosmos/gogoproto/proto"

"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)

Expand Down Expand Up @@ -79,3 +83,22 @@ type TxEncoder func(tx Tx) ([]byte, error)
func MsgTypeURL(msg Msg) string {
return "/" + proto.MessageName(msg)
}

// GetMsgFromTypeURL returns a `sdk.Msg` message type from a type URL
func GetMsgFromTypeURL(cdc codec.Codec, input string) (Msg, error) {
var msg Msg
bz, err := json.Marshal(struct {
Type string `json:"@type"`
}{
Type: input,
})
if err != nil {
return nil, err
}

if err := cdc.UnmarshalInterfaceJSON(bz, &msg); err != nil {
return nil, fmt.Errorf("failed to determine sdk.Msg for %s URL : %w", input, err)
}

return msg, nil
}
10 changes: 10 additions & 0 deletions types/tx_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -33,3 +34,12 @@ func (s *testMsgSuite) TestMsg() {
func (s *testMsgSuite) TestMsgTypeURL() {
s.Require().Equal("/testdata.TestMsg", sdk.MsgTypeURL(new(testdata.TestMsg)))
}

func (s *testMsgSuite) TestGetMsgFromTypeURL() {
msg := new(testdata.TestMsg)
cdc := codec.NewProtoCodec(testdata.NewTestInterfaceRegistry())

result, err := sdk.GetMsgFromTypeURL(cdc, "/testdata.TestMsg")
s.Require().NoError(err)
s.Require().Equal(msg, result)
}
80 changes: 26 additions & 54 deletions x/gov/client/cli/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,6 @@ const (
draftMetadataFileName = "draft_metadata.json"
)

// ProposalMetadata is the metadata of a proposal
// This metadata is supposed to live off-chain when submitted in a proposal
type ProposalMetadata struct {
Title string `json:"title"`
Authors string `json:"authors"`
Summary string `json:"summary"`
Details string `json:"details"`
ProposalForumUrl string `json:"proposal_forum_url"` // named 'Url' instead of 'URL' for avoiding the camel case split
VoteOptionContext string `json:"vote_option_context"`
}

// Prompt prompts the user for all values of the given type.
// data is the struct to be filled
// namePrefix is the name to be display as "Enter <namePrefix> <field>"
Expand Down Expand Up @@ -115,21 +104,22 @@ func Prompt[T any](data T, namePrefix string) (T, error) {
return data, nil
}

type proposalTypes struct {
Type string
type proposalType struct {
Name string
MsgType string
Msg sdk.Msg
}

// Prompt the proposal type values and return the proposal and its metadata
func (p *proposalTypes) Prompt(cdc codec.Codec) (*proposal, ProposalMetadata, error) {
func (p *proposalType) Prompt(cdc codec.Codec) (*proposal, types.ProposalMetadata, error) {
proposal := &proposal{}

// set metadata
metadata, err := Prompt(ProposalMetadata{}, "proposal")
metadata, err := Prompt(types.ProposalMetadata{}, "proposal")
if err != nil {
return nil, metadata, fmt.Errorf("failed to set proposal metadata: %w", err)
}
// the metadata must be saved on IPFS, set placeholder
proposal.Metadata = "ipfs://CID"

// set deposit
Expand Down Expand Up @@ -160,60 +150,42 @@ func (p *proposalTypes) Prompt(cdc codec.Codec) (*proposal, ProposalMetadata, er
return proposal, metadata, nil
}

var supportedProposalTypes = []proposalTypes{
var suggestedProposalTypes = []proposalType{
{
Type: proposalText,
Name: proposalText,
MsgType: "", // no message for text proposal
},
{
Type: "community-pool-spend",
Name: "community-pool-spend",
MsgType: "/cosmos.distribution.v1beta1.MsgCommunityPoolSpend",
},
{
Type: "software-upgrade",
Name: "software-upgrade",
MsgType: "/cosmos.upgrade.v1beta1.MsgSoftwareUpgrade",
},
{
Type: "cancel-software-upgrade",
Name: "cancel-software-upgrade",
MsgType: "/cosmos.upgrade.v1beta1.MsgCancelUpgrade",
},
{
Type: proposalOther,
Name: proposalOther,
MsgType: "", // user will input the message type
},
}

func getProposalTypes() []string {
types := make([]string, len(supportedProposalTypes))
for i, p := range supportedProposalTypes {
types[i] = p.Type
func getProposalSuggestions() []string {
types := make([]string, len(suggestedProposalTypes))
for i, p := range suggestedProposalTypes {
types[i] = p.Name
}
return types
}

func getProposalMsg(cdc codec.Codec, input string) (sdk.Msg, error) {
var msg sdk.Msg
bz, err := json.Marshal(struct {
Type string `json:"@type"`
}{
Type: input,
})
if err != nil {
return nil, err
}

if err := cdc.UnmarshalInterfaceJSON(bz, &msg); err != nil {
return nil, fmt.Errorf("failed to determined sdk.Msg from %s proposal type : %w", input, err)
}

return msg, nil
}

// NewCmdDraftProposal let a user generate a draft proposal.
func NewCmdDraftProposal() *cobra.Command {
cmd := &cobra.Command{
Use: "draft-proposal",
Short: "Generate a draft proposal json file. The generated proposal json contains only one message.",
Short: "Generate a draft proposal json file. The generated proposal json contains only one message (skeleton).",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
Expand All @@ -224,24 +196,24 @@ func NewCmdDraftProposal() *cobra.Command {
// prompt proposal type
proposalTypesPrompt := promptui.Select{
Label: "Select proposal type",
Items: getProposalTypes(),
Items: getProposalSuggestions(),
}

_, proposalType, err := proposalTypesPrompt.Run()
_, selectedProposalType, err := proposalTypesPrompt.Run()
if err != nil {
return fmt.Errorf("failed to prompt proposal types: %w", err)
}

var proposal proposalTypes
for _, p := range supportedProposalTypes {
if strings.EqualFold(p.Type, proposalType) {
var proposal proposalType
for _, p := range suggestedProposalTypes {
if strings.EqualFold(p.Name, selectedProposalType) {
proposal = p
break
}
}

// create any proposal type
if proposal.Type == proposalOther {
if proposal.Name == proposalOther {
// prompt proposal type
msgPrompt := promptui.Select{
Label: "Select proposal message type:",
Expand All @@ -261,23 +233,23 @@ func NewCmdDraftProposal() *cobra.Command {
}

if proposal.MsgType != "" {
proposal.Msg, err = getProposalMsg(clientCtx.Codec, proposal.MsgType)
proposal.Msg, err = sdk.GetMsgFromTypeURL(clientCtx.Codec, proposal.MsgType)
if err != nil {
// should never happen
panic(err)
}
}

prop, metadata, err := proposal.Prompt(clientCtx.Codec)
result, metadata, err := proposal.Prompt(clientCtx.Codec)
if err != nil {
return err
}

if err := writeFile(draftMetadataFileName, metadata); err != nil {
if err := writeFile(draftProposalFileName, result); err != nil {
return err
}

if err := writeFile(draftProposalFileName, prop); err != nil {
if err := writeFile(draftMetadataFileName, metadata); err != nil {
return err
}

Expand Down
12 changes: 12 additions & 0 deletions x/gov/types/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package types

// ProposalMetadata is the metadata of a proposal
// This metadata is supposed to live off-chain when submitted in a proposal
type ProposalMetadata struct {
Title string `json:"title"`
Authors string `json:"authors"`
Summary string `json:"summary"`
Details string `json:"details"`
ProposalForumUrl string `json:"proposal_forum_url"` // named 'Url' instead of 'URL' for avoiding the camel case split
VoteOptionContext string `json:"vote_option_context"`
}
Loading

0 comments on commit 7eb259f

Please sign in to comment.