mirror of
https://github.com/serai-dex/serai.git
synced 2025-01-22 18:54:40 +00:00
Add a next block notification system to Tributary
Also adds a loop missing from the prior commit.
This commit is contained in:
parent
7312428a44
commit
2508633de9
4 changed files with 116 additions and 91 deletions
|
@ -237,6 +237,10 @@ pub(crate) async fn scan_tributaries<
|
|||
let reader = tributary.reader();
|
||||
let mut tributary_db = tributary::TributaryDb::new(raw_db.clone());
|
||||
loop {
|
||||
// Obtain the next block notification now to prevent obtaining it immediately after
|
||||
// the next block occurs
|
||||
let next_block_notification = tributary.next_block_notification().await;
|
||||
|
||||
tributary::scanner::handle_new_blocks::<_, _, _, _, _, _, P>(
|
||||
&mut tributary_db,
|
||||
&key,
|
||||
|
@ -276,10 +280,10 @@ pub(crate) async fn scan_tributaries<
|
|||
)
|
||||
.await;
|
||||
|
||||
// Sleep for half the block time
|
||||
// TODO: Define a notification system for when a new block occurs
|
||||
sleep(Duration::from_secs((Tributary::<D, Transaction, P>::block_time() / 2).into()))
|
||||
.await;
|
||||
next_block_notification
|
||||
.await
|
||||
.map_err(|_| "")
|
||||
.expect("tributary dropped its notifications?");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -361,6 +365,7 @@ pub async fn handle_p2p<D: Db, P: P2p>(
|
|||
tokio::spawn({
|
||||
let p2p = p2p.clone();
|
||||
async move {
|
||||
loop {
|
||||
let mut msg: Message<P> = recv.recv().await.unwrap();
|
||||
match msg.kind {
|
||||
P2pMessageKind::KeepAlive => {}
|
||||
|
@ -374,14 +379,14 @@ pub async fn handle_p2p<D: Db, P: P2p>(
|
|||
}
|
||||
|
||||
// TODO2: Rate limit this per timestamp
|
||||
// And/or slash on Heartbeat which justifies a response, since the node obviously was
|
||||
// offline and we must now use our bandwidth to compensate for them?
|
||||
// And/or slash on Heartbeat which justifies a response, since the node obviously
|
||||
// was offline and we must now use our bandwidth to compensate for them?
|
||||
// TODO: Dedicated task for heartbeats
|
||||
P2pMessageKind::Heartbeat(msg_genesis) => {
|
||||
assert_eq!(msg_genesis, genesis);
|
||||
if msg.msg.len() != 40 {
|
||||
log::error!("validator sent invalid heartbeat");
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
|
||||
let tributary_read = &tributary.tributary;
|
||||
|
@ -420,7 +425,7 @@ pub async fn handle_p2p<D: Db, P: P2p>(
|
|||
}
|
||||
if !selected {
|
||||
log::debug!("received heartbeat and not selected to respond");
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
|
||||
log::debug!("received heartbeat and selected to respond");
|
||||
|
@ -433,7 +438,9 @@ pub async fn handle_p2p<D: Db, P: P2p>(
|
|||
res.extend(reader.commit(&next).unwrap());
|
||||
// Also include the timestamp used within the Heartbeat
|
||||
res.extend(&msg.msg[32 .. 40]);
|
||||
p2p.send(msg.sender, P2pMessageKind::Block(tributary.spec.genesis()), res).await;
|
||||
p2p
|
||||
.send(msg.sender, P2pMessageKind::Block(tributary.spec.genesis()), res)
|
||||
.await;
|
||||
latest = next;
|
||||
}
|
||||
}
|
||||
|
@ -443,7 +450,7 @@ pub async fn handle_p2p<D: Db, P: P2p>(
|
|||
let mut msg_ref: &[u8] = msg.msg.as_ref();
|
||||
let Ok(block) = Block::<Transaction>::read(&mut msg_ref) else {
|
||||
log::error!("received block message with an invalidly serialized block");
|
||||
return;
|
||||
continue;
|
||||
};
|
||||
// Get just the commit
|
||||
msg.msg.drain(.. (msg.msg.len() - msg_ref.len()));
|
||||
|
@ -454,6 +461,7 @@ pub async fn handle_p2p<D: Db, P: P2p>(
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::collections::HashMap;
|
||||
use std::collections::{VecDeque, HashMap};
|
||||
|
||||
use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto};
|
||||
|
||||
|
@ -13,7 +13,7 @@ use crate::{
|
|||
transaction::{Signed, TransactionKind, Transaction as TransactionTrait},
|
||||
};
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Blockchain<D: Db, T: TransactionTrait> {
|
||||
db: Option<D>,
|
||||
genesis: [u8; 32],
|
||||
|
@ -24,6 +24,8 @@ pub(crate) struct Blockchain<D: Db, T: TransactionTrait> {
|
|||
|
||||
provided: ProvidedTransactions<D, T>,
|
||||
mempool: Mempool<D, T>,
|
||||
|
||||
pub(crate) next_block_notifications: VecDeque<tokio::sync::oneshot::Sender<()>>,
|
||||
}
|
||||
|
||||
impl<D: Db, T: TransactionTrait> Blockchain<D, T> {
|
||||
|
@ -76,6 +78,8 @@ impl<D: Db, T: TransactionTrait> Blockchain<D, T> {
|
|||
|
||||
provided: ProvidedTransactions::new(db.clone(), genesis),
|
||||
mempool: Mempool::new(db, genesis),
|
||||
|
||||
next_block_notifications: VecDeque::new(),
|
||||
};
|
||||
|
||||
if let Some((block_number, tip)) = {
|
||||
|
@ -274,6 +278,10 @@ impl<D: Db, T: TransactionTrait> Blockchain<D, T> {
|
|||
txn.commit();
|
||||
self.db = Some(db);
|
||||
|
||||
for tx in self.next_block_notifications.drain(..) {
|
||||
let _ = tx.send(());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -336,6 +336,15 @@ impl<D: Db, T: TransactionTrait, P: P2p> Tributary<D, T, P> {
|
|||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a Future which will resolve once the next block has been added.
|
||||
pub async fn next_block_notification(
|
||||
&self,
|
||||
) -> impl Send + Sync + core::future::Future<Output = Result<(), impl Send + Sync>> {
|
||||
let (tx, rx) = tokio::sync::oneshot::channel();
|
||||
self.network.blockchain.write().await.next_block_notifications.push_back(tx);
|
||||
rx
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
|
|
@ -104,7 +104,7 @@ fn invalid_block() {
|
|||
|
||||
{
|
||||
// Add a valid transaction
|
||||
let mut blockchain = blockchain.clone();
|
||||
let (_, mut blockchain) = new_blockchain(genesis, &[tx.1.signer]);
|
||||
assert!(blockchain.add_transaction::<N>(
|
||||
true,
|
||||
Transaction::Application(tx.clone()),
|
||||
|
@ -129,7 +129,7 @@ fn invalid_block() {
|
|||
|
||||
{
|
||||
// Invalid signature
|
||||
let mut blockchain = blockchain.clone();
|
||||
let (_, mut blockchain) = new_blockchain(genesis, &[tx.1.signer]);
|
||||
assert!(blockchain.add_transaction::<N>(
|
||||
true,
|
||||
Transaction::Application(tx),
|
||||
|
|
Loading…
Reference in a new issue