@@ -300,6 +300,10 @@ enum ChannelState {
300
300
/// We've successfully negotiated a closing_signed dance. At this point ChannelManager is about
301
301
/// to drop us, but we store this anyway.
302
302
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,
303
307
}
304
308
const BOTH_SIDES_SHUTDOWN_MASK: u32 = ChannelState::LocalShutdownSent as u32 | ChannelState::RemoteShutdownSent as u32;
305
309
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 {
1188
1192
did_channel_update
1189
1193
}
1190
1194
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.
1192
1197
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
1194
1200
}
1195
1201
1196
1202
/// 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 {
1922
1928
1923
1929
/// Returns transaction if there is pending funding transaction that is yet to broadcast
1924
1930
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 {
1926
1933
self.funding_transaction.clone()
1927
1934
} else {
1928
1935
None
@@ -2472,7 +2479,7 @@ impl<SP: Deref> Channel<SP> where
2472
2479
/// Handles a funding_signed message from the remote end.
2473
2480
/// If this call is successful, broadcast the funding transaction (and not before!)
2474
2481
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
2476
2483
) -> Result<ChannelMonitor<<SP::Target as SignerProvider>::Signer>, ChannelError>
2477
2484
where
2478
2485
L::Target: Logger
@@ -2547,7 +2554,11 @@ impl<SP: Deref> Channel<SP> where
2547
2554
counterparty_initial_commitment_tx.to_countersignatory_value_sat(), logger);
2548
2555
2549
2556
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
+ }
2551
2562
self.context.cur_holder_commitment_transaction_number -= 1;
2552
2563
self.context.cur_counterparty_commitment_transaction_number -= 1;
2553
2564
@@ -2558,6 +2569,13 @@ impl<SP: Deref> Channel<SP> where
2558
2569
Ok(channel_monitor)
2559
2570
}
2560
2571
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
+
2561
2579
/// Handles a channel_ready message from our peer. If we've already sent our channel_ready
2562
2580
/// and the channel is now usable (and public), this may generate an announcement_signatures to
2563
2581
/// reply with.
@@ -3674,7 +3692,7 @@ impl<SP: Deref> Channel<SP> where
3674
3692
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
3675
3693
// first received the funding_signed.
3676
3694
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 {
3678
3696
self.context.funding_transaction.take()
3679
3697
} else { None };
3680
3698
// That said, if the funding transaction is already confirmed (ie we're active with a
@@ -7687,7 +7705,7 @@ mod tests {
7687
7705
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
7688
7706
7689
7707
// 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();
7691
7709
7692
7710
// Put some inbound and outbound HTLCs in A's channel.
7693
7711
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 {
7814
7832
let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
7815
7833
7816
7834
// 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();
7818
7836
7819
7837
// Now disconnect the two nodes and check that the commitment point in
7820
7838
// Node B's channel_reestablish message is sane.
@@ -8002,7 +8020,7 @@ mod tests {
8002
8020
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
8003
8021
8004
8022
// 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();
8006
8024
8007
8025
// Make sure that receiving a channel update will update the Channel as expected.
8008
8026
let update = ChannelUpdate {
0 commit comments