Skip to content

Commit

Permalink
f - Tidy up unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jkczyz committed Feb 13, 2021
1 parent 38cce22 commit f18348f
Showing 1 changed file with 20 additions and 35 deletions.
55 changes: 20 additions & 35 deletions lightning-block-sync/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,29 +138,25 @@ mod tests {
#[tokio::test]
async fn sync_from_same_chain() {
let mut chain = Blockchain::default().with_height(4);
let new_tip = chain.tip();
let old_tip_1 = chain.at_height(1);
let old_tip_2 = chain.at_height(2);
let old_tip_3 = chain.at_height(3);

let mut listener_1 = MockChainListener::new()
.expect_block_connected(*old_tip_2)
.expect_block_connected(*old_tip_3)
.expect_block_connected(*new_tip);
.expect_block_connected(*chain.at_height(2))
.expect_block_connected(*chain.at_height(3))
.expect_block_connected(*chain.at_height(4));
let mut listener_2 = MockChainListener::new()
.expect_block_connected(*old_tip_3)
.expect_block_connected(*new_tip);
.expect_block_connected(*chain.at_height(3))
.expect_block_connected(*chain.at_height(4));
let mut listener_3 = MockChainListener::new()
.expect_block_connected(*new_tip);
.expect_block_connected(*chain.at_height(4));

let listeners = vec![
(old_tip_1.block_hash, &mut listener_1 as &mut dyn ChainListener),
(old_tip_2.block_hash, &mut listener_2 as &mut dyn ChainListener),
(old_tip_3.block_hash, &mut listener_3 as &mut dyn ChainListener),
(chain.at_height(1).block_hash, &mut listener_1 as &mut dyn ChainListener),
(chain.at_height(2).block_hash, &mut listener_2 as &mut dyn ChainListener),
(chain.at_height(3).block_hash, &mut listener_3 as &mut dyn ChainListener),
];
let mut cache = chain.header_cache(0..=4);
match sync_listeners(&mut chain, Network::Bitcoin, &mut cache, listeners).await {
Ok(header) => assert_eq!(header, new_tip),
Ok(header) => assert_eq!(header, chain.tip()),
Err(e) => panic!("Unexpected error: {:?}", e),
}
}
Expand All @@ -172,11 +168,6 @@ mod tests {
let fork_chain_2 = main_chain.fork_at_height(2);
let fork_chain_3 = main_chain.fork_at_height(3);

let new_tip = main_chain.tip();
let old_tip_1 = fork_chain_1.tip();
let old_tip_2 = fork_chain_2.tip();
let old_tip_3 = fork_chain_3.tip();

let mut listener_1 = MockChainListener::new()
.expect_block_disconnected(*fork_chain_1.at_height(4))
.expect_block_disconnected(*fork_chain_1.at_height(3))
Expand All @@ -194,15 +185,15 @@ mod tests {
.expect_block_connected(*main_chain.at_height(4));

let listeners = vec![
(old_tip_1.block_hash, &mut listener_1 as &mut dyn ChainListener),
(old_tip_2.block_hash, &mut listener_2 as &mut dyn ChainListener),
(old_tip_3.block_hash, &mut listener_3 as &mut dyn ChainListener),
(fork_chain_1.tip().block_hash, &mut listener_1 as &mut dyn ChainListener),
(fork_chain_2.tip().block_hash, &mut listener_2 as &mut dyn ChainListener),
(fork_chain_3.tip().block_hash, &mut listener_3 as &mut dyn ChainListener),
];
let mut cache = fork_chain_1.header_cache(2..=4);
cache.extend(fork_chain_2.header_cache(3..=4));
cache.extend(fork_chain_3.header_cache(4..=4));
match sync_listeners(&mut main_chain, Network::Bitcoin, &mut cache, listeners).await {
Ok(header) => assert_eq!(header, new_tip),
Ok(header) => assert_eq!(header, main_chain.tip()),
Err(e) => panic!("Unexpected error: {:?}", e),
}
}
Expand All @@ -214,11 +205,6 @@ mod tests {
let fork_chain_2 = fork_chain_1.fork_at_height(2);
let fork_chain_3 = fork_chain_2.fork_at_height(3);

let new_tip = main_chain.tip();
let old_tip_1 = fork_chain_1.tip();
let old_tip_2 = fork_chain_2.tip();
let old_tip_3 = fork_chain_3.tip();

let mut listener_1 = MockChainListener::new()
.expect_block_disconnected(*fork_chain_1.at_height(4))
.expect_block_disconnected(*fork_chain_1.at_height(3))
Expand All @@ -242,15 +228,15 @@ mod tests {
.expect_block_connected(*main_chain.at_height(4));

let listeners = vec![
(old_tip_1.block_hash, &mut listener_1 as &mut dyn ChainListener),
(old_tip_2.block_hash, &mut listener_2 as &mut dyn ChainListener),
(old_tip_3.block_hash, &mut listener_3 as &mut dyn ChainListener),
(fork_chain_1.tip().block_hash, &mut listener_1 as &mut dyn ChainListener),
(fork_chain_2.tip().block_hash, &mut listener_2 as &mut dyn ChainListener),
(fork_chain_3.tip().block_hash, &mut listener_3 as &mut dyn ChainListener),
];
let mut cache = fork_chain_1.header_cache(2..=4);
cache.extend(fork_chain_2.header_cache(3..=4));
cache.extend(fork_chain_3.header_cache(4..=4));
match sync_listeners(&mut main_chain, Network::Bitcoin, &mut cache, listeners).await {
Ok(header) => assert_eq!(header, new_tip),
Ok(header) => assert_eq!(header, main_chain.tip()),
Err(e) => panic!("Unexpected error: {:?}", e),
}
}
Expand All @@ -259,13 +245,12 @@ mod tests {
async fn cache_connected_and_keep_disconnected_blocks() {
let mut main_chain = Blockchain::default().with_height(2);
let fork_chain = main_chain.fork_at_height(1);

let new_tip = main_chain.tip();
let old_tip = fork_chain.tip();

let mut listener = MockChainListener::new()
.expect_block_disconnected(*fork_chain.at_height(2))
.expect_block_connected(*main_chain.at_height(2));
.expect_block_disconnected(*old_tip)
.expect_block_connected(*new_tip);

let listeners = vec![(old_tip.block_hash, &mut listener as &mut dyn ChainListener)];
let mut cache = fork_chain.header_cache(2..=2);
Expand Down

0 comments on commit f18348f

Please sign in to comment.