-
Notifications
You must be signed in to change notification settings - Fork 4
/
service.go
94 lines (78 loc) · 2.69 KB
/
service.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package node
import (
"context"
"fmt"
"github.com/cometbft/cometbft/crypto/secp256k1"
gogogrpc "github.com/cosmos/gogoproto/grpc"
"github.com/ethereum/go-ethereum/crypto"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// RegisterNodeService registers the node gRPC service on the provided gRPC router.
func RegisterNodeService(clientCtx client.Context, server gogogrpc.Server) {
RegisterServiceServer(server, NewQueryServer(clientCtx))
}
// RegisterGRPCGatewayRoutes mounts the node gRPC service's GRPC-gateway routes
// on the given mux object.
func RegisterGRPCGatewayRoutes(clientConn gogogrpc.ClientConn, mux *runtime.ServeMux) {
RegisterServiceHandlerClient(context.Background(), mux, NewServiceClient(clientConn))
}
// to check queryServer implements ServiceServer
var _ ServiceServer = queryServer{}
// queryServer implements ServiceServer
type queryServer struct {
clientCtx client.Context
}
// NewQueryServer returns new queryServer from provided client.Context
func NewQueryServer(clientCtx client.Context) ServiceServer {
return queryServer{
clientCtx: clientCtx,
}
}
// ChainID returns QueryChainIDResponse that has chain id from ctx
func (s queryServer) ChainID(ctx context.Context, _ *QueryChainIDRequest) (*QueryChainIDResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return &QueryChainIDResponse{
ChainID: sdkCtx.ChainID(),
}, nil
}
// EVMValidators returns top 100 validators' address and voting power order by voting power
func (s queryServer) EVMValidators(
ctx context.Context,
_ *QueryEVMValidatorsRequest,
) (*QueryEVMValidatorsResponse, error) {
node, err := s.clientCtx.GetNode()
if err != nil {
return nil, err
}
// Get top 100 validators for now
var page int = 1
var perPage int = 100
validators, err := node.Validators(context.Background(), nil, &page, &perPage)
if err != nil {
return nil, err
}
evmValidatorsResponse := QueryEVMValidatorsResponse{}
evmValidatorsResponse.BlockHeight = validators.BlockHeight
evmValidatorsResponse.Validators = []ValidatorMinimal{}
// put each validator's address and voting power to the response
for _, validator := range validators.Validators {
pubKeyBytes, ok := validator.PubKey.(secp256k1.PubKey)
if !ok {
return nil, fmt.Errorf("can't get validator public key")
}
if pubkey, err := crypto.DecompressPubkey(pubKeyBytes[:]); err != nil {
return nil, err
} else {
evmValidatorsResponse.Validators = append(
evmValidatorsResponse.Validators,
ValidatorMinimal{
Address: crypto.PubkeyToAddress(*pubkey).String(),
VotingPower: validator.VotingPower,
},
)
}
}
return &evmValidatorsResponse, nil
}