Repository navigation
Expand file tree
/
Copy pathsupernode.go
More file actions
338 lines (299 loc) · 14.1 KB
/
Copy pathsupernode.go
File metadata and controls
338 lines (299 loc) · 14.1 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
package dsl
import (
"context"
"time"
"github.com/ethereum-optimism/optimism/op-devstack/stack"
"github.com/ethereum-optimism/optimism/op-e2e/e2eutils/wait"
opclient "github.com/ethereum-optimism/optimism/op-service/client"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-supernode/supernode/activity/interop"
)
// Supernode wraps a stack.Supernode interface for DSL operations
type Supernode struct {
SuperRootQuerier
inner stack.Supernode
testControl stack.SupernodeTestControl
managedVNs []*L2CLNode
}
// ManageVN registers a VN-proxy L2CLNode as hosted by this supernode so that
// Start() can re-establish its DSL-managed peer connections after a Stop/Start
// cycle. Initial peering is done by the sysgo runtime; this hook only matters
// across restarts driven through Supernode.Start.
func (s *Supernode) ManageVN(vn *L2CLNode) {
s.managedVNs = append(s.managedVNs, vn)
}
// NewSupernode creates a new Supernode DSL wrapper
func NewSupernode(inner stack.Supernode) *Supernode {
return &Supernode{
SuperRootQuerier: SuperRootQuerier{commonImpl: commonFromT(inner.T()), api: inner.QueryAPI(), userRPC: inner.UserRPC()},
inner: inner,
}
}
// NewSupernodeWithTestControl creates a new Supernode DSL wrapper with test control support.
// The testControl parameter can be nil if no test control is needed.
func NewSupernodeWithTestControl(inner stack.Supernode, testControl stack.SupernodeTestControl) *Supernode {
return &Supernode{
SuperRootQuerier: SuperRootQuerier{commonImpl: commonFromT(inner.T()), api: inner.QueryAPI(), userRPC: inner.UserRPC()},
inner: inner,
testControl: testControl,
}
}
func (s *Supernode) Name() string {
return s.inner.Name()
}
func (s *Supernode) String() string {
return s.inner.Name()
}
// Escape returns the underlying stack.Supernode
func (s *Supernode) Escape() stack.Supernode {
return s.inner
}
func (s *Supernode) ClientRPC() opclient.RPC {
return s.inner.ClientRPC()
}
func (s *Supernode) UserRPC() string {
return s.inner.UserRPC()
}
// AwaitFinalizationAdvanced reads the supernode's current finalized timestamp
// from supernode_syncStatus and waits until it strictly advances past that
// value, then returns the new finalized timestamp. The first call therefore
// guarantees finalization has progressed past genesis (since genesis is the
// initial finalized timestamp).
func (s *Supernode) AwaitFinalizationAdvanced() uint64 {
ctx, cancel := context.WithTimeout(s.ctx, 5*DefaultTimeout)
defer cancel()
initial, err := s.inner.QueryAPI().SyncStatus(ctx)
s.require.NoError(err, "failed to read initial supernode sync status")
start := initial.FinalizedTimestamp
var ts uint64
err = wait.For(ctx, 1*time.Second, func() (bool, error) {
status, err := s.inner.QueryAPI().SyncStatus(ctx)
if err != nil {
return false, nil // Ignore transient errors.
}
if status.FinalizedTimestamp <= start {
return false, nil
}
ts = status.FinalizedTimestamp
return true, nil
})
s.require.NoError(err, "supernode finalized timestamp did not advance past %d in time", start)
return ts
}
// SuperRootAt returns the validated super-root at the given timestamp,
// asserting that all expectedChainIDs are present. The timestamp must already
// be finalized; this method does not wait for finalization.
func (s *Supernode) SuperRootAt(timestamp uint64, expectedChainIDs ...eth.ChainID) eth.SuperRootAtTimestampResponse {
ctx, cancel := context.WithTimeout(s.ctx, DefaultTimeout)
defer cancel()
resp, err := s.inner.QueryAPI().SuperRootAtTimestamp(ctx, timestamp)
s.require.NoError(err, "failed to query super-root at timestamp %d", timestamp)
s.require.NotNil(resp.Data, "supernode returned no super-root data at finalized timestamp %d", timestamp)
for _, chainID := range expectedChainIDs {
s.require.Contains(resp.ChainIDs, chainID, "supernode super-root at timestamp %d missing chain %s", timestamp, chainID)
}
return resp
}
// interopActivity returns the currently running interop activity, failing
// the test if test control is not wired up or the activity is not present.
// All methods below that exercise the interop activity route through this
// helper so that the nil-guard is written in exactly one place.
func (s *Supernode) interopActivity() *interop.Interop {
s.require.NotNil(s.testControl, "operation requires test control; use NewSupernodeWithTestControl")
ia := s.testControl.InteropActivity()
s.require.NotNil(ia, "interop activity not present (supernode stopped or interop disabled)")
return ia
}
// PauseInterop pauses the interop activity at the given timestamp.
// When the interop activity attempts to process this timestamp, it returns early.
// This function is for integration test control only.
// Requires the Supernode to be created with NewSupernodeWithTestControl.
func (s *Supernode) PauseInterop(ts uint64) {
s.interopActivity().PauseAt(ts)
}
// ResumeInterop clears any pause on the interop activity, allowing normal processing.
// This function is for integration test control only.
// Requires the Supernode to be created with NewSupernodeWithTestControl.
func (s *Supernode) ResumeInterop() {
s.interopActivity().Resume()
}
// RestartWithFreshDataDir stops the supernode, deletes its on-disk data
// directory in full, and starts a fresh supernode against the same chain
// containers, virtual nodes, and externally-visible RPC address.
// Requires NewSupernodeWithTestControl.
func (s *Supernode) RestartWithFreshDataDir() {
s.require.NotNil(s.testControl,
"RestartWithFreshDataDir requires test control; use NewSupernodeWithTestControl")
s.log.Info("restarting supernode with fresh data dir")
err := s.testControl.RestartWithFreshDataDir()
s.require.NoError(err, "failed to restart supernode with fresh data dir")
}
// Stop halts the supernode while preserving its data directory and RPC
// address. Requires NewSupernodeWithTestControl.
func (s *Supernode) Stop() {
s.require.NotNil(s.testControl, "Stop requires test control; use NewSupernodeWithTestControl")
s.log.Info("stopping supernode")
s.testControl.Stop()
}
// Start brings the supernode back up after Stop. Requires NewSupernodeWithTestControl.
// After the supernode is up, any VNs registered via ManageVN have their
// DSL-managed peer connections re-established, mirroring L2CLNode.Start.
func (s *Supernode) Start() {
s.require.NotNil(s.testControl, "Start requires test control; use NewSupernodeWithTestControl")
s.log.Info("starting supernode")
s.testControl.Start()
for _, vn := range s.managedVNs {
vn.restoreManagedPeers()
}
}
// BackfillAttempts returns the number of log-backfill attempts since the
// running interop activity's most recent (re)start.
// Requires the Supernode to be created with NewSupernodeWithTestControl.
func (s *Supernode) BackfillAttempts() int32 {
return s.interopActivity().BackfillAttempts()
}
// AwaitBackfillAttempts blocks until BackfillAttempts() >= minAttempts or the
// timeout elapses. Fails the test on timeout.
// Requires the Supernode to be created with NewSupernodeWithTestControl.
func (s *Supernode) AwaitBackfillAttempts(minAttempts int32) {
ia := s.interopActivity()
ctx, cancel := context.WithTimeout(s.ctx, 3*DefaultTimeout)
defer cancel()
err := wait.For(ctx, 500*time.Millisecond, func() (bool, error) {
return ia.BackfillAttempts() >= minAttempts, nil
})
s.require.NoErrorf(err, "backfill did not reach %d attempts in time (got %d)",
minAttempts, ia.BackfillAttempts())
}
// AwaitBackfillCompleted blocks until the interop activity finishes its
// log backfill phase, or the timeout elapses. Fails the test on timeout.
// Requires the Supernode to be created with NewSupernodeWithTestControl.
func (s *Supernode) AwaitBackfillCompleted() {
ia := s.interopActivity()
ctx, cancel := context.WithTimeout(s.ctx, 3*DefaultTimeout)
defer cancel()
err := wait.For(ctx, 500*time.Millisecond, func() (bool, error) {
return ia.BackfillCompleted(), nil
})
s.require.NoError(err, "backfill did not complete in time")
}
// ActivationTimestamp returns the configured interop activation timestamp.
// Requires NewSupernodeWithTestControl.
func (s *Supernode) ActivationTimestamp() uint64 {
return s.interopActivity().ActivationTimestamp()
}
// VerificationStartTimestamp returns the L2 timestamp the current interop
// activity began verifying at. Returns 0 before cold-start init completes.
// Requires NewSupernodeWithTestControl.
func (s *Supernode) VerificationStartTimestamp() uint64 {
return s.interopActivity().VerificationStartTimestamp()
}
// AwaitVerificationStartsAt blocks until cold-start init completes, then
// asserts VerificationStartTimestamp equals expected.
// Requires NewSupernodeWithTestControl.
func (s *Supernode) AwaitVerificationStartsAt(expected uint64) {
ia := s.interopActivity()
ctx, cancel := context.WithTimeout(s.ctx, 3*DefaultTimeout)
defer cancel()
err := wait.For(ctx, 500*time.Millisecond, func() (bool, error) {
return ia.BackfillCompleted(), nil
})
s.require.NoError(err, "cold-start initialization did not complete in time")
actual := ia.VerificationStartTimestamp()
s.require.Equalf(expected, actual,
"verificationStartTimestamp mismatch after cold-start init: expected %d, got %d",
expected, actual)
s.log.Info("verification start timestamp confirmed", "expected", expected, "actual", actual)
}
// AssertBackfillCovers verifies, for each supplied chain, that the interop
// logs DB contains blocks spanning from a first-seal at or near the expected
// T_lo all the way to a latest-seal at or near the safe tip. Specifically it
// asserts the three invariants log backfill must preserve:
//
// 1. firstSealed.Timestamp + blockTime > ActivationTimestamp
// (the first seal is at most one block before activation; when activation
// is not aligned to a block boundary, the block representing the chain
// state as of activation is the correct pairing anchor and is sealed).
// 2. firstSealed.Timestamp < FirstVerifiableTimestamp()
// (the post-backfill handoff happens strictly after the backfilled range)
// 3. firstSealed.Timestamp <= max(ActivationTimestamp, latestSealed.Timestamp - depth)
// + blockTime (backfill reached ~depth back,
// or all the way to activation if the chain is younger than depth)
//
// This is the strongest test-side evidence that backfill actually populated
// the DB, rather than the supernode simply resuming off of existing disk state.
// Requires the Supernode to be created with NewSupernodeWithTestControl.
func (s *Supernode) AssertBackfillCovers(depth time.Duration, blockTime uint64, chains ...eth.ChainID) {
s.require.Positive(len(chains), "AssertBackfillCovers requires at least one chain")
ia := s.interopActivity()
activation := ia.ActivationTimestamp()
backfillHandoff := ia.FirstVerifiableTimestamp()
depthSec := uint64(depth / time.Second)
for _, chainID := range chains {
first, err := ia.FirstSealedBlock(chainID)
s.require.NoErrorf(err, "chain %s: first sealed block must be readable", chainID)
latest, hasLatest, err := ia.LatestSealedBlock(chainID)
s.require.NoErrorf(err, "chain %s: latest sealed block must be readable", chainID)
s.require.Truef(hasLatest, "chain %s: logs DB must contain at least one sealed block after backfill", chainID)
s.require.Greaterf(first.Timestamp+blockTime, activation,
"chain %s: first seal ts %d must be within one block time (%d) of activation ts %d",
chainID, first.Timestamp, blockTime, activation)
s.require.Lessf(first.Timestamp, backfillHandoff,
"chain %s: first seal ts %d must be < backfill handoff ts %d (backfill must hand off strictly after the sealed range)",
chainID, first.Timestamp, backfillHandoff)
expectedLowerBound := activation
if latest.Timestamp > activation+depthSec {
expectedLowerBound = latest.Timestamp - depthSec
}
s.require.LessOrEqualf(first.Timestamp, expectedLowerBound+blockTime,
"chain %s: first seal ts %d should be within one block of expected lower bound %d (latest ts %d, depth %s)",
chainID, first.Timestamp, expectedLowerBound, latest.Timestamp, depth)
s.require.Greaterf(latest.Number, first.Number,
"chain %s: backfill should produce multiple sealed blocks (first=%d, latest=%d)",
chainID, first.Number, latest.Number)
s.log.Info("backfill coverage verified",
"chain", chainID,
"first_num", first.Number, "first_ts", first.Timestamp,
"latest_num", latest.Number, "latest_ts", latest.Timestamp,
"activation", activation, "backfill_handoff", backfillHandoff,
"depth_sec", depthSec)
}
}
// EnsureInteropPaused pauses interop after the latest local-safe timestamp.
// It verifies the last timestamp before the pause and returns the first unverified timestamp.
// Requires the Supernode to be created with NewSupernodeWithTestControl.
func (s *Supernode) EnsureInteropPaused(pauseOffset uint64, clNodes ...*L2CLNode) uint64 {
s.require.NotEmpty(clNodes, "EnsureInteropPaused requires at least one CL node")
ia := s.interopActivity()
var localSafeTimestamp uint64
for _, cl := range clNodes {
status := cl.SyncStatus()
localSafeTimestamp = max(localSafeTimestamp, status.LocalSafeL2.Time)
s.log.Info("EnsureInteropPaused: initial sync status",
"chain", cl.ChainID(),
"local_safe_num", status.LocalSafeL2.Number,
"local_safe_ts", status.LocalSafeL2.Time)
}
pauseTimestamp := localSafeTimestamp + pauseOffset
awaitTimestamp := pauseTimestamp - 1
ia.PauseAt(pauseTimestamp)
s.AwaitValidatedTimestamp(awaitTimestamp)
s.log.Info("EnsureInteropPaused: validation confirmed before pause", "timestamp", awaitTimestamp)
ctx, cancel := context.WithTimeout(s.ctx, DefaultTimeout)
defer cancel()
for ts := pauseTimestamp; ts < pauseTimestamp+100; ts++ {
resp, err := s.inner.QueryAPI().SuperRootAtTimestamp(ctx, ts)
if err != nil || resp.Data == nil {
s.log.Info("EnsureInteropPaused: confirmed interop is paused",
"intendedPauseTimestamp", pauseTimestamp,
"actualPauseTimestamp", ts)
return ts
}
s.log.Warn("EnsureInteropPaused: pause came in late, timestamp already verified",
"timestamp", ts,
"intendedPause", pauseTimestamp)
}
s.t.Error("EnsureInteropPaused: failed to find unverified timestamp within 100 timestamps")
s.t.FailNow()
return pauseTimestamp
}