newton-bootnode 0.4.14

Passive snapshot and delta store for late-joining operator bootstrap
Documentation
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Sealed snapshot persistence backed by redb.
//!
//! Stores full JMT snapshots keyed by `sequence_no`. The unified tree has a single
//! snapshot stream (no per-subtree keys) — namespace separation is at the leaf-key
//! level inside the snapshot body, matching the unified-tree design in the state-tree
//! crate.
//!
//! **Memory:** `get` and `latest_at_most` return only the lightweight
//! [`SnapshotHeader`] from a dedicated header table. The body lives in a
//! separate table and is only read via [`SnapshotStore::read_body`], which
//! streams to a caller-supplied `Write` impl.
//!
//! **Staleness (§S.19):** the store is policy-free — `latest_at_most` returns
//! snapshots of any age. Consumer-side staleness gating (e.g., operator bootstrap
//! checking `MAX_SNAPSHOT_STALENESS_SECS`) is enforced at the boundary, not here.

use std::{io::Write, path::Path};

use alloy_primitives::B256;
use redb::{ReadableDatabase, ReadableTable, TableDefinition};
use serde::{Deserialize, Serialize};
use tracing::{debug, info, warn};

use crate::error::BootnodeError;

const HEADERS_TABLE: TableDefinition<'_, u64, &[u8]> = TableDefinition::new("snapshot_headers");
const BODIES_TABLE: TableDefinition<'_, u64, &[u8]> = TableDefinition::new("snapshot_bodies");

const SNAPSHOT_FORMAT_V1: u8 = 1;

/// 256 MiB — upper bound on a valid JMT snapshot body.
///
/// Chosen to accommodate the maximum projected unified-tree size for the
/// foreseeable deployment window while preventing OOM from a corrupt record
/// claiming a multi-GB body. Revisit when tree size projections change.
pub const MAX_SNAPSHOT_BODY_BYTES: usize = 256 * 1024 * 1024;

/// Lightweight metadata for a sealed snapshot — no body allocation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SnapshotHeader {
    /// On-disk format version. Current: [`SNAPSHOT_FORMAT_V1`].
    pub format_version: u8,
    /// State-tree sequence number at which this snapshot was taken.
    pub sequence_no: u64,
    /// State root hash of the unified JMT at `sequence_no`.
    pub state_root: B256,
    /// UNIX timestamp (seconds) when the snapshot was sealed.
    pub sealed_at_ts: u64,
    /// Size of the serialized body in bytes.
    pub body_len: u64,
}

/// redb-backed store for sealed full-tree snapshots.
///
/// The store is policy-free: it persists and retrieves snapshots without
/// filtering by age. Staleness enforcement is the consumer's responsibility.
#[derive(Debug)]
pub struct SnapshotStore {
    db: redb::Database,
}

impl SnapshotStore {
    /// Open or create the snapshot store at `path`.
    pub fn open(path: impl AsRef<Path>) -> Result<Self, BootnodeError> {
        let path = path.as_ref();
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let db = redb::Database::create(path)?;
        let txn = db.begin_write()?;
        txn.open_table(HEADERS_TABLE)?;
        txn.open_table(BODIES_TABLE)?;
        txn.commit()?;
        info!(path = %path.display(), "snapshot store opened");
        Ok(Self { db })
    }

    /// Persist a snapshot. Overwrites any existing snapshot at the same `sequence_no`.
    ///
    /// Header and body are written in a single transaction.
    pub fn put(&self, header: &SnapshotHeader, body: &[u8]) -> Result<(), BootnodeError> {
        if body.len() > MAX_SNAPSHOT_BODY_BYTES {
            return Err(BootnodeError::SnapshotBodyTooLarge {
                size: body.len(),
                max: MAX_SNAPSHOT_BODY_BYTES,
            });
        }
        if header.body_len != body.len() as u64 {
            return Err(BootnodeError::SnapshotBodyLenMismatch {
                header_body_len: header.body_len,
                actual: body.len() as u64,
            });
        }
        let header_bytes = rmp_serde::to_vec(header)?;
        debug!(
            sequence_no = header.sequence_no,
            header_bytes = header_bytes.len(),
            body_bytes = body.len(),
            "snapshot put"
        );
        let txn = self.db.begin_write()?;
        {
            let mut headers = txn.open_table(HEADERS_TABLE)?;
            let mut bodies = txn.open_table(BODIES_TABLE)?;
            headers.insert(header.sequence_no, header_bytes.as_slice())?;
            bodies.insert(header.sequence_no, body)?;
        }
        txn.commit()?;
        Ok(())
    }

