serai-processor-bin

Moves the coordinator loop out of serai-bitcoin-processor, completing it.

Fixes a potential race condition in the message-queue regarding multiple
sockets sending messages at once.
This commit is contained in:
Luke Parker
2024-09-11 18:56:23 -04:00
parent fcd5fb85df
commit b6811f9015
22 changed files with 705 additions and 594 deletions

View File

@@ -70,6 +70,8 @@ impl<S: ScannerFeed> OutputWithInInstruction<S> {
create_db!(
ScannerGlobal {
StartBlock: () -> u64,
QueuedKey: <K: Encode>(key: K) -> (),
ActiveKeys: <K: Borshy>() -> Vec<SeraiKeyDbEntry<K>>,
@@ -106,8 +108,11 @@ create_db!(
pub(crate) struct ScannerGlobalDb<S: ScannerFeed>(PhantomData<S>);
impl<S: ScannerFeed> ScannerGlobalDb<S> {
pub(crate) fn has_any_key_been_queued(getter: &impl Get) -> bool {
ActiveKeys::<EncodableG<KeyFor<S>>>::get(getter).is_some()
pub(crate) fn start_block(getter: &impl Get) -> Option<u64> {
StartBlock::get(getter)
}
pub(crate) fn set_start_block(txn: &mut impl DbTxn, block: u64) {
StartBlock::set(txn, &block)
}
/// Queue a key.

View File

@@ -344,17 +344,10 @@ impl<S: ScannerFeed> Scanner<S> {
/// Create a new scanner.
///
/// This will begin its execution, spawning several asynchronous tasks.
pub async fn new<Sch: Scheduler<S>>(
mut db: impl Db,
feed: S,
start_block: u64,
start_key: KeyFor<S>,
) -> Self {
if !ScannerGlobalDb::<S>::has_any_key_been_queued(&db) {
let mut txn = db.txn();
ScannerGlobalDb::<S>::queue_key(&mut txn, start_block, start_key);
txn.commit();
}
///
/// This will return None if the Scanner was never initialized.
pub async fn new<Sch: Scheduler<S>>(db: impl Db, feed: S) -> Option<Self> {
let start_block = ScannerGlobalDb::<S>::start_block(&db)?;
let index_task = index::IndexTask::new(db.clone(), feed.clone(), start_block).await;
let scan_task = scan::ScanTask::new(db.clone(), feed.clone(), start_block);
@@ -381,7 +374,28 @@ impl<S: ScannerFeed> Scanner<S> {
// window its allowed to scan
tokio::spawn(eventuality_task.continually_run(eventuality_task_def, vec![scan_handle]));
Self { substrate_handle, _S: PhantomData }
Some(Self { substrate_handle, _S: PhantomData })
}
/// Initialize the scanner.
///
/// This will begin its execution, spawning several asynchronous tasks.
///
/// This passes through to `Scanner::new` if prior called.
pub async fn initialize<Sch: Scheduler<S>>(
mut db: impl Db,
feed: S,
start_block: u64,
start_key: KeyFor<S>,
) -> Self {
if ScannerGlobalDb::<S>::start_block(&db).is_none() {
let mut txn = db.txn();
ScannerGlobalDb::<S>::set_start_block(&mut txn, start_block);
ScannerGlobalDb::<S>::queue_key(&mut txn, start_block, start_key);
txn.commit();
}
Self::new::<Sch>(db, feed).await.unwrap()
}
/// Acknowledge a Batch having been published on Serai.