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

move CosmosSDK/gov to irishub/gov #45

Merged
merged 1 commit into from
Jul 24, 2018
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
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/irisnet/irishub/modules/gov"
"github.com/cosmos/cosmos-sdk/x/ibc"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"
Expand Down
2 changes: 1 addition & 1 deletion cmd/iriscli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/tx"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
govcmd "github.com/cosmos/cosmos-sdk/x/gov/client/cli"
govcmd "github.com/irisnet/irishub/modules/gov/client/cli"
ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/client/cli"
slashingcmd "github.com/cosmos/cosmos-sdk/x/slashing/client/cli"
stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli"
Expand Down
247 changes: 247 additions & 0 deletions modules/gov/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/irisnet/irishub/modules/gov"
"github.com/pkg/errors"
)

const (
flagProposalID = "proposalID"
flagTitle = "title"
flagDescription = "description"
flagProposalType = "type"
flagDeposit = "deposit"
flagProposer = "proposer"
flagDepositer = "depositer"
flagVoter = "voter"
flagOption = "option"
)

// submit a proposal tx
func GetCmdSubmitProposal(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "submit-proposal",
Short: "Submit a proposal along with an initial deposit",
RunE: func(cmd *cobra.Command, args []string) error {
title := viper.GetString(flagTitle)
description := viper.GetString(flagDescription)
strProposalType := viper.GetString(flagProposalType)
initialDeposit := viper.GetString(flagDeposit)

// get the from address from the name flag
from, err := sdk.AccAddressFromBech32(viper.GetString(flagProposer))
if err != nil {
return err
}

amount, err := sdk.ParseCoins(initialDeposit)
if err != nil {
return err
}

proposalType, err := gov.ProposalTypeFromString(strProposalType)
if err != nil {
return err
}

// create the message
msg := gov.NewMsgSubmitProposal(title, description, proposalType, from, amount)

err = msg.ValidateBasic()
if err != nil {
return err
}

// build and sign the transaction, then broadcast to Tendermint
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
// proposalID must be returned, and it is a part of response
ctx.PrintResponse = true

err = ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, []sdk.Msg{msg}, cdc)
if err != nil {
return err
}
return nil
},
}

cmd.Flags().String(flagTitle, "", "title of proposal")
cmd.Flags().String(flagDescription, "", "description of proposal")
cmd.Flags().String(flagProposalType, "", "proposalType of proposal")
cmd.Flags().String(flagDeposit, "", "deposit of proposal")
cmd.Flags().String(flagProposer, "", "proposer of proposal")

return cmd
}

// set a new Deposit transaction
func GetCmdDeposit(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "deposit",
Short: "deposit tokens for activing proposal",
RunE: func(cmd *cobra.Command, args []string) error {
// get the from address from the name flag
depositer, err := sdk.AccAddressFromBech32(viper.GetString(flagDepositer))
if err != nil {
return err
}

proposalID := viper.GetInt64(flagProposalID)

amount, err := sdk.ParseCoins(viper.GetString(flagDeposit))
if err != nil {
return err
}

// create the message
msg := gov.NewMsgDeposit(depositer, proposalID, amount)

err = msg.ValidateBasic()
if err != nil {
return err
}

// build and sign the transaction, then broadcast to Tendermint
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))

err = ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, []sdk.Msg{msg}, cdc)
if err != nil {
return err
}
return nil
},
}

cmd.Flags().String(flagProposalID, "", "proposalID of proposal depositing on")
cmd.Flags().String(flagDepositer, "", "depositer of deposit")
cmd.Flags().String(flagDeposit, "", "amount of deposit")

return cmd
}

// set a new Vote transaction
func GetCmdVote(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "vote",
Short: "vote for an active proposal, options: Yes/No/NoWithVeto/Abstain",
RunE: func(cmd *cobra.Command, args []string) error {

bechVoter := viper.GetString(flagVoter)
voter, err := sdk.AccAddressFromBech32(bechVoter)
if err != nil {
return err
}

proposalID := viper.GetInt64(flagProposalID)

option := viper.GetString(flagOption)

byteVoteOption, err := gov.VoteOptionFromString(option)
if err != nil {
return err
}

// create the message
msg := gov.NewMsgVote(voter, proposalID, byteVoteOption)

err = msg.ValidateBasic()
if err != nil {
return err
}

fmt.Printf("Vote[Voter:%s,ProposalID:%d,Option:%s]", bechVoter, msg.ProposalID, msg.Option)

// build and sign the transaction, then broadcast to Tendermint
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))

err = ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, []sdk.Msg{msg}, cdc)
if err != nil {
return err
}
return nil
},
}

cmd.Flags().String(flagProposalID, "", "proposalID of proposal voting on")
cmd.Flags().String(flagVoter, "", "bech32 voter address")
cmd.Flags().String(flagOption, "", "vote option {Yes, No, NoWithVeto, Abstain}")

