Skip to content

Commit 8b3573b

Browse files
committed
Rewrite ChannelManager::peer_connected
Exposing ChannelPhase in ChannelManager has led to verbose match statements, which need to be modified each time a ChannelPhase is added. Making ChannelPhase an implementation detail of Channel would help avoid this. As a step in this direction, update ChannelManager::peer_connected to use ChannelPhase::as_funded_mut and a new ChannelPhase::maybe_get_open_channel method.
1 parent 052428e commit 8b3573b

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

lightning/src/ln/channel.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::ln::msgs;
3939
use crate::ln::msgs::{ClosingSigned, ClosingSignedFeeRange, DecodeError};
4040
use crate::ln::script::{self, ShutdownScript};
4141
use crate::ln::channel_state::{ChannelShutdownState, CounterpartyForwardingInfo, InboundHTLCDetails, InboundHTLCStateDetails, OutboundHTLCDetails, OutboundHTLCStateDetails};
42-
use crate::ln::channelmanager::{self, PendingHTLCStatus, HTLCSource, SentHTLCId, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, PaymentClaimDetails, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
42+
use crate::ln::channelmanager::{self, OpenChannelMessage, PendingHTLCStatus, HTLCSource, SentHTLCId, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, PaymentClaimDetails, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
4343
use crate::ln::chan_utils::{
4444
CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, htlc_success_tx_weight,
4545
htlc_timeout_tx_weight, make_funding_redeemscript, ChannelPublicKeys, CommitmentTransaction,
@@ -1242,6 +1242,43 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
12421242
ChannelPhase::UnfundedV2(_) => false,
12431243
}
12441244
}
1245+
1246+
pub fn maybe_get_open_channel<L: Deref>(
1247+
&mut self, chain_hash: ChainHash, logger: &L,
1248+
) -> Option<OpenChannelMessage> where L::Target: Logger {
1249+
match self {
1250+
ChannelPhase::Funded(_) => None,
1251+
ChannelPhase::UnfundedOutboundV1(chan) => {
1252+
let logger = WithChannelContext::from(logger, &chan.context, None);
1253+
chan.get_open_channel(chain_hash, &&logger)
1254+
.map(|msg| OpenChannelMessage::V1(msg))
1255+
},
1256+
ChannelPhase::UnfundedInboundV1(_) => {
1257+
// Since unfunded inbound channel maps are cleared upon disconnecting a peer,
1258+
// they are not persisted and won't be recovered after a crash.
1259+
// Therefore, they shouldn't exist at this point.
1260+
debug_assert!(false);
1261+
None
1262+
},
1263+
#[cfg(dual_funding)]
1264+
ChannelPhase::UnfundedV2(chan) => {
1265+
if chan.context.is_outbound() {
1266+
Some(OpenChannelMessage::V2(chan.get_open_channel_v2(chain_hash)))
1267+
} else {
1268+
// Since unfunded inbound channel maps are cleared upon disconnecting a peer,
1269+
// they are not persisted and won't be recovered after a crash.
1270+
// Therefore, they shouldn't exist at this point.
1271+
debug_assert!(false);
1272+
None
1273+
}
1274+
},
1275+
#[cfg(not(dual_funding))]
1276+
ChannelPhase::UnfundedV2(_) => {
1277+
debug_assert!(false);
1278+
None
1279+
},
1280+
}
1281+
}
12451282
}
12461283

12471284
/// Contains all state common to unfunded inbound/outbound channels.

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11646,41 +11646,29 @@ where
1164611646
let pending_msg_events = &mut peer_state.pending_msg_events;
1164711647

1164811648
for (_, phase) in peer_state.channel_by_id.iter_mut() {
11649-
match phase {
11650-
ChannelPhase::Funded(chan) => {
11649+
match phase.as_funded_mut() {
11650+
Some(chan) => {
1165111651
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
1165211652
pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish {
1165311653
node_id: chan.context.get_counterparty_node_id(),
1165411654
msg: chan.get_channel_reestablish(&&logger),
1165511655
});
1165611656
},
11657-
ChannelPhase::UnfundedOutboundV1(chan) => {
11658-
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
11659-
if let Some(msg) = chan.get_open_channel(self.chain_hash, &&logger) {
11657+
None => match phase.maybe_get_open_channel(self.chain_hash, &self.logger) {
11658+
Some(OpenChannelMessage::V1(msg)) => {
1166011659
pending_msg_events.push(events::MessageSendEvent::SendOpenChannel {
11661-
node_id: chan.context.get_counterparty_node_id(),
11660+
node_id: phase.context().get_counterparty_node_id(),
1166211661
msg,
1166311662
});
11664-
}
11665-
},
11666-
ChannelPhase::UnfundedV2(chan) => {
11667-
if chan.context.is_outbound() {
11663+
},
11664+
#[cfg(dual_funding)]
11665+
Some(OpenChannelMessage::V2(msg)) => {
1166811666
pending_msg_events.push(events::MessageSendEvent::SendOpenChannelV2 {
11669-
node_id: chan.context.get_counterparty_node_id(),
11670-
msg: chan.get_open_channel_v2(self.chain_hash),
11667+
node_id: phase.context().get_counterparty_node_id(),
11668+
msg,
1167111669
});
11672-
} else {
11673-
// Since unfunded inbound channel maps are cleared upon disconnecting a peer,
11674-
// they are not persisted and won't be recovered after a crash.
11675-
// Therefore, they shouldn't exist at this point.
11676-
debug_assert!(false);
11677-
}
11678-
},
11679-
ChannelPhase::UnfundedInboundV1(_) => {
11680-
// Since unfunded inbound channel maps are cleared upon disconnecting a peer,
11681-
// they are not persisted and won't be recovered after a crash.
11682-
// Therefore, they shouldn't exist at this point.
11683-
debug_assert!(false);
11670+
},
11671+
None => {},
1168411672
},
1168511673
}
1168611674
}

0 commit comments

Comments
 (0)