Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ linters-settings:
gosec:
excludes:
- G115 # integer overflow conversion int -> uint8 (gosec)
- G507
revive:
ignore-generated-header: true
severity: warning
Expand Down Expand Up @@ -364,4 +365,4 @@ issues:
- goconst
- gosec
- noctx
- wrapcheck
- wrapcheck
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/landslidenetwork/slide-sdk

go 1.22.7
go 1.22.8

require (
cosmossdk.io/log v1.3.1
Expand All @@ -14,7 +14,10 @@ require (
github.com/prometheus/client_golang v1.19.0
github.com/prometheus/client_model v0.6.1
github.com/stretchr/testify v1.9.0
github.com/supranational/blst v0.3.13
go.uber.org/mock v0.4.0
golang.org/x/crypto v0.25.0
golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0
golang.org/x/sync v0.7.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20240709173604-40e1e62336c5
google.golang.org/grpc v1.64.1
Expand Down Expand Up @@ -51,9 +54,9 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
github.com/bits-and-blooms/bitset v1.8.0 // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chzyer/readline v1.5.1 // indirect
Expand Down Expand Up @@ -186,8 +189,6 @@ require (
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.22.0 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d/go.mod h1:6QX/PXZ
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 h1:41iFGWnSlI2gVpmOtVTJZNodLdLQLn/KsJqFvXwnd/s=
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bits-and-blooms/bitset v1.8.0 h1:FD+XqgOZDUxxZ8hzoBFuV9+cGWY9CslN6d5MS5JVb4c=
github.com/bits-and-blooms/bitset v1.8.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/bits-and-blooms/bitset v1.10.0 h1:ePXTeiPEazB5+opbv5fr8umg2R/1NlzgDsyepwsSr88=
github.com/bits-and-blooms/bitset v1.10.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U=
github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
github.com/btcsuite/btcd/btcutil v1.1.3 h1:xfbtw8lwpp0G6NwSHb+UE67ryTFHJAiNuipusjXSohQ=
Expand All @@ -290,8 +290,8 @@ github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
Expand Down Expand Up @@ -1011,6 +1011,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/supranational/blst v0.3.13 h1:AYeSxdOMacwu7FBmpfloBz5pbFXDmJL33RuwnKtmTjk=
github.com/supranational/blst v0.3.13/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2lyGa2E=
Expand Down
144 changes: 144 additions & 0 deletions grpcutils/gvalidators/validator_state_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright (C) 2019-2024, 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/landslidenetwork/slide-sdk/utils/crypto/bls"
"github.com/landslidenetwork/slide-sdk/utils/ids"
"github.com/landslidenetwork/slide-sdk/utils/validators"

pb "github.com/landslidenetwork/slide-sdk/proto/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 {
// PublicKeyFromValidUncompressedBytes is used rather than
// PublicKeyFromCompressedBytes because it is significantly faster
// due to the avoidance of decompression and key re-verification. 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 = bls.PublicKeyFromValidUncompressedBytes(validator.PublicKey)
if publicKey == nil {
return nil, errFailedPublicKeyDeserialize
}
}
vdrs[nodeID] = &validators.GetValidatorOutput{
NodeID: nodeID,
PublicKey: publicKey,
Weight: validator.Weight,
}
}
return vdrs, nil
}

func (c *Client) GetCurrentValidatorSet(
ctx context.Context,
subnetID ids.ID,
) (map[ids.ID]*validators.GetCurrentValidatorOutput, uint64, error) {
resp, err := c.client.GetCurrentValidatorSet(ctx, &pb.GetCurrentValidatorSetRequest{
SubnetId: subnetID[:],
})
if err != nil {
return nil, 0, err
}

vdrs := make(map[ids.ID]*validators.GetCurrentValidatorOutput, len(resp.Validators))
for _, validator := range resp.Validators {
nodeID, err := ids.ToNodeID(validator.NodeId)
if err != nil {
return nil, 0, err
}
var publicKey *bls.PublicKey
if len(validator.PublicKey) > 0 {
// PublicKeyFromValidUncompressedBytes is used rather than
// PublicKeyFromCompressedBytes because it is significantly faster
// due to the avoidance of decompression and key re-verification. 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 = bls.PublicKeyFromValidUncompressedBytes(validator.PublicKey)
if publicKey == nil {
return nil, 0, errFailedPublicKeyDeserialize
}
}
validationID, err := ids.ToID(validator.ValidationId)
if err != nil {
return nil, 0, err
}

vdrs[validationID] = &validators.GetCurrentValidatorOutput{
ValidationID: validationID,
NodeID: nodeID,
PublicKey: publicKey,
Weight: validator.Weight,
StartTime: validator.StartTime,
MinNonce: validator.MinNonce,
IsActive: validator.IsActive,
IsL1Validator: validator.IsL1Validator,
}
}
return vdrs, resp.GetCurrentHeight(), nil
}
Loading
Loading