Skip to content

Commit e6d6833

Browse files
feat(privval): expose GetPubKey over gRPC (#2881)
## Description While working on the Fibre side implementation I realised that I needed to expose the `GetPubKey` method as well <!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/celestiaorg/celestia-core/pull/2881" target="_blank"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-open-in-devin-review-dark.svg?v=1"> <img src="https://static.devin.ai/assets/gh-open-in-devin-review-light.svg?v=1" alt="Open with Devin"> </picture> </a> <!-- devin-review-badge-end -->
1 parent 88d3c08 commit e6d6833

File tree

4 files changed

+151
-82
lines changed

4 files changed

+151
-82
lines changed

privval/grpc_server.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package privval
33
import (
44
"context"
55

6+
cryptoenc "github.com/cometbft/cometbft/crypto/encoding"
67
"github.com/cometbft/cometbft/libs/log"
8+
"github.com/cometbft/cometbft/proto/tendermint/crypto"
79
privvalproto "github.com/cometbft/cometbft/proto/tendermint/privval"
810
"github.com/cometbft/cometbft/types"
911
)
@@ -47,3 +49,36 @@ func (s *PrivValidatorGRPCServer) SignRawBytes(
4749
Signature: sig,
4850
}, nil
4951
}
52+
53+
func (s *PrivValidatorGRPCServer) GetPubKey(
54+
_ context.Context,
55+
req *privvalproto.PubKeyRequest,
56+
) (*privvalproto.PubKeyResponse, error) {
57+
pubKey, err := s.privVal.GetPubKey()
58+
if err != nil {
59+
s.logger.Error("GetPubKey failed", "err", err)
60+
return &privvalproto.PubKeyResponse{
61+
PubKey: crypto.PublicKey{},
62+
Error: &privvalproto.RemoteSignerError{
63+
Code: 0,
64+
Description: err.Error(),
65+
},
66+
}, nil
67+
}
68+
69+
pk, err := cryptoenc.PubKeyToProto(pubKey)
70+
if err != nil {
71+
s.logger.Error("GetPubKey failed", "err", err)
72+
return &privvalproto.PubKeyResponse{
73+
PubKey: crypto.PublicKey{},
74+
Error: &privvalproto.RemoteSignerError{
75+
Code: 0,
76+
Description: err.Error(),
77+
},
78+
}, nil
79+
}
80+
81+
return &privvalproto.PubKeyResponse{
82+
PubKey: pk,
83+
}, nil
84+
}

privval/grpc_server_test.go

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ import (
1818

1919
const testChainID = "test-chain"
2020

21-
func setupGRPCServer(t *testing.T) (privvalproto.PrivValidatorAPIClient, types.PrivValidator) {
21+
func setupGRPCServerWithPV(t *testing.T, pv types.PrivValidator) privvalproto.PrivValidatorAPIClient {
2222
t.Helper()
2323

24-
pv := types.NewMockPV()
25-
2624
lis, err := net.Listen("tcp", "127.0.0.1:0")
2725
require.NoError(t, err)
2826

@@ -41,11 +39,12 @@ func setupGRPCServer(t *testing.T) (privvalproto.PrivValidatorAPIClient, types.P
4139
require.NoError(t, err)
4240
t.Cleanup(func() { _ = conn.Close() })
4341

44-
return privvalproto.NewPrivValidatorAPIClient(conn), pv
42+
return privvalproto.NewPrivValidatorAPIClient(conn)
4543
}
4644

4745
func TestGRPCServerSignRawBytes(t *testing.T) {
48-
client, pv := setupGRPCServer(t)
46+
pv := types.NewMockPV()
47+
client := setupGRPCServerWithPV(t, pv)
4948

5049
rawBytes := []byte("test commitment data")
5150
uniqueID := "fiber-commitment"
@@ -66,31 +65,27 @@ func TestGRPCServerSignRawBytes(t *testing.T) {
6665
}
6766

6867
func TestGRPCServerSignRawBytesError(t *testing.T) {
69-
pv := types.NewErroringMockPV()
70-
71-
lis, err := net.Listen("tcp", "127.0.0.1:0")
72-
require.NoError(t, err)
73-
74-
srv := grpc.NewServer()
75-
privvalproto.RegisterPrivValidatorAPIServer(srv, privval.NewPrivValidatorGRPCServer(
76-
pv,
77-
log.NewNopLogger(),
78-
))
79-
go func() { _ = srv.Serve(lis) }()
80-
t.Cleanup(srv.Stop)
81-
82-
conn, err := grpc.NewClient(
83-
lis.Addr().String(),
84-
grpc.WithTransportCredentials(insecure.NewCredentials()),
85-
)
86-
require.NoError(t, err)
87-
t.Cleanup(func() { _ = conn.Close() })
68+
client := setupGRPCServerWithPV(t, types.NewErroringMockPV())
8869

89-
client := privvalproto.NewPrivValidatorAPIClient(conn)
9070
resp, err := client.SignRawBytes(context.Background(), &privvalproto.SignRawBytesRequest{
9171
RawBytes: []byte("test data"),
9272
UniqueId: "fiber-commitment",
9373
})
9474
require.NoError(t, err)
9575
require.NotNil(t, resp.Error)
9676
}
77+
78+
func TestGRPCServerGetPubKey(t *testing.T) {
79+
pv := types.NewMockPV()
80+
client := setupGRPCServerWithPV(t, pv)
81+
82+
resp, err := client.GetPubKey(context.Background(), &privvalproto.PubKeyRequest{
83+
ChainId: testChainID,
84+
})
85+
require.NoError(t, err)
86+
assert.Nil(t, resp.Error)
87+
88+
expectedPubKey, err := pv.GetPubKey()
89+
require.NoError(t, err)
90+
assert.Equal(t, expectedPubKey.Bytes(), resp.PubKey.GetEd25519())
91+
}

proto/tendermint/privval/types.pb.go

Lines changed: 95 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/tendermint/privval/types.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ message PingResponse {}
7878
// from the node's privval without direct access to the key.
7979
service PrivValidatorAPI {
8080
rpc SignRawBytes(SignRawBytesRequest) returns (SignedRawBytesResponse);
81+
rpc GetPubKey(PubKeyRequest) returns (PubKeyResponse);
8182
}
8283

8384
message Message {

0 commit comments

Comments
 (0)