From 9676584ffeb0f20336c7a91f12e395fa6e2e39fd Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sun, 16 Apr 2023 01:03:32 -0400 Subject: [PATCH] Resolve #245 --- processor/src/coins/bitcoin.rs | 7 +++++++ processor/src/coins/mod.rs | 1 + processor/src/coins/monero.rs | 4 ++++ processor/src/scanner.rs | 12 +++++++++++- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/processor/src/coins/bitcoin.rs b/processor/src/coins/bitcoin.rs index 2940833e..e491c92f 100644 --- a/processor/src/coins/bitcoin.rs +++ b/processor/src/coins/bitcoin.rs @@ -197,6 +197,13 @@ impl BlockTrait for Block { hash.reverse(); hash } + + fn parent(&self) -> Self::Id { + let mut hash = *self.header.prev_blockhash.as_raw_hash().as_byte_array(); + hash.reverse(); + hash + } + fn median_fee(&self) -> Fee { // TODO Fee(20) diff --git a/processor/src/coins/mod.rs b/processor/src/coins/mod.rs index f9ff6e43..1f4d212a 100644 --- a/processor/src/coins/mod.rs +++ b/processor/src/coins/mod.rs @@ -175,6 +175,7 @@ pub trait Block: Send + Sync + Sized + Clone + Debug { // This is currently bounded to being 32-bytes. type Id: 'static + Id; fn id(&self) -> Self::Id; + fn parent(&self) -> Self::Id; fn median_fee(&self) -> C::Fee; } diff --git a/processor/src/coins/monero.rs b/processor/src/coins/monero.rs index 9f48fcb6..4d4b8531 100644 --- a/processor/src/coins/monero.rs +++ b/processor/src/coins/monero.rs @@ -142,6 +142,10 @@ impl BlockTrait for Block { self.0 } + fn parent(&self) -> Self::Id { + self.1.header.previous + } + fn median_fee(&self) -> Fee { // TODO Fee { per_weight: 80000, mask: 10000 } diff --git a/processor/src/scanner.rs b/processor/src/scanner.rs index 1d766226..867e6cf2 100644 --- a/processor/src/scanner.rs +++ b/processor/src/scanner.rs @@ -395,12 +395,22 @@ impl Scanner { let block_id = block.id(); if let Some(id) = ScannerDb::::block(&scanner.db.0, i) { - // TODO2: Also check this block builds off the previous block if id != block_id { panic!("reorg'd from finalized {} to {}", hex::encode(id), hex::encode(block_id)); } } else { info!("Found new block: {}", hex::encode(&block_id)); + + if let Some(id) = ScannerDb::::block(&scanner.db.0, i.saturating_sub(1)) { + if id != block.parent() { + panic!( + "block {} doesn't build off expected parent {}", + hex::encode(block_id), + hex::encode(id), + ); + } + } + let mut txn = scanner.db.0.txn(); ScannerDb::::save_block(&mut txn, i, &block_id); txn.commit();