Skip to content

Commit 21d8e48

Browse files
committed
feat(l1): harden state-history invariants
Per review: - Document the version-bump strategy on JOURNAL_VERSION so PRs 2/3/4 have a policy to follow: drain at finality on bump, or grow per-version decode arms if history needs to survive the upgrade. - Add debug_assert!(layers_to_commit.len() == 1) in TrieLayerCache::commit to runtime-enforce the single-layer invariant the ATTRIBUTION NOTE describes; catches any future change that quietly merges multiple blocks into one journal entry.
1 parent ec89fa8 commit 21d8e48

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

crates/storage/journal.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
use ethrex_common::H256;
2626

2727
/// Current version of the journal entry codec.
28+
///
29+
/// Bumping this constant changes the wire format. The decoder rejects any
30+
/// other version with [`JournalDecodeError::VersionMismatch`]: a v(N) binary
31+
/// will refuse to interpret v(N+1) entries (forward safety) and will also
32+
/// refuse to read v(N-1) entries written by a previous binary (no implicit
33+
/// fallback). The plan for the rollback consumer (PR 2/3/4) is to drain
34+
/// the journal at a finality boundary on upgrade, so the v(N) journal
35+
/// starts empty after the bump; a future bump that needs to keep history
36+
/// across the upgrade should introduce per-version `decode_vN` arms here
37+
/// rather than re-encoding existing entries.
2838
pub const JOURNAL_VERSION: u8 = 1;
2939

3040
/// A single reverse-diff entry: `(on_disk_key, previous_value_or_none)`.

crates/storage/layering.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ impl TrieLayerCache {
277277
// rollback consumer (PR 2/3) would then be unable to reconstruct
278278
// intermediate pre-images. Single-layer commits are an invariant of the
279279
// current write path; revisit this if that ever changes.
280+
debug_assert!(
281+
layers_to_commit.len() == 1,
282+
"multi-layer commit would corrupt journal attribution (see ATTRIBUTION NOTE above): \
283+
got {} layers, expected 1",
284+
layers_to_commit.len()
285+
);
280286
let top_layer = layers_to_commit.first()?;
281287
let top_layer_id = top_layer.id;
282288
let committed_block_number = top_layer.block_number;

0 commit comments

Comments
 (0)