2023-04-20 05:05:17 -04:00
|
|
|
#![allow(dead_code)]
|
2023-04-15 17:38:47 -04:00
|
|
|
#![allow(unused_variables)]
|
2023-04-16 03:16:53 -04:00
|
|
|
#![allow(unreachable_code)]
|
|
|
|
|
#![allow(clippy::diverging_sub_expression)]
|
2023-04-15 17:38:47 -04:00
|
|
|
|
2023-04-23 03:48:50 -04:00
|
|
|
use std::{
|
|
|
|
|
sync::Arc,
|
|
|
|
|
time::Duration,
|
|
|
|
|
collections::{VecDeque, HashMap},
|
|
|
|
|
};
|
2023-04-17 00:50:56 -04:00
|
|
|
|
2023-04-16 03:16:53 -04:00
|
|
|
use zeroize::Zeroizing;
|
|
|
|
|
|
|
|
|
|
use ciphersuite::{group::ff::Field, Ciphersuite, Ristretto};
|
2023-04-15 17:38:47 -04:00
|
|
|
|
2023-04-16 03:16:53 -04:00
|
|
|
use serai_db::{Db, MemDb};
|
2023-04-15 17:38:47 -04:00
|
|
|
use serai_client::Serai;
|
|
|
|
|
|
2023-04-23 03:48:50 -04:00
|
|
|
use tokio::{sync::RwLock, time::sleep};
|
|
|
|
|
|
|
|
|
|
use ::tributary::Tributary;
|
2023-04-17 00:50:56 -04:00
|
|
|
|
2023-04-20 05:05:17 -04:00
|
|
|
mod tributary;
|
2023-04-23 03:48:50 -04:00
|
|
|
use crate::tributary::{TributarySpec, Transaction};
|
2023-04-16 00:51:56 -04:00
|
|
|
|
|
|
|
|
mod p2p;
|
|
|
|
|
pub use p2p::*;
|
|
|
|
|
|
2023-04-17 02:10:33 -04:00
|
|
|
pub mod processor;
|
|
|
|
|
use processor::Processor;
|
|
|
|
|
|
2023-04-15 17:38:47 -04:00
|
|
|
mod substrate;
|
2023-04-11 19:04:53 -04:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2023-04-22 22:27:12 -04:00
|
|
|
pub mod tests;
|
2023-04-11 19:04:53 -04:00
|
|
|
|
2023-04-23 03:48:50 -04:00
|
|
|
// This is a static to satisfy lifetime expectations
|
|
|
|
|
lazy_static::lazy_static! {
|
|
|
|
|
static ref NEW_TRIBUTARIES: Arc<RwLock<VecDeque<TributarySpec>>> = Arc::new(
|
|
|
|
|
RwLock::new(VecDeque::new())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 02:10:33 -04:00
|
|
|
async fn run<D: Db, Pro: Processor, P: P2p>(
|
2023-04-20 05:05:17 -04:00
|
|
|
raw_db: D,
|
2023-04-16 03:16:53 -04:00
|
|
|
key: Zeroizing<<Ristretto as Ciphersuite>::F>,
|
|
|
|
|
p2p: P,
|
2023-04-17 02:10:33 -04:00
|
|
|
mut processor: Pro,
|
2023-04-16 03:16:53 -04:00
|
|
|
serai: Serai,
|
|
|
|
|
) {
|
2023-04-23 03:48:50 -04:00
|
|
|
let add_new_tributary = |spec: TributarySpec| async {
|
|
|
|
|
NEW_TRIBUTARIES.write().await.push_back(spec);
|
|
|
|
|
// TODO: Save this tributary's information to the databae before returning
|
|
|
|
|
};
|
2023-04-20 05:05:17 -04:00
|
|
|
|
|
|
|
|
{
|
2023-04-23 03:48:50 -04:00
|
|
|
let mut substrate_db = substrate::SubstrateDb::new(raw_db.clone());
|
|
|
|
|
let mut last_substrate_block = substrate_db.last_block();
|
|
|
|
|
|
|
|
|
|
let p2p = p2p.clone();
|
|
|
|
|
|
2023-04-20 05:05:17 -04:00
|
|
|
let key = key.clone();
|
|
|
|
|
let mut processor = processor.clone();
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
loop {
|
|
|
|
|
match substrate::handle_new_blocks(
|
|
|
|
|
&mut substrate_db,
|
|
|
|
|
&key,
|
2023-04-23 03:48:50 -04:00
|
|
|
add_new_tributary,
|
2023-04-20 05:05:17 -04:00
|
|
|
&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 17:38:47 -04:00
|
|
|
|
2023-04-20 05:05:17 -04:00
|
|
|
{
|
2023-04-23 03:48:50 -04:00
|
|
|
struct ActiveTributary<D: Db, P: P2p> {
|
|
|
|
|
spec: TributarySpec,
|
|
|
|
|
tributary: Tributary<D, Transaction, P>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut tributaries = HashMap::<[u8; 32], ActiveTributary<D, P>>::new();
|
|
|
|
|
|
|
|
|
|
async fn add_tributary<D: Db, P: P2p>(
|
|
|
|
|
db: D,
|
|
|
|
|
key: Zeroizing<<Ristretto as Ciphersuite>::F>,
|
|
|
|
|
p2p: P,
|
|
|
|
|
tributaries: &mut HashMap<[u8; 32], ActiveTributary<D, P>>,
|
|
|
|
|
spec: TributarySpec,
|
|
|
|
|
) {
|
|
|
|
|
let tributary = Tributary::<_, Transaction, _>::new(
|
|
|
|
|
db,
|
|
|
|
|
spec.genesis(),
|
|
|
|
|
spec.start_time(),
|
|
|
|
|
key,
|
|
|
|
|
spec.validators(),
|
|
|
|
|
p2p,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
tributaries.insert(tributary.genesis(), ActiveTributary { spec, tributary });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Reload tributaries
|
|
|
|
|
|
|
|
|
|
let mut tributary_db = tributary::TributaryDb::new(raw_db.clone());
|
2023-04-20 05:05:17 -04:00
|
|
|
tokio::spawn(async move {
|
|
|
|
|
loop {
|
2023-04-23 03:48:50 -04:00
|
|
|
// The following handle_new_blocks function may take an arbitrary amount of time
|
|
|
|
|
// If registering a new tributary waited for a lock on the tributaries table, the substrate
|
|
|
|
|
// scanner may wait on a lock for an arbitrary amount of time
|
|
|
|
|
// By instead using the distinct NEW_TRIBUTARIES, there should be minimal
|
|
|
|
|
// competition/blocking
|
|
|
|
|
{
|
|
|
|
|
let mut new_tributaries = NEW_TRIBUTARIES.write().await;
|
|
|
|
|
while let Some(spec) = new_tributaries.pop_front() {
|
|
|
|
|
add_tributary(raw_db.clone(), key.clone(), p2p.clone(), &mut tributaries, spec).await;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (genesis, ActiveTributary { spec, tributary }) in tributaries.iter_mut() {
|
2023-04-20 05:05:17 -04:00
|
|
|
tributary::scanner::handle_new_blocks::<_, _, P>(
|
|
|
|
|
&mut tributary_db,
|
|
|
|
|
&key,
|
|
|
|
|
&mut processor,
|
2023-04-23 03:48:50 -04:00
|
|
|
spec,
|
|
|
|
|
tributary,
|
2023-04-20 05:05:17 -04:00
|
|
|
)
|
|
|
|
|
.await;
|
2023-04-17 00:50:56 -04:00
|
|
|
}
|
2023-04-23 03:48:50 -04:00
|
|
|
|
2023-04-20 05:05:17 -04:00
|
|
|
sleep(Duration::from_secs(3)).await;
|
2023-04-17 00:50:56 -04:00
|
|
|
}
|
2023-04-20 05:05:17 -04:00
|
|
|
});
|
|
|
|
|
}
|
2023-04-15 17:38:47 -04:00
|
|
|
|
2023-04-17 00:50:56 -04:00
|
|
|
loop {
|
2023-04-15 17:38:47 -04:00
|
|
|
// Handle all messages from processors
|
2023-04-17 00:50:56 -04:00
|
|
|
todo!()
|
2023-04-15 17:38:47 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 09:21:35 -04:00
|
|
|
#[tokio::main]
|
2023-04-15 17:38:47 -04:00
|
|
|
async fn main() {
|
2023-04-16 00:51:56 -04:00
|
|
|
let db = MemDb::new(); // TODO
|
2023-04-17 02:10:33 -04:00
|
|
|
|
2023-04-16 03:16:53 -04:00
|
|
|
let key = Zeroizing::new(<Ristretto as Ciphersuite>::F::ZERO); // TODO
|
2023-04-22 10:49:52 -04:00
|
|
|
let p2p = LocalP2p::new(1).swap_remove(0); // TODO
|
2023-04-17 02:10:33 -04:00
|
|
|
|
|
|
|
|
let processor = processor::MemProcessor::new(); // TODO
|
|
|
|
|
|
2023-04-16 00:51:56 -04:00
|
|
|
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 02:10:33 -04:00
|
|
|
sleep(Duration::from_secs(5)).await;
|
2023-04-16 00:51:56 -04:00
|
|
|
continue
|
|
|
|
|
};
|
|
|
|
|
return serai;
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-04-17 02:10:33 -04:00
|
|
|
run(db, key, p2p, processor, serai().await).await
|
2023-04-15 17:38:47 -04:00
|
|
|
}
|