staking non-tests go files compile

This commit is contained in:
rigelrozanski 2018-03-17 17:22:11 +01:00
parent 865b116837
commit 3cb5bdb166
2 changed files with 152 additions and 158 deletions

View File

@ -11,7 +11,6 @@ import (
crypto "github.com/tendermint/go-crypto" crypto "github.com/tendermint/go-crypto"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/builder" "github.com/cosmos/cosmos-sdk/client/builder"
"github.com/cosmos/cosmos-sdk/wire" // XXX fix "github.com/cosmos/cosmos-sdk/wire" // XXX fix
"github.com/cosmos/cosmos-sdk/x/stake" "github.com/cosmos/cosmos-sdk/x/stake"
@ -45,9 +44,6 @@ func GetCmdQueryCandidates(cdc *wire.Codec, storeName string) *cobra.Command {
Short: "Query for the set of validator-candidates pubkeys", Short: "Query for the set of validator-candidates pubkeys",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
var pks []crypto.PubKey
prove := !viper.GetBool(client.FlagTrustNode)
key := PrefixedKey(stake.Name, stake.CandidatesAddrKey) key := PrefixedKey(stake.Name, stake.CandidatesAddrKey)
res, err := builder.Query(key, storeName) res, err := builder.Query(key, storeName)
@ -88,7 +84,6 @@ func GetCmdQueryCandidate(cdc *wire.Codec, storeName string) *cobra.Command {
return err return err
} }
prove := !viper.GetBool(client.FlagTrustNode)
key := PrefixedKey(stake.Name, stake.GetCandidateKey(addr)) key := PrefixedKey(stake.Name, stake.GetCandidateKey(addr))
res, err := builder.Query(key, storeName) res, err := builder.Query(key, storeName)
@ -135,7 +130,6 @@ func GetCmdQueryDelegatorBond(cdc *wire.Codec, storeName string) *cobra.Command
} }
delegator := crypto.Address(bz) delegator := crypto.Address(bz)
prove := !viper.GetBool(client.FlagTrustNode)
key := PrefixedKey(stake.Name, stake.GetDelegatorBondKey(delegator, addr, cdc)) key := PrefixedKey(stake.Name, stake.GetDelegatorBondKey(delegator, addr, cdc))
res, err := builder.Query(key, storeName) res, err := builder.Query(key, storeName)
@ -178,7 +172,6 @@ func GetCmdQueryDelegatorBonds(cdc *wire.Codec, storeName string) *cobra.Command
} }
delegator := crypto.Address(bz) delegator := crypto.Address(bz)
prove := !viper.GetBool(client.FlagTrustNode)
key := PrefixedKey(stake.Name, stake.GetDelegatorBondsKey(delegator, cdc)) key := PrefixedKey(stake.Name, stake.GetDelegatorBondsKey(delegator, cdc))
res, err := builder.Query(key, storeName) res, err := builder.Query(key, storeName)

View File

