Skip to content

Commit 9cde27d

Browse files
committed
test(blocks): bind latest_ts to binary-search output via interior window
The wiring tests added in this PR's first commit prove the memo is consulted and the head closure is wired correctly, but the chosen windows both rely on the bound-overlap short-circuits in `compute_block_range_given_bounds`. Under those windows, a regression that swaps `genesis_ts` and `latest_ts` at the compute call site — or otherwise threads the wrong `latest_ts` into the binary search — still produces the right answer because both values lie outside the queried range in the same direction. Add a third test with a window strictly inside chain history. Both bounds drive the binary search, and the returned `(2, 2)` depends on `latest_ts == 1400` being honestly fed to `compute_block_range_given_bounds`; a regression that supplies `genesis_ts` (1000) in its place fires the `end_ts >= latest_ts` short-circuit and yields `(2, latest)`. Surfaced by `/pr-review` during PR #8.
1 parent a36f0d3 commit 9cde27d

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

src/blocks/window.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,5 +1689,43 @@ mod tests {
16891689
);
16901690
assert_eq!(head_counter.load(Ordering::SeqCst), 1);
16911691
}
1692+
1693+
#[tokio::test]
1694+
async fn interior_window_threads_memoized_latest_ts_through_binary_search() {
1695+
// Window strictly inside chain history: both bounds drive the
1696+
// binary search, so the returned `(start, end)` depends on
1697+
// `latest_ts` being honestly threaded from the memo into
1698+
// `compute_block_range_given_bounds`. A regression that swaps
1699+
// `genesis_ts` and `latest_ts` at the call site, or zeroes
1700+
// out `latest_block` after the head closure runs, changes
1701+
// the answer (the `end_ts >= latest_ts` short-circuit fires
1702+
// under the wrong `latest_ts`, clamping `end_block` to
1703+
// `latest` instead of the correct interior block).
1704+
//
1705+
// The earlier tests prove the memo is consulted; this one
1706+
// proves the memoized values are actually fed to the binary
1707+
// search.
1708+
let timestamps = vec![1000, 1100, 1200, 1300, 1400];
1709+
let latest: BlockNumber = 4;
1710+
let bounds_memo = ChainBoundsMemo::new(DEFAULT_HEAD_TTL);
1711+
1712+
let log = Arc::new(StdMutex::new(Vec::<BlockNumber>::new()));
1713+
let head_counter = Arc::new(AtomicUsize::new(0));
1714+
1715+
let (s, e) = block_range_for_timestamps_with(
1716+
&bounds_memo,
1717+
UnixTimestamp(1150),
1718+
UnixTimestamp(1250),
1719+
counting_fetch_ts(timestamps, log.clone()),
1720+
counting_fetch_head(latest, head_counter.clone()),
1721+
)
1722+
.await
1723+
.unwrap();
1724+
1725+
// First block with ts >= 1150 is block 2 (ts=1200).
1726+
// Last block with ts <= 1250 is also block 2 (ts=1200).
1727+
assert_eq!((s, e), (2, 2));
1728+
assert_eq!(head_counter.load(Ordering::SeqCst), 1);
1729+
}
16921730
}
16931731
}

0 commit comments

Comments
 (0)