Skip to content

Commit 24976bc

Browse files
committed
Add ChainNotifier tests for more error cases
1 parent 2774200 commit 24976bc

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

lightning-block-sync/src/lib.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,52 @@ mod chain_notifier_tests {
589589
Ok(_) => {},
590590
}
591591
}
592+
593+
#[tokio::test]
594+
async fn sync_from_chain_without_headers() {
595+
let mut chain = Blockchain::default().with_height(3).without_headers();
596+
597+
let new_tip = chain.tip();
598+
let old_tip = chain.at_height(1);
599+
let mut listener = MockChainListener::new();
600+
let mut notifier = ChainNotifier { header_cache: chain.header_cache(0..=1) };
601+
let mut poller = poller::ChainPoller::new(&mut chain as &mut dyn BlockSource, Network::Testnet);
602+
match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await {
603+
Err((_, tip)) => assert_eq!(tip, None),
604+
Ok(_) => panic!("Expected error"),
605+
}
606+
}
607+
608+
#[tokio::test]
609+
async fn sync_from_chain_without_any_new_blocks() {
610+
let mut chain = Blockchain::default().with_height(3).without_blocks(2..);
611+
612+
let new_tip = chain.tip();
613+
let old_tip = chain.at_height(1);
614+
let mut listener = MockChainListener::new();
615+
let mut notifier = ChainNotifier { header_cache: chain.header_cache(0..=3) };
616+
let mut poller = poller::ChainPoller::new(&mut chain as &mut dyn BlockSource, Network::Testnet);
617+
match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await {
618+
Err((_, tip)) => assert_eq!(tip, Some(old_tip)),
619+
Ok(_) => panic!("Expected error"),
620+
}
621+
}
622+
623+
#[tokio::test]
624+
async fn sync_from_chain_without_some_new_blocks() {
625+
let mut chain = Blockchain::default().with_height(3).without_blocks(3..);
626+
627+
let new_tip = chain.tip();
628+
let old_tip = chain.at_height(1);
629+
let mut listener = MockChainListener::new()
630+
.expect_block_connected(*chain.at_height(2));
631+
let mut notifier = ChainNotifier { header_cache: chain.header_cache(0..=3) };
632+
let mut poller = poller::ChainPoller::new(&mut chain as &mut dyn BlockSource, Network::Testnet);
633+
match notifier.sync_listener(new_tip, &old_tip, &mut poller, &mut listener).await {
634+
Err((_, tip)) => assert_eq!(tip, Some(chain.at_height(2))),
635+
Ok(_) => panic!("Expected error"),
636+
}
637+
}
592638
}
593639

594640
#[cfg(test)]

lightning-block-sync/src/test_utils.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::collections::VecDeque;
99
#[derive(Default)]
1010
pub struct Blockchain {
1111
pub blocks: Vec<Block>,
12+
without_blocks: Option<std::ops::RangeFrom<usize>>,
1213
without_headers: bool,
1314
malformed_headers: bool,
1415
}
@@ -45,6 +46,10 @@ impl Blockchain {
4546
self
4647
}
4748

49+
pub fn without_blocks(self, range: std::ops::RangeFrom<usize>) -> Self {
50+
Self { without_blocks: Some(range), ..self }
51+
}
52+
4853
pub fn without_headers(self) -> Self {
4954
Self { without_headers: true, ..self }
5055
}
@@ -62,7 +67,7 @@ impl Blockchain {
6267
block.header.nonce += 1;
6368
prev_blockhash = block.block_hash();
6469
}
65-
Self { blocks, ..*self }
70+
Self { blocks, without_blocks: None, ..*self }
6671
}
6772

6873
pub fn at_height(&self, height: usize) -> ValidatedBlockHeader {
@@ -124,8 +129,14 @@ impl BlockSource for Blockchain {
124129

125130
fn get_block<'a>(&'a mut self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block> {
126131
Box::pin(async move {
127-
for block in self.blocks.iter() {
132+
for (height, block) in self.blocks.iter().enumerate() {
128133
if block.header.block_hash() == *header_hash {
134+
if let Some(without_blocks) = &self.without_blocks {
135+
if without_blocks.contains(&height) {
136+
return Err(BlockSourceError::Persistent);
137+
}
138+
}
139+
129140
return Ok(block.clone());
130141
}
131142
}

0 commit comments

Comments
 (0)