    /// Retrieve the header at exactly `sequence_no`, if any.
    pub fn get(&self, sequence_no: u64) -> Result<Option<SnapshotHeader>, BootnodeError> {
        let txn = self.db.begin_read()?;
        let table = txn.open_table(HEADERS_TABLE)?;
        match table.get(sequence_no)? {
            Some(guard) => Ok(Some(decode_header(guard.value())?)),
            None => Ok(None),
        }
    }

    /// Return the header with the largest `sequence_no <= up_to`, if any.
    pub fn latest_at_most(&self, up_to: u64) -> Result<Option<SnapshotHeader>, BootnodeError> {
        let txn = self.db.begin_read()?;
        let table = txn.open_table(HEADERS_TABLE)?;
        let mut range = table.range(..=up_to)?;
        match range.next_back() {
            Some(Ok((_, guard))) => Ok(Some(decode_header(guard.value())?)),
            Some(Err(e)) => Err(e.into()),
            None => Ok(None),
        }
    }

    /// Stream the body of snapshot `sequence_no` into `writer`.
    ///
    /// Both tables are opened from the same read transaction for snapshot
    /// isolation. The header is the source of truth for existence:
    ///
    /// - **Header missing**: returns `Ok(None)` regardless of `BODIES_TABLE`
    ///   state. The canonical "not sealed" signal. An orphan body without a
    ///   matching header is silently inaccessible through this method —
    ///   correct, because the only insertion path (`put`) writes header+body
    ///   atomically, so an orphan body would be evidence of either external
    ///   tampering or a redb-internal bug, neither of which `read_body`
    ///   should service.
    /// - **Header present, body missing**: returns
    ///   [`BootnodeError::SnapshotBodyLenMismatch`] with `actual: 0`. Indicates
    ///   a torn write or partial restoration.
    /// - **Header present, body present, lengths differ**: returns
    ///   [`BootnodeError::SnapshotBodyLenMismatch`] with the actual length.
    ///   Indicates corruption or a write-side invariant violation.
    /// - **Header present, body present, lengths match**: streams body to
    ///   `writer`, returns `Ok(Some(body_len))`.
    pub fn read_body(&self, sequence_no: u64, writer: &mut impl Write) -> Result<Option<u64>, BootnodeError> {
        let txn = self.db.begin_read()?;
        let headers = txn.open_table(HEADERS_TABLE)?;
        let header = match headers.get(sequence_no)? {
            Some(guard) => decode_header(guard.value())?,
            None => return Ok(None),
        };
        let bodies = txn.open_table(BODIES_TABLE)?;
        let Some(guard) = bodies.get(sequence_no)? else {
            warn!(
                sequence_no,
                header_body_len = header.body_len,
                "orphan snapshot header — body missing on disk"
            );
            return Err(BootnodeError::SnapshotBodyLenMismatch {
                header_body_len: header.body_len,
                actual: 0,
            });
        };
        let body = guard.value();
        let actual = body.len() as u64;
        if actual != header.body_len {
            warn!(
                sequence_no,
                header_body_len = header.body_len,
                actual,
                "snapshot body length drift between header and bodies tables"
            );
            return Err(BootnodeError::SnapshotBodyLenMismatch {
                header_body_len: header.body_len,
                actual,
            });
        }
        writer.write_all(body)?;
        Ok(Some(actual))
    }
}

fn decode_header(bytes: &[u8]) -> Result<SnapshotHeader, BootnodeError> {
    let hdr: SnapshotHeader = rmp_serde::from_slice(bytes)?;
    if hdr.format_version != SNAPSHOT_FORMAT_V1 {
        warn!(version = hdr.format_version, "unsupported snapshot format");
        return Err(BootnodeError::UnsupportedSnapshotFormat(hdr.format_version));
    }
    Ok(hdr)
}

#[cfg(test)]
mod tests {
    use super::*;

