github.com/MetalBlockchain/metalgo@v1.11.9/snow/validators/gvalidators/validator_state_server.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package gvalidators 5 6 import ( 7 "context" 8 9 "google.golang.org/protobuf/types/known/emptypb" 10 11 "github.com/MetalBlockchain/metalgo/ids" 12 "github.com/MetalBlockchain/metalgo/snow/validators" 13 "github.com/MetalBlockchain/metalgo/utils/crypto/bls" 14 15 pb "github.com/MetalBlockchain/metalgo/proto/pb/validatorstate" 16 ) 17 18 var _ pb.ValidatorStateServer = (*Server)(nil) 19 20 type Server struct { 21 pb.UnsafeValidatorStateServer 22 state validators.State 23 } 24 25 func NewServer(state validators.State) *Server { 26 return &Server{state: state} 27 } 28 29 func (s *Server) GetMinimumHeight(ctx context.Context, _ *emptypb.Empty) (*pb.GetMinimumHeightResponse, error) { 30 height, err := s.state.GetMinimumHeight(ctx) 31 return &pb.GetMinimumHeightResponse{Height: height}, err 32 } 33 34 func (s *Server) GetCurrentHeight(ctx context.Context, _ *emptypb.Empty) (*pb.GetCurrentHeightResponse, error) { 35 height, err := s.state.GetCurrentHeight(ctx) 36 return &pb.GetCurrentHeightResponse{Height: height}, err 37 } 38 39 func (s *Server) GetSubnetID(ctx context.Context, req *pb.GetSubnetIDRequest) (*pb.GetSubnetIDResponse, error) { 40 chainID, err := ids.ToID(req.ChainId) 41 if err != nil { 42 return nil, err 43 } 44 45 subnetID, err := s.state.GetSubnetID(ctx, chainID) 46 return &pb.GetSubnetIDResponse{ 47 SubnetId: subnetID[:], 48 }, err 49 } 50 51 func (s *Server) GetValidatorSet(ctx context.Context, req *pb.GetValidatorSetRequest) (*pb.GetValidatorSetResponse, error) { 52 subnetID, err := ids.ToID(req.SubnetId) 53 if err != nil { 54 return nil, err 55 } 56 57 vdrs, err := s.state.GetValidatorSet(ctx, req.Height, subnetID) 58 if err != nil { 59 return nil, err 60 } 61 62 resp := &pb.GetValidatorSetResponse{ 63 Validators: make([]*pb.Validator, len(vdrs)), 64 } 65 66 i := 0 67 for _, vdr := range vdrs { 68 vdrPB := &pb.Validator{ 69 NodeId: vdr.NodeID.Bytes(), 70 Weight: vdr.Weight, 71 } 72 if vdr.PublicKey != nil { 73 // This is a performance optimization to avoid the cost of compression 74 // from PublicKeyToCompressedBytes. 75 vdrPB.PublicKey = bls.PublicKeyToUncompressedBytes(vdr.PublicKey) 76 } 77 resp.Validators[i] = vdrPB 78 i++ 79 } 80 return resp, nil 81 }