Make monero-serai Block::number not panic on invalid blocks

This commit is contained in:
Luke Parker 2024-01-06 00:02:55 -05:00
parent e7b0ed3e7e
commit 1ba2d8d832
No known key found for this signature in database
2 changed files with 17 additions and 12 deletions

View file

@ -58,10 +58,10 @@ pub struct Block {
} }
impl Block { impl Block {
pub fn number(&self) -> usize { pub fn number(&self) -> Option<u64> {
match self.miner_tx.prefix.inputs.first() { match self.miner_tx.prefix.inputs.first() {
Some(Input::Gen(number)) => (*number).try_into().unwrap(), Some(Input::Gen(number)) => Some(*number),
_ => panic!("invalid block, miner TX didn't have a Input::Gen"), _ => None,
} }
} }

View file

@ -174,9 +174,9 @@ impl BlockTrait<Monero> for Block {
const BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW: u64 = 60; const BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW: u64 = 60;
// If Monero doesn't have enough blocks to build a window, it doesn't define a network time // If Monero doesn't have enough blocks to build a window, it doesn't define a network time
if (u64::try_from(self.number()).unwrap() + 1) < BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW { if (self.number().unwrap() + 1) < BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW {
// Use the block number as the time // Use the block number as the time
return self.number().try_into().unwrap(); return self.number().unwrap();
} }
let mut timestamps = vec![self.header.timestamp]; let mut timestamps = vec![self.header.timestamp];
@ -194,7 +194,7 @@ impl BlockTrait<Monero> for Block {
timestamps.push(parent_block.header.timestamp); timestamps.push(parent_block.header.timestamp);
parent = parent_block.parent(); parent = parent_block.parent();
if parent_block.number() == 0 { if parent_block.number().unwrap() == 0 {
break; break;
} }
} }
@ -212,7 +212,7 @@ impl BlockTrait<Monero> for Block {
// Monero also solely requires the block's time not be less than the median, it doesn't ensure // Monero also solely requires the block's time not be less than the median, it doesn't ensure
// it advances the median forward // it advances the median forward
// Ensure monotonicity despite both these issues by adding the block number to the median time // Ensure monotonicity despite both these issues by adding the block number to the median time
res + u64::try_from(self.number()).unwrap() res + self.number().unwrap()
} }
} }
@ -585,16 +585,21 @@ impl Network for Monero {
if let Some((_, eventuality)) = eventualities.map.get(&tx.prefix.extra) { if let Some((_, eventuality)) = eventualities.map.get(&tx.prefix.extra) {
if eventuality.matches(&tx) { if eventuality.matches(&tx) {
res.insert(eventualities.map.remove(&tx.prefix.extra).unwrap().0, (block.number(), tx)); res.insert(
eventualities.map.remove(&tx.prefix.extra).unwrap().0,
(usize::try_from(block.number().unwrap()).unwrap(), tx),
);
} }
} }
} }
eventualities.block_number += 1; eventualities.block_number += 1;
assert_eq!(eventualities.block_number, block.number()); assert_eq!(eventualities.block_number, usize::try_from(block.number().unwrap()).unwrap());
} }
for block_num in (eventualities.block_number + 1) .. block.number() { for block_num in
(eventualities.block_number + 1) .. usize::try_from(block.number().unwrap()).unwrap()
{
let block = { let block = {
let mut block; let mut block;
while { while {
@ -612,7 +617,7 @@ impl Network for Monero {
// Also check the current block // Also check the current block
check_block(self, eventualities, block, &mut res).await; check_block(self, eventualities, block, &mut res).await;
assert_eq!(eventualities.block_number, block.number()); assert_eq!(eventualities.block_number, usize::try_from(block.number().unwrap()).unwrap());
res res
} }
@ -687,7 +692,7 @@ impl Network for Monero {
#[cfg(test)] #[cfg(test)]
async fn get_block_number(&self, id: &[u8; 32]) -> usize { async fn get_block_number(&self, id: &[u8; 32]) -> usize {
self.rpc.get_block(*id).await.unwrap().number() self.rpc.get_block(*id).await.unwrap().number().try_into().unwrap()
} }
#[cfg(test)] #[cfg(test)]