    const TEST_BODY: &[u8] = &[0xAB; 64];

    fn test_header(sequence_no: u64, sealed_at_ts: u64) -> SnapshotHeader {
        SnapshotHeader {
            format_version: SNAPSHOT_FORMAT_V1,
            sequence_no,
            state_root: B256::from([sequence_no as u8; 32]),
            sealed_at_ts,
            body_len: TEST_BODY.len() as u64,
        }
    }

    #[test]
    fn roundtrip_header() {
        let dir = tempfile::tempdir().unwrap();
        let store = SnapshotStore::open(dir.path().join("snap.redb")).unwrap();

        let hdr = test_header(42, 1_700_000_000);
        store.put(&hdr, TEST_BODY).unwrap();

        let got = store.get(42).unwrap().expect("should exist");
        assert_eq!(got.format_version, SNAPSHOT_FORMAT_V1);
        assert_eq!(got.sequence_no, hdr.sequence_no);
        assert_eq!(got.state_root, hdr.state_root);
        assert_eq!(got.sealed_at_ts, hdr.sealed_at_ts);
        assert_eq!(got.body_len, TEST_BODY.len() as u64);
    }

    #[test]
    fn read_body_roundtrip() {
        let dir = tempfile::tempdir().unwrap();
        let store = SnapshotStore::open(dir.path().join("snap.redb")).unwrap();

        let hdr = test_header(42, 1_700_000_000);
        store.put(&hdr, TEST_BODY).unwrap();

        let mut buf = Vec::new();
        let n = store.read_body(42, &mut buf).unwrap().expect("should exist");
        assert_eq!(n, TEST_BODY.len() as u64);
        assert_eq!(buf, TEST_BODY);
    }

    #[test]
    fn read_body_missing_returns_none() {
        let dir = tempfile::tempdir().unwrap();
        let store = SnapshotStore::open(dir.path().join("snap.redb")).unwrap();

        assert!(store.read_body(99, &mut Vec::new()).unwrap().is_none());
    }

    #[test]
    fn get_missing_returns_none() {
        let dir = tempfile::tempdir().unwrap();
        let store = SnapshotStore::open(dir.path().join("snap.redb")).unwrap();

        assert!(store.get(99).unwrap().is_none());
    }

    #[test]
    fn latest_at_most_finds_right_snap() {
        let dir = tempfile::tempdir().unwrap();
        let store = SnapshotStore::open(dir.path().join("snap.redb")).unwrap();

        for seq in [10, 20, 30, 40] {
            store.put(&test_header(seq, 1_000_000 + seq), TEST_BODY).unwrap();
        }

        let hdr = store.latest_at_most(25).unwrap().expect("should find 20");
        assert_eq!(hdr.sequence_no, 20);

        let hdr = store.latest_at_most(40).unwrap().expect("should find 40");
        assert_eq!(hdr.sequence_no, 40);

        assert!(store.latest_at_most(5).unwrap().is_none());
    }

    #[test]
    fn sealed_at_ts_preserved_including_epoch_zero() {
        let dir = tempfile::tempdir().unwrap();
        let store = SnapshotStore::open(dir.path().join("snap.redb")).unwrap();

        store.put(&test_header(1, 0), TEST_BODY).unwrap();

        let hdr = store.get(1).unwrap().expect("should exist");
        assert_eq!(hdr.sealed_at_ts, 0);

        let hdr = store.latest_at_most(100).unwrap().expect("should find it");
        assert_eq!(hdr.sealed_at_ts, 0);
    }

    #[test]
    fn sequence_no_zero_roundtrip() {
        let dir = tempfile::tempdir().unwrap();
        let store = SnapshotStore::open(dir.path().join("snap.redb")).unwrap();

        store.put(&test_header(0, 0), TEST_BODY).unwrap();

        let hdr = store.get(0).unwrap().expect("seq 0 should exist");
        assert_eq!(hdr.sequence_no, 0);

        let hdr = store
            .latest_at_most(0)
            .unwrap()
            .expect("latest_at_most(0) should return seq 0");
        assert_eq!(hdr.sequence_no, 0);
    }

