2018-02-23 10:23:24 -08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-02-28 23:16:54 -08:00
|
|
|
"net/http"
|
2018-02-23 10:23:24 -08:00
|
|
|
"strconv"
|
2019-02-05 19:20:07 -08:00
|
|
|
"strings"
|
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
"github.com/gorilla/mux"
|
2018-02-23 10:23:24 -08:00
|
|
|
"github.com/spf13/cobra"
|
2019-02-14 08:53:36 -08:00
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2018-06-05 21:53:04 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2018-02-23 10:23:24 -08:00
|
|
|
)
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// TODO these next two functions feel kinda hacky based on their placement
|
|
|
|
|
2018-05-28 15:50:07 -07:00
|
|
|
//ValidatorCommand returns the validator set for a given height
|
2020-07-17 13:20:45 -07:00
|
|
|
func ValidatorCommand() *cobra.Command {
|
2018-02-23 10:23:24 -08:00
|
|
|
cmd := &cobra.Command{
|
2018-09-26 07:00:49 -07:00
|
|
|
Use: "tendermint-validator-set [height]",
|
2018-05-31 18:38:15 -07:00
|
|
|
Short: "Get the full tendermint validator set at given height",
|
2018-04-26 14:32:20 -07:00
|
|
|
Args: cobra.MaximumNArgs(1),
|
2019-02-05 19:20:07 -08:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-07-17 13:20:45 -07:00
|
|
|
clientCtx := client.GetClientContextFromCmd(cmd)
|
|
|
|
|
2019-02-05 19:20:07 -08:00
|
|
|
var height *int64
|
|
|
|
|
|
|
|
// optional height
|
|
|
|
if len(args) > 0 {
|
|
|
|
h, err := strconv.Atoi(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if h > 0 {
|
|
|
|
tmp := int64(h)
|
|
|
|
height = &tmp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 13:20:45 -07:00
|
|
|
page, _ := cmd.Flags().GetInt(flags.FlagPage)
|
|
|
|
limit, _ := cmd.Flags().GetInt(flags.FlagLimit)
|
2019-02-05 19:20:07 -08:00
|
|
|
|
2020-07-17 13:20:45 -07:00
|
|
|
result, err := GetValidators(clientCtx, height, page, limit)
|
2019-02-05 19:20:07 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
return clientCtx.PrintOutput(result)
|
2019-02-05 19:20:07 -08:00
|
|
|
},
|
2018-02-23 10:23:24 -08:00
|
|
|
}
|
2019-02-05 19:20:07 -08:00
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
|
2020-04-11 09:52:36 -07:00
|
|
|
cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
|
2020-01-16 13:46:51 -08:00
|
|
|
cmd.Flags().Int(flags.FlagPage, 0, "Query a specific page of paginated results")
|
|
|
|
cmd.Flags().Int(flags.FlagLimit, 100, "Query number of results returned per page")
|
2019-02-05 19:20:07 -08:00
|
|
|
|
2018-02-23 10:23:24 -08:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2018-06-05 21:53:04 -07:00
|
|
|
// Validator output in bech32 format
|
|
|
|
type ValidatorOutput struct {
|
2019-02-05 19:20:07 -08:00
|
|
|
Address sdk.ConsAddress `json:"address"`
|
|
|
|
PubKey string `json:"pub_key"`
|
|
|
|
ProposerPriority int64 `json:"proposer_priority"`
|
|
|
|
VotingPower int64 `json:"voting_power"`
|
2018-06-05 21:53:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validators at a certain height output in bech32 format
|
|
|
|
type ResultValidatorsOutput struct {
|
|
|
|
BlockHeight int64 `json:"block_height"`
|
|
|
|
Validators []ValidatorOutput `json:"validators"`
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:20:07 -08:00
|
|
|
func (rvo ResultValidatorsOutput) String() string {
|
|
|
|
var b strings.Builder
|
|
|
|
|
|
|
|
b.WriteString(fmt.Sprintf("block height: %d\n", rvo.BlockHeight))
|
|
|
|
|
|
|
|
for _, val := range rvo.Validators {
|
|
|
|
b.WriteString(
|
|
|
|
fmt.Sprintf(`
|
|
|
|
Address: %s
|
|
|
|
Pubkey: %s
|
|
|
|
ProposerPriority: %d
|
|
|
|
VotingPower: %d
|
|
|
|
`,
|
|
|
|
val.Address, val.PubKey, val.ProposerPriority, val.VotingPower,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2018-06-05 21:53:04 -07:00
|
|
|
func bech32ValidatorOutput(validator *tmtypes.Validator) (ValidatorOutput, error) {
|
2020-01-09 06:04:28 -08:00
|
|
|
bechValPubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, validator.PubKey)
|
2018-06-05 21:53:04 -07:00
|
|
|
if err != nil {
|
|
|
|
return ValidatorOutput{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValidatorOutput{
|
2019-02-05 19:20:07 -08:00
|
|
|
Address: sdk.ConsAddress(validator.Address),
|
2018-11-28 14:29:55 -08:00
|
|
|
PubKey: bechValPubkey,
|
|
|
|
ProposerPriority: validator.ProposerPriority,
|
|
|
|
VotingPower: validator.VotingPower,
|
2018-06-05 21:53:04 -07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
// GetValidators from client
|
2020-06-01 05:46:03 -07:00
|
|
|
func GetValidators(clientCtx client.Context, height *int64, page, limit int) (ResultValidatorsOutput, error) {
|
2018-02-28 23:16:54 -08:00
|
|
|
// get the node
|
2020-06-01 05:46:03 -07:00
|
|
|
node, err := clientCtx.GetNode()
|
2018-02-28 23:16:54 -08:00
|
|
|
if err != nil {
|
2019-02-05 19:20:07 -08:00
|
|
|
return ResultValidatorsOutput{}, err
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
|
|
|
|
2020-01-16 13:46:51 -08:00
|
|
|
validatorsRes, err := node.Validators(height, page, limit)
|
2018-02-28 23:16:54 -08:00
|
|
|
if err != nil {
|
2019-02-05 19:20:07 -08:00
|
|
|
return ResultValidatorsOutput{}, err
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
|
|
|
|
2018-06-05 21:53:04 -07:00
|
|
|
outputValidatorsRes := ResultValidatorsOutput{
|
|
|
|
BlockHeight: validatorsRes.BlockHeight,
|
|
|
|
Validators: make([]ValidatorOutput, len(validatorsRes.Validators)),
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-06-05 21:53:04 -07:00
|
|
|
for i := 0; i < len(validatorsRes.Validators); i++ {
|
|
|
|
outputValidatorsRes.Validators[i], err = bech32ValidatorOutput(validatorsRes.Validators[i])
|
|
|
|
if err != nil {
|
2019-02-05 19:20:07 -08:00
|
|
|
return ResultValidatorsOutput{}, err
|
2018-02-23 10:23:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:20:07 -08:00
|
|
|
return outputValidatorsRes, nil
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// REST
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// Validator Set at a height REST handler
|
2020-06-01 05:46:03 -07:00
|
|
|
func ValidatorSetRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
2018-04-25 07:49:31 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-01-17 09:33:59 -08:00
|
|
|
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 100)
|
2020-01-16 13:46:51 -08:00
|
|
|
if err != nil {
|
2020-01-17 09:33:59 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse pagination parameters")
|
2020-01-16 13:46:51 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-17 09:33:59 -08:00
|
|
|
vars := mux.Vars(r)
|
2018-04-25 07:49:31 -07:00
|
|
|
height, err := strconv.ParseInt(vars["height"], 10, 64)
|
|
|
|
if err != nil {
|
2020-01-17 09:33:59 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse block height")
|
2018-04-25 07:49:31 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
chainHeight, err := GetChainHeight(clientCtx)
|
2019-06-26 13:30:36 -07:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, "failed to parse chain height")
|
|
|
|
return
|
|
|
|
}
|
2018-04-25 07:49:31 -07:00
|
|
|
if height > chainHeight {
|
2019-06-26 13:30:36 -07:00
|
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, "requested block height is bigger then the chain length")
|
2018-04-25 07:49:31 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
output, err := GetValidators(clientCtx, &height, page, limit)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2018-04-25 07:49:31 -07:00
|
|
|
return
|
|
|
|
}
|
2020-06-01 05:46:03 -07:00
|
|
|
rest.PostProcessResponse(w, clientCtx, output)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
|
|
|
}
|
2018-02-23 10:23:24 -08:00
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// Latest Validator Set REST handler
|
2020-06-01 05:46:03 -07:00
|
|
|
func LatestValidatorSetRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
2018-04-25 07:49:31 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-01-17 09:33:59 -08:00
|
|
|
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 100)
|
2020-01-16 13:46:51 -08:00
|
|
|
if err != nil {
|
2020-01-17 09:33:59 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse pagination parameters")
|
2020-01-16 13:46:51 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
output, err := GetValidators(clientCtx, nil, page, limit)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2018-04-25 07:49:31 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
rest.PostProcessResponse(w, clientCtx, output)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
2018-02-23 10:23:24 -08:00
|
|
|
}
|