@ -11,8 +11,9 @@ import (
crypto "github.com/tendermint/go-crypto" crypto "github.com/tendermint/go-crypto"
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/builder"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/stake" "github.com/cosmos/cosmos-sdk/x/stake"
) )
@ -29,113 +30,113 @@ const (
FlagDetails = "details" FlagDetails = "details"
) )
// nolint // common flagsets to add to various functions
var ( var (
CmdDeclareCandidacy = &cobra.Command{ fsPk = flag.NewFlagSet("", flag.ContinueOnError)
Use: "declare-candidacy", fsAmount = flag.NewFlagSet("", flag.ContinueOnError)
Short: "create new validator-candidate account and delegate some coins to it", fsShares = flag.NewFlagSet("", flag.ContinueOnError)
RunE: cmdDeclareCandidacy, fsCandidate = flag.NewFlagSet("", flag.ContinueOnError)
}
CmdEditCandidacy = &cobra.Command{
Use: "edit-candidacy",
Short: "edit and existing validator-candidate account",
RunE: cmdEditCandidacy,
}
CmdDelegate = &cobra.Command{
Use: "delegate",
Short: "delegate coins to an existing validator/candidate",
RunE: cmdDelegate,
}
CmdUnbond = &cobra.Command{
Use: "unbond",
Short: "unbond coins from a validator/candidate",
RunE: cmdUnbond,
}
) )
func init() { func init() {
// define the flags
fsPk := flag.NewFlagSet("", flag.ContinueOnError)
fsPk.String(FlagPubKey, "", "PubKey of the validator-candidate") fsPk.String(FlagPubKey, "", "PubKey of the validator-candidate")
fsAmount := flag.NewFlagSet("", flag.ContinueOnError)
fsAmount.String(FlagAmount, "1fermion", "Amount of coins to bond") fsAmount.String(FlagAmount, "1fermion", "Amount of coins to bond")
fsShares := flag.NewFlagSet("", flag.ContinueOnError)
fsShares.String(FlagShares, "", "Amount of shares to unbond, either in decimal or keyword MAX (ex. 1.23456789, 99, MAX)") fsShares.String(FlagShares, "", "Amount of shares to unbond, either in decimal or keyword MAX (ex. 1.23456789, 99, MAX)")
fsCandidate := flag.NewFlagSet("", flag.ContinueOnError)
fsCandidate.String(FlagMoniker, "", "validator-candidate name") fsCandidate.String(FlagMoniker, "", "validator-candidate name")
fsCandidate.String(FlagIdentity, "", "optional keybase signature") fsCandidate.String(FlagIdentity, "", "optional keybase signature")
fsCandidate.String(FlagWebsite, "", "optional website") fsCandidate.String(FlagWebsite, "", "optional website")
fsCandidate.String(FlagDetails, "", "optional detailed description space") fsCandidate.String(FlagDetails, "", "optional detailed description space")
// add the flags
CmdDelegate.Flags().AddFlagSet(fsPk)
CmdDelegate.Flags().AddFlagSet(fsAmount)
CmdUnbond.Flags().AddFlagSet(fsPk)
CmdUnbond.Flags().AddFlagSet(fsShares)
CmdDeclareCandidacy.Flags().AddFlagSet(fsPk)
CmdDeclareCandidacy.Flags().AddFlagSet(fsAmount)
CmdDeclareCandidacy.Flags().AddFlagSet(fsCandidate)
CmdEditCandidacy.Flags().AddFlagSet(fsPk)
CmdEditCandidacy.Flags().AddFlagSet(fsCandidate)
} }
func cmdDeclareCandidacy(cmd *cobra.Command, args []string) error { //_________________________________________________________________________________________
// create declare candidacy command
func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "declare-candidacy",
Short: "create new validator-candidate account and delegate some coins to it",
RunE: func(cmd *cobra.Command, args []string) error {
amount, err := sdk.ParseCoin(viper.GetString(FlagAmount)) amount, err := sdk.ParseCoin(viper.GetString(FlagAmount))
if err != nil { if err != nil {
return err return err
} }
addr, err := GetAddress(viper.GetString(FlagAddress)) addr, err := GetAddress(viper.GetString(FlagAddress))
if err != nil { if err != nil {
return err return err
} }
pk, err := GetPubKey(viper.GetString(FlagPubKey)) pk, err := GetPubKey(viper.GetString(FlagPubKey))
if err != nil { if err != nil {
return err return err
} }
if viper.GetString(FlagMoniker) == "" { if viper.GetString(FlagMoniker) == "" {
return fmt.Errorf("please enter a moniker for the validator-candidate using --moniker") return fmt.Errorf("please enter a moniker for the validator-candidate using --moniker")
} }
description := stake.Description{ description := stake.Description{
Moniker: viper.GetString(FlagMoniker), Moniker: viper.GetString(FlagMoniker),
Identity: viper.GetString(FlagIdentity), Identity: viper.GetString(FlagIdentity),
Website: viper.GetString(FlagWebsite), Website: viper.GetString(FlagWebsite),
Details: viper.GetString(FlagDetails), Details: viper.GetString(FlagDetails),
} }
msg := stake.NewMsgDeclareCandidacy(addr, pk, amount, description)
tx := stake.NewMsgDeclareCandidacy(addr, pk, amount, description) // build and sign the transaction, then broadcast to Tendermint
return doTx(tx) res, err := builder.SignBuildBroadcast(msg, cdc)
if err != nil {
return err
} }
func cmdEditCandidacy(cmd *cobra.Command, args []string) error { fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
return nil
},
}
cmd.Flags().AddFlagSet(fsPk)
cmd.Flags().AddFlagSet(fsAmount)
cmd.Flags().AddFlagSet(fsCandidate)
return cmd
}
// create edit candidacy command
func GetCmdEditCandidacy(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "edit-candidacy",
Short: "edit and existing validator-candidate account",
RunE: func(cmd *cobra.Command, args []string) error {
addr, err := GetAddress(viper.GetString(FlagAddress)) addr, err := GetAddress(viper.GetString(FlagAddress))
if err != nil { if err != nil {
return err return err
} }
description := stake.Description{ description := stake.Description{
Moniker: viper.GetString(FlagMoniker), Moniker: viper.GetString(FlagMoniker),
Identity: viper.GetString(FlagIdentity), Identity: viper.GetString(FlagIdentity),
Website: viper.GetString(FlagWebsite), Website: viper.GetString(FlagWebsite),
Details: viper.GetString(FlagDetails), Details: viper.GetString(FlagDetails),
} }
msg := stake.NewMsgEditCandidacy(addr, description)
tx := stake.NewMsgEditCandidacy(addr, description) // build and sign the transaction, then broadcast to Tendermint
return doTx(tx) res, err := builder.SignBuildBroadcast(msg, cdc)
if err != nil {
return err
} }
func cmdDelegate(cmd *cobra.Command, args []string) error { fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
return nil
},
}
cmd.Flags().AddFlagSet(fsPk)
cmd.Flags().AddFlagSet(fsCandidate)
return cmd
}
// create edit candidacy command
func GetCmdDelegate(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "delegate",
Short: "delegate coins to an existing validator/candidate",
RunE: func(cmd *cobra.Command, args []string) error {
amount, err := sdk.ParseCoin(viper.GetString(FlagAmount)) amount, err := sdk.ParseCoin(viper.GetString(FlagAmount))
if err != nil { if err != nil {
return err return err
@ -146,13 +147,30 @@ func cmdDelegate(cmd *cobra.Command, args []string) error {
return err return err
} }
tx := stake.NewMsgDelegate(addr, amount) msg := stake.NewMsgDelegate(addr, amount)
return doTx(tx)
// build and sign the transaction, then broadcast to Tendermint
res, err := builder.SignBuildBroadcast(msg, cdc)
if err != nil {
return err
} }
func cmdUnbond(cmd *cobra.Command, args []string) error { fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
return nil
},
}
// TODO once go-wire refactored the shares can be broadcast as a Rat instead of a string cmd.Flags().AddFlagSet(fsPk)
cmd.Flags().AddFlagSet(fsAmount)
return cmd
}
// create edit candidacy command
func GetCmdUnbond(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "unbond",
Short: "unbond coins from a validator/candidate",
RunE: func(cmd *cobra.Command, args []string) error {
// check the shares before broadcasting // check the shares before broadcasting
sharesStr := viper.GetString(FlagShares) sharesStr := viper.GetString(FlagShares)
@ -173,8 +191,22 @@ func cmdUnbond(cmd *cobra.Command, args []string) error {
return err return err
} }
tx := stake.NewMsgUnbond(addr, sharesStr) msg := stake.NewMsgUnbond(addr, sharesStr)
return doTx(tx)
// build and sign the transaction, then broadcast to Tendermint
res, err := builder.SignBuildBroadcast(msg, cdc)
if err != nil {
return err
}
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
return nil
},
}
cmd.Flags().AddFlagSet(fsPk)
cmd.Flags().AddFlagSet(fsShares)
return cmd
} }
//______________________________________________________________________________________ //______________________________________________________________________________________
@ -202,44 +234,13 @@ func GetPubKey(pubKeyStr string) (pk crypto.PubKey, err error) {
} }
// GetPubKey - create an Address from a pubkey string // GetPubKey - create an Address from a pubkey string
func GetAddress(Address string) (addr sdk.Address, err error) { func GetAddress(address string) (addr sdk.Address, err error) {
if len(Address) == 0 { if len(address) == 0 {
return addr, errors.New("must use provide address") return addr, errors.New("must use provide address")
} }
bz, err := hex.DecodeString(addr) bz, err := hex.DecodeString(address)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return sdk.Address(bz), nil return sdk.Address(bz), nil
} }
//______________________________________________________________________________________
// XXX consolidate to client
func doTx(tx []byte) {
uri := viper.GetString(client.FlagNode)
if uri == "" {
return errors.New("Must define which node to query with --node")
}
node := client.GetNode(uri)
result, err := node.BroadcastTxCommit(tx)
if err != nil {
return err
}
if result.CheckTx.Code != uint32(0) {
fmt.Printf("CheckTx failed: (%d) %s\n",
result.CheckTx.Code,
result.CheckTx.Log)
}
if result.DeliverTx.Code != uint32(0) {
fmt.Printf("DeliverTx failed: (%d) %s\n",
result.DeliverTx.Code,
result.DeliverTx.Log)
}
fmt.Printf("Committed at block %d. Hash: %s\n", result.Height, result.Hash.String())
return nil
}