    #[test]
    fn unsupported_format_version_rejected() {
        let dir = tempfile::tempdir().unwrap();
        let store = SnapshotStore::open(dir.path().join("snap.redb")).unwrap();

        let bad_hdr = SnapshotHeader {
            format_version: 99,
            sequence_no: 1,
            state_root: B256::from([1u8; 32]),
            sealed_at_ts: 1_000,
            body_len: 64,
        };
        let bytes = rmp_serde::to_vec(&bad_hdr).unwrap();
        let txn = store.db.begin_write().unwrap();
        {
            let mut table = txn.open_table(HEADERS_TABLE).unwrap();
            table.insert(1u64, bytes.as_slice()).unwrap();
        }
        txn.commit().unwrap();

        let err = store.get(1).unwrap_err();
        assert!(matches!(err, BootnodeError::UnsupportedSnapshotFormat(99)));
    }

    #[test]
    fn read_body_rejects_cross_table_body_len_drift() {
        let dir = tempfile::tempdir().unwrap();
        let store = SnapshotStore::open(dir.path().join("snap.redb")).unwrap();

        // Write a header claiming a 64-byte body, then a 32-byte body directly
        // to BODIES_TABLE — bypassing `put` so the cross-table invariant is
        // violated on disk.
        let mut hdr = test_header(7, 1_000);
        hdr.body_len = 64;
        let header_bytes = rmp_serde::to_vec(&hdr).unwrap();

        let txn = store.db.begin_write().unwrap();
        {
            let mut headers = txn.open_table(HEADERS_TABLE).unwrap();
            headers.insert(7u64, header_bytes.as_slice()).unwrap();
            let mut bodies = txn.open_table(BODIES_TABLE).unwrap();
            let short_body = vec![0xCC; 32];
            bodies.insert(7u64, short_body.as_slice()).unwrap();
        }
        txn.commit().unwrap();

        let mut buf = Vec::new();
        let err = store.read_body(7, &mut buf).unwrap_err();
        assert!(matches!(
            err,
            BootnodeError::SnapshotBodyLenMismatch {
                header_body_len: 64,
                actual: 32
            }
        ));
        assert!(buf.is_empty(), "writer must not see partial bytes on mismatch");
    }

    #[test]
    fn read_body_rejects_orphan_header() {
        let dir = tempfile::tempdir().unwrap();
        let store = SnapshotStore::open(dir.path().join("snap.redb")).unwrap();

        // Header present, body absent (torn write).
        let hdr = test_header(9, 1_000);
        let header_bytes = rmp_serde::to_vec(&hdr).unwrap();

        let txn = store.db.begin_write().unwrap();
        {
            let mut headers = txn.open_table(HEADERS_TABLE).unwrap();
            headers.insert(9u64, header_bytes.as_slice()).unwrap();
        }
        txn.commit().unwrap();

        let err = store.read_body(9, &mut Vec::new()).unwrap_err();
        assert!(matches!(err, BootnodeError::SnapshotBodyLenMismatch { actual: 0, .. }));
    }

    #[test]
    fn put_rejects_body_len_mismatch() {
        let dir = tempfile::tempdir().unwrap();
        let store = SnapshotStore::open(dir.path().join("snap.redb")).unwrap();

        let mut hdr = test_header(1, 1_000);
        hdr.body_len = 999;

        let err = store.put(&hdr, TEST_BODY).unwrap_err();
        assert!(matches!(err, BootnodeError::SnapshotBodyLenMismatch { .. }));
        assert!(store.get(1).unwrap().is_none());
    }

    #[test]
    fn put_rejects_oversized_body() {
        let dir = tempfile::tempdir().unwrap();
        let store = SnapshotStore::open(dir.path().join("snap.redb")).unwrap();

        let big_body = vec![0xCC; MAX_SNAPSHOT_BODY_BYTES + 1];
        let hdr = SnapshotHeader {
            format_version: SNAPSHOT_FORMAT_V1,
            sequence_no: 1,
            state_root: B256::ZERO,
            sealed_at_ts: 1_000,
            body_len: big_body.len() as u64,
        };

        let err = store.put(&hdr, &big_body).unwrap_err();
        assert!(matches!(err, BootnodeError::SnapshotBodyTooLarge { .. }));
        assert!(store.get(1).unwrap().is_none());
    }
}