Add a next block notification system to Tributary

Also adds a loop missing from the prior commit.
This commit is contained in:
Luke Parker 2023-09-25 23:11:36 -04:00
parent 7312428a44
commit 2508633de9
No known key found for this signature in database
4 changed files with 116 additions and 91 deletions

View file

@ -237,6 +237,10 @@ pub(crate) async fn scan_tributaries<
let reader = tributary.reader(); let reader = tributary.reader();
let mut tributary_db = tributary::TributaryDb::new(raw_db.clone()); let mut tributary_db = tributary::TributaryDb::new(raw_db.clone());
loop { 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>( tributary::scanner::handle_new_blocks::<_, _, _, _, _, _, P>(
&mut tributary_db, &mut tributary_db,
&key, &key,
@ -276,10 +280,10 @@ pub(crate) async fn scan_tributaries<
) )
.await; .await;
// Sleep for half the block time next_block_notification
// TODO: Define a notification system for when a new block occurs .await
sleep(Duration::from_secs((Tributary::<D, Transaction, P>::block_time() / 2).into())) .map_err(|_| "")
.await; .expect("tributary dropped its notifications?");
} }
} }
}); });
@ -361,6 +365,7 @@ pub async fn handle_p2p<D: Db, P: P2p>(
tokio::spawn({ tokio::spawn({
let p2p = p2p.clone(); let p2p = p2p.clone();
async move { async move {
loop {
let mut msg: Message<P> = recv.recv().await.unwrap(); let mut msg: Message<P> = recv.recv().await.unwrap();
match msg.kind { match msg.kind {
P2pMessageKind::KeepAlive => {} P2pMessageKind::KeepAlive => {}
@ -374,14 +379,14 @@ pub async fn handle_p2p<D: Db, P: P2p>(
} }
// TODO2: Rate limit this per timestamp // TODO2: Rate limit this per timestamp
// And/or slash on Heartbeat which justifies a response, since the node obviously was // And/or slash on Heartbeat which justifies a response, since the node obviously
// offline and we must now use our bandwidth to compensate for them? // was offline and we must now use our bandwidth to compensate for them?
// TODO: Dedicated task for heartbeats // TODO: Dedicated task for heartbeats
P2pMessageKind::Heartbeat(msg_genesis) => { P2pMessageKind::Heartbeat(msg_genesis) => {
assert_eq!(msg_genesis, genesis); assert_eq!(msg_genesis, genesis);
if msg.msg.len() != 40 { if msg.msg.len() != 40 {
log::error!("validator sent invalid heartbeat"); log::error!("validator sent invalid heartbeat");
return; continue;
} }
let tributary_read = &tributary.tributary; let tributary_read = &tributary.tributary;
@ -420,7 +425,7 @@ pub async fn handle_p2p<D: Db, P: P2p>(
} }
if !selected { if !selected {
log::debug!("received heartbeat and not selected to respond"); log::debug!("received heartbeat and not selected to respond");
return; continue;
} }
log::debug!("received heartbeat and selected to respond"); 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()); res.extend(reader.commit(&next).unwrap());
// Also include the timestamp used within the Heartbeat // Also include the timestamp used within the Heartbeat
res.extend(&msg.msg[32 .. 40]); 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; 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 mut msg_ref: &[u8] = msg.msg.as_ref();
let Ok(block) = Block::<Transaction>::read(&mut msg_ref) else { let Ok(block) = Block::<Transaction>::read(&mut msg_ref) else {
log::error!("received block message with an invalidly serialized block"); log::error!("received block message with an invalidly serialized block");
return; continue;
}; };
// Get just the commit // Get just the commit
msg.msg.drain(.. (msg.msg.len() - msg_ref.len())); msg.msg.drain(.. (msg.msg.len() - msg_ref.len()));
@ -454,6 +461,7 @@ pub async fn handle_p2p<D: Db, P: P2p>(
} }
} }
} }
}
}); });
} }
} }

View file

@ -1,4 +1,4 @@
use std::collections::HashMap; use std::collections::{VecDeque, HashMap};
use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto}; use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto};
@ -13,7 +13,7 @@ use crate::{
transaction::{Signed, TransactionKind, Transaction as TransactionTrait}, transaction::{Signed, TransactionKind, Transaction as TransactionTrait},
}; };
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Debug)]
pub(crate) struct Blockchain<D: Db, T: TransactionTrait> { pub(crate) struct Blockchain<D: Db, T: TransactionTrait> {
db: Option<D>, db: Option<D>,
genesis: [u8; 32], genesis: [u8; 32],
@ -24,6 +24,8 @@ pub(crate) struct Blockchain<D: Db, T: TransactionTrait> {
provided: ProvidedTransactions<D, T>, provided: ProvidedTransactions<D, T>,
mempool: Mempool<D, T>, mempool: Mempool<D, T>,
pub(crate) next_block_notifications: VecDeque<tokio::sync::oneshot::Sender<()>>,
} }
impl<D: Db, T: TransactionTrait> Blockchain<D, T> { 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), provided: ProvidedTransactions::new(db.clone(), genesis),
mempool: Mempool::new(db, genesis), mempool: Mempool::new(db, genesis),
next_block_notifications: VecDeque::new(),
}; };
if let Some((block_number, tip)) = { if let Some((block_number, tip)) = {
@ -274,6 +278,10 @@ impl<D: Db, T: TransactionTrait> Blockchain<D, T> {
txn.commit(); txn.commit();
self.db = Some(db); self.db = Some(db);
for tx in self.next_block_notifications.drain(..) {
let _ = tx.send(());
}
Ok(()) Ok(())
} }
} }

View file

@ -336,6 +336,15 @@ impl<D: Db, T: TransactionTrait, P: P2p> Tributary<D, T, P> {
_ => false, _ => 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)] #[derive(Clone)]

View file

@ -104,7 +104,7 @@ fn invalid_block() {
{ {
// Add a valid transaction // Add a valid transaction
let mut blockchain = blockchain.clone(); let (_, mut blockchain) = new_blockchain(genesis, &[tx.1.signer]);
assert!(blockchain.add_transaction::<N>( assert!(blockchain.add_transaction::<N>(
true, true,
Transaction::Application(tx.clone()), Transaction::Application(tx.clone()),
@ -129,7 +129,7 @@ fn invalid_block() {
{ {
// Invalid signature // Invalid signature
let mut blockchain = blockchain.clone(); let (_, mut blockchain) = new_blockchain(genesis, &[tx.1.signer]);
assert!(blockchain.add_transaction::<N>( assert!(blockchain.add_transaction::<N>(
true, true,
Transaction::Application(tx), Transaction::Application(tx),