-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathhelpers_test.go
More file actions
360 lines (320 loc) · 10.6 KB
/
helpers_test.go
File metadata and controls
360 lines (320 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//nolint:unused
package node
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"testing"
"time"
testutils "github.com/celestiaorg/utils/test"
"github.com/evstack/ev-node/block"
coreexecutor "github.com/evstack/ev-node/core/execution"
coresequencer "github.com/evstack/ev-node/core/sequencer"
"github.com/evstack/ev-node/test/testda"
"github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
evconfig "github.com/evstack/ev-node/pkg/config"
"github.com/evstack/ev-node/pkg/p2p"
remote_signer "github.com/evstack/ev-node/pkg/signer/noop"
"github.com/evstack/ev-node/pkg/store"
"github.com/evstack/ev-node/types"
)
const (
// TestDAAddress is the address used by the dummy gRPC service
// NOTE: this should be unique per test package to avoid
// "bind: listen address already in use" because multiple packages
// are tested in parallel
TestDAAddress = "grpc://localhost:7990"
// TestDANamespace is a sample namespace used by the dummy DA client
TestDANamespace = "mock-namespace"
// MockExecutorAddress is a sample address used by the mock executor
MockExecutorAddress = "127.0.0.1:40041"
)
// sharedDummyDA is a shared DummyDA instance for multi-node tests.
var (
sharedDummyDA *testda.DummyDA
sharedDummyDAOnce sync.Once
)
func getSharedDummyDA(maxBlobSize uint64) *testda.DummyDA {
sharedDummyDAOnce.Do(func() {
sharedDummyDA = testda.New(testda.WithMaxBlobSize(maxBlobSize))
})
return sharedDummyDA
}
func resetSharedDummyDA() {
if sharedDummyDA != nil {
sharedDummyDA.Reset()
}
}
func newDummyDAClient(maxBlobSize uint64) *testda.DummyDA {
if maxBlobSize == 0 {
maxBlobSize = testda.DefaultMaxBlobSize
}
return getSharedDummyDA(maxBlobSize)
}
func createTestComponents(t *testing.T, config evconfig.Config) (coreexecutor.Executor, coresequencer.Sequencer, block.DAClient, crypto.PrivKey, datastore.Batching, func()) {
executor := coreexecutor.NewDummyExecutor()
sequencer := coresequencer.NewDummySequencer()
daClient := newDummyDAClient(0)
// Create genesis and keys for P2P client
_, genesisValidatorKey, _ := types.GetGenesisWithPrivkey("test-chain")
ds, err := store.NewTestInMemoryKVStore()
require.NoError(t, err)
stop := daClient.StartHeightTicker(config.DA.BlockTime.Duration)
return executor, sequencer, daClient, genesisValidatorKey, ds, stop
}
func getTestConfig(t *testing.T, n int) evconfig.Config {
// Use a higher base port to reduce chances of conflicts with system services
startPort := 40000 // Spread port ranges further apart
return evconfig.Config{
RootDir: t.TempDir(),
Node: evconfig.NodeConfig{
Aggregator: true,
BlockTime: evconfig.DurationWrapper{Duration: 100 * time.Millisecond},
MaxPendingHeadersAndData: 1000,
LazyBlockInterval: evconfig.DurationWrapper{Duration: 5 * time.Second},
ScrapeInterval: evconfig.DurationWrapper{Duration: time.Second},
},
DA: evconfig.DAConfig{
BlockTime: evconfig.DurationWrapper{Duration: 200 * time.Millisecond},
Address: TestDAAddress,
Namespace: TestDANamespace,
MaxSubmitAttempts: 30,
},
P2P: evconfig.P2PConfig{
ListenAddress: fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", startPort+n),
},
RPC: evconfig.RPCConfig{
Address: fmt.Sprintf("127.0.0.1:%d", 8000+n),
},
Instrumentation: &evconfig.InstrumentationConfig{},
}
}
// newTestNode is a private helper that creates a node and returns it with a unified cleanup function.
func newTestNode(
t *testing.T,
config evconfig.Config,
executor coreexecutor.Executor,
sequencer coresequencer.Sequencer,
daClient block.DAClient,
privKey crypto.PrivKey,
ds datastore.Batching,
stopDAHeightTicker func(),
) (*FullNode, func()) {
// Generate genesis and keys
genesis, genesisValidatorKey, _ := types.GetGenesisWithPrivkey("test-chain")
remoteSigner, err := remote_signer.NewNoopSigner(genesisValidatorKey)
require.NoError(t, err)
logger := zerolog.Nop()
if testing.Verbose() {
logger = zerolog.New(zerolog.NewTestWriter(t))
}
p2pClient, err := newTestP2PClient(config, privKey, ds, genesis.ChainID, logger)
require.NoError(t, err)
node, err := NewNode(
config,
executor,
sequencer,
daClient,
remoteSigner,
p2pClient,
genesis,
ds,
DefaultMetricsProvider(evconfig.DefaultInstrumentationConfig()),
logger,
NodeOptions{},
)
require.NoError(t, err)
cleanup := func() {
if stopDAHeightTicker != nil {
stopDAHeightTicker()
}
}
return node.(*FullNode), cleanup
}
func createNodeWithCleanup(t *testing.T, config evconfig.Config) (*FullNode, func()) {
resetSharedDummyDA()
executor, sequencer, daClient, privKey, ds, stopDAHeightTicker := createTestComponents(t, config)
return newTestNode(t, config, executor, sequencer, daClient, privKey, ds, stopDAHeightTicker)
}
func createNodeWithCustomComponents(
t *testing.T,
config evconfig.Config,
executor coreexecutor.Executor,
sequencer coresequencer.Sequencer,
daClient block.DAClient,
privKey crypto.PrivKey,
ds datastore.Batching,
stopDAHeightTicker func(),
) (*FullNode, func()) {
return newTestNode(t, config, executor, sequencer, daClient, privKey, ds, stopDAHeightTicker)
}
// Creates the given number of nodes the given nodes using the given wait group to synchronize them
func createNodesWithCleanup(t *testing.T, num int, config evconfig.Config) ([]*FullNode, []func()) {
t.Helper()
require := require.New(t)
resetSharedDummyDA()
nodes := make([]*FullNode, num)
cleanups := make([]func(), num)
// Generate genesis and keys
genesis, genesisValidatorKey, _ := types.GetGenesisWithPrivkey("test-chain")
remoteSigner, err := remote_signer.NewNoopSigner(genesisValidatorKey)
require.NoError(err)
aggListenAddress := config.P2P.ListenAddress
aggPeers := config.P2P.Peers
executor, sequencer, daClient, aggPrivKey, ds, stopDAHeightTicker := createTestComponents(t, config)
if d, ok := daClient.(*testda.DummyDA); ok {
d.Reset()
}
aggPeerID, err := peer.IDFromPrivateKey(aggPrivKey)
require.NoError(err)
logger := zerolog.Nop()
if testing.Verbose() {
logger = zerolog.New(zerolog.NewTestWriter(t))
}
aggP2PClient, err := newTestP2PClient(config, aggPrivKey, ds, genesis.ChainID, logger)
require.NoError(err)
aggNode, err := NewNode(
config,
executor,
sequencer,
daClient,
remoteSigner,
aggP2PClient,
genesis,
ds,
DefaultMetricsProvider(evconfig.DefaultInstrumentationConfig()),
logger,
NodeOptions{},
)
require.NoError(err)
// Update cleanup to cancel the context instead of calling Stop
cleanup := func() {
stopDAHeightTicker()
}
nodes[0], cleanups[0] = aggNode.(*FullNode), cleanup
config.Node.Aggregator = false
peersList := []string{}
if aggPeers != "none" {
aggPeerAddress := fmt.Sprintf("%s/p2p/%s", aggListenAddress, aggPeerID.Loggable()["peerID"].(string))
peersList = append(peersList, aggPeerAddress)
}
for i := 1; i < num; i++ {
if aggPeers != "none" {
config.P2P.Peers = strings.Join(peersList, ",")
}
config.P2P.ListenAddress = fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 40001+i)
config.RPC.Address = fmt.Sprintf("127.0.0.1:%d", 8001+i)
executor, sequencer, daClient, nodePrivKey, ds, stopDAHeightTicker := createTestComponents(t, config)
stopDAHeightTicker()
nodeP2PClient, err := newTestP2PClient(config, nodePrivKey, ds, genesis.ChainID, logger)
require.NoError(err)
node, err := NewNode(
config,
executor,
sequencer,
daClient,
nil,
nodeP2PClient,
genesis,
ds,
DefaultMetricsProvider(evconfig.DefaultInstrumentationConfig()),
logger,
NodeOptions{},
)
require.NoError(err)
cleanup := func() {
// No-op: ticker already stopped
}
nodes[i], cleanups[i] = node.(*FullNode), cleanup
nodePeerID, err := peer.IDFromPrivateKey(nodePrivKey)
require.NoError(err)
peersList = append(peersList, fmt.Sprintf("%s/p2p/%s", config.P2P.ListenAddress, nodePeerID.Loggable()["peerID"].(string)))
}
return nodes, cleanups
}
// newTestP2PClient creates a p2p.Client for testing.
func newTestP2PClient(config evconfig.Config, privKey crypto.PrivKey, ds datastore.Batching, chainID string, logger zerolog.Logger) (*p2p.Client, error) {
return p2p.NewClient(config.P2P, privKey, ds, chainID, logger, nil)
}
// Helper to create N contexts and cancel functions
func createNodeContexts(n int) ([]context.Context, []context.CancelFunc) {
ctxs := make([]context.Context, n)
cancels := make([]context.CancelFunc, n)
for i := range n {
ctx, cancel := context.WithCancel(context.Background())
ctxs[i] = ctx
cancels[i] = cancel
}
return ctxs, cancels
}
// Helper to start a single node in a goroutine and add to wait group
func startNodeInBackground(t *testing.T, nodes []*FullNode, ctxs []context.Context, wg *sync.WaitGroup, idx int, errChan chan<- error) {
wg.Add(1)
go func(node *FullNode, ctx context.Context, idx int) {
defer wg.Done()
err := node.Run(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
if errChan != nil {
errChan <- err
} else {
t.Logf("Error running node %d: %v", idx, err)
}
}
}(nodes[idx], ctxs[idx], idx)
}
// Helper to cancel all contexts and wait for goroutines with timeout
func shutdownAndWait[T ~func()](t *testing.T, cancels []T, wg *sync.WaitGroup, timeout time.Duration) {
for _, cancel := range cancels {
cancel()
}
waitCh := make(chan struct{})
go func() {
wg.Wait()
close(waitCh)
}()
select {
case <-waitCh:
// Nodes stopped successfully
case <-time.After(timeout):
t.Log("Warning: Not all nodes stopped gracefully within timeout")
}
}
// Helper to check that all nodes are synced up to a given height (all block hashes match for all heights up to maxHeight)
func assertAllNodesSynced(t *testing.T, nodes []*FullNode, maxHeight uint64) {
t.Helper()
for height := uint64(1); height <= maxHeight; height++ {
var refHash []byte
for i, node := range nodes {
header, _, err := node.Store.GetBlockData(context.Background(), height)
require.NoError(t, err)
if i == 0 {
refHash = header.Hash()
} else {
headerHash := header.Hash()
require.EqualValues(t, refHash, headerHash, "Block hash mismatch at height %d between node 0 and node %d", height, i)
}
}
}
}
func verifyNodesSynced(node1, syncingNode Node, source Source) error {
return testutils.Retry(300, 100*time.Millisecond, func() error {
sequencerHeight, err := getNodeHeight(node1, source)
if err != nil {
return err
}
syncingHeight, err := getNodeHeight(syncingNode, source)
if err != nil {
return err
}
if sequencerHeight >= syncingHeight {
return nil
}
return fmt.Errorf("nodes not synced: sequencer at height %v, syncing node at height %v", sequencerHeight, syncingHeight)
})
}