Don't expose IndexDb throughout the crate

This commit is contained in:
Luke Parker 2024-08-27 00:44:11 -04:00
parent 9ab8ba0215
commit 2bddf00222
5 changed files with 42 additions and 11 deletions

View file

@ -78,7 +78,7 @@ pub trait ContinuallyRan: Sized {
} }
} }
Err(e) => { Err(e) => {
log::debug!("{}", e); log::warn!("{}", e);
increase_sleep_before_next_task(&mut current_sleep_before_next_task); increase_sleep_before_next_task(&mut current_sleep_before_next_task);
} }
} }

View file

@ -143,9 +143,6 @@ impl<S: ScannerFeed> ScannerDb<S> {
"setting start block but prior set start block" "setting start block but prior set start block"
); );
crate::index::IndexDb::set_block(txn, start_block, id);
crate::index::IndexDb::set_latest_finalized_block(txn, start_block);
NextToScanForOutputsBlock::set(txn, &start_block); NextToScanForOutputsBlock::set(txn, &start_block);
// We can receive outputs in this block, but any descending transactions will be in the next // We can receive outputs in this block, but any descending transactions will be in the next
// block. This, with the check on-set, creates a bound that this value in the DB is non-zero. // block. This, with the check on-set, creates a bound that this value in the DB is non-zero.

View file

@ -1,11 +1,17 @@
use serai_db::{DbTxn, Db}; use serai_db::{Get, DbTxn, Db};
use primitives::{task::ContinuallyRan, BlockHeader}; use primitives::{task::ContinuallyRan, BlockHeader};
use crate::ScannerFeed; use crate::ScannerFeed;
mod db; mod db;
pub(crate) use db::IndexDb; use db::IndexDb;
/// Panics if an unindexed block's ID is requested.
pub(crate) fn block_id(getter: &impl Get, block_number: u64) -> [u8; 32] {
IndexDb::block_id(getter, block_number)
.unwrap_or_else(|| panic!("requested block ID for unindexed block {block_number}"))
}
/* /*
This processor should build its own index of the blockchain, yet only for finalized blocks which This processor should build its own index of the blockchain, yet only for finalized blocks which
@ -20,6 +26,36 @@ struct IndexFinalizedTask<D: Db, S: ScannerFeed> {
feed: S, feed: S,
} }
impl<D: Db, S: ScannerFeed> IndexFinalizedTask<D, S> {
pub(crate) async fn new(mut db: D, feed: S, start_block: u64) -> Self {
if IndexDb::block_id(&db, start_block).is_none() {
// Fetch the block for its ID
let block = {
let mut delay = Self::DELAY_BETWEEN_ITERATIONS;
loop {
match feed.unchecked_block_header_by_number(start_block).await {
Ok(block) => break block,
Err(e) => {
log::warn!("IndexFinalizedTask couldn't fetch start block {start_block}: {e:?}");
tokio::time::sleep(core::time::Duration::from_secs(delay)).await;
delay += Self::DELAY_BETWEEN_ITERATIONS;
delay = delay.min(Self::MAX_DELAY_BETWEEN_ITERATIONS);
}
};
}
};
// Initialize the DB
let mut txn = db.txn();
IndexDb::set_block(&mut txn, start_block, block.id());
IndexDb::set_latest_finalized_block(&mut txn, start_block);
txn.commit();
}
Self { db, feed }
}
}
#[async_trait::async_trait] #[async_trait::async_trait]
impl<D: Db, S: ScannerFeed> ContinuallyRan for IndexFinalizedTask<D, S> { impl<D: Db, S: ScannerFeed> ContinuallyRan for IndexFinalizedTask<D, S> {
async fn run_iteration(&mut self) -> Result<bool, String> { async fn run_iteration(&mut self) -> Result<bool, String> {

View file

@ -113,8 +113,7 @@ pub trait ScannerFeed: Send + Sync {
// Check the ID of this block is the expected ID // Check the ID of this block is the expected ID
{ {
let expected = crate::index::IndexDb::block_id(getter, number) let expected = crate::index::block_id(getter, number);
.expect("requested a block which wasn't indexed");
if block.id() != expected { if block.id() != expected {
panic!( panic!(
"finalized chain reorganized from {} to {} at {}", "finalized chain reorganized from {} to {} at {}",

View file

@ -7,7 +7,7 @@ use serai_in_instructions_primitives::{MAX_BATCH_SIZE, Batch};
use primitives::ReceivedOutput; use primitives::ReceivedOutput;
// TODO: Localize to ReportDb? // TODO: Localize to ReportDb?
use crate::{db::ScannerDb, index::IndexDb, ScannerFeed, ContinuallyRan}; use crate::{db::ScannerDb, index, ScannerFeed, ContinuallyRan};
/* /*
This task produces Batches for notable blocks, with all InInstructions, in an ordered fashion. This task produces Batches for notable blocks, with all InInstructions, in an ordered fashion.
@ -65,8 +65,7 @@ impl<D: Db, S: ScannerFeed> ContinuallyRan for ReportTask<D, S> {
}; };
let network = S::NETWORK; let network = S::NETWORK;
let block_hash = let block_hash = index::block_id(&txn, b);
IndexDb::block_id(&txn, b).expect("reporting block we didn't save the ID for");
let mut batch_id = ScannerDb::<S>::acquire_batch_id(&mut txn); let mut batch_id = ScannerDb::<S>::acquire_batch_id(&mut txn);
// start with empty batch // start with empty batch