Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cost of BLS key serialization for gRPC #1343

Merged
merged 6 commits into from
Apr 19, 2023
Merged
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
16 changes: 12 additions & 4 deletions snow/validators/gvalidators/validator_state_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package gvalidators

import (
"context"
"errors"

"google.golang.org/protobuf/types/known/emptypb"

Expand All @@ -15,7 +16,10 @@ import (
pb "github.com/ava-labs/avalanchego/proto/pb/validatorstate"
)

var _ validators.State = (*Client)(nil)
var (
_ validators.State = (*Client)(nil)
errFailedPublicKeyDeserialize = errors.New("couldn't deserialize public key")
)

type Client struct {
client pb.ValidatorStateClient
Expand Down Expand Up @@ -72,9 +76,13 @@ func (c *Client) GetValidatorSet(
}
var publicKey *bls.PublicKey
if len(validator.PublicKey) > 0 {
publicKey, err = bls.PublicKeyFromBytes(validator.PublicKey)
if err != nil {
return nil, err
// 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{
Expand Down
5 changes: 3 additions & 2 deletions snow/validators/gvalidators/validator_state_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"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"
)
Expand Down Expand Up @@ -70,7 +69,9 @@ func (s *Server) GetValidatorSet(ctx context.Context, req *pb.GetValidatorSetReq
Weight: vdr.Weight,
}
if vdr.PublicKey != nil {
vdrPB.PublicKey = bls.PublicKeyToBytes(vdr.PublicKey)
// This is a performance optimization to avoid the cost of compression
// from PublicKeyToBytes.
vdrPB.PublicKey = vdr.PublicKey.Serialize()
}
resp.Validators[i] = vdrPB
i++
Expand Down
13 changes: 13 additions & 0 deletions snow/validators/gvalidators/validator_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ func TestGetValidatorSet(t *testing.T) {
require.Error(err)
}

func TestPublicKeyDeserialize(t *testing.T) {
require := require.New(t)

sk, err := bls.NewSecretKey()
require.NoError(err)
pk := bls.PublicFromSecretKey(sk)

pkBytes := pk.Serialize()
pkDe := new(bls.PublicKey).Deserialize(pkBytes)
require.NotNil(pkDe)
require.EqualValues(pk, pkDe)
}

// BenchmarkGetValidatorSet measures the time it takes complete a gRPC client
// request based on a mocked validator set.
func BenchmarkGetValidatorSet(b *testing.B) {
Expand Down