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

WIP: Governance Module (Bianjie) #1041

Closed
wants to merge 14 commits into from
2 changes: 1 addition & 1 deletion client/context/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (ctx CoreContext) SignAndBuild(name, passphrase string, msg sdk.Msg, cdc *w
ChainID: chainID,
Sequences: []int64{sequence},
Msg: msg,
Fee: sdk.NewStdFee(10000, sdk.Coin{}), // TODO run simulate to estimate gas?
Fee: sdk.NewStdFee(100000, sdk.Coin{}), // TODO run simulate to estimate gas?
}

keybase, err := keys.GetKeyBase()
Expand Down
3 changes: 3 additions & 0 deletions client/lcd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
auth "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
bank "github.com/cosmos/cosmos-sdk/x/bank/client/rest"
ibc "github.com/cosmos/cosmos-sdk/x/ibc/client/rest"
gov "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
)

const (
Expand Down Expand Up @@ -83,5 +84,7 @@ func createHandler(cdc *wire.Codec) http.Handler {
auth.RegisterRoutes(ctx, r, cdc, "acc")
bank.RegisterRoutes(ctx, r, cdc, kb)
ibc.RegisterRoutes(ctx, r, cdc, kb)
gov.RegisterRoutes(ctx, r, cdc, kb)

return r
}
12 changes: 10 additions & 2 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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/cosmos/cosmos-sdk/x/ibc"
"github.com/cosmos/cosmos-sdk/x/stake"
)
Expand All @@ -38,12 +39,14 @@ type GaiaApp struct {
keyAccount *sdk.KVStoreKey
keyIBC *sdk.KVStoreKey
keyStake *sdk.KVStoreKey
keyGov *sdk.KVStoreKey

// Manage getting and setting accounts
accountMapper sdk.AccountMapper
coinKeeper bank.Keeper
ibcMapper ibc.Mapper
stakeKeeper stake.Keeper
govKeeper gov.Keeper
}

func NewGaiaApp(logger log.Logger, db dbm.DB) *GaiaApp {
Expand All @@ -57,6 +60,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB) *GaiaApp {
keyAccount: sdk.NewKVStoreKey("acc"),
keyIBC: sdk.NewKVStoreKey("ibc"),
keyStake: sdk.NewKVStoreKey("stake"),
keyGov: sdk.NewKVStoreKey("gov"),
}

// define the accountMapper
Expand All @@ -70,17 +74,20 @@ func NewGaiaApp(logger log.Logger, db dbm.DB) *GaiaApp {
app.coinKeeper = bank.NewKeeper(app.accountMapper)
app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.coinKeeper, app.RegisterCodespace(stake.DefaultCodespace))
app.govKeeper = gov.NewKeeper(app.keyGov, app.coinKeeper, app.stakeKeeper)

// register message routes
app.Router().
AddRoute("bank", bank.NewHandler(app.coinKeeper)).
AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.coinKeeper)).
AddRoute("stake", stake.NewHandler(app.stakeKeeper))
AddRoute("stake", stake.NewHandler(app.stakeKeeper)).
AddRoute("gov", gov.NewHandler(app.govKeeper))

// initialize BaseApp
app.SetInitChainer(app.initChainer)
app.SetBeginBlocker(gov.NewBeginBlocker(app.govKeeper))
app.SetEndBlocker(stake.NewEndBlocker(app.stakeKeeper))
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyIBC, app.keyStake)
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyIBC, app.keyStake, app.keyGov)
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, stake.FeeHandler))
err := app.LoadLatestVersion(app.keyMain)
if err != nil {
Expand All @@ -99,6 +106,7 @@ func MakeCodec() *wire.Codec {
auth.RegisterWire(cdc)
sdk.RegisterWire(cdc)
wire.RegisterCrypto(cdc)
gov.RegisterWire(cdc)
return cdc
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/gaia/cmd/gaiacli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/version"
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"
ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/client/cli"
stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli"

Expand Down Expand Up @@ -48,6 +49,7 @@ func main() {
stakecmd.GetCmdQueryCandidate("stake", cdc),
stakecmd.GetCmdQueryCandidates("stake", cdc),
stakecmd.GetCmdQueryDelegatorBond("stake", cdc),
govcmd.GetProposalCmd("gov", cdc),
//stakecmd.GetCmdQueryDelegatorBonds("stake", cdc),
)...)
rootCmd.AddCommand(
Expand All @@ -59,6 +61,9 @@ func main() {
stakecmd.GetCmdEditCandidacy(cdc),
stakecmd.GetCmdDelegate(cdc),
stakecmd.GetCmdUnbond(cdc),
govcmd.SubmitProposalCmd(cdc),
govcmd.DepositCmd(cdc),
govcmd.VoteCmd(cdc),
)...)

// add proxy, version and key info
Expand Down
180 changes: 180 additions & 0 deletions x/gov/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package cli

import (
"fmt"

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

"encoding/hex"
"github.com/pkg/errors"
"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/cosmos/cosmos-sdk/x/gov"
"strconv"
)

const (
flagTitle = "title"
flagDescription = "description"
flagProposalType = "type"
flagInitialDeposit = "deposit"
flagproposer = "proposer"
)

// submit a proposal tx
func SubmitProposalCmd(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "submitproposal",
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)
proposalType := viper.GetString(flagProposalType)
initialDeposit := viper.GetString(flagInitialDeposit)

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

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

// create the message
msg := gov.NewMsgSubmitProposal(title, description, proposalType, from, amount)
// build and sign the transaction, then broadcast to Tendermint
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))

res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, msg, cdc)
if err != nil {
return err
}

fmt.Printf("Committed at block:%d. Hash:%s.Response:%+v \n", res.Height, res.Hash.String(), res.DeliverTx)
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(flagInitialDeposit, "", "deposit of proposal")
cmd.Flags().String(flagproposer, "", "proposer of proposal")

return cmd
}

// set a new Deposit transaction
func DepositCmd(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "deposit [depositer] [proposalID] [amount]",
Short: "deposit your token [steak] for activing proposalI",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
// get the from address from the name flag
depositer, err := sdk.GetAddress(args[0])
if err != nil {
return err
}

proposalID, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
}

amount, err := sdk.ParseCoins(args[2])
if err != nil {
return err
}

// create the message
msg := gov.NewMsgDeposit(proposalID, depositer, amount)
// build and sign the transaction, then broadcast to Tendermint
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))

res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, msg, cdc)
if err != nil {
return err
}
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
return nil
},
}
return cmd
}

// set a new Vote transaction
func VoteCmd(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "vote [voter] [proposalID] [option]",
Short: "vote for current actived proposal,option:Yes/NO/NoWithVeto/Abstain",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
voter, err := sdk.GetAddress(args[0])
if err != nil {
return err
}

proposalID, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
}

option := args[2]
// create the message
msg := gov.NewMsgVote(voter, proposalID, option)

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

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

res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, msg, cdc)
if err != nil {
return err
}
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
return nil
},
}
return cmd
}

func GetProposalCmd(storeName string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "proposal [proposalID]",
Short: "query proposal details",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
proposalID, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return err
}
ctx := context.NewCoreContextFromViper()

key, _ := cdc.MarshalBinary(proposalID)
res, err := ctx.Query(key, storeName)
if len(res) == 0 || err != nil {
return errors.Errorf("proposalID [%d] is not existed", proposalID)
}

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

return nil
},
}
return cmd
}
Loading