Skip to content

Commit

Permalink
Make monero-serai Block::number not panic on invalid blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
kayabaNerve committed Jan 6, 2024
1 parent e7b0ed3 commit 1ba2d8d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
6 changes: 3 additions & 3 deletions coins/monero/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ pub struct Block {
}

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

Expand Down
23 changes: 14 additions & 9 deletions processor/src/networks/monero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ impl BlockTrait<Monero> for Block {
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 (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
return self.number().try_into().unwrap();
return self.number().unwrap();
}

let mut timestamps = vec![self.header.timestamp];
Expand All @@ -194,7 +194,7 @@ impl BlockTrait<Monero> for Block {
timestamps.push(parent_block.header.timestamp);
parent = parent_block.parent();

if parent_block.number() == 0 {
if parent_block.number().unwrap() == 0 {
break;
}
}
Expand All @@ -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
// it advances the median forward
// 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()
}
}

Expand Down Expand Up @@ -585,16 +585,21 @@ impl Network for Monero {

if let Some((_, eventuality)) = eventualities.map.get(&tx.prefix.extra) {
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;
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 mut block;
while {
Expand All @@ -612,7 +617,7 @@ impl Network for Monero {

// Also check the current block
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
}
Expand Down Expand Up @@ -687,7 +692,7 @@ impl Network for Monero {

#[cfg(test)]
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)]
Expand Down

0 comments on commit 1ba2d8d

Please sign in to comment.