This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
validators.go
64 lines (53 loc) · 1.71 KB
/
validators.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package rpc
import (
"net/http"
"github.com/ethereum/go-ethereum/crypto"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"
)
type ValidatorMinimal struct {
Address string `json:"address"`
VotingPower int64 `json:"voting_power"`
}
type ValidatorsMinimal struct {
BlockHeight int64 `json:"block_height"`
Validators []ValidatorMinimal `json:"validators"`
}
func GetEVMValidators(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
node, err := cliCtx.GetNode()
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
validators, err := node.Validators(nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
validatorsMinimal := ValidatorsMinimal{}
validatorsMinimal.BlockHeight = validators.BlockHeight
validatorsMinimal.Validators = []ValidatorMinimal{}
for _, validator := range validators.Validators {
pubKeyBytes, ok := validator.PubKey.(secp256k1.PubKeySecp256k1)
if !ok {
rest.WriteErrorResponse(w, http.StatusInternalServerError, "fail to cast pubkey")
return
}
if pubkey, err := crypto.DecompressPubkey(pubKeyBytes[:]); err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
} else {
validatorsMinimal.Validators = append(
validatorsMinimal.Validators,
ValidatorMinimal{
Address: crypto.PubkeyToAddress(*pubkey).String(),
VotingPower: validator.VotingPower,
},
)
}
}
rest.PostProcessResponseBare(w, cliCtx, validatorsMinimal)
}
}