Skip to content

Commit 5cdb112

Browse files
committed
refactor(core): unify grpc clients (#3999)
1 parent 4885326 commit 5cdb112

22 files changed

+228
-370
lines changed

core/client.go

-67
This file was deleted.

core/exchange_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func TestExchange_StoreHistoricIfArchival(t *testing.T) {
138138

139139
// initialize store with genesis block
140140
genHeight := int64(1)
141-
genBlock, err := fetcher.GetBlock(ctx, &genHeight)
141+
genBlock, err := fetcher.GetBlock(ctx, genHeight)
142142
require.NoError(t, err)
143143
genHeader, err := ce.Get(ctx, genBlock.Header.Hash().Bytes())
144144
require.NoError(t, err)
@@ -170,8 +170,7 @@ func createCoreFetcher(t *testing.T, cfg *testnode.Config) (*BlockFetcher, testn
170170
require.NoError(t, err)
171171
host, port, err := net.SplitHostPort(cctx.GRPCClient.Target())
172172
require.NoError(t, err)
173-
client := NewClient(host, port)
174-
require.NoError(t, client.Start())
173+
client := newTestClient(t, host, port)
175174
fetcher, err := NewBlockFetcher(client)
176175
require.NoError(t, err)
177176
return fetcher, cctx

core/fetcher.go

+4-38
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package core
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"io"
87
"sync/atomic"
@@ -13,14 +12,13 @@ import (
1312
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
1413
coregrpc "github.com/tendermint/tendermint/rpc/grpc"
1514
"github.com/tendermint/tendermint/types"
15+
"google.golang.org/grpc"
1616

1717
libhead "github.com/celestiaorg/go-header"
1818
)
1919

2020
const newBlockSubscriber = "NewBlock/Events"
2121

22-
var ErrClientNotRunning = errors.New("gRPC connection to core node is not running")
23-
2422
type SignedBlock struct {
2523
Header *types.Header `json:"header"`
2624
Commit *types.Commit `json:"commit"`
@@ -34,17 +32,17 @@ var (
3432
)
3533

3634
type BlockFetcher struct {
37-
client *Client
35+
client coregrpc.BlockAPIClient
3836

3937
doneCh chan struct{}
4038
cancel context.CancelFunc
4139
isListeningForBlocks atomic.Bool
4240
}
4341

4442
// NewBlockFetcher returns a new `BlockFetcher`.
45-
func NewBlockFetcher(client *Client) (*BlockFetcher, error) {
43+
func NewBlockFetcher(conn *grpc.ClientConn) (*BlockFetcher, error) {
4644
return &BlockFetcher{
47-
client: client,
45+
client: coregrpc.NewBlockAPIClient(conn),
4846
}, nil
4947
}
5048

@@ -62,10 +60,6 @@ func (f *BlockFetcher) Stop(ctx context.Context) error {
6260

6361
// GetBlockInfo queries Core for additional block information, like Commit and ValidatorSet.
6462
func (f *BlockFetcher) GetBlockInfo(ctx context.Context, height int64) (*types.Commit, *types.ValidatorSet, error) {
65-
// return error if the client is still not started
66-
if !f.client.IsRunning() {
67-
return nil, nil, ErrClientNotRunning
68-
}
6963
commit, err := f.Commit(ctx, height)
7064
if err != nil {
7165
return nil, nil, fmt.Errorf("core/fetcher: getting commit at height %d: %w", height, err)
@@ -87,10 +81,6 @@ func (f *BlockFetcher) GetBlockInfo(ctx context.Context, height int64) (*types.C
8781
// GetBlock queries Core for a `Block` at the given height.
8882
// if the height is nil, use the latest height
8983
func (f *BlockFetcher) GetBlock(ctx context.Context, height int64) (*SignedBlock, error) {
90-
// return error if the client is still not started
91-
if !f.client.IsRunning() {
92-
return nil, ErrClientNotRunning
93-
}
9484
stream, err := f.client.BlockByHeight(ctx, &coregrpc.BlockByHeightRequest{Height: height})
9585
if err != nil {
9686
return nil, err
@@ -103,10 +93,6 @@ func (f *BlockFetcher) GetBlock(ctx context.Context, height int64) (*SignedBlock
10393
}
10494

10595
func (f *BlockFetcher) GetBlockByHash(ctx context.Context, hash libhead.Hash) (*types.Block, error) {
106-
// return error if the client is still not started
107-
if !f.client.IsRunning() {
108-
return nil, ErrClientNotRunning
109-
}
11096
if hash == nil {
11197
return nil, fmt.Errorf("cannot get block with nil hash")
11298
}
@@ -125,10 +111,6 @@ func (f *BlockFetcher) GetBlockByHash(ctx context.Context, hash libhead.Hash) (*
125111
// GetSignedBlock queries Core for a `Block` at the given height.
126112
// if the height is nil, use the latest height.
127113
func (f *BlockFetcher) GetSignedBlock(ctx context.Context, height int64) (*SignedBlock, error) {
128-
// return error if the client is still not started
129-
if !f.client.IsRunning() {
130-
return nil, ErrClientNotRunning
131-
}
132114
stream, err := f.client.BlockByHeight(ctx, &coregrpc.BlockByHeightRequest{Height: height})
133115
if err != nil {
134116
return nil, err
@@ -140,10 +122,6 @@ func (f *BlockFetcher) GetSignedBlock(ctx context.Context, height int64) (*Signe
140122
// the given height.
141123
// If the height is nil, use the latest height.
142124
func (f *BlockFetcher) Commit(ctx context.Context, height int64) (*types.Commit, error) {
143-
// return error if the client is still not started
144-
if !f.client.IsRunning() {
145-
return nil, ErrClientNotRunning
146-
}
147125
res, err := f.client.Commit(ctx, &coregrpc.CommitRequest{Height: height})
148126
if err != nil {
149127
return nil, err
@@ -165,10 +143,6 @@ func (f *BlockFetcher) Commit(ctx context.Context, height int64) (*types.Commit,
165143
// block at the given height.
166144
// If the height is nil, use the latest height.
167145
func (f *BlockFetcher) ValidatorSet(ctx context.Context, height int64) (*types.ValidatorSet, error) {
168-
// return error if the client is still not started
169-
if !f.client.IsRunning() {
170-
return nil, ErrClientNotRunning
171-
}
172146
res, err := f.client.ValidatorSet(ctx, &coregrpc.ValidatorSetRequest{Height: height})
173147
if err != nil {
174148
return nil, err
@@ -189,10 +163,6 @@ func (f *BlockFetcher) ValidatorSet(ctx context.Context, height int64) (*types.V
189163
// SubscribeNewBlockEvent subscribes to new block events from Core, returning
190164
// a new block event channel on success.
191165
func (f *BlockFetcher) SubscribeNewBlockEvent(ctx context.Context) (<-chan types.EventDataSignedBlock, error) {
192-
// return error if the client is still not started
193-
if !f.client.IsRunning() {
194-
return nil, ErrClientNotRunning
195-
}
196166
if f.isListeningForBlocks.Load() {
197167
return nil, fmt.Errorf("already subscribed to new blocks")
198168
}
@@ -252,10 +222,6 @@ func (f *BlockFetcher) SubscribeNewBlockEvent(ctx context.Context) (<-chan types
252222
// syncing, and false for already caught up. It can also return an error
253223
// in the case of a failed status request.
254224
func (f *BlockFetcher) IsSyncing(ctx context.Context) (bool, error) {
255-
// return error if the client is still not started
256-
if !f.client.IsRunning() {
257-
return false, ErrClientNotRunning
258-
}
259225
resp, err := f.client.Status(ctx, &coregrpc.StatusRequest{})
260226
if err != nil {
261227
return false, err

core/fetcher_no_race_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ func TestBlockFetcherHeaderValues(t *testing.T) {
2222
node := StartTestNode(t)
2323
host, port, err := net.SplitHostPort(node.GRPCClient.Target())
2424
require.NoError(t, err)
25-
client := NewClient(host, port)
26-
require.NoError(t, client.Start())
25+
client := newTestClient(t, host, port)
2726
fetcher, err := NewBlockFetcher(client)
2827
require.NoError(t, err)
2928

core/fetcher_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ func TestBlockFetcher_GetBlock_and_SubscribeNewBlockEvent(t *testing.T) {
1616

1717
host, port, err := net.SplitHostPort(StartTestNode(t).GRPCClient.Target())
1818
require.NoError(t, err)
19-
client := NewClient(host, port)
20-
require.NoError(t, client.Start())
19+
client := newTestClient(t, host, port)
2120
fetcher, err := NewBlockFetcher(client)
2221
require.NoError(t, err)
23-
2422
// generate some blocks
2523
newBlockChan, err := fetcher.SubscribeNewBlockEvent(ctx)
2624
require.NoError(t, err)

core/header_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ func TestMakeExtendedHeaderForEmptyBlock(t *testing.T) {
2323

2424
host, port, err := net.SplitHostPort(StartTestNode(t).GRPCClient.Target())
2525
require.NoError(t, err)
26-
client := NewClient(host, port)
27-
require.NoError(t, client.Start())
26+
client := newTestClient(t, host, port)
2827
fetcher, err := NewBlockFetcher(client)
2928
require.NoError(t, err)
30-
3129
sub, err := fetcher.SubscribeNewBlockEvent(ctx)
3230
require.NoError(t, err)
3331
<-sub

core/testing.go

+19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package core
22

33
import (
4+
"context"
5+
"net"
46
"testing"
57
"time"
68

9+
"github.com/stretchr/testify/require"
710
tmrand "github.com/tendermint/tendermint/libs/rand"
11+
"google.golang.org/grpc"
12+
"google.golang.org/grpc/connectivity"
13+
"google.golang.org/grpc/credentials/insecure"
814

915
"github.com/celestiaorg/celestia-app/v3/test/util/genesis"
1016
"github.com/celestiaorg/celestia-app/v3/test/util/testnode"
@@ -60,3 +66,16 @@ func generateRandomAccounts(n int) []string {
6066
}
6167
return accounts
6268
}
69+
70+
func newTestClient(t *testing.T, ip, port string) *grpc.ClientConn {
71+
t.Helper()
72+
opt := grpc.WithTransportCredentials(insecure.NewCredentials())
73+
endpoint := net.JoinHostPort(ip, port)
74+
client, err := grpc.NewClient(endpoint, opt)
75+
require.NoError(t, err)
76+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
77+
t.Cleanup(cancel)
78+
ready := client.WaitForStateChange(ctx, connectivity.Ready)
79+
require.True(t, ready)
80+
return client
81+
}

go.mod

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c
88
github.com/alecthomas/jsonschema v0.0.0-20220216202328-9eeeec9d044b
99
github.com/benbjohnson/clock v1.3.5
10-
github.com/celestiaorg/celestia-app/v3 v3.2.0
10+
github.com/celestiaorg/celestia-app/v3 v3.3.0-rc0
1111
github.com/celestiaorg/go-fraud v0.2.1
1212
github.com/celestiaorg/go-header v0.6.3
1313
github.com/celestiaorg/go-libp2p-messenger v0.2.0
@@ -55,7 +55,7 @@ require (
5555
github.com/rollkit/go-da v0.8.0
5656
github.com/spf13/cobra v1.8.1
5757
github.com/spf13/pflag v1.0.5
58-
github.com/stretchr/testify v1.9.0
58+
github.com/stretchr/testify v1.10.0
5959
github.com/tendermint/tendermint v0.34.29
6060
go.opentelemetry.io/contrib/instrumentation/runtime v0.45.0
6161
go.opentelemetry.io/otel v1.31.0
@@ -69,10 +69,10 @@ require (
6969
go.opentelemetry.io/proto/otlp v1.3.1
7070
go.uber.org/fx v1.23.0
7171
go.uber.org/zap v1.27.0
72-
golang.org/x/crypto v0.29.0
72+
golang.org/x/crypto v0.31.0
7373
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c
74-
golang.org/x/sync v0.9.0
75-
golang.org/x/text v0.20.0
74+
golang.org/x/sync v0.10.0
75+
golang.org/x/text v0.21.0
7676
google.golang.org/grpc v1.68.0
7777
google.golang.org/protobuf v1.35.1
7878
)
@@ -128,7 +128,7 @@ require (
128128
github.com/cosmos/iavl v0.19.6 // indirect
129129
github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v6 v6.1.2 // indirect
130130
github.com/cosmos/ibc-go/v6 v6.3.0 // indirect
131-
github.com/cosmos/ledger-cosmos-go v0.13.2 // indirect
131+
github.com/cosmos/ledger-cosmos-go v0.14.0 // indirect
132132
github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c // indirect
133133
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
134134
github.com/cskr/pubsub v1.0.2 // indirect
@@ -330,10 +330,10 @@ require (
330330
go.uber.org/mock v0.5.0 // indirect
331331
go.uber.org/multierr v1.11.0 // indirect
332332
golang.org/x/mod v0.21.0 // indirect
333-
golang.org/x/net v0.31.0 // indirect
333+
golang.org/x/net v0.33.0 // indirect
334334
golang.org/x/oauth2 v0.23.0 // indirect
335-
golang.org/x/sys v0.27.0 // indirect
336-
golang.org/x/term v0.26.0 // indirect
335+
golang.org/x/sys v0.28.0 // indirect
336+
golang.org/x/term v0.27.0 // indirect
337337
golang.org/x/time v0.5.0 // indirect
338338
golang.org/x/tools v0.26.0 // indirect
339339
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
@@ -352,12 +352,12 @@ require (
352352
)
353353

354354
replace (
355-
github.com/cosmos/cosmos-sdk => github.com/celestiaorg/cosmos-sdk v1.25.1-sdk-v0.46.16
355+
github.com/cosmos/cosmos-sdk => github.com/celestiaorg/cosmos-sdk v1.27.0-sdk-v0.46.16
356356
github.com/filecoin-project/dagstore => github.com/celestiaorg/dagstore v0.0.0-20230824094345-537c012aa403
357357
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
358358
// broken goleveldb needs to be replaced for the cosmos-sdk and celestia-app
359359
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
360-
github.com/tendermint/tendermint => github.com/celestiaorg/celestia-core v1.44.1-tm-v0.34.35
360+
github.com/tendermint/tendermint => github.com/celestiaorg/celestia-core v1.45.0-tm-v0.34.35
361361
)
362362

363363
replace github.com/ipfs/boxo => github.com/celestiaorg/boxo v0.0.0-20241118122411-70a650316c3b

0 commit comments

Comments
 (0)