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,152 +30,183 @@ 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 { //_________________________________________________________________________________________
amount, err := sdk.ParseCoin(viper.GetString(FlagAmount))
if err != nil { // create declare candidacy command
return err 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))
if err != nil {
return err
}
addr, err := GetAddress(viper.GetString(FlagAddress))
if err != nil {
return err
}
pk, err := GetPubKey(viper.GetString(FlagPubKey))
if err != nil {
return err
}
if viper.GetString(FlagMoniker) == "" {
return fmt.Errorf("please enter a moniker for the validator-candidate using --moniker")
}
description := stake.Description{
Moniker: viper.GetString(FlagMoniker),
Identity: viper.GetString(FlagIdentity),
Website: viper.GetString(FlagWebsite),
Details: viper.GetString(FlagDetails),
}
msg := stake.NewMsgDeclareCandidacy(addr, pk, amount, description)
// 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
},
} }
addr, err := GetAddress(viper.GetString(FlagAddress)) cmd.Flags().AddFlagSet(fsPk)
if err != nil { cmd.Flags().AddFlagSet(fsAmount)
return err cmd.Flags().AddFlagSet(fsCandidate)
} return cmd
pk, err := GetPubKey(viper.GetString(FlagPubKey))
if err != nil {
return err
}
if viper.GetString(FlagMoniker) == "" {
return fmt.Errorf("please enter a moniker for the validator-candidate using --moniker")
}
description := stake.Description{
Moniker: viper.GetString(FlagMoniker),
Identity: viper.GetString(FlagIdentity),
Website: viper.GetString(FlagWebsite),
Details: viper.GetString(FlagDetails),
}
tx := stake.NewMsgDeclareCandidacy(addr, pk, amount, description)
return doTx(tx)
} }
func cmdEditCandidacy(cmd *cobra.Command, args []string) error { // 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{
Moniker: viper.GetString(FlagMoniker),
Identity: viper.GetString(FlagIdentity),
Website: viper.GetString(FlagWebsite),
Details: viper.GetString(FlagDetails),
}
msg := stake.NewMsgEditCandidacy(addr, description)
// 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
},
} }
description := stake.Description{ cmd.Flags().AddFlagSet(fsPk)
Moniker: viper.GetString(FlagMoniker), cmd.Flags().AddFlagSet(fsCandidate)
Identity: viper.GetString(FlagIdentity), return cmd
Website: viper.GetString(FlagWebsite),
Details: viper.GetString(FlagDetails),
}
tx := stake.NewMsgEditCandidacy(addr, description)
return doTx(tx)
} }
func cmdDelegate(cmd *cobra.Command, args []string) error { // create edit candidacy command
amount, err := sdk.ParseCoin(viper.GetString(FlagAmount)) func GetCmdDelegate(cdc *wire.Codec) *cobra.Command {
if err != nil { cmd := &cobra.Command{
return err 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))
if err != nil {
return err
}
addr, err := GetAddress(viper.GetString(FlagAddress))
if err != nil {
return err
}
msg := stake.NewMsgDelegate(addr, amount)
// 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
},
} }
addr, err := GetAddress(viper.GetString(FlagAddress)) cmd.Flags().AddFlagSet(fsPk)
if err != nil { cmd.Flags().AddFlagSet(fsAmount)
return err return cmd
}
tx := stake.NewMsgDelegate(addr, amount)
return doTx(tx)
} }
func cmdUnbond(cmd *cobra.Command, args []string) error { // 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 {
// TODO once go-wire refactored the shares can be broadcast as a Rat instead of a string // check the shares before broadcasting
sharesStr := viper.GetString(FlagShares)
var shares sdk.Rat
if sharesStr != "MAX" {
var err error
shares, err = sdk.NewRatFromDecimal(sharesStr)
if err != nil {
return err
}
if !shares.GT(sdk.ZeroRat) {
return fmt.Errorf("shares must be positive integer or decimal (ex. 123, 1.23456789)")
}
}
// check the shares before broadcasting addr, err := GetAddress(viper.GetString(FlagAddress))
sharesStr := viper.GetString(FlagShares) if err != nil {
var shares sdk.Rat return err
if sharesStr != "MAX" { }
var err error
shares, err = sdk.NewRatFromDecimal(sharesStr) msg := stake.NewMsgUnbond(addr, sharesStr)
if err != nil {
return err // build and sign the transaction, then broadcast to Tendermint
} res, err := builder.SignBuildBroadcast(msg, cdc)
if !shares.GT(sdk.ZeroRat) { if err != nil {
return fmt.Errorf("shares must be positive integer or decimal (ex. 123, 1.23456789)") return err
} }
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
return nil
},
} }
addr, err := GetAddress(viper.GetString(FlagAddress)) cmd.Flags().AddFlagSet(fsPk)
if err != nil { cmd.Flags().AddFlagSet(fsShares)
return err return cmd
}
tx := stake.NewMsgUnbond(addr, sharesStr)
return doTx(tx)
} }
//______________________________________________________________________________________ //______________________________________________________________________________________
@ -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
}