2018-02-23 10:23:24 -08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2018-09-26 07:00:49 -07:00
|
|
|
"bytes"
|
2018-02-23 10:23:24 -08:00
|
|
|
"fmt"
|
2018-02-28 23:16:54 -08:00
|
|
|
"net/http"
|
2018-02-23 10:23:24 -08:00
|
|
|
"strconv"
|
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
"github.com/gorilla/mux"
|
2018-02-23 10:23:24 -08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2018-10-05 05:11:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/utils"
|
2018-06-05 21:53:04 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-09-25 14:36:42 -07:00
|
|
|
"github.com/spf13/viper"
|
2018-10-03 08:48:23 -07:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
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
|
2018-05-28 15:00:37 -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),
|
2018-02-28 23:16:54 -08:00
|
|
|
RunE: printValidators,
|
2018-02-23 10:23:24 -08:00
|
|
|
}
|
2018-06-13 15:13:51 -07:00
|
|
|
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
|
2018-09-25 14:36:42 -07:00
|
|
|
viper.BindPFlag(client.FlagNode, cmd.Flags().Lookup(client.FlagNode))
|
2018-09-14 11:41:21 -07:00
|
|
|
cmd.Flags().Bool(client.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
|
2018-09-25 14:36:42 -07:00
|
|
|
viper.BindPFlag(client.FlagTrustNode, cmd.Flags().Lookup(client.FlagTrustNode))
|
2018-09-14 11:41:21 -07:00
|
|
|
cmd.Flags().String(client.FlagChainID, "", "Chain ID of Tendermint node")
|
2018-09-25 14:36:42 -07:00
|
|
|
viper.BindPFlag(client.FlagChainID, cmd.Flags().Lookup(client.FlagChainID))
|
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 {
|
2018-11-28 14:29:55 -08:00
|
|
|
Address sdk.ValAddress `json:"address"` // in bech32
|
|
|
|
PubKey string `json:"pub_key"` // in bech32
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func bech32ValidatorOutput(validator *tmtypes.Validator) (ValidatorOutput, error) {
|
2018-08-30 21:06:44 -07:00
|
|
|
bechValPubkey, err := sdk.Bech32ifyConsPub(validator.PubKey)
|
2018-06-05 21:53:04 -07:00
|
|
|
if err != nil {
|
|
|
|
return ValidatorOutput{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValidatorOutput{
|
2018-11-28 14:29:55 -08:00
|
|
|
Address: sdk.ValAddress(validator.Address),
|
|
|
|
PubKey: bechValPubkey,
|
|
|
|
ProposerPriority: validator.ProposerPriority,
|
|
|
|
VotingPower: validator.VotingPower,
|
2018-06-05 21:53:04 -07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
func getValidators(cliCtx context.CLIContext, height *int64) ([]byte, error) {
|
2018-02-28 23:16:54 -08:00
|
|
|
// get the node
|
2018-08-06 11:11:30 -07:00
|
|
|
node, err := cliCtx.GetNode()
|
2018-02-28 23:16:54 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-06-05 21:53:04 -07:00
|
|
|
validatorsRes, err := node.Validators(height)
|
2018-02-28 23:16:54 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-14 11:41:21 -07:00
|
|
|
if !cliCtx.TrustNode {
|
2018-10-03 08:48:23 -07:00
|
|
|
check, err := cliCtx.Verify(validatorsRes.BlockHeight)
|
2018-09-14 11:41:21 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-03 08:48:23 -07:00
|
|
|
if !bytes.Equal(check.ValidatorsHash, tmtypes.NewValidatorSet(validatorsRes.Validators).Hash()) {
|
2018-09-14 11:41:21 -07:00
|
|
|
return nil, fmt.Errorf("got invalid validatorset")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 04:00:24 -07:00
|
|
|
if cliCtx.Indent {
|
|
|
|
return cdc.MarshalJSONIndent(outputValidatorsRes, "", " ")
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
2018-10-04 04:00:24 -07:00
|
|
|
return cdc.MarshalJSON(outputValidatorsRes)
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CMD
|
|
|
|
|
|
|
|
func printValidators(cmd *cobra.Command, args []string) error {
|
2018-02-23 10:23:24 -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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
output, err := getValidators(context.NewCLIContext(), height)
|
2018-02-28 15:26:39 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-23 10:23:24 -08:00
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
fmt.Println(string(output))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// REST
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// Validator Set at a height REST handler
|
2018-08-06 11:11:30 -07:00
|
|
|
func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
2018-04-25 07:49:31 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-04-25 07:49:31 -07:00
|
|
|
height, err := strconv.ParseInt(vars["height"], 10, 64)
|
|
|
|
if err != nil {
|
2018-10-24 06:19:48 -07:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2018-04-25 07:49:31 -07:00
|
|
|
w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/validatorsets/{height}'."))
|
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
|
|
|
chainHeight, err := GetChainHeight(cliCtx)
|
2018-04-25 07:49:31 -07:00
|
|
|
if height > chainHeight {
|
2018-10-24 06:19:48 -07:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2018-04-25 07:49:31 -07:00
|
|
|
w.Write([]byte("ERROR: Requested block height is bigger then the chain length."))
|
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
|
|
|
output, err := getValidators(cliCtx, &height)
|
2018-04-25 07:49:31 -07:00
|
|
|
if err != nil {
|
2018-10-24 06:19:48 -07:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2018-04-25 07:49:31 -07:00
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2018-10-04 04:00:24 -07:00
|
|
|
utils.PostProcessResponse(w, cdc, output, cliCtx.Indent)
|
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
|
2018-08-06 11:11:30 -07:00
|
|
|
func LatestValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
2018-04-25 07:49:31 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2018-08-06 11:11:30 -07:00
|
|
|
height, err := GetChainHeight(cliCtx)
|
2018-04-25 07:49:31 -07:00
|
|
|
if err != nil {
|
2018-10-24 06:19:48 -07:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2018-04-25 07:49:31 -07:00
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
|
|
|
output, err := getValidators(cliCtx, &height)
|
2018-04-25 07:49:31 -07:00
|
|
|
if err != nil {
|
2018-10-24 06:19:48 -07:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2018-04-25 07:49:31 -07:00
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2018-10-04 04:00:24 -07:00
|
|
|
utils.PostProcessResponse(w, cdc, output, cliCtx.Indent)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
2018-02-23 10:23:24 -08:00
|
|
|
}
|