miden_node_store/state/view/scoped.rs
1//! Proof-of-validation block-number types issued by [`StateView`](super::StateView).
2//!
3//! Tip-scoped database queries take these types instead of raw block numbers, so a query whose
4//! bound was not validated against a state view's tip is not expressible: the only constructors
5//! live on [`StateView`](super::StateView), which checks the bound against its pinned snapshot.
6//!
7//! Chain tips are monotonic, so a value issued by an older view remains valid for any later
8//! state — holding one across view lifetimes is sound, if pointless.
9
10use std::ops::{Deref, RangeInclusive};
11
12use miden_protocol::block::BlockNumber;
13
14/// A block number proven to be at or below the issuing view's chain tip.
15#[derive(Debug, Clone, Copy)]
16pub struct ScopedBlockNum(BlockNumber);
17
18impl ScopedBlockNum {
19 /// Issued by [`StateView`](super::StateView) after validating the bound against its tip.
20 pub(super) fn new(block_num: BlockNumber) -> Self {
21 Self(block_num)
22 }
23
24 /// Constructs a scoped block number without validation.
25 ///
26 /// Test-only: lets database tests exercise scoped queries without a running state.
27 #[cfg(test)]
28 pub(crate) fn new_unchecked(block_num: BlockNumber) -> Self {
29 Self(block_num)
30 }
31}
32
33/// The validated block number is read by dereferencing (`*scoped`); [`DerefMut`] is deliberately
34/// not implemented, as mutating the inner value would invalidate the proof.
35///
36/// [`DerefMut`]: std::ops::DerefMut
37impl Deref for ScopedBlockNum {
38 type Target = BlockNumber;
39
40 fn deref(&self) -> &BlockNumber {
41 &self.0
42 }
43}
44
45/// A block range whose upper bound is proven to be at or below the issuing view's chain tip.
46#[derive(Debug, Clone)]
47pub struct ScopedBlockRange(RangeInclusive<BlockNumber>);
48
49impl ScopedBlockRange {
50 /// Issued by [`StateView`](super::StateView) after validating the range against its tip.
51 pub(super) fn new(range: RangeInclusive<BlockNumber>) -> Self {
52 Self(range)
53 }
54
55 /// Returns the start of the validated range.
56 pub(crate) fn start(&self) -> BlockNumber {
57 *self.0.start()
58 }
59
60 /// Returns the end of the validated range.
61 pub(crate) fn end(&self) -> BlockNumber {
62 *self.0.end()
63 }
64
65 /// Returns the end of the validated range as a scoped block number.
66 ///
67 /// Sound because the range's upper bound is exactly what its proof covers.
68 pub(crate) fn scoped_end(&self) -> ScopedBlockNum {
69 ScopedBlockNum(*self.0.end())
70 }
71}