Files
serai/processor/scanner/src/report/mod.rs

105 lines
3.6 KiB
Rust
Raw Normal View History

2024-08-23 22:29:15 -04:00
use scale::Encode;
use serai_db::{DbTxn, Db};
2024-08-23 22:29:15 -04:00
use serai_primitives::BlockHash;
use serai_in_instructions_primitives::{MAX_BATCH_SIZE, Batch};
2024-08-24 23:43:31 -04:00
// TODO: Localize to Report?
use crate::{
db::{ScannerDb, ScanToReportDb},
2024-08-28 19:00:02 -04:00
index,
scan::next_to_scan_for_outputs_block,
ScannerFeed, ContinuallyRan,
};
2024-08-23 22:29:15 -04:00
/*
This task produces Batches for notable blocks, with all InInstructions, in an ordered fashion.
We only report blocks once both tasks, scanning for received outputs and checking for resolved
Eventualities, have processed the block. This ensures we know if this block is notable, and have
the InInstructions for it.
*/
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 = {
// Fetch the next to scan block
2024-08-28 19:00:02 -04:00
let next_to_scan = next_to_scan_for_outputs_block::<S>(&self.db)
2024-08-20 16:39:03 -04:00
.expect("ReportTask run before writing the start block");
// If we haven't done any work, return
if next_to_scan == 0 {
return Ok(false);
}
// The last scanned block is the block prior to this
#[allow(clippy::let_and_return)]
let last_scanned = next_to_scan - 1;
// 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: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");
for b in next_to_potentially_report ..= highest_reportable {
2024-08-23 22:29:15 -04:00
let mut txn = self.db.txn();
// Receive the InInstructions for this block
// We always do this as we can't trivially tell if we should recv InInstructions before we do
let in_instructions = ScanToReportDb::<S>::recv_in_instructions(&mut txn, b);
let notable = ScannerDb::<S>::is_block_notable(&txn, b);
if !notable {
assert!(in_instructions.is_empty(), "block wasn't notable yet had InInstructions");
}
// If this block is notable, create the Batch(s) for it
if notable {
2024-08-23 22:29:15 -04:00
let network = S::NETWORK;
let block_hash = index::block_id(&txn, b);
let mut batch_id = ScannerDb::<S>::acquire_batch_id(&mut txn);
2024-08-23 22:29:15 -04:00
// start with empty batch
let mut batches =
vec![Batch { network, id: batch_id, block: BlockHash(block_hash), instructions: vec![] }];
for instruction in in_instructions {
let batch = batches.last_mut().unwrap();
batch.instructions.push(instruction);
2024-08-23 22:29:15 -04:00
// check if batch is over-size
if batch.encode().len() > MAX_BATCH_SIZE {
// pop the last instruction so it's back in size
let instruction = batch.instructions.pop().unwrap();
// bump the id for the new batch
batch_id = ScannerDb::<S>::acquire_batch_id(&mut txn);
2024-08-23 22:29:15 -04:00
// make a new batch with this instruction included
batches.push(Batch {
network,
id: batch_id,
block: BlockHash(block_hash),
instructions: vec![instruction],
});
}
}
todo!("TODO: Set/emit batches");
}
// Update the next to potentially report block
ScannerDb::<S>::set_next_to_potentially_report_block(&mut txn, b + 1);
2024-08-23 22:29:15 -04:00
txn.commit();
}
// Run dependents if we decided to report any blocks
Ok(next_to_potentially_report <= highest_reportable)
}
}