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
4 changed files with 116 additions and 91 deletions

View File

@@ -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(())
}
}

View File

@@ -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)]

View File

@@ -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),