2023-04-20 09:05:17 +00:00
|
|
|
#![allow(dead_code)]
|
2023-04-15 21:38:47 +00:00
|
|
|
#![allow(unused_variables)]
|
2023-04-16 07:16:53 +00:00
|
|
|
#![allow(unreachable_code)]
|
|
|
|
#![allow(clippy::diverging_sub_expression)]
|
2023-04-15 21:38:47 +00:00
|
|
|
|
2023-04-23 07:48:50 +00:00
|
|
|
use std::{
|
|
|
|
sync::Arc,
|
|
|
|
time::Duration,
|
|
|
|
collections::{VecDeque, HashMap},
|
|
|
|
};
|
2023-04-17 04:50:56 +00:00
|
|
|
|
2023-04-16 07:16:53 +00:00
|
|
|
use zeroize::Zeroizing;
|
|
|
|
|
|
|
|
use ciphersuite::{group::ff::Field, Ciphersuite, Ristretto};
|
2023-04-15 21:38:47 +00:00
|
|
|
|
2023-04-16 07:16:53 +00:00
|
|
|
use serai_db::{Db, MemDb};
|
2023-04-15 21:38:47 +00:00
|
|
|
use serai_client::Serai;
|
|
|
|
|
2023-04-23 07:48:50 +00:00
|
|
|
use tokio::{sync::RwLock, time::sleep};
|
|
|
|
|
|
|
|
use ::tributary::Tributary;
|
2023-04-17 04:50:56 +00:00
|
|
|
|
2023-04-20 09:05:17 +00:00
|
|
|
mod tributary;
|
2023-04-23 07:48:50 +00:00
|
|
|
use crate::tributary::{TributarySpec, Transaction};
|
2023-04-16 04:51:56 +00:00
|
|
|
|
2023-04-23 08:31:00 +00:00
|
|
|
mod db;
|
|
|
|
use db::MainDb;
|
|
|
|
|
2023-04-16 04:51:56 +00:00
|
|
|
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;
|
2023-04-11 23:04:53 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2023-04-23 02:27:12 +00:00
|
|
|
pub mod tests;
|
2023-04-11 23:04:53 +00:00
|
|
|
|
2023-04-23 07:48:50 +00:00
|
|
|
// This is a static to satisfy lifetime expectations
|
|
|
|
lazy_static::lazy_static! {
|
2023-04-23 22:29:50 +00:00
|
|
|
static ref NEW_TRIBUTARIES: RwLock<VecDeque<TributarySpec>> = RwLock::new(VecDeque::new());
|
2023-04-23 07:48:50 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2023-04-16 07:16:53 +00:00
|
|
|
key: Zeroizing<<Ristretto as Ciphersuite>::F>,
|
|
|
|
p2p: P,
|
2023-04-17 06:10:33 +00:00
|
|
|
mut processor: Pro,
|
2023-04-16 07:16:53 +00:00
|
|
|
serai: Serai,
|
|
|
|
) {
|
2023-04-23 08:31:00 +00:00
|
|
|
let add_new_tributary = |db, spec: TributarySpec| async {
|
2023-04-23 20:56:23 +00:00
|
|
|
// Save it to the database
|
2023-04-23 08:31:00 +00:00
|
|
|
MainDb(db).add_active_tributary(&spec);
|
2023-04-23 20:56:23 +00:00
|
|
|
// Add it to the queue
|
|
|
|
// If we reboot before this is read from the queue, the fact it was saved to the database
|
|
|
|
// means it'll be handled on reboot
|
2023-04-23 07:48:50 +00:00
|
|
|
NEW_TRIBUTARIES.write().await.push_back(spec);
|
|
|
|
};
|
2023-04-20 09:05:17 +00:00
|
|
|
|
2023-04-23 20:56:23 +00:00
|
|
|
// Handle new Substrate blocks
|
2023-04-20 09:05:17 +00:00
|
|
|
{
|
2023-04-23 07:48:50 +00:00
|
|
|
let mut substrate_db = substrate::SubstrateDb::new(raw_db.clone());
|
|
|
|
let mut last_substrate_block = substrate_db.last_block();
|
|
|
|
|
2023-04-20 09:05:17 +00: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 07:48:50 +00:00
|
|
|
add_new_tributary,
|
2023-04-20 09:05:17 +00:00
|
|
|
&mut processor,
|
|
|
|
&serai,
|
|
|
|
&mut last_substrate_block,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
2023-04-23 22:29:50 +00:00
|
|
|
// TODO: Should this use a notification system for new blocks?
|
|
|
|
// Right now it's sleeping for half the block time.
|
2023-04-20 09:05:17 +00:00
|
|
|
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-23 20:56:23 +00:00
|
|
|
// Handle the Tributaries
|
2023-04-20 09:05:17 +00:00
|
|
|
{
|
2023-04-23 07:48:50 +00:00
|
|
|
struct ActiveTributary<D: Db, P: P2p> {
|
|
|
|
spec: TributarySpec,
|
2023-04-23 22:29:50 +00:00
|
|
|
tributary: Arc<RwLock<Tributary<D, Transaction, P>>>,
|
2023-04-23 07:48:50 +00:00
|
|
|
}
|
2023-04-23 22:29:50 +00:00
|
|
|
|
|
|
|
// Arc so this can be shared between the Tributary scanner task and the P2P task
|
|
|
|
// Write locks on this may take a while to acquire
|
2023-04-23 20:56:23 +00:00
|
|
|
let tributaries = Arc::new(RwLock::new(HashMap::<[u8; 32], ActiveTributary<D, P>>::new()));
|
2023-04-23 07:48:50 +00:00
|
|
|
|
|
|
|
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(
|
2023-04-23 20:56:23 +00:00
|
|
|
// TODO: Use a db on a distinct volume
|
2023-04-23 07:48:50 +00:00
|
|
|
db,
|
|
|
|
spec.genesis(),
|
|
|
|
spec.start_time(),
|
|
|
|
key,
|
|
|
|
spec.validators(),
|
|
|
|
p2p,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2023-04-23 22:29:50 +00:00
|
|
|
tributaries.insert(
|
|
|
|
tributary.genesis(),
|
|
|
|
ActiveTributary { spec, tributary: Arc::new(RwLock::new(tributary)) },
|
|
|
|
);
|
2023-04-23 07:48:50 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 20:56:23 +00:00
|
|
|
// Reload active tributaries from the database
|
2023-04-23 08:31:00 +00:00
|
|
|
// TODO: Can MainDb take a borrow?
|
|
|
|
for spec in MainDb(raw_db.clone()).active_tributaries().1 {
|
2023-04-23 20:56:23 +00:00
|
|
|
add_tributary(
|
|
|
|
raw_db.clone(),
|
|
|
|
key.clone(),
|
|
|
|
p2p.clone(),
|
|
|
|
&mut *tributaries.write().await,
|
|
|
|
spec,
|
|
|
|
)
|
|
|
|
.await;
|
2023-04-23 08:31:00 +00:00
|
|
|
}
|
2023-04-23 07:48:50 +00:00
|
|
|
|
2023-04-23 20:56:23 +00:00
|
|
|
// Handle new Tributary blocks
|
2023-04-23 07:48:50 +00:00
|
|
|
let mut tributary_db = tributary::TributaryDb::new(raw_db.clone());
|
2023-04-23 20:56:23 +00:00
|
|
|
{
|
|
|
|
let tributaries = tributaries.clone();
|
|
|
|
let p2p = p2p.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
loop {
|
|
|
|
// The following handle_new_blocks function may take an arbitrary amount of time
|
2023-04-23 22:29:50 +00:00
|
|
|
// Accordingly, it may take a long time to acquire a write lock on the tributaries table
|
|
|
|
// By definition of NEW_TRIBUTARIES, we allow tributaries to be added almost immediately,
|
|
|
|
// meaning the Substrate scanner won't become blocked on this
|
2023-04-23 20:56:23 +00:00
|
|
|
{
|
|
|
|
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(),
|
|
|
|
// This is a short-lived write acquisition, which is why it should be fine
|
|
|
|
&mut *tributaries.write().await,
|
|
|
|
spec,
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
}
|
2023-04-23 07:48:50 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 22:29:50 +00:00
|
|
|
// TODO: Instead of holding this lock long term, should this take in Arc RwLock and
|
|
|
|
// re-acquire read locks?
|
2023-04-23 20:56:23 +00:00
|
|
|
for ActiveTributary { spec, tributary } in tributaries.read().await.values() {
|
|
|
|
tributary::scanner::handle_new_blocks::<_, _, P>(
|
|
|
|
&mut tributary_db,
|
|
|
|
&key,
|
|
|
|
&mut processor,
|
|
|
|
spec,
|
2023-04-23 22:29:50 +00:00
|
|
|
&*tributary.read().await,
|
2023-04-23 20:56:23 +00:00
|
|
|
)
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
2023-04-23 22:29:50 +00:00
|
|
|
// Sleep for half the block time
|
|
|
|
// TODO: Should we define a notification system for when a new block occurs?
|
|
|
|
sleep(Duration::from_secs(Tributary::<D, Transaction, P>::block_time() / 2)).await;
|
2023-04-17 04:50:56 +00:00
|
|
|
}
|
2023-04-23 20:56:23 +00:00
|
|
|
});
|
|
|
|
}
|
2023-04-23 07:48:50 +00:00
|
|
|
|
2023-04-23 20:56:23 +00:00
|
|
|
// Handle P2P messages
|
|
|
|
{
|
|
|
|
tokio::spawn(async move {
|
|
|
|
loop {
|
|
|
|
let msg = p2p.receive().await;
|
|
|
|
match msg.kind {
|
|
|
|
P2pMessageKind::Tributary(genesis) => {
|
|
|
|
let tributaries_read = tributaries.read().await;
|
|
|
|
let Some(tributary) = tributaries_read.get(&genesis) else {
|
|
|
|
log::debug!("received p2p message for unknown network");
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2023-04-23 22:29:50 +00:00
|
|
|
// This is misleading being read, as it will mutate the Tributary, yet there's
|
|
|
|
// greater efficiency when it is read
|
|
|
|
// The safety of it is also justified by Tributary::handle_message's documentation
|
|
|
|
if tributary.tributary.read().await.handle_message(&msg.msg).await {
|
2023-04-23 20:56:23 +00:00
|
|
|
P2p::broadcast(&p2p, msg.kind, msg.msg).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-04-20 09:05:17 +00:00
|
|
|
}
|
2023-04-15 21:38:47 +00:00
|
|
|
|
2023-04-17 04:50:56 +00:00
|
|
|
loop {
|
2023-04-15 21:38:47 +00:00
|
|
|
// Handle all messages from processors
|
2023-04-17 04:50:56 +00:00
|
|
|
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() {
|
2023-04-16 04:51:56 +00:00
|
|
|
let db = MemDb::new(); // TODO
|
2023-04-17 06:10:33 +00:00
|
|
|
|
2023-04-16 07:16:53 +00:00
|
|
|
let key = Zeroizing::new(<Ristretto as Ciphersuite>::F::ZERO); // TODO
|
2023-04-22 14:49:52 +00:00
|
|
|
let p2p = LocalP2p::new(1).swap_remove(0); // TODO
|
2023-04-17 06:10:33 +00:00
|
|
|
|
|
|
|
let processor = processor::MemProcessor::new(); // TODO
|
|
|
|
|
2023-04-16 04:51:56 +00: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 06:10:33 +00:00
|
|
|
sleep(Duration::from_secs(5)).await;
|
2023-04-16 04:51:56 +00:00
|
|
|
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
|
|
|
}
|