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"
|
|
|
|
|
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-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-06-04 18:35:07 -07:00
|
|
|
Use: "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
|
|
|
}
|
|
|
|
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to")
|
|
|
|
// TODO: change this to false when we can
|
|
|
|
cmd.Flags().Bool(client.FlagTrustNode, true, "Don't verify proofs for responses")
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2018-04-25 07:49:31 -07:00
|
|
|
func getValidators(ctx context.CoreContext, height *int64) ([]byte, error) {
|
2018-02-28 23:16:54 -08:00
|
|
|
// get the node
|
2018-04-25 07:49:31 -07:00
|
|
|
node, err := ctx.GetNode()
|
2018-02-28 23:16:54 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := node.Validators(height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-07 10:56:49 -07:00
|
|
|
output, err := cdc.MarshalJSON(res)
|
2018-02-28 23:16:54 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-04-25 07:49:31 -07:00
|
|
|
output, err := getValidators(context.NewCoreContextFromViper(), 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-04-25 13:32:22 -07:00
|
|
|
func ValidatorSetRequestHandlerFn(ctx context.CoreContext) http.HandlerFunc {
|
2018-04-25 07:49:31 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
height, err := strconv.ParseInt(vars["height"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/validatorsets/{height}'."))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
chainHeight, err := GetChainHeight(ctx)
|
|
|
|
if height > chainHeight {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
w.Write([]byte("ERROR: Requested block height is bigger then the chain length."))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
output, err := getValidators(ctx, &height)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(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
|
2018-04-25 13:32:22 -07:00
|
|
|
func LatestValidatorSetRequestHandlerFn(ctx context.CoreContext) http.HandlerFunc {
|
2018-04-25 07:49:31 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
height, err := GetChainHeight(ctx)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
output, err := getValidators(ctx, &height)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(output)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
2018-02-23 10:23:24 -08:00
|
|
|
}
|