2020-03-13 23:13:42 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2022-01-08 00:39:27 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2020-03-13 23:13:42 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"strconv"
|
2021-06-07 16:08:03 +00:00
|
|
|
"strings"
|
2020-03-27 18:34:03 +00:00
|
|
|
"time"
|
2020-03-13 23:13:42 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/tendermint/tendermint/crypto"
|
2020-03-13 23:13:42 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-04-27 18:19:05 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
2020-03-13 23:13:42 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-03-23 20:57:15 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/version"
|
2023-04-04 00:08:45 +00:00
|
|
|
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
|
2022-01-08 00:39:27 +00:00
|
|
|
paramsproposal "github.com/cosmos/cosmos-sdk/x/params/types/proposal"
|
2020-03-13 23:13:42 +00:00
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/committee/types"
|
2020-03-13 23:13:42 +00:00
|
|
|
)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
const PARAMS_CHANGE_PROPOSAL_EXAMPLE = `
|
|
|
|
{
|
2022-09-01 20:20:37 +00:00
|
|
|
"@type": "/cosmos.params.v1beta1.ParameterChangeProposal",
|
2022-01-08 00:39:27 +00:00
|
|
|
"title": "title",
|
|
|
|
"description": "description",
|
|
|
|
"changes": [{ "subspace": "subspace", "key": "key", "value": "value" }]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const COMMITTEE_CHANGE_PROPOSAL_EXAMPLE = `
|
|
|
|
{
|
2024-05-01 17:43:59 +00:00
|
|
|
"@type": "/0g.committee.v1beta1.CommitteeChangeProposal",
|
2022-01-08 00:39:27 +00:00
|
|
|
"title": "A Title",
|
|
|
|
"description": "A proposal description.",
|
|
|
|
"new_committee": {
|
2024-05-01 17:43:59 +00:00
|
|
|
"@type": "/0g.committee.v1beta1.MemberCommittee",
|
2022-01-08 00:39:27 +00:00
|
|
|
"base_committee": {
|
|
|
|
"id": "34",
|
|
|
|
"description": "member committee",
|
2024-05-01 05:53:58 +00:00
|
|
|
"members": ["0g1ze7y9qwdddejmy7jlw4cymqqlt2wh05yhwmrv2"],
|
2022-01-08 00:39:27 +00:00
|
|
|
"permissions": [],
|
|
|
|
"vote_threshold": "1.000000000000000000",
|
|
|
|
"proposal_duration": "86400s",
|
|
|
|
"tally_option": "TALLY_OPTION_DEADLINE"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const COMMITTEE_DELETE_PROPOSAL_EXAMPLE = `
|
|
|
|
{
|
2024-05-01 17:43:59 +00:00
|
|
|
"@type": "/0g.committee.v1beta1.CommitteeDeleteProposal",
|
2022-01-08 00:39:27 +00:00
|
|
|
"title": "A Title",
|
|
|
|
"description": "A proposal description.",
|
|
|
|
"committee_id": "1"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func GetTxCmd() *cobra.Command {
|
2020-03-13 23:13:42 +00:00
|
|
|
txCmd := &cobra.Command{
|
|
|
|
Use: types.ModuleName,
|
|
|
|
Short: "committee governance transactions subcommands",
|
|
|
|
DisableFlagParsing: true,
|
|
|
|
SuggestionsMinimumDistance: 2,
|
|
|
|
RunE: client.ValidateCmd,
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
cmds := []*cobra.Command{
|
|
|
|
getCmdVote(),
|
|
|
|
getCmdSubmitProposal(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
flags.AddTxFlagsToCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
txCmd.AddCommand(cmds...)
|
2020-03-13 23:13:42 +00:00
|
|
|
|
|
|
|
return txCmd
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
// getCmdSubmitProposal returns the command to submit a proposal to a committee
|
|
|
|
func getCmdSubmitProposal() *cobra.Command {
|
2020-03-13 23:13:42 +00:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "submit-proposal [committee-id] [proposal-file]",
|
2020-03-23 20:57:15 +00:00
|
|
|
Short: "Submit a governance proposal to a particular committee",
|
|
|
|
Long: fmt.Sprintf(`Submit a proposal to a committee so they can vote on it.
|
|
|
|
|
|
|
|
The proposal file must be the json encoded forms of the proposal type you want to submit.
|
|
|
|
For example:
|
|
|
|
%s
|
2022-01-08 00:39:27 +00:00
|
|
|
`, PARAMS_CHANGE_PROPOSAL_EXAMPLE),
|
2020-03-23 20:57:15 +00:00
|
|
|
Args: cobra.ExactArgs(2),
|
2022-01-08 00:39:27 +00:00
|
|
|
Example: fmt.Sprintf("%s tx %s submit-proposal 1 your-proposal.json", version.AppName, types.ModuleName),
|
2020-03-13 23:13:42 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-01-08 00:39:27 +00:00
|
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-13 23:13:42 +00:00
|
|
|
|
|
|
|
// Get proposing address
|
2022-01-08 00:39:27 +00:00
|
|
|
proposer := clientCtx.GetFromAddress()
|
2020-03-13 23:13:42 +00:00
|
|
|
|
|
|
|
// Get committee ID
|
|
|
|
committeeID, err := strconv.ParseUint(args[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("committee-id %s not a valid int", args[0])
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
// Get proposal content
|
|
|
|
var pubProposal types.PubProposal
|
|
|
|
contents, err := ioutil.ReadFile(args[1])
|
2020-03-13 23:13:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
if err := clientCtx.Codec.UnmarshalInterfaceJSON(contents, &pubProposal); err != nil {
|
2020-03-13 23:13:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = pubProposal.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build message and run basic validation
|
2022-01-08 00:39:27 +00:00
|
|
|
msg, err := types.NewMsgSubmitProposal(pubProposal, proposer, committeeID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-13 23:13:42 +00:00
|
|
|
err = msg.ValidateBasic()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign and broadcast message
|
2022-01-08 00:39:27 +00:00
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
|
2020-03-13 23:13:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
// getCmdVote returns the command to vote on a proposal.
|
|
|
|
func getCmdVote() *cobra.Command {
|
2020-03-13 23:13:42 +00:00
|
|
|
return &cobra.Command{
|
2021-06-07 16:08:03 +00:00
|
|
|
Use: "vote [proposal-id] [vote]",
|
|
|
|
Args: cobra.ExactArgs(2),
|
2020-03-23 20:57:15 +00:00
|
|
|
Short: "Vote for an active proposal",
|
2021-06-07 16:08:03 +00:00
|
|
|
Long: "Submit a [yes/no/abstain] vote for the proposal with id [proposal-id].",
|
2022-01-08 00:39:27 +00:00
|
|
|
Example: fmt.Sprintf("%s tx %s vote 2 yes", version.AppName, types.ModuleName),
|
2020-03-13 23:13:42 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-01-08 00:39:27 +00:00
|
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-13 23:13:42 +00:00
|
|
|
|
|
|
|
// Get voting address
|
2022-01-08 00:39:27 +00:00
|
|
|
from := clientCtx.GetFromAddress()
|
2020-03-13 23:13:42 +00:00
|
|
|
|
|
|
|
// validate that the proposal id is a uint
|
|
|
|
proposalID, err := strconv.ParseUint(args[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("proposal-id %s not a valid int, please input a valid proposal-id", args[0])
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:08:03 +00:00
|
|
|
rawVote := strings.ToLower(strings.TrimSpace(args[1]))
|
|
|
|
if len(rawVote) == 0 {
|
|
|
|
return fmt.Errorf("must specify a vote")
|
|
|
|
}
|
|
|
|
|
|
|
|
var vote types.VoteType
|
|
|
|
switch rawVote {
|
|
|
|
case "yes", "y":
|
2022-01-08 00:39:27 +00:00
|
|
|
vote = types.VOTE_TYPE_YES
|
2021-06-07 16:08:03 +00:00
|
|
|
case "no", "n":
|
2022-01-08 00:39:27 +00:00
|
|
|
vote = types.VOTE_TYPE_NO
|
2021-06-07 16:08:03 +00:00
|
|
|
case "abstain", "a":
|
2022-01-08 00:39:27 +00:00
|
|
|
vote = types.VOTE_TYPE_ABSTAIN
|
2021-06-07 16:08:03 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("must specify a valid vote type: (yes/y, no/n, abstain/a)")
|
|
|
|
}
|
|
|
|
|
2020-03-13 23:13:42 +00:00
|
|
|
// Build vote message and run basic validation
|
2021-06-07 16:08:03 +00:00
|
|
|
msg := types.NewMsgVote(from, proposalID, vote)
|
2020-03-13 23:13:42 +00:00
|
|
|
err = msg.ValidateBasic()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
|
2020-03-13 23:13:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-03-23 14:32:50 +00:00
|
|
|
|
2020-03-23 20:57:15 +00:00
|
|
|
// GetGovCmdSubmitProposal returns a command to submit a proposal to the gov module. It is passed to the gov module for use on its command subtree.
|
2022-01-08 00:39:27 +00:00
|
|
|
func GetGovCmdSubmitProposal() *cobra.Command {
|
2020-03-23 14:32:50 +00:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "committee [proposal-file] [deposit]",
|
|
|
|
Short: "Submit a governance proposal to change a committee.",
|
2020-03-23 20:57:15 +00:00
|
|
|
Long: fmt.Sprintf(`Submit a governance proposal to create, alter, or delete a committee.
|
|
|
|
|
|
|
|
The proposal file must be the json encoded form of the proposal type you want to submit.
|
|
|
|
For example, to create or update a committee:
|
|
|
|
%s
|
|
|
|
|
|
|
|
and to delete a committee:
|
|
|
|
%s
|
2022-01-08 00:39:27 +00:00
|
|
|
`, COMMITTEE_CHANGE_PROPOSAL_EXAMPLE, COMMITTEE_DELETE_PROPOSAL_EXAMPLE),
|
2020-03-23 20:57:15 +00:00
|
|
|
Args: cobra.ExactArgs(2),
|
2020-03-23 14:32:50 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-01-08 00:39:27 +00:00
|
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-23 14:32:50 +00:00
|
|
|
|
|
|
|
// Get proposing address
|
2022-01-08 00:39:27 +00:00
|
|
|
proposer := clientCtx.GetFromAddress()
|
2020-03-23 14:32:50 +00:00
|
|
|
|
|
|
|
// Get the deposit
|
2022-01-08 00:39:27 +00:00
|
|
|
deposit, err := sdk.ParseCoinsNormalized(args[1])
|
2020-03-23 14:32:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the proposal
|
|
|
|
bz, err := ioutil.ReadFile(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-04 00:08:45 +00:00
|
|
|
var content govv1beta1.Content
|
2022-01-08 00:39:27 +00:00
|
|
|
if err := clientCtx.Codec.UnmarshalInterfaceJSON(bz, &content); err != nil {
|
2020-03-23 14:32:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = content.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build message and run basic validation
|
2023-04-04 00:08:45 +00:00
|
|
|
msg, err := govv1beta1.NewMsgSubmitProposal(content, deposit, proposer)
|
2022-01-08 00:39:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-23 14:32:50 +00:00
|
|
|
err = msg.ValidateBasic()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign and broadcast message
|
2022-01-08 00:39:27 +00:00
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
|
2020-03-23 14:32:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
2020-03-23 20:57:15 +00:00
|
|
|
|
2020-04-24 22:36:08 +00:00
|
|
|
// MustGetExampleCommitteeChangeProposal is a helper function to return an example json proposal
|
2022-01-08 00:39:27 +00:00
|
|
|
func MustGetExampleCommitteeChangeProposal(cdc codec.Codec) string {
|
|
|
|
exampleChangeProposal, err := types.NewCommitteeChangeProposal(
|
2020-03-23 20:57:15 +00:00
|
|
|
"A Title",
|
|
|
|
"A description of this proposal.",
|
2022-01-08 00:39:27 +00:00
|
|
|
types.MustNewMemberCommittee(
|
2020-03-23 20:57:15 +00:00
|
|
|
1,
|
2020-03-27 18:34:03 +00:00
|
|
|
"The description of this committee.",
|
2020-04-24 18:15:57 +00:00
|
|
|
[]sdk.AccAddress{sdk.AccAddress(crypto.AddressHash([]byte("exampleAddress")))},
|
2020-03-27 18:34:03 +00:00
|
|
|
[]types.Permission{
|
2022-01-08 00:39:27 +00:00
|
|
|
&types.GodPermission{},
|
2020-03-27 18:34:03 +00:00
|
|
|
},
|
|
|
|
sdk.MustNewDecFromStr("0.8"),
|
|
|
|
time.Hour*24*7,
|
2022-01-08 00:39:27 +00:00
|
|
|
types.TALLY_OPTION_FIRST_PAST_THE_POST,
|
2020-03-23 20:57:15 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
exampleChangeProposalBz, err := cdc.MarshalJSON(&exampleChangeProposal)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
var out bytes.Buffer
|
|
|
|
if err = json.Indent(&out, exampleChangeProposalBz, "", " "); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out.String()
|
2020-03-23 20:57:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 22:36:08 +00:00
|
|
|
// MustGetExampleCommitteeDeleteProposal is a helper function to return an example json proposal
|
2022-01-08 00:39:27 +00:00
|
|
|
func MustGetExampleCommitteeDeleteProposal(cdc codec.Codec) string {
|
2020-03-23 20:57:15 +00:00
|
|
|
exampleDeleteProposal := types.NewCommitteeDeleteProposal(
|
|
|
|
"A Title",
|
|
|
|
"A description of this proposal.",
|
|
|
|
1,
|
|
|
|
)
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := cdc.MarshalJSON(&exampleDeleteProposal)
|
2020-03-23 20:57:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
var out bytes.Buffer
|
|
|
|
if err = json.Indent(&out, bz, "", " "); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out.String()
|
2020-03-23 20:57:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 22:36:08 +00:00
|
|
|
// MustGetExampleParameterChangeProposal is a helper function to return an example json proposal
|
2022-01-08 00:39:27 +00:00
|
|
|
func MustGetExampleParameterChangeProposal(cdc codec.Codec) string {
|
2021-06-07 16:08:03 +00:00
|
|
|
value := fmt.Sprintf("\"%d\"", 1000000000)
|
2022-01-08 00:39:27 +00:00
|
|
|
exampleParameterChangeProposal := paramsproposal.NewParameterChangeProposal(
|
2020-03-23 20:57:15 +00:00
|
|
|
"A Title",
|
|
|
|
"A description of this proposal.",
|
2022-01-08 00:39:27 +00:00
|
|
|
[]paramsproposal.ParamChange{paramsproposal.NewParamChange("cdp", "SurplusAuctionThreshold", value)},
|
2020-03-23 20:57:15 +00:00
|
|
|
)
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := cdc.MarshalJSON(exampleParameterChangeProposal)
|
2020-03-23 20:57:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
var out bytes.Buffer
|
|
|
|
if err = json.Indent(&out, bz, "", " "); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out.String()
|
2020-03-23 20:57:15 +00:00
|
|
|
}
|