return cmd
}

// Command to Get a Proposal Information
func GetCmdQueryProposal(storeName string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "query-proposal",
Short: "query proposal details",
RunE: func(cmd *cobra.Command, args []string) error {
proposalID := viper.GetInt64(flagProposalID)

ctx := context.NewCoreContextFromViper()

res, err := ctx.QueryStore(gov.KeyProposal(proposalID), storeName)
if len(res) == 0 || err != nil {
return errors.Errorf("proposalID [%d] is not existed", proposalID)
}

var proposal gov.Proposal
cdc.MustUnmarshalBinary(res, &proposal)
output, err := wire.MarshalJSONIndent(cdc, proposal)
if err != nil {
return err
}
fmt.Println(string(output))
return nil
},
}

cmd.Flags().String(flagProposalID, "", "proposalID of proposal being queried")

return cmd
}

// Command to Get a Proposal Information
func GetCmdQueryVote(storeName string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "query-vote",
Short: "query vote",
RunE: func(cmd *cobra.Command, args []string) error {
proposalID := viper.GetInt64(flagProposalID)

voterAddr, err := sdk.AccAddressFromBech32(viper.GetString(flagVoter))
if err != nil {
return err
}

ctx := context.NewCoreContextFromViper()

res, err := ctx.QueryStore(gov.KeyVote(proposalID, voterAddr), storeName)
if len(res) == 0 || err != nil {
return errors.Errorf("proposalID [%d] does not exist", proposalID)
}

var vote gov.Vote
cdc.MustUnmarshalBinary(res, &vote)
output, err := wire.MarshalJSONIndent(cdc, vote)
if err != nil {
return err
}
fmt.Println(string(output))
return nil
},
}

cmd.Flags().String(flagProposalID, "", "proposalID of proposal voting on")
cmd.Flags().String(flagVoter, "", "bech32 voter address")

return cmd
}
120 changes: 120 additions & 0 deletions modules/gov/depositsvotes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package gov

import (
"encoding/json"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"
)

// Vote
type Vote struct {
Voter sdk.AccAddress `json:"voter"` // address of the voter
ProposalID int64 `json:"proposal_id"` // proposalID of the proposal
Option VoteOption `json:"option"` // option from OptionSet chosen by the voter
}

// Deposit
type Deposit struct {
Depositer sdk.AccAddress `json:"depositer"` // Address of the depositer
ProposalID int64 `json:"proposal_id"` // proposalID of the proposal
Amount sdk.Coins `json:"amount"` // Deposit amount
}

// Type that represents VoteOption as a byte
type VoteOption byte

//nolint
const (
OptionEmpty VoteOption = 0x00
OptionYes VoteOption = 0x01
OptionAbstain VoteOption = 0x02
OptionNo VoteOption = 0x03
OptionNoWithVeto VoteOption = 0x04
)

// String to proposalType byte. Returns ff if invalid.
func VoteOptionFromString(str string) (VoteOption, error) {
switch str {
case "Yes":
return OptionYes, nil
case "Abstain":
return OptionAbstain, nil
case "No":
return OptionNo, nil
case "NoWithVeto":
return OptionNoWithVeto, nil
default:
return VoteOption(0xff), errors.Errorf("'%s' is not a valid vote option", str)
}
}

// Is defined VoteOption
func validVoteOption(option VoteOption) bool {
if option == OptionYes ||
option == OptionAbstain ||
option == OptionNo ||
option == OptionNoWithVeto {
return true
}
return false
}

// Marshal needed for protobuf compatibility
func (vo VoteOption) Marshal() ([]byte, error) {
return []byte{byte(vo)}, nil
}

// Unmarshal needed for protobuf compatibility
func (vo *VoteOption) Unmarshal(data []byte) error {
*vo = VoteOption(data[0])
return nil
}

// Marshals to JSON using string
func (vo VoteOption) MarshalJSON() ([]byte, error) {
return json.Marshal(vo.String())
}

// Unmarshals from JSON assuming Bech32 encoding
func (vo *VoteOption) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return nil
}

bz2, err := VoteOptionFromString(s)
if err != nil {
return err
}
*vo = bz2
return nil
}

// Turns VoteOption byte to String
func (vo VoteOption) String() string {
switch vo {
case OptionYes:
return "Yes"
case OptionAbstain:
return "Abstain"
case OptionNo:
return "No"
case OptionNoWithVeto:
return "NoWithVeto"
default:
return ""
}
}

// For Printf / Sprintf, returns bech32 when using %s
func (vo VoteOption) Format(s fmt.State, verb rune) {
switch verb {
case 's':
s.Write([]byte(fmt.Sprintf("%s", vo.String())))
default:
s.Write([]byte(fmt.Sprintf("%v", byte(vo))))
}
}
Loading