2024-08-28 20:16:06 -04:00
|
|
|
use core::marker::PhantomData;
|
|
|
|
|
|
2024-08-23 22:29:15 -04:00
|
|
|
use scale::Encode;
|
2024-08-26 22:49:57 -04:00
|
|
|
use serai_db::{DbTxn, Db};
|
2024-08-20 16:24:18 -04:00
|
|
|
|
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
|
|
|
|
2024-08-29 00:01:31 -04:00
|
|
|
use primitives::task::ContinuallyRan;
|
2024-08-27 02:14:59 -04:00
|
|
|
use crate::{
|
2024-08-30 01:19:29 -04:00
|
|
|
db::{Returnable, ScannerGlobalDb, ScanToReportDb},
|
2024-08-28 19:00:02 -04:00
|
|
|
index,
|
|
|
|
|
scan::next_to_scan_for_outputs_block,
|
2024-08-29 00:01:31 -04:00
|
|
|
ScannerFeed, BatchPublisher,
|
2024-08-27 02:14:59 -04:00
|
|
|
};
|
2024-08-20 16:24:18 -04:00
|
|
|
|
2024-08-28 19:58:28 -04:00
|
|
|
mod db;
|
2024-08-30 01:19:29 -04:00
|
|
|
pub(crate) use db::ReturnInformation;
|
2024-08-28 19:58:28 -04:00
|
|
|
use db::ReportDb;
|
|
|
|
|
|
2024-08-30 01:19:29 -04:00
|
|
|
pub(crate) fn take_return_information<S: ScannerFeed>(
|
|
|
|
|
txn: &mut impl DbTxn,
|
|
|
|
|
id: u32,
|
|
|
|
|
) -> Option<Vec<Option<ReturnInformation<S>>>> {
|
|
|
|
|
ReportDb::<S>::take_return_information(txn, id)
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2024-08-28 20:16:06 -04:00
|
|
|
#[allow(non_snake_case)]
|
2024-08-29 00:01:31 -04:00
|
|
|
pub(crate) struct ReportTask<D: Db, S: ScannerFeed, B: BatchPublisher> {
|
2024-08-20 16:24:18 -04:00
|
|
|
db: D,
|
2024-08-29 00:01:31 -04:00
|
|
|
batch_publisher: B,
|
2024-08-28 20:16:06 -04:00
|
|
|
_S: PhantomData<S>,
|
2024-08-20 16:24:18 -04:00
|
|
|
}
|
|
|
|
|
|
2024-08-29 00:01:31 -04:00
|
|
|
impl<D: Db, S: ScannerFeed, B: BatchPublisher> ReportTask<D, S, B> {
|
|
|
|
|
pub(crate) fn new(mut db: D, batch_publisher: B, start_block: u64) -> Self {
|
2024-08-30 01:19:29 -04:00
|
|
|
if ReportDb::<S>::next_to_potentially_report_block(&db).is_none() {
|
2024-08-28 19:58:28 -04:00
|
|
|
// Initialize the DB
|
|
|
|
|
let mut txn = db.txn();
|
2024-08-30 01:19:29 -04:00
|
|
|
ReportDb::<S>::set_next_to_potentially_report_block(&mut txn, start_block);
|
2024-08-28 19:58:28 -04:00
|
|
|
txn.commit();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-29 00:01:31 -04:00
|
|
|
Self { db, batch_publisher, _S: PhantomData }
|
2024-08-28 19:58:28 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 16:24:18 -04:00
|
|
|
#[async_trait::async_trait]
|
2024-08-29 00:01:31 -04:00
|
|
|
impl<D: Db, S: ScannerFeed, B: BatchPublisher> ContinuallyRan for ReportTask<D, S, B> {
|
2024-08-20 16:24:18 -04:00
|
|
|
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-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");
|
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-30 01:19:29 -04:00
|
|
|
let next_to_potentially_report = ReportDb::<S>::next_to_potentially_report_block(&self.db)
|
2024-08-20 16:39:03 -04:00
|
|
|
.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-23 22:29:15 -04:00
|
|
|
let mut txn = self.db.txn();
|
|
|
|
|
|
2024-08-27 02:14:59 -04:00
|
|
|
// 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);
|
2024-08-28 19:58:28 -04:00
|
|
|
let notable = ScannerGlobalDb::<S>::is_block_notable(&txn, b);
|
2024-08-27 02:14:59 -04:00
|
|
|
if !notable {
|
|
|
|
|
assert!(in_instructions.is_empty(), "block wasn't notable yet had InInstructions");
|
|
|
|
|
}
|
2024-08-24 17:30:02 -04:00
|
|
|
// If this block is notable, create the Batch(s) for it
|
2024-08-27 02:14:59 -04:00
|
|
|
if notable {
|
2024-08-23 22:29:15 -04:00
|
|
|
let network = S::NETWORK;
|
2024-08-27 00:44:11 -04:00
|
|
|
let block_hash = index::block_id(&txn, b);
|
2024-08-30 01:19:29 -04:00
|
|
|
let mut batch_id = ReportDb::<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![] }];
|
2024-08-30 01:19:29 -04:00
|
|
|
// We also track the return information for the InInstructions within a Batch in case they
|
|
|
|
|
// error
|
|
|
|
|
let mut return_information = vec![vec![]];
|
|
|
|
|
|
|
|
|
|
for Returnable { return_address, in_instruction } in in_instructions {
|
|
|
|
|
let balance = in_instruction.balance;
|
2024-08-23 22:29:15 -04:00
|
|
|
|
|
|
|
|
let batch = batches.last_mut().unwrap();
|
2024-08-30 01:19:29 -04:00
|
|
|
batch.instructions.push(in_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
|
2024-08-30 01:19:29 -04:00
|
|
|
let in_instruction = batch.instructions.pop().unwrap();
|
2024-08-23 22:29:15 -04:00
|
|
|
|
|
|
|
|
// bump the id for the new batch
|
2024-08-30 01:19:29 -04:00
|
|
|
batch_id = ReportDb::<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),
|
2024-08-30 01:19:29 -04:00
|
|
|
instructions: vec![in_instruction],
|
2024-08-23 22:29:15 -04:00
|
|
|
});
|
2024-08-30 01:19:29 -04:00
|
|
|
// Since we're allocating a new batch, allocate a new set of return addresses for it
|
|
|
|
|
return_information.push(vec![]);
|
2024-08-23 22:29:15 -04:00
|
|
|
}
|
2024-08-30 01:19:29 -04:00
|
|
|
|
|
|
|
|
// For the set of return addresses for the InInstructions for the batch we just pushed
|
|
|
|
|
// onto, push this InInstruction's return addresses
|
|
|
|
|
return_information
|
|
|
|
|
.last_mut()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.push(return_address.map(|address| ReturnInformation { address, balance }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save the return addresses to the databse
|
|
|
|
|
assert_eq!(batches.len(), return_information.len());
|
|
|
|
|
for (batch, return_information) in batches.iter().zip(&return_information) {
|
|
|
|
|
assert_eq!(batch.instructions.len(), return_information.len());
|
|
|
|
|
ReportDb::<S>::save_return_information(&mut txn, batch.id, return_information);
|
2024-08-23 22:29:15 -04:00
|
|
|
}
|
|
|
|
|
|
2024-08-29 00:01:31 -04:00
|
|
|
for batch in batches {
|
|
|
|
|
self
|
|
|
|
|
.batch_publisher
|
|
|
|
|
.publish_batch(batch)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| format!("failed to publish batch: {e:?}"))?;
|
|
|
|
|
}
|
2024-08-20 16:24:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the next to potentially report block
|
2024-08-30 01:19:29 -04:00
|
|
|
ReportDb::<S>::set_next_to_potentially_report_block(&mut txn, b + 1);
|
2024-08-23 22:29:15 -04:00
|
|
|
|
2024-08-20 16:24:18 -04:00
|
|
|
txn.commit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run dependents if we decided to report any blocks
|
|
|
|
|
Ok(next_to_potentially_report <= highest_reportable)
|
|
|
|
|
}
|
|
|
|
|
}
|