Tributary test wait_for_tx_inclusion function

This commit is contained in:
Luke Parker
2023-04-23 01:52:19 -04:00
parent 710e6e5217
commit e0dc5d29ad
3 changed files with 53 additions and 37 deletions

View File

@@ -20,7 +20,7 @@ use tokio::time::sleep;
use serai_db::MemDb;
use tributary::Tributary;
use tributary::{Transaction as TransactionTrait, Tributary};
use crate::{
P2pMessageKind, P2p, LocalP2p,
@@ -108,6 +108,44 @@ pub async fn run_tributaries(
}
}
pub async fn wait_for_tx_inclusion(
tributary: &Tributary<MemDb, Transaction, LocalP2p>,
mut last_checked: [u8; 32],
hash: [u8; 32],
) -> [u8; 32] {
loop {
let tip = tributary.tip();
if tip == last_checked {
sleep(Duration::from_secs(1)).await;
continue;
}
let mut queue = vec![tributary.block(&tip).unwrap()];
let mut block = None;
while {
let parent = queue.last().unwrap().parent();
if parent == tributary.genesis() {
false
} else {
block = Some(tributary.block(&parent).unwrap());
block.as_ref().unwrap().hash() != last_checked
}
} {
queue.push(block.take().unwrap());
}
while let Some(block) = queue.pop() {
for tx in &block.transactions {
if tx.hash() == hash {
return block.hash();
}
}
}
last_checked = tip;
}
}
#[tokio::test]
async fn tributary_test() {
let keys = new_keys(&mut OsRng);