serai/coordinator/src/main.rs

113 lines
2.4 KiB
Rust
Raw Normal View History

2023-04-20 09:05:17 +00:00
#![allow(dead_code)]
2023-04-15 21:38:47 +00:00
#![allow(unused_variables)]
#![allow(unreachable_code)]
#![allow(clippy::diverging_sub_expression)]
2023-04-15 21:38:47 +00:00
2023-04-20 09:05:17 +00:00
use std::{time::Duration, collections::HashMap};
use zeroize::Zeroizing;
use ciphersuite::{group::ff::Field, Ciphersuite, Ristretto};
2023-04-15 21:38:47 +00:00
use serai_db::{Db, MemDb};
2023-04-15 21:38:47 +00:00
use serai_client::Serai;
use tokio::time::sleep;
2023-04-20 09:05:17 +00:00
mod tributary;
mod p2p;
pub use p2p::*;
2023-04-17 06:10:33 +00:00
pub mod processor;
use processor::Processor;
2023-04-15 21:38:47 +00:00
mod substrate;
#[cfg(test)]
mod tests;
2023-04-17 06:10:33 +00:00
async fn run<D: Db, Pro: Processor, P: P2p>(
2023-04-20 09:05:17 +00:00
raw_db: D,
key: Zeroizing<<Ristretto as Ciphersuite>::F>,
p2p: P,
2023-04-17 06:10:33 +00:00
mut processor: Pro,
serai: Serai,
) {
2023-04-20 09:05:17 +00:00
let mut substrate_db = substrate::SubstrateDb::new(raw_db.clone());
let mut last_substrate_block = substrate_db.last_block();
let mut last_tributary_block = HashMap::<[u8; 32], _>::new();
{
let key = key.clone();
let mut processor = processor.clone();
tokio::spawn(async move {
loop {
match substrate::handle_new_blocks(
&mut substrate_db,
&key,
&p2p,
&mut processor,
&serai,
&mut last_substrate_block,
)
.await
{
Ok(()) => sleep(Duration::from_secs(3)).await,
Err(e) => {
log::error!("couldn't communicate with serai node: {e}");
sleep(Duration::from_secs(5)).await;
}
}
}
});
}
2023-04-15 21:38:47 +00:00
2023-04-20 09:05:17 +00:00
{
let mut tributary_db = tributary::TributaryDb::new(raw_db);
tokio::spawn(async move {
loop {
for (_, last_block) in last_tributary_block.iter_mut() {
tributary::scanner::handle_new_blocks::<_, _, P>(
&mut tributary_db,
&key,
&mut processor,
todo!(),
todo!(),
last_block,
)
.await;
}
2023-04-20 09:05:17 +00:00
sleep(Duration::from_secs(3)).await;
}
2023-04-20 09:05:17 +00:00
});
}
2023-04-15 21:38:47 +00:00
loop {
2023-04-15 21:38:47 +00:00
// Handle all messages from processors
todo!()
2023-04-15 21:38:47 +00:00
}
}
2023-04-11 13:21:35 +00:00
#[tokio::main]
2023-04-15 21:38:47 +00:00
async fn main() {
let db = MemDb::new(); // TODO
2023-04-17 06:10:33 +00:00
let key = Zeroizing::new(<Ristretto as Ciphersuite>::F::ZERO); // TODO
let p2p = LocalP2p {}; // TODO
2023-04-17 06:10:33 +00:00
let processor = processor::MemProcessor::new(); // TODO
let serai = || async {
loop {
let Ok(serai) = Serai::new("ws://127.0.0.1:9944").await else {
log::error!("couldn't connect to the Serai node");
2023-04-17 06:10:33 +00:00
sleep(Duration::from_secs(5)).await;
continue
};
return serai;
}
};
2023-04-17 06:10:33 +00:00
run(db, key, p2p, processor, serai().await).await
2023-04-15 21:38:47 +00:00
}