Skip to content

Commit 8616cf1

Browse files
committed
f add during-handshake disconnect test
1 parent b2e70f2 commit 8616cf1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

lightning/src/ln/peer_handler.rs

+33
Original file line numberDiff line numberDiff line change
@@ -1749,4 +1749,37 @@ mod tests {
17491749
assert_eq!(cfgs[1].routing_handler.chan_upds_recvd.load(Ordering::Acquire), 100);
17501750
assert_eq!(cfgs[1].routing_handler.chan_anns_recvd.load(Ordering::Acquire), 50);
17511751
}
1752+
1753+
#[test]
1754+
fn test_handshake_timeout() {
1755+
// Tests that we time out a peer still waiting on handshake completion after a full timer
1756+
// tick.
1757+
let cfgs = create_peermgr_cfgs(2);
1758+
cfgs[0].routing_handler.request_full_sync.store(true, Ordering::Release);
1759+
cfgs[1].routing_handler.request_full_sync.store(true, Ordering::Release);
1760+
let peers = create_network(2, &cfgs);
1761+
1762+
let secp_ctx = Secp256k1::new();
1763+
let a_id = PublicKey::from_secret_key(&secp_ctx, &peers[0].our_node_secret);
1764+
let mut fd_a = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) };
1765+
let mut fd_b = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) };
1766+
let initial_data = peers[1].new_outbound_connection(a_id, fd_b.clone()).unwrap();
1767+
peers[0].new_inbound_connection(fd_a.clone()).unwrap();
1768+
1769+
// If we get a single timer tick before completion, that's fine
1770+
assert_eq!(peers[0].peers.lock().unwrap().peers.len(), 1);
1771+
peers[0].timer_tick_occurred();
1772+
assert_eq!(peers[0].peers.lock().unwrap().peers.len(), 1);
1773+
1774+
assert_eq!(peers[0].read_event(&mut fd_a, &initial_data).unwrap(), false);
1775+
peers[0].process_events();
1776+
assert_eq!(peers[1].read_event(&mut fd_b, &fd_a.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
1777+
peers[1].process_events();
1778+
1779+
// ...but if we get a second timer tick, we should disconnect the peer
1780+
peers[0].timer_tick_occurred();
1781+
assert_eq!(peers[0].peers.lock().unwrap().peers.len(), 0);
1782+
1783+
assert!(peers[0].read_event(&mut fd_a, &fd_b.outbound_data.lock().unwrap().split_off(0)).is_err());
1784+
}
17521785
}

0 commit comments

Comments
 (0)