-
Notifications
You must be signed in to change notification settings - Fork 660
/
validator_state_client.go
95 lines (82 loc) · 2.49 KB
/
validator_state_client.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
95
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package gvalidators
import (
"context"
"errors"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
pb "github.com/ava-labs/avalanchego/proto/pb/validatorstate"
)
var (
_ validators.State = (*Client)(nil)
errFailedPublicKeyDeserialize = errors.New("couldn't deserialize public key")
)
type Client struct {
client pb.ValidatorStateClient
}
func NewClient(client pb.ValidatorStateClient) *Client {
return &Client{client: client}
}
func (c *Client) GetMinimumHeight(ctx context.Context) (uint64, error) {
resp, err := c.client.GetMinimumHeight(ctx, &emptypb.Empty{})
if err != nil {
return 0, err
}
return resp.Height, nil
}
func (c *Client) GetCurrentHeight(ctx context.Context) (uint64, error) {
resp, err := c.client.GetCurrentHeight(ctx, &emptypb.Empty{})
if err != nil {
return 0, err
}
return resp.Height, nil
}
func (c *Client) GetSubnetID(ctx context.Context, chainID ids.ID) (ids.ID, error) {
resp, err := c.client.GetSubnetID(ctx, &pb.GetSubnetIDRequest{
ChainId: chainID[:],
})
if err != nil {
return ids.Empty, err
}
return ids.ToID(resp.SubnetId)
}
func (c *Client) GetValidatorSet(
ctx context.Context,
height uint64,
subnetID ids.ID,
) (map[ids.NodeID]*validators.GetValidatorOutput, error) {
resp, err := c.client.GetValidatorSet(ctx, &pb.GetValidatorSetRequest{
Height: height,
SubnetId: subnetID[:],
})
if err != nil {
return nil, err
}
vdrs := make(map[ids.NodeID]*validators.GetValidatorOutput, len(resp.Validators))
for _, validator := range resp.Validators {
nodeID, err := ids.ToNodeID(validator.NodeId)
if err != nil {
return nil, err
}
var publicKey *bls.PublicKey
if len(validator.PublicKey) > 0 {
// This is a performance optimization to avoid the cost of compression
// and key re-verification with PublicKeyFromBytes. We can safely
// assume that the BLS Public Keys are verified before being added
// to the P-Chain and served by the gRPC server.
publicKey = new(bls.PublicKey).Deserialize(validator.PublicKey)
if publicKey == nil {
return nil, errFailedPublicKeyDeserialize
}
}
vdrs[nodeID] = &validators.GetValidatorOutput{
NodeID: nodeID,
PublicKey: publicKey,
Weight: validator.Weight,
}
}
return vdrs, nil
}