Skip to content

Commit bf4a132

Browse files
Make rpc-signer client handle the connection
1 parent ca2d04c commit bf4a132

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

config/config.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
"time"
1919

2020
"github.com/spf13/viper"
21-
"google.golang.org/grpc"
22-
"google.golang.org/grpc/credentials/insecure"
2321

2422
"github.com/ava-labs/avalanchego/api/server"
2523
"github.com/ava-labs/avalanchego/chains"
@@ -700,19 +698,11 @@ func getStakingSigner(ctx context.Context, v *viper.Viper) (bls.Signer, error) {
700698
case !ephemeralSignerEnabled && !contentKeyIsSet && !keyPathIsSet && rpcSignerURLIsSet:
701699
rpcSignerURL := v.GetString(StakingRPCSignerKey)
702700

703-
// the rpc-signer client should call a proxy server (on the same machine) that forwards
704-
// the request to the actual signer instead of relying on tls-credentials
705-
conn, err := grpc.NewClient(rpcSignerURL, grpc.WithTransportCredentials(insecure.NewCredentials()))
701+
signer, err := rpcsigner.NewClient(ctx, rpcSignerURL)
706702
if err != nil {
707703
return nil, fmt.Errorf("couldn't create rpc signer client: %w", err)
708704
}
709705

710-
signer, err := rpcsigner.NewClient(ctx, conn)
711-
if err != nil {
712-
conn.Close()
713-
return nil, fmt.Errorf("couldn't create rpc signer client: %w", err)
714-
}
715-
716706
return signer, nil
717707

718708
case ephemeralSignerEnabled || contentKeyIsSet || keyPathIsSet || rpcSignerURLIsSet:
@@ -748,7 +738,7 @@ func getStakingConfig(ctx context.Context, v *viper.Viper, networkID uint32) (no
748738
StakingKeyPath: getExpandedArg(v, StakingTLSKeyPathKey),
749739
StakingCertPath: getExpandedArg(v, StakingCertPathKey),
750740
StakingSignerPath: getExpandedArg(v, StakingSignerKeyPathKey),
751-
StakingSignerRpc: getExpandedArg(v, StakingRPCSignerKey),
741+
StakingSignerRPC: getExpandedArg(v, StakingRPCSignerKey),
752742
}
753743
if !config.SybilProtectionEnabled && config.SybilProtectionDisabledWeight == 0 {
754744
return node.StakingConfig{}, errSybilProtectionDisabledStakerWeights

config/config_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ import (
1515
"reflect"
1616
"testing"
1717

18-
"google.golang.org/grpc"
19-
2018
"github.com/spf13/pflag"
2119
"github.com/spf13/viper"
2220
"github.com/stretchr/testify/require"
21+
"google.golang.org/grpc"
2322

2423
"github.com/ava-labs/avalanchego/chains"
2524
"github.com/ava-labs/avalanchego/ids"

config/node/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ type StakingConfig struct {
8282
StakingKeyPath string `json:"stakingKeyPath"`
8383
StakingCertPath string `json:"stakingCertPath"`
8484
StakingSignerPath string `json:"stakingSignerPath"`
85-
StakingSignerRpc string `json:"stakingSignerRpc"`
85+
StakingSignerRPC string `json:"stakingSignerRpc"`
8686
}
8787

8888
type StateSyncConfig struct {

utils/crypto/bls/signer/rpcsigner/client.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ package rpcsigner
55

66
import (
77
"context"
8+
"fmt"
89

910
"google.golang.org/grpc"
11+
"google.golang.org/grpc/backoff"
12+
"google.golang.org/grpc/credentials/insecure"
1013

1114
"github.com/ava-labs/avalanchego/utils/crypto/bls"
1215

@@ -21,17 +24,30 @@ type Client struct {
2124
pk *bls.PublicKey
2225
}
2326

24-
func NewClient(ctx context.Context, conn *grpc.ClientConn) (*Client, error) {
27+
func NewClient(ctx context.Context, rpcSignerURL string) (*Client, error) {
28+
// TODO: figure out the best parameters here given the target block-time
29+
opts := grpc.WithConnectParams(grpc.ConnectParams{
30+
Backoff: backoff.DefaultConfig,
31+
})
32+
33+
// the rpc-signer client should call a proxy server (on the same machine) that forwards
34+
// the request to the actual signer instead of relying on tls-credentials
35+
conn, err := grpc.NewClient(rpcSignerURL, opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
36+
if err != nil {
37+
return nil, fmt.Errorf("couldn't create rpc signer client: %w", err)
38+
}
2539
client := pb.NewSignerClient(conn)
2640

2741
pubkeyResponse, err := client.PublicKey(ctx, &pb.PublicKeyRequest{})
2842
if err != nil {
43+
conn.Close()
2944
return nil, err
3045
}
3146

3247
pkBytes := pubkeyResponse.GetPublicKey()
3348
pk, err := bls.PublicKeyFromCompressedBytes(pkBytes)
3449
if err != nil {
50+
conn.Close()
3551
return nil, err
3652
}
3753

@@ -46,19 +62,25 @@ func (c *Client) PublicKey() *bls.PublicKey {
4662
return c.pk
4763
}
4864

65+
// Sign a message. The [Client] already handles transient connection errors. If this method fails, it will
66+
// render the client in an unusable state and the client should be discarded.
4967
func (c *Client) Sign(message []byte) (*bls.Signature, error) {
5068
resp, err := c.client.Sign(context.TODO(), &pb.SignRequest{Message: message})
5169
if err != nil {
70+
c.conn.Close()
5271
return nil, err
5372
}
5473
signature := resp.GetSignature()
5574

5675
return bls.SignatureFromBytes(signature)
5776
}
5877

78+
// [SignProofOfPossession] has the same behavior as [Sign] but will product a different signature.
79+
// See BLS spec for more details.
5980
func (c *Client) SignProofOfPossession(message []byte) (*bls.Signature, error) {
6081
resp, err := c.client.SignProofOfPossession(context.TODO(), &pb.SignProofOfPossessionRequest{Message: message})
6182
if err != nil {
83+
c.conn.Close()
6284
return nil, err
6385
}
6486
signature := resp.GetSignature()

0 commit comments

Comments
 (0)