Skip to content

Commit cc00689

Browse files
committed
Batch funding for v1 channel establishments
1 parent 6f58072 commit cc00689

File tree

4 files changed

+370
-38
lines changed

4 files changed

+370
-38
lines changed

lightning/src/events/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ pub enum ClosureReason {
168168
/// The counterparty requested a cooperative close of a channel that had not been funded yet.
169169
/// The channel has been immediately closed.
170170
CounterpartyCoopClosedUnfundedChannel,
171+
/// Another channel in the same funding batch closed before the funding transaction
172+
/// was ready to be broadcast.
173+
FundingBatchClosure,
171174
}
172175

173176
impl core::fmt::Display for ClosureReason {
@@ -188,6 +191,7 @@ impl core::fmt::Display for ClosureReason {
188191
ClosureReason::DisconnectedPeer => f.write_str("the peer disconnected prior to the channel being funded"),
189192
ClosureReason::OutdatedChannelManager => f.write_str("the ChannelManager read from disk was stale compared to ChannelMonitor(s)"),
190193
ClosureReason::CounterpartyCoopClosedUnfundedChannel => f.write_str("the peer requested the unfunded channel be closed"),
194+
ClosureReason::FundingBatchClosure => f.write_str("another channel in the same funding batch closed"),
191195
}
192196
}
193197
}
@@ -202,6 +206,7 @@ impl_writeable_tlv_based_enum_upgradable!(ClosureReason,
202206
(10, DisconnectedPeer) => {},
203207
(12, OutdatedChannelManager) => {},
204208
(13, CounterpartyCoopClosedUnfundedChannel) => {},
209+
(15, FundingBatchClosure) => {}
205210
);
206211

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

lightning/src/ln/channel.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ enum ChannelState {
301301
/// We've successfully negotiated a closing_signed dance. At this point ChannelManager is about
302302
/// to drop us, but we store this anyway.
303303
ShutdownComplete = 4096,
304+
/// Flag which is set on `FundingSent` to indicate this channel is funded in a batch and the
305+
/// broadcasting of the funding transaction is being held until all channels in the batch
306+
/// have received funding_signed and have their monitors persisted.
307+
WaitingForBatch = 1 << 13,
304308
}
305309
const BOTH_SIDES_SHUTDOWN_MASK: u32 = ChannelState::LocalShutdownSent as u32 | ChannelState::RemoteShutdownSent as u32;
306310
const MULTI_STATE_FLAGS: u32 = BOTH_SIDES_SHUTDOWN_MASK | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32;
@@ -1198,9 +1202,11 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
11981202
did_channel_update
11991203
}
12001204

1201-
/// Returns true if funding_created was sent/received.
1205+
/// Returns true if funding_signed was sent/received and the
1206+
/// funding transaction has been broadcast if necessary.
12021207
pub fn is_funding_initiated(&self) -> bool {
1203-
self.channel_state >= ChannelState::FundingSent as u32
1208+
self.channel_state >= ChannelState::FundingSent as u32 &&
1209+
self.channel_state & ChannelState::WaitingForBatch as u32 == 0
12041210
}
12051211

12061212
/// Transaction nomenclature is somewhat confusing here as there are many different cases - a
@@ -1940,7 +1946,8 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
19401946

19411947
/// Returns transaction if there is pending funding transaction that is yet to broadcast
19421948
pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
1943-
if self.channel_state & (ChannelState::FundingCreated as u32) != 0 {
1949+
if self.channel_state & ChannelState::FundingCreated as u32 != 0 ||
1950+
self.channel_state & ChannelState::WaitingForBatch as u32 != 0 {
19441951
self.funding_transaction.clone()
19451952
} else {
19461953
None
@@ -2487,7 +2494,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
24872494
/// Handles a funding_signed message from the remote end.
24882495
/// If this call is successful, broadcast the funding transaction (and not before!)
24892496
pub fn funding_signed<SP: Deref, L: Deref>(
2490-
&mut self, msg: &msgs::FundingSigned, best_block: BestBlock, signer_provider: &SP, logger: &L
2497+
&mut self, msg: &msgs::FundingSigned, best_block: BestBlock, signer_provider: &SP, is_batch_funding: bool, logger: &L
24912498
) -> Result<ChannelMonitor<Signer>, ChannelError>
24922499
where
24932500
SP::Target: SignerProvider<Signer = Signer>,
@@ -2557,7 +2564,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
25572564
channel_monitor.provide_latest_counterparty_commitment_tx(counterparty_initial_bitcoin_tx.txid, Vec::new(), self.context.cur_counterparty_commitment_transaction_number, self.context.counterparty_cur_commitment_point.unwrap(), logger);
25582565

25592566
assert_eq!(self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32), 0); // We have no had any monitor(s) yet to fail update!
2560-
self.context.channel_state = ChannelState::FundingSent as u32;
2567+
if is_batch_funding {
2568+
self.context.channel_state = ChannelState::FundingSent as u32 | ChannelState::WaitingForBatch as u32;
2569+
} else {
2570+
self.context.channel_state = ChannelState::FundingSent as u32;
2571+
}
25612572
self.context.cur_holder_commitment_transaction_number -= 1;
25622573
self.context.cur_counterparty_commitment_transaction_number -= 1;
25632574

@@ -2568,6 +2579,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
25682579
Ok(channel_monitor)
25692580
}
25702581

2582+
/// Updates the state of the channel to indicate that all channels in the batch
2583+
/// have received funding_signed and persisted their monitors.
2584+
/// The funding transaction is consequently allowed to be broadcast.
2585+
pub fn set_batch_ready(&mut self) {
2586+
self.context.channel_state &= !(ChannelState::WaitingForBatch as u32);
2587+
}
2588+
25712589
/// Handles a channel_ready message from our peer. If we've already sent our channel_ready
25722590
/// and the channel is now usable (and public), this may generate an announcement_signatures to
25732591
/// reply with.
@@ -3656,7 +3674,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36563674
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
36573675
// first received the funding_signed.
36583676
let mut funding_broadcastable =
3659-
if self.context.is_outbound() && self.context.channel_state & !MULTI_STATE_FLAGS >= ChannelState::FundingSent as u32 {
3677+
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 {
36603678
self.context.funding_transaction.take()
36613679
} else { None };
36623680
// That said, if the funding transaction is already confirmed (ie we're active with a
@@ -7642,7 +7660,7 @@ mod tests {
76427660
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
76437661

76447662
// Node B --> Node A: funding signed
7645-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
7663+
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
76467664

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

77717789
// Node B --> Node A: funding signed
7772-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
7790+
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
77737791

77747792
// Now disconnect the two nodes and check that the commitment point in
77757793
// Node B's channel_reestablish message is sane.
@@ -7957,7 +7975,7 @@ mod tests {
79577975
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
79587976

79597977
// Node B --> Node A: funding signed
7960-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
7978+
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
79617979

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

0 commit comments

Comments
 (0)