Skip to content

Commit aa26c15

Browse files
committed
channel: add connectivity online offline events
1 parent 0ea9d76 commit aa26c15

3 files changed

Lines changed: 81 additions & 14 deletions

File tree

crates/fiber-lib/src/fiber/channel.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3902,6 +3902,28 @@ impl ChannelActorState {
39023902
}
39033903
}
39043904

3905+
fn notify_channel_connectivity(&self, connectivity_state: ChannelConnectivityState) {
3906+
let Some(channel_outpoint) = self.get_funding_transaction_outpoint() else {
3907+
return;
3908+
};
3909+
let event = match connectivity_state {
3910+
ChannelConnectivityState::Online => NetworkServiceEvent::ChannelOnline(
3911+
self.get_remote_pubkey(),
3912+
self.get_id(),
3913+
channel_outpoint,
3914+
),
3915+
ChannelConnectivityState::Offline => NetworkServiceEvent::ChannelOffline(
3916+
self.get_remote_pubkey(),
3917+
self.get_id(),
3918+
channel_outpoint,
3919+
),
3920+
ChannelConnectivityState::Syncing => return,
3921+
};
3922+
self.network()
3923+
.send_message(NetworkActorMessage::new_notification(event))
3924+
.expect(ASSUME_NETWORK_ACTOR_ALIVE);
3925+
}
3926+
39053927
pub(crate) fn mark_reestablishing_offline(&mut self) {
39063928
self.clear_waiting_peer_response();
39073929
self.reestablishing = true;
@@ -3913,6 +3935,7 @@ impl ChannelActorState {
39133935

39143936
fn on_peer_disconnected(&mut self) {
39153937
self.mark_reestablishing_offline();
3938+
self.notify_channel_connectivity(ChannelConnectivityState::Offline);
39163939
if let Some(outpoint) = self.get_funding_transaction_outpoint() {
39173940
self.network()
39183941
.send_message(NetworkActorMessage::new_event(
@@ -6048,6 +6071,7 @@ impl ChannelActorState {
60486071
self.increment_local_commitment_number();
60496072
self.increment_remote_commitment_number();
60506073
self.connectivity_state = ChannelConnectivityState::Online;
6074+
self.notify_channel_connectivity(ChannelConnectivityState::Online);
60516075
let pubkey = self.get_remote_pubkey();
60526076
self.on_owned_channel_updated(myself, false);
60536077
self.network()
@@ -6068,6 +6092,7 @@ impl ChannelActorState {
60686092

60696093
self.reestablishing = false;
60706094
self.connectivity_state = ChannelConnectivityState::Online;
6095+
self.notify_channel_connectivity(ChannelConnectivityState::Online);
60716096

60726097
// If the channel is already ready, we should notify the network actor.
60736098
// so that we update the network.outpoint_channel_map

crates/fiber-lib/src/fiber/network.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,10 @@ pub enum NetworkServiceEvent {
526526
// The channel is ready to use (with funding transaction confirmed
527527
// and both parties sent ChannelReady messages).
528528
ChannelReady(Pubkey, Hash256, OutPoint),
529+
// The channel connectivity is online and normal operations may resume.
530+
ChannelOnline(Pubkey, Hash256, OutPoint),
531+
// The channel connectivity is offline and normal operations are paused.
532+
ChannelOffline(Pubkey, Hash256, OutPoint),
529533
ChannelClosed(Pubkey, Hash256, Byte32),
530534
ChannelAbandon(Hash256),
531535
ChannelFundingAborted(Hash256),

crates/fiber-lib/src/fiber/tests/channel.rs

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -499,31 +499,69 @@ async fn do_test_owned_channel_saved_to_graph_on_reconnected(public: bool) {
499499
})
500500
.await;
501501

502-
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
502+
node1
503+
.expect_event(|event| match event {
504+
NetworkServiceEvent::ChannelOffline(pubkey, channel_id, _) => {
505+
assert_eq!(pubkey, &node2.pubkey);
506+
assert_eq!(channel_id, &_new_channel_id);
507+
true
508+
}
509+
_ => false,
510+
})
511+
.await;
512+
node2
513+
.expect_event(|event| match event {
514+
NetworkServiceEvent::ChannelOffline(pubkey, channel_id, _) => {
515+
assert_eq!(pubkey, &node1.pubkey);
516+
assert_eq!(channel_id, &_new_channel_id);
517+
true
518+
}
519+
_ => false,
520+
})
521+
.await;
522+
523+
wait_until_async_timeout(|| async {
524+
node1.get_network_graph_channels().await.is_empty()
525+
&& node2.get_network_graph_channels().await.is_empty()
526+
})
527+
.await;
528+
503529
let node1_channels = node1.get_network_graph_channels().await;
504530
assert_eq!(node1_channels, vec![]);
505531
let node2_channels = node2.get_network_graph_channels().await;
506532
assert_eq!(node2_channels, vec![]);
507533

508-
node1.connect_to(&mut node2).await;
534+
node1.connect_to_nonblocking(&node2).await;
509535

510536
node1
511-
.expect_debug_event("Reestablished channel in ChannelReady")
537+
.expect_event(|event| match event {
538+
NetworkServiceEvent::ChannelOnline(pubkey, channel_id, _) => {
539+
assert_eq!(pubkey, &node2.pubkey);
540+
assert_eq!(channel_id, &_new_channel_id);
541+
true
542+
}
543+
_ => false,
544+
})
512545
.await;
513546
node2
514-
.expect_debug_event("Reestablished channel in ChannelReady")
547+
.expect_event(|event| match event {
548+
NetworkServiceEvent::ChannelOnline(pubkey, channel_id, _) => {
549+
assert_eq!(pubkey, &node1.pubkey);
550+
assert_eq!(channel_id, &_new_channel_id);
551+
true
552+
}
553+
_ => false,
554+
})
515555
.await;
516556

517-
let mut node1_channels = vec![];
518-
let mut node2_channels = vec![];
519-
for _ in 0..100 {
520-
node1_channels = node1.get_network_graph_channels().await;
521-
node2_channels = node2.get_network_graph_channels().await;
522-
if !node1_channels.is_empty() && !node2_channels.is_empty() {
523-
break;
524-
}
525-
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
526-
}
557+
wait_until_async_timeout(|| async {
558+
!node1.get_network_graph_channels().await.is_empty()
559+
&& !node2.get_network_graph_channels().await.is_empty()
560+
})
561+
.await;
562+
563+
let node1_channels = node1.get_network_graph_channels().await;
564+
let node2_channels = node2.get_network_graph_channels().await;
527565
assert_ne!(node1_channels, vec![]);
528566
assert_ne!(node2_channels, vec![]);
529567
}

0 commit comments

Comments
 (0)