Break coordinator main into multiple functions

Also moves from std::sync::RwLock to tokio::sync::RwLock to prevent wasting
cycles on spinning.
This commit is contained in:
Luke Parker
2023-04-23 23:15:15 -04:00
parent be8c25aef0
commit c476f9b640
10 changed files with 263 additions and 245 deletions

View File

@@ -118,20 +118,20 @@ pub async fn wait_for_tx_inclusion(
hash: [u8; 32],
) -> [u8; 32] {
loop {
let tip = tributary.tip();
let tip = tributary.tip().await;
if tip == last_checked {
sleep(Duration::from_secs(1)).await;
continue;
}
let mut queue = vec![tributary.block(&tip).unwrap()];
let mut queue = vec![tributary.block(&tip).await.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 = Some(tributary.block(&parent).await.unwrap());
block.as_ref().unwrap().hash() != last_checked
}
} {
@@ -176,7 +176,7 @@ async fn tributary_test() {
}
}
let tip = tributaries[0].1.tip();
let tip = tributaries[0].1.tip().await;
if tip != last_block {
last_block = tip;
blocks += 1;
@@ -202,11 +202,18 @@ async fn tributary_test() {
}
}
// handle_message informed the Tendermint machine, yet it still has to process it
// Sleep for a second accordingly
// TODO: Is there a better way to handle this?
sleep(Duration::from_secs(1)).await;
// All tributaries should agree on the tip
let mut final_block = None;
for (_, tributary) in tributaries {
final_block = final_block.or_else(|| Some(tributary.tip()));
if tributary.tip() != final_block.unwrap() {
if final_block.is_none() {
final_block = Some(tributary.tip().await);
}
if tributary.tip().await != final_block.unwrap() {
panic!("tributary had different tip");
}
}

View File

@@ -47,7 +47,7 @@ async fn dkg_test() {
txs.push(tx);
}
let block_before_tx = tributaries[0].1.tip();
let block_before_tx = tributaries[0].1.tip().await;
// Publish all commitments but one
for (i, tx) in txs.iter().enumerate().skip(1) {
@@ -87,10 +87,10 @@ async fn dkg_test() {
// Instantiate a scanner and verify it has nothing to report
let (mut scanner_db, mut processor) = new_processor(&keys[0], &spec, &tributaries[0].1).await;
assert!(processor.0.read().unwrap().is_empty());
assert!(processor.0.read().await.is_empty());
// Publish the last commitment
let block_before_tx = tributaries[0].1.tip();
let block_before_tx = tributaries[0].1.tip().await;
assert!(tributaries[0].1.add_transaction(txs[0].clone()).await);
wait_for_tx_inclusion(&tributaries[0].1, block_before_tx, txs[0].hash()).await;
sleep(Duration::from_secs(Tributary::<MemDb, Transaction, LocalP2p>::block_time().into())).await;
@@ -98,7 +98,7 @@ async fn dkg_test() {
// Verify the scanner emits a KeyGen::Commitments message
handle_new_blocks(&mut scanner_db, &keys[0], &mut processor, &spec, &tributaries[0].1).await;
{
let mut msgs = processor.0.write().unwrap();
let mut msgs = processor.0.write().await;
assert_eq!(msgs.pop_front().unwrap(), expected_commitments);
assert!(msgs.is_empty());
}
@@ -106,7 +106,7 @@ async fn dkg_test() {
// Verify all keys exhibit this scanner behavior
for (i, key) in keys.iter().enumerate() {
let (_, processor) = new_processor(key, &spec, &tributaries[i].1).await;
let mut msgs = processor.0.write().unwrap();
let mut msgs = processor.0.write().await;
assert_eq!(msgs.pop_front().unwrap(), expected_commitments);
assert!(msgs.is_empty());
}
@@ -128,7 +128,7 @@ async fn dkg_test() {
txs.push(tx);
}
let block_before_tx = tributaries[0].1.tip();
let block_before_tx = tributaries[0].1.tip().await;
for (i, tx) in txs.iter().enumerate().skip(1) {
assert!(tributaries[i].1.add_transaction(tx.clone()).await);
}
@@ -138,10 +138,10 @@ async fn dkg_test() {
// With just 4 sets of shares, nothing should happen yet
handle_new_blocks(&mut scanner_db, &keys[0], &mut processor, &spec, &tributaries[0].1).await;
assert!(processor.0.write().unwrap().is_empty());
assert!(processor.0.write().await.is_empty());
// Publish the final set of shares
let block_before_tx = tributaries[0].1.tip();
let block_before_tx = tributaries[0].1.tip().await;
assert!(tributaries[0].1.add_transaction(txs[0].clone()).await);
wait_for_tx_inclusion(&tributaries[0].1, block_before_tx, txs[0].hash()).await;
sleep(Duration::from_secs(Tributary::<MemDb, Transaction, LocalP2p>::block_time().into())).await;
@@ -170,7 +170,7 @@ async fn dkg_test() {
// Any scanner which has handled the prior blocks should only emit the new event
handle_new_blocks(&mut scanner_db, &keys[0], &mut processor, &spec, &tributaries[0].1).await;
{
let mut msgs = processor.0.write().unwrap();
let mut msgs = processor.0.write().await;
assert_eq!(msgs.pop_front().unwrap(), shares_for(0));
assert!(msgs.is_empty());
}
@@ -178,7 +178,7 @@ async fn dkg_test() {
// Yet new scanners should emit all events
for (i, key) in keys.iter().enumerate() {
let (_, processor) = new_processor(key, &spec, &tributaries[i].1).await;
let mut msgs = processor.0.write().unwrap();
let mut msgs = processor.0.write().await;
assert_eq!(msgs.pop_front().unwrap(), expected_commitments);
assert_eq!(msgs.pop_front().unwrap(), shares_for(i));
assert!(msgs.is_empty());

View File

@@ -34,7 +34,7 @@ async fn tx_test() {
OsRng.fill_bytes(&mut commitments);
// Create the TX with a null signature so we can get its sig hash
let block_before_tx = tributaries[sender].1.tip();
let block_before_tx = tributaries[sender].1.tip().await;
let mut tx =
Transaction::DkgCommitments(attempt, commitments.clone(), Transaction::empty_signed());
tx.sign(&mut OsRng, spec.genesis(), &key, 0);
@@ -46,7 +46,7 @@ async fn tx_test() {
// All tributaries should have acknowledged this transaction in a block
for (_, tributary) in tributaries {
let block = tributary.block(&included_in).unwrap();
let block = tributary.block(&included_in).await.unwrap();
assert_eq!(block.transactions, vec![tx.clone()]);
}
}