@@ -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;
@@ -1185,9 +1189,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1185
1189
did_channel_update
1186
1190
}
1187
1191
1188
- /// Returns true if funding_created was sent/received.
1192
+ /// Returns true if funding_signed was sent/received and the
1193
+ /// funding transaction has been broadcast if necessary.
1189
1194
pub fn is_funding_initiated(&self) -> bool {
1190
- self.channel_state >= ChannelState::FundingSent as u32
1195
+ self.channel_state >= ChannelState::FundingSent as u32 &&
1196
+ self.channel_state & ChannelState::WaitingForBatch as u32 == 0
1191
1197
}
1192
1198
1193
1199
/// Transaction nomenclature is somewhat confusing here as there are many different cases - a
@@ -1918,7 +1924,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1918
1924
1919
1925
/// Returns transaction if there is pending funding transaction that is yet to broadcast
1920
1926
pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
1921
- if self.channel_state & (ChannelState::FundingCreated as u32) != 0 {
1927
+ if self.channel_state & ChannelState::FundingCreated as u32 != 0 ||
1928
+ self.channel_state & ChannelState::WaitingForBatch as u32 != 0 {
1922
1929
self.funding_transaction.clone()
1923
1930
} else {
1924
1931
None
@@ -2468,7 +2475,7 @@ impl<SP: Deref> Channel<SP> where
2468
2475
/// Handles a funding_signed message from the remote end.
2469
2476
/// If this call is successful, broadcast the funding transaction (and not before!)
2470
2477
pub fn funding_signed<L: Deref>(
2471
- &mut self, msg: &msgs::FundingSigned, best_block: BestBlock, signer_provider: &SP, logger: &L
2478
+ &mut self, msg: &msgs::FundingSigned, best_block: BestBlock, signer_provider: &SP, is_batch_funding: bool, logger: &L
2472
2479
) -> Result<ChannelMonitor<<SP::Target as SignerProvider>::Signer>, ChannelError>
2473
2480
where
2474
2481
L::Target: Logger
@@ -2543,7 +2550,11 @@ impl<SP: Deref> Channel<SP> where
2543
2550
counterparty_initial_commitment_tx.to_countersignatory_value_sat(), logger);
2544
2551
2545
2552
assert_eq!(self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32), 0); // We have no had any monitor(s) yet to fail update!
2546
- self.context.channel_state = ChannelState::FundingSent as u32;
2553
+ if is_batch_funding {
2554
+ self.context.channel_state = ChannelState::FundingSent as u32 | ChannelState::WaitingForBatch as u32;
2555
+ } else {
2556
+ self.context.channel_state = ChannelState::FundingSent as u32;
2557
+ }
2547
2558
self.context.cur_holder_commitment_transaction_number -= 1;
2548
2559
self.context.cur_counterparty_commitment_transaction_number -= 1;
2549
2560
@@ -2554,6 +2565,13 @@ impl<SP: Deref> Channel<SP> where
2554
2565
Ok(channel_monitor)
2555
2566
}
2556
2567
2568
+ /// Updates the state of the channel to indicate that all channels in the batch
2569
+ /// have received funding_signed and persisted their monitors.
2570
+ /// The funding transaction is consequently allowed to be broadcast.
2571
+ pub fn set_batch_ready(&mut self) {
2572
+ self.context.channel_state &= !(ChannelState::WaitingForBatch as u32);
2573
+ }
2574
+
2557
2575
/// Handles a channel_ready message from our peer. If we've already sent our channel_ready
2558
2576
/// and the channel is now usable (and public), this may generate an announcement_signatures to
2559
2577
/// reply with.
@@ -3671,7 +3689,7 @@ impl<SP: Deref> Channel<SP> where
3671
3689
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
3672
3690
// first received the funding_signed.
3673
3691
let mut funding_broadcastable =
3674
- if self.context.is_outbound() && self.context.channel_state & !MULTI_STATE_FLAGS >= ChannelState::FundingSent as u32 {
3692
+ 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 {
3675
3693
self.context.funding_transaction.take()
3676
3694
} else { None };
3677
3695
// That said, if the funding transaction is already confirmed (ie we're active with a
@@ -7684,7 +7702,7 @@ mod tests {
7684
7702
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
7685
7703
7686
7704
// Node B --> Node A: funding signed
7687
- let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
7705
+ let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
7688
7706
7689
7707
// Put some inbound and outbound HTLCs in A's channel.
7690
7708
let htlc_amount_msat = 11_092_000; // put an amount below A's effective dust limit but above B's.
@@ -7811,7 +7829,7 @@ mod tests {
7811
7829
let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
7812
7830
7813
7831
// Node B --> Node A: funding signed
7814
- let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
7832
+ let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
7815
7833
7816
7834
// Now disconnect the two nodes and check that the commitment point in
7817
7835
// Node B's channel_reestablish message is sane.
@@ -7999,7 +8017,7 @@ mod tests {
7999
8017
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
8000
8018
8001
8019
// Node B --> Node A: funding signed
8002
- let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
8020
+ let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
8003
8021
8004
8022
// Make sure that receiving a channel update will update the Channel as expected.
8005
8023
let update = ChannelUpdate {
0 commit comments