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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//! Resumable `eth_getLogs` scan vocabulary (#1092/#1106/#1108).
//!
//! This module owns the cursor + sink vocabulary every on-chain watcher shares;
//! the loop that drives it lives in
//! [`multiplexed_poller`](super::multiplexed_poller), which reads head once per
//! tick, scans each route's `[cursor, head]` gap in bounded windows
//! (`blockchain.get_logs_max_block_span`) via one merged `eth_getLogs`, hands
//! each log to the owning [`LogSink`], then advances — and, for a route whose [`CursorStart`] owns a
//! [`Checkpoint`], durably records — the cursor per window. The **first tick's**
//! large range *is* the historical backfill; later ticks are the live tail. The
//! poller uses `eth_getLogs` rather than alloy's `watch_logs` (`eth_newFilter` +
//! `eth_getFilterChanges`), which the default public Arbitrum Sepolia RPC and
//! most keyless endpoints reject with `-32601` (#1106).
//!
//! Two properties matter for correctness:
//!
//! - **Resumability (#1108).** Persisting the cursor *per window* during the
//! first big backfill means a rate-limited cold start that dies part-way
//! through resumes near where it stopped instead of re-scanning from the
//! configured floor. Combined with the per-tick backoff (which never exits the
//! process — only the startup preflight does), a throttled boot makes forward
//! progress across restarts rather than crash-looping.
//! - **Idempotency.** A tick that errors mid-window leaves the cursor at the
//! last *completed* window, so the failed window re-scans on retry; a shallow
//! reorg on resume re-scans `reorg_margin` blocks. Every sink must therefore
//! apply logs idempotently (dedup by id / set insertion / authoritative
//! re-read).
use Future;
use ;
use Log;
use ;
use sanitize_error_sources as n;
use ;
use Arc;
use CancellationToken;
use AbortOnDropHandle;
use warn;
/// A durable per-key scan checkpoint: the store plus the key it reads and writes
/// under. Carried by [`CursorStart::FromCheckpoint`], which persists its cursor
/// forward and reads its resume floor back from the same key.
pub
/// Where a [`CursorStart::FromCheckpoint`] watcher starts on a first-ever boot,
/// when no cursor has ever been persisted.
pub
/// How the **first** tick derives its scan floor, and whether the cursor is
/// persisted forward.
///
/// Persistence rides inside the variants that own a [`Checkpoint`] rather than
/// on a separate field, so two invalid shapes cannot be constructed: a watcher
/// that resumes from a checkpoint cannot be configured without one, and a
/// watcher that re-derives its floor from head cannot declare a margin or window
/// it never reads.
pub
/// Per-log consumer. Decodes the log and applies it to the watcher's in-memory
/// projection. Implementations MUST be idempotent under re-scan (see the module
/// doc) and MUST NOT return `Err` for an *undecodable* log — a deterministic
/// re-scan of a permanently-undecodable log would hot-loop the cursor forever,
/// so a decode failure is logged and skipped (`Ok`). Return `Err` only for a
/// *retryable* failure (a durable-write error, a follow-up RPC that should be
/// retried) so the tick backs off and re-scans the window.
pub
/// Divisor turning a sink's own reconcile cadence into the minimum age its last
/// reconcile must have before a watcher recovery forces another.
///
/// A recovery edge fires whenever a route comes back from an errored tick, and
/// an endpoint that flaps rather than staying down produces one on roughly the
/// backoff interval. Without a floor each flap costs a full authoritative
/// re-read aimed at an endpoint that is already failing. Fifteen keeps the
/// forced repair an order of magnitude faster than the cadence — which is the
/// entire point of the edge — while bounding what a flap can cost.
const RECOVERY_FLOOR_DIVISOR: u32 = 15;
/// Clear a cadence clock so the reconcile on this same tick re-reads, unless it
/// already read within `interval / RECOVERY_FLOOR_DIVISOR`.
///
/// The idiom for [`LogSink::on_recovered`] in a sink whose `on_tick_complete`
/// re-reads authoritative state on a cadence. Such a sink is at its most stale
/// exactly when a route recovers: the reconcile is skipped entirely while the
/// route is errored, so the cadence repair has not been running, and a reconcile
/// whose own read then failed stamped the clock anyway and deferred a further
/// interval.
pub
/// The instant types a sink stamps its cadence clock with. Sinks differ —
/// `blacklist_watcher` runs on `tokio::time` so its tests can pause the clock —
/// and [`clear_cadence_on_recovery`] only ever asks one question of either.
pub
/// A loop-level observability hook carried on a
/// [`Route`](super::multiplexed_poller::Route). Boxed so a watcher can close over
/// its `Arc<Metrics>` without the generic poller knowing the concrete metric.
///
/// Hooks SHOULD be panic-free: in practice every hook is a `metric_hook` closure
/// that only bumps an infallible counter/gauge. `on_task_panic` additionally runs
/// from a `Drop` guard during an unwind — where a second panic would abort the
/// process — so the poller's guard wraps that one call in `catch_unwind` as a
/// backstop; the other hooks fire on normal paths and are not guarded.
pub type WatcherHook = ;
pub
/// Resolve a [`CursorStart::FromCheckpoint`] watcher's initial scan floor from
/// its stored checkpoint and the current scan upper bound. `Some(b)` →
/// `b - margin` (reorg rewind), floored at `from_block` (the contract does not
/// exist below its deploy block) and clamped `<= head` (a checkpoint momentarily
/// ahead of a lagging RPC head never inverts the range). `None` (first-ever,
/// cold-store boot) → `head`: nothing predates this node, so there is no history
/// to replay. Pure so the start is unit-testable without a live provider.
/// Generalizes the settlement watcher's original `resolve_backfill_start` (which
/// was `None => head`).
pub const
/// Resolve a [`CursorStart::HeadMinusWindow`] watcher's floor: `head - window`,
/// clamped `>= floor` and `<= head`. `window == 0` → `head` (live-from-head, no
/// backfill). Pure and unit-testable.
pub const
/// Owns a running watcher task and the shutdown token that stops it. The token
/// is minted inside [`super::multiplexed_poller::spawn`] and never escapes except
/// through [`shutdown`], so no call site can conjure or drop an inert token — the
/// mistake #1230 fixed by convention, made unrepresentable (#1236). [`shutdown`]
/// is the graceful path (the loop flushes every route's cursor and returns); the
/// wrapped [`AbortOnDropHandle`] guard is the hard safety net when the handle drops
/// without a prior `shutdown`.
///
/// [`shutdown`]: WatcherHandle::shutdown
///
/// Public because [`multiplexed_poller::spawn`](super::multiplexed_poller::spawn)
/// returns it and external integration tests drive that spawn.