Skip to content

Commit ec6cabd

Browse files
committed
Batch funding for v1 channel establishments
1 parent 073f078 commit ec6cabd

File tree

4 files changed

+380
-38
lines changed

4 files changed

+380
-38
lines changed

lightning/src/events/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ pub enum ClosureReason {
199199
/// The counterparty requested a cooperative close of a channel that had not been funded yet.
200200
/// The channel has been immediately closed.
201201
CounterpartyCoopClosedUnfundedChannel,
202+
/// Another channel in the same funding batch closed before the funding transaction
203+
/// was ready to be broadcast.
204+
FundingBatchClosure,
202205
}
203206

204207
impl core::fmt::Display for ClosureReason {
@@ -219,6 +222,7 @@ impl core::fmt::Display for ClosureReason {
219222
ClosureReason::DisconnectedPeer => f.write_str("the peer disconnected prior to the channel being funded"),
220223
ClosureReason::OutdatedChannelManager => f.write_str("the ChannelManager read from disk was stale compared to ChannelMonitor(s)"),
221224
ClosureReason::CounterpartyCoopClosedUnfundedChannel => f.write_str("the peer requested the unfunded channel be closed"),
225+
ClosureReason::FundingBatchClosure => f.write_str("another channel in the same funding batch closed"),
222226
}
223227
}
224228
}
@@ -233,6 +237,7 @@ impl_writeable_tlv_based_enum_upgradable!(ClosureReason,
233237
(10, DisconnectedPeer) => {},
234238
(12, OutdatedChannelManager) => {},
235239
(13, CounterpartyCoopClosedUnfundedChannel) => {},
240+
(15, FundingBatchClosure) => {}
236241
);
237242

238243
/// Intended destination of a failed HTLC as indicated in [`Event::HTLCHandlingFailed`].

lightning/src/ln/channel.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ enum ChannelState {
300300
/// We've successfully negotiated a closing_signed dance. At this point ChannelManager is about
301301
/// to drop us, but we store this anyway.
302302
ShutdownComplete = 4096,
303+
/// Flag which is set on `FundingSent` to indicate this channel is funded in a batch and the
304+
/// broadcasting of the funding transaction is being held until all channels in the batch
305+
/// have received funding_signed and have their monitors persisted.
306+
WaitingForBatch = 1 << 13,
303307
}
304308
const BOTH_SIDES_SHUTDOWN_MASK: u32 = ChannelState::LocalShutdownSent as u32 | ChannelState::RemoteShutdownSent as u32;
305309
const MULTI_STATE_FLAGS: u32 = BOTH_SIDES_SHUTDOWN_MASK | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32;
@@ -1188,9 +1192,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
11881192
did_channel_update
11891193
}
11901194

1191-
/// Returns true if funding_created was sent/received.
1195+
/// Returns true if funding_signed was sent/received and the
1196+
/// funding transaction has been broadcast if necessary.
11921197
pub fn is_funding_initiated(&self) -> bool {
1193-
self.channel_state >= ChannelState::FundingSent as u32
1198+
self.channel_state >= ChannelState::FundingSent as u32 &&
1199+
self.channel_state & ChannelState::WaitingForBatch as u32 == 0
11941200
}
11951201

11961202
/// Transaction nomenclature is somewhat confusing here as there are many different cases - a
@@ -1922,7 +1928,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19221928

19231929
/// Returns transaction if there is pending funding transaction that is yet to broadcast
19241930
pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
1925-
if self.channel_state & (ChannelState::FundingCreated as u32) != 0 {
1931+
if self.channel_state & ChannelState::FundingCreated as u32 != 0 ||
1932+
self.channel_state & ChannelState::WaitingForBatch as u32 != 0 {
19261933
self.funding_transaction.clone()
19271934
} else {
19281935
None
@@ -2472,7 +2479,7 @@ impl<SP: Deref> Channel<SP> where
24722479
/// Handles a funding_signed message from the remote end.
24732480
/// If this call is successful, broadcast the funding transaction (and not before!)
24742481
pub fn funding_signed<L: Deref>(
2475-
&mut self, msg: &msgs::FundingSigned, best_block: BestBlock, signer_provider: &SP, logger: &L
2482+
&mut self, msg: &msgs::FundingSigned, best_block: BestBlock, signer_provider: &SP, is_batch_funding: bool, logger: &L
24762483
) -> Result<ChannelMonitor<<SP::Target as SignerProvider>::Signer>, ChannelError>
24772484
where
24782485
L::Target: Logger
@@ -2547,7 +2554,11 @@ impl<SP: Deref> Channel<SP> where
25472554
counterparty_initial_commitment_tx.to_countersignatory_value_sat(), logger);
25482555

25492556
assert_eq!(self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32), 0); // We have no had any monitor(s) yet to fail update!
2550-
self.context.channel_state = ChannelState::FundingSent as u32;
2557+
if is_batch_funding {
2558+
self.context.channel_state = ChannelState::FundingSent as u32 | ChannelState::WaitingForBatch as u32;
2559+
} else {
2560+
self.context.channel_state = ChannelState::FundingSent as u32;
2561+
}
25512562
self.context.cur_holder_commitment_transaction_number -= 1;
25522563
self.context.cur_counterparty_commitment_transaction_number -= 1;
25532564

@@ -2558,6 +2569,13 @@ impl<SP: Deref> Channel<SP> where
25582569
Ok(channel_monitor)
25592570
}
25602571

2572+
/// Updates the state of the channel to indicate that all channels in the batch
2573+
/// have received funding_signed and persisted their monitors.
2574+
/// The funding transaction is consequently allowed to be broadcast.
2575+
pub fn set_batch_ready(&mut self) {
2576+
self.context.channel_state &= !(ChannelState::WaitingForBatch as u32);
2577+
}
2578+
25612579
/// Handles a channel_ready message from our peer. If we've already sent our channel_ready
25622580
/// and the channel is now usable (and public), this may generate an announcement_signatures to
25632581
/// reply with.
@@ -3674,7 +3692,7 @@ impl<SP: Deref> Channel<SP> where
36743692
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
36753693
// first received the funding_signed.
36763694
let mut funding_broadcastable =
3677-
if self.context.is_outbound() && self.context.channel_state & !MULTI_STATE_FLAGS >= ChannelState::FundingSent as u32 {
3695+
if self.context.is_outbound() && self.context.channel_state & !MULTI_STATE_FLAGS >= ChannelState::FundingSent as u32 && self.context.channel_state & ChannelState::WaitingForBatch as u32 == 0 {
36783696
self.context.funding_transaction.take()
36793697
} else { None };
36803698
// That said, if the funding transaction is already confirmed (ie we're active with a
@@ -7687,7 +7705,7 @@ mod tests {
76877705
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
76887706

76897707
// Node B --> Node A: funding signed
7690-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
7708+
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
76917709

76927710
// Put some inbound and outbound HTLCs in A's channel.
76937711
let htlc_amount_msat = 11_092_000; // put an amount below A's effective dust limit but above B's.
@@ -7814,7 +7832,7 @@ mod tests {
78147832
let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
78157833

78167834
// Node B --> Node A: funding signed
7817-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
7835+
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
78187836

78197837
// Now disconnect the two nodes and check that the commitment point in
78207838
// Node B's channel_reestablish message is sane.
@@ -8002,7 +8020,7 @@ mod tests {
80028020
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
80038021

80048022
// Node B --> Node A: funding signed
8005-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
8023+
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
80068024

80078025
// Make sure that receiving a channel update will update the Channel as expected.
80088026
let update = ChannelUpdate {

0 commit comments

Comments
 (0)