diff --git a/miden-tx/src/testing/mock_chain/mod.rs b/miden-tx/src/testing/mock_chain/mod.rs index 8dd7b3796..5c63d2aae 100644 --- a/miden-tx/src/testing/mock_chain/mod.rs +++ b/miden-tx/src/testing/mock_chain/mod.rs @@ -653,7 +653,9 @@ impl MockChain { /// This will also make all the objects currently pending available for use. /// If `block_num` is `Some(number)`, blocks will be generated up to `number`. pub fn seal_block(&mut self, block_num: Option) -> Block { - let next_block_num = self.blocks.last().map_or(0, |b| b.header().block_num().child()); + let next_block_num = + self.blocks.last().map_or(0, |b| b.header().block_num().child().as_u32()); + let target_block_num = block_num.unwrap_or(next_block_num); if target_block_num < next_block_num { diff --git a/objects/src/block/block_number.rs b/objects/src/block/block_number.rs index 5e4bfa862..51efc33de 100644 --- a/objects/src/block/block_number.rs +++ b/objects/src/block/block_number.rs @@ -22,14 +22,17 @@ impl BlockNumber { /// exponent. pub const EPOCH_LENGTH_EXPONENT: u8 = 16; + /// Genesis BlockNumber + pub const GENESIS: Self = Self(0); + /// Returns the previous block number - pub fn parent(&self) -> u32 { - self.checked_sub(1).expect("Cannot go below Genesis block!").0 + pub fn parent(self) -> Option { + self.checked_sub(1) } /// Returns the next block number - pub fn child(&mut self) -> u32 { - self.0 + 1 + pub fn child(self) -> BlockNumber { + self + 1 } /// Creates the [`BlockNumber`] corresponding to the epoch block for the provided `epoch`. @@ -62,7 +65,7 @@ impl BlockNumber { self.0 as usize } - /// Checked integer subtraction. Computes `self - rhs`, returning `None` if overflow occurred. + /// Checked integer subtraction. Computes `self - rhs`, returning `None` if underflow occurred. pub fn checked_sub(&self, rhs: u32) -> Option { self.0.checked_sub(rhs).map(Self) } diff --git a/objects/src/errors.rs b/objects/src/errors.rs index d93ee04d3..9b5413a06 100644 --- a/objects/src/errors.rs +++ b/objects/src/errors.rs @@ -356,7 +356,7 @@ pub enum TransactionInputError { #[error("block in which input note with id {0} was created is not in chain mmr")] InputNoteBlockNotInChainMmr(NoteId), #[error("input note with id {0} was not created in block {1}")] - InputNoteNotInBlock(NoteId, u32), + InputNoteNotInBlock(NoteId, BlockNumber), #[error("account ID computed from seed is invalid")] InvalidAccountIdSeed(#[source] AccountIdError), #[error( diff --git a/objects/src/transaction/inputs.rs b/objects/src/transaction/inputs.rs index 8ae09ebe5..b011cbc10 100644 --- a/objects/src/transaction/inputs.rs +++ b/objects/src/transaction/inputs.rs @@ -402,10 +402,7 @@ fn validate_is_in_block( .note_path() .verify(note_index, note_hash, &block_header.note_root()) .map_err(|_| { - TransactionInputError::InputNoteNotInBlock( - note.id(), - proof.location().block_num().as_u32(), - ) + TransactionInputError::InputNoteNotInBlock(note.id(), proof.location().block_num()) }) }