2024-08-20 16:24:18 -04:00
|
|
|
/*
|
|
|
|
|
We only report blocks once both tasks, scanning for received ouputs and eventualities, have
|
|
|
|
|
processed the block. This ensures we've performed all ncessary options.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use serai_db::{Db, DbTxn};
|
|
|
|
|
|
2024-08-23 21:21:02 -04:00
|
|
|
use primitives::{Id, OutputType, Block};
|
2024-08-20 16:24:18 -04:00
|
|
|
|
|
|
|
|
// TODO: Localize to ReportDb?
|
2024-08-20 16:39:03 -04:00
|
|
|
use crate::{db::ScannerDb, ScannerFeed, ContinuallyRan};
|
2024-08-20 16:24:18 -04:00
|
|
|
|
|
|
|
|
struct ReportTask<D: Db, S: ScannerFeed> {
|
|
|
|
|
db: D,
|
|
|
|
|
feed: S,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
|
impl<D: Db, S: ScannerFeed> ContinuallyRan for ReportTask<D, S> {
|
|
|
|
|
async fn run_iteration(&mut self) -> Result<bool, String> {
|
|
|
|
|
let highest_reportable = {
|
2024-08-20 19:37:47 -04:00
|
|
|
// Fetch the next to scan block
|
2024-08-20 16:39:03 -04:00
|
|
|
let next_to_scan = ScannerDb::<S>::next_to_scan_for_outputs_block(&self.db)
|
|
|
|
|
.expect("ReportTask run before writing the start block");
|
2024-08-20 16:24:18 -04:00
|
|
|
// If we haven't done any work, return
|
2024-08-20 19:37:47 -04:00
|
|
|
if next_to_scan == 0 {
|
2024-08-20 16:24:18 -04:00
|
|
|
return Ok(false);
|
|
|
|
|
}
|
2024-08-20 19:37:47 -04:00
|
|
|
// The last scanned block is the block prior to this
|
|
|
|
|
#[allow(clippy::let_and_return)]
|
2024-08-20 16:24:18 -04:00
|
|
|
let last_scanned = next_to_scan - 1;
|
2024-08-20 19:37:47 -04:00
|
|
|
// The last scanned block is the highest reportable block as we only scan blocks within a
|
|
|
|
|
// window where it's safe to immediately report the block
|
|
|
|
|
// See `eventuality.rs` for more info
|
|
|
|
|
last_scanned
|
2024-08-20 16:24:18 -04:00
|
|
|
};
|
|
|
|
|
|
2024-08-20 16:39:03 -04:00
|
|
|
let next_to_potentially_report = ScannerDb::<S>::next_to_potentially_report_block(&self.db)
|
|
|
|
|
.expect("ReportTask run before writing the start block");
|
2024-08-20 16:24:18 -04:00
|
|
|
|
|
|
|
|
for b in next_to_potentially_report ..= highest_reportable {
|
2024-08-20 16:39:03 -04:00
|
|
|
if ScannerDb::<S>::is_block_notable(&self.db, b) {
|
2024-08-23 21:21:02 -04:00
|
|
|
let in_instructions = todo!("TODO");
|
|
|
|
|
// TODO: Also pull the InInstructions from forwarding
|
2024-08-20 16:24:18 -04:00
|
|
|
todo!("TODO: Make Batches, which requires handling Forwarded within this crate");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut txn = self.db.txn();
|
|
|
|
|
// Update the next to potentially report block
|
|
|
|
|
ScannerDb::<S>::set_next_to_potentially_report_block(&mut txn, b + 1);
|
|
|
|
|
txn.commit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run dependents if we decided to report any blocks
|
|
|
|
|
Ok(next_to_potentially_report <= highest_reportable)
|
|
|
|
|
}
|
|
|
|
|
}
|