-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathcomponents_test.go
More file actions
266 lines (227 loc) · 8.2 KB
/
Copy pathcomponents_test.go
File metadata and controls
266 lines (227 loc) · 8.2 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
package block
import (
"context"
crand "crypto/rand"
"errors"
"testing"
"testing/synctest"
"time"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/sync"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
coresequencer "github.com/evstack/ev-node/core/sequencer"
"github.com/evstack/ev-node/pkg/config"
datypes "github.com/evstack/ev-node/pkg/da/types"
"github.com/evstack/ev-node/pkg/genesis"
"github.com/evstack/ev-node/pkg/signer/noop"
"github.com/evstack/ev-node/pkg/store"
testmocks "github.com/evstack/ev-node/test/mocks"
)
// Test the error channel mechanism works as intended
func TestBlockComponents_ExecutionClientFailure_StopsNode(t *testing.T) {
// Create a mock component that simulates execution client failure
errorCh := make(chan error, 1)
criticalError := errors.New("execution client connection lost")
// Create BlockComponents with error channel
bc := &Components{
errorCh: errorCh,
}
// Simulate an execution client failure by sending error to channel
go func() {
time.Sleep(50 * time.Millisecond) // Small delay to ensure Start() is running
errorCh <- criticalError
}()
// Start should block until error is received, then return the error
ctx := context.Background()
err := bc.Start(ctx)
// Verify the error is properly wrapped and returned
require.Error(t, err)
assert.Contains(t, err.Error(), "critical execution client failure")
assert.Contains(t, err.Error(), "execution client connection lost")
}
// Simple lifecycle test without creating full components
func TestBlockComponents_StartStop_Lifecycle(t *testing.T) {
// Test that Start and Stop work without hanging
bc := &Components{
errorCh: make(chan error, 1),
}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
// Start should complete when context is cancelled
err := bc.Start(ctx)
assert.Contains(t, err.Error(), "context")
}
func TestNewSyncComponents_Creation(t *testing.T) {
ds := sync.MutexWrap(datastore.NewMapDatastore())
memStore := store.New(ds)
cfg := config.DefaultConfig()
gen := genesis.Genesis{
ChainID: "test-chain",
InitialHeight: 1,
StartTime: time.Now(),
ProposerAddress: []byte("test-proposer"),
}
mockExec := testmocks.NewMockExecutor(t)
daClient := testmocks.NewMockClient(t)
daClient.On("GetHeaderNamespace").Return(datypes.NamespaceFromString("ns").Bytes()).Maybe()
daClient.On("GetDataNamespace").Return(datypes.NamespaceFromString("data-ns").Bytes()).Maybe()
daClient.On("GetForcedInclusionNamespace").Return([]byte(nil)).Maybe()
daClient.On("HasForcedInclusionNamespace").Return(false).Maybe()
components, err := NewSyncComponents(
cfg,
gen,
memStore,
mockExec,
daClient,
zerolog.Nop(),
NopMetrics(),
DefaultBlockOptions(),
nil,
)
require.NoError(t, err)
assert.NotNil(t, components)
assert.NotNil(t, components.Syncer)
assert.NotNil(t, components.Submitter)
assert.NotNil(t, components.Cache)
assert.NotNil(t, components.Pruner)
assert.NotNil(t, components.errorCh)
assert.Nil(t, components.Executor) // Sync nodes don't have executors
}
func TestNewAggregatorComponents_Creation(t *testing.T) {
ds := sync.MutexWrap(datastore.NewMapDatastore())
memStore := store.New(ds)
cfg := config.DefaultConfig()
// Create a test signer first
priv, _, err := crypto.GenerateEd25519Key(crand.Reader)
require.NoError(t, err)
mockSigner, err := noop.NewNoopSigner(priv)
require.NoError(t, err)
// Get the signer's address to use as proposer
signerAddr, err := mockSigner.GetAddress()
require.NoError(t, err)
gen := genesis.Genesis{
ChainID: "test-chain",
InitialHeight: 1,
StartTime: time.Now(),
ProposerAddress: signerAddr,
}
mockExec := testmocks.NewMockExecutor(t)
mockSeq := testmocks.NewMockSequencer(t)
daClient := testmocks.NewMockClient(t)
daClient.On("GetHeaderNamespace").Return(datypes.NamespaceFromString("ns").Bytes()).Maybe()
daClient.On("GetDataNamespace").Return(datypes.NamespaceFromString("data-ns").Bytes()).Maybe()
daClient.On("GetForcedInclusionNamespace").Return([]byte(nil)).Maybe()
daClient.On("HasForcedInclusionNamespace").Return(false).Maybe()
components, err := newAggregatorComponents(
cfg,
gen,
memStore,
mockExec,
mockSeq,
daClient,
mockSigner,
zerolog.Nop(),
NopMetrics(),
DefaultBlockOptions(),
nil,
)
require.NoError(t, err)
assert.NotNil(t, components)
assert.NotNil(t, components.Executor)
assert.NotNil(t, components.Submitter)
assert.NotNil(t, components.Cache)
assert.NotNil(t, components.Pruner)
assert.NotNil(t, components.errorCh)
assert.Nil(t, components.Syncer) // Aggregator nodes currently don't create syncers in this constructor
}
// This test verifies that when the executor's execution client calls fail,
// the error is properly propagated through the error channel and stops the node
func TestExecutor_RealExecutionClientFailure_StopsNode(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
ds := sync.MutexWrap(datastore.NewMapDatastore())
memStore := store.New(ds)
cfg := config.DefaultConfig()
cfg.Node.BlockTime.Duration = 50 * time.Millisecond // Fast for testing
// Create test signer
priv, _, err := crypto.GenerateEd25519Key(crand.Reader)
require.NoError(t, err)
testSigner, err := noop.NewNoopSigner(priv)
require.NoError(t, err)
addr, err := testSigner.GetAddress()
require.NoError(t, err)
gen := genesis.Genesis{
ChainID: "test-chain",
InitialHeight: 1,
StartTime: time.Now().Add(-time.Second), // Start in past to trigger immediate execution
ProposerAddress: addr,
}
// Create mock executor that will fail on ExecuteTxs
mockExec := testmocks.NewMockExecutor(t)
mockSeq := testmocks.NewMockSequencer(t)
daClient := testmocks.NewMockClient(t)
daClient.On("GetHeaderNamespace").Return(datypes.NamespaceFromString("ns").Bytes()).Maybe()
daClient.On("GetDataNamespace").Return(datypes.NamespaceFromString("data-ns").Bytes()).Maybe()
daClient.On("GetForcedInclusionNamespace").Return([]byte(nil)).Maybe()
daClient.On("HasForcedInclusionNamespace").Return(false).Maybe()
// Mock InitChain to succeed initially
mockExec.On("InitChain", mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return([]byte("state-root"), nil).Once()
// Mock SetDAHeight to be called during initialization
mockSeq.On("SetDAHeight", uint64(0)).Return().Once()
// Mock GetNextBatch to return empty batch
mockSeq.On("GetNextBatch", mock.Anything, mock.Anything).
Return(&coresequencer.GetNextBatchResponse{
Batch: &coresequencer.Batch{Transactions: nil},
Timestamp: time.Now(),
}, nil).Maybe()
// Mock GetTxs for reaper (return empty to avoid interfering with test)
mockExec.On("GetTxs", mock.Anything).
Return([][]byte{}, nil).Maybe()
// Mock ExecuteTxs to fail with a critical error
criticalError := errors.New("execution client RPC connection failed")
mockExec.On("ExecuteTxs", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(nil, criticalError).Maybe()
// Create aggregator node
components, err := newAggregatorComponents(
cfg,
gen,
memStore,
mockExec,
mockSeq,
daClient,
testSigner,
zerolog.Nop(),
NopMetrics(),
DefaultBlockOptions(),
nil,
)
require.NoError(t, err)
// Start should return with error when execution client fails.
// With synctest the fake clock advances the retry delays instantly.
ctx, cancel := context.WithTimeout(t.Context(), 35*time.Second)
defer cancel()
// Run Start in a goroutine to handle the blocking call
startErrCh := make(chan error, 1)
go func() {
startErrCh <- components.Start(ctx)
}()
// Wait for either the error or timeout
synctest.Wait()
select {
case err = <-startErrCh:
// We expect an error containing the critical execution client failure
require.Error(t, err)
assert.Contains(t, err.Error(), "critical execution client failure")
assert.Contains(t, err.Error(), "execution client RPC connection failed")
case <-ctx.Done():
t.Fatal("timeout waiting for critical error to propagate")
}
// Clean up
stopErr := components.Stop()
assert.NoError(t, stopErr)
})
}