Skip to content

Commit 8e3e8da

Browse files
fix: track block in MMR (#2353)
1 parent 0a90aeb commit 8e3e8da

File tree

4 files changed

+63
-26
lines changed

4 files changed

+63
-26
lines changed

CHANGELOG.md

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
11
# Changelog
22

3-
## 0.14.0 (TBD)
4-
5-
### Features
6-
7-
- Enable `CodeBuilder` to add advice map entries to compiled scripts ([#2275](https://github.com/0xMiden/miden-base/pull/2275)).
8-
- Added `BlockNumber::MAX` constant to represent the maximum block number ([#2324](https://github.com/0xMiden/miden-base/pull/2324)).
9-
10-
### Changes
11-
12-
- [BREAKING] Renamed `WellKnownComponent` to `StandardAccountComponent`, `WellKnownNote` to `StandardNote`, and `WellKnownNoteAttachment` to `StandardNoteAttachment` ([#2332](https://github.com/0xMiden/miden-base/pull/2332)).
13-
- Skip requests to the `DataStore` for asset vault witnesses which are already in transaction inputs ([#2298](https://github.com/0xMiden/miden-base/pull/2298)).
14-
- [BREAKING] refactored `TransactionAuthenticator::get_public_key()` method to return `Arc<PublicKey> `instead of `&PublicKey` ([#2304](https://github.com/0xMiden/miden-base/pull/2304)).
15-
- [BREAKING] Renamed `NoteInputs` to `NoteStorage` to better reflect that values are stored data associated with a note rather than inputs ([#1662](https://github.com/0xMiden/miden-base/issues/1662), [#2316](https://github.com/0xMiden/miden-base/issues/2316)).
16-
- Removed `NoteType::Encrypted` ([#2315](https://github.com/0xMiden/miden-base/pull/2315)).
17-
18-
# 0.13.3 (TBD)
3+
## 0.13.3 (2026-01-27)
194

205
- Added standards for working with `NetworkAccountTarget` attachments ([#2338](https://github.com/0xMiden/miden-base/pull/2338)).
6+
- Fixed `PartialBlockchain::add_block()` not adding block headers to the `blocks` map when `track=true`, which caused `prune_to()` to never untrack old blocks, leading to unbounded memory growth ([#2353](https://github.com/0xMiden/miden-base/pull/2353)).
217

228
## 0.13.2 (2026-01-21)
239

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ homepage = "https://miden.xyz"
2121
license = "MIT"
2222
repository = "https://github.com/0xMiden/miden-base"
2323
rust-version = "1.90"
24-
version = "0.13.2"
24+
version = "0.13.3"
2525

2626
[profile.release]
2727
codegen-units = 1

crates/miden-protocol/src/transaction/partial_blockchain.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,18 @@ impl PartialBlockchain {
178178
/// provided block header is for the next block in the chain.
179179
///
180180
/// If `track` parameter is set to true, the authentication path for the provided block header
181-
/// will be added to this partial blockchain.
181+
/// will be added to this partial blockchain, and the block header will be stored for later
182+
/// retrieval.
182183
///
183184
/// # Panics
184185
/// Panics if the `block_header.block_num` is not equal to the current chain length (i.e., the
185186
/// provided block header is not the next block in the chain).
186187
pub fn add_block(&mut self, block_header: &BlockHeader, track: bool) {
187188
assert_eq!(block_header.block_num(), self.chain_length());
188189
self.mmr.add(block_header.commitment(), track);
190+
if track {
191+
self.blocks.insert(block_header.block_num(), block_header.clone());
192+
}
189193
}
190194

191195
/// Drop every block header whose number is strictly less than `to.end`.
@@ -492,4 +496,51 @@ mod tests {
492496
assert!(!chain.mmr().is_tracked(block_num as usize));
493497
}
494498
}
499+
500+
#[test]
501+
fn add_block_with_track_adds_to_blocks() {
502+
let mut blockchain = PartialBlockchain::default();
503+
let header = int_to_block_header(0);
504+
505+
blockchain.add_block(&header, true);
506+
507+
assert!(blockchain.contains_block(0.into()));
508+
assert_eq!(blockchain.num_tracked_blocks(), 1);
509+
}
510+
511+
#[test]
512+
fn add_block_without_track_does_not_add_to_blocks() {
513+
let mut blockchain = PartialBlockchain::default();
514+
let header = int_to_block_header(0);
515+
516+
blockchain.add_block(&header, false);
517+
518+
assert!(!blockchain.contains_block(0.into()));
519+
assert_eq!(blockchain.num_tracked_blocks(), 0);
520+
}
521+
522+
#[test]
523+
fn prune_to_removes_tracked_blocks() {
524+
let mut blockchain = PartialBlockchain::default();
525+
// Add 10 blocks with tracking
526+
for i in 0..10u32 {
527+
let header = int_to_block_header(i);
528+
blockchain.add_block(&header, true);
529+
}
530+
assert_eq!(blockchain.num_tracked_blocks(), 10);
531+
532+
// Prune to keep only last 4
533+
blockchain.prune_to(..6.into());
534+
535+
assert_eq!(blockchain.num_tracked_blocks(), 4);
536+
for i in 0u32..6 {
537+
assert!(!blockchain.contains_block(i.into()));
538+
// Verify the underlying MMR also untracked the block
539+
assert!(!blockchain.mmr().is_tracked(i as usize));
540+
}
541+
for i in 6u32..10 {
542+
assert!(blockchain.contains_block(i.into()));
543+
assert!(blockchain.mmr().is_tracked(i as usize));
544+
}
545+
}
495546
}

0 commit comments

Comments
 (0)