@@ -301,6 +301,10 @@ enum ChannelState {
301
301
/// We've successfully negotiated a closing_signed dance. At this point ChannelManager is about
302
302
/// to drop us, but we store this anyway.
303
303
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,
304
308
}
305
309
const BOTH_SIDES_SHUTDOWN_MASK: u32 = ChannelState::LocalShutdownSent as u32 | ChannelState::RemoteShutdownSent as u32;
306
310
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> {
1198
1202
did_channel_update
1199
1203
}
1200
1204
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.
1202
1207
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
1204
1210
}
1205
1211
1206
1212
/// Transaction nomenclature is somewhat confusing here as there are many different cases - a
@@ -1940,7 +1946,8 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
1940
1946
1941
1947
/// Returns transaction if there is pending funding transaction that is yet to broadcast
1942
1948
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 {
1944
1951
self.funding_transaction.clone()
1945
1952
} else {
1946
1953
None
@@ -2487,7 +2494,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2487
2494
/// Handles a funding_signed message from the remote end.
2488
2495
/// If this call is successful, broadcast the funding transaction (and not before!)
2489
2496
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
2491
2498
) -> Result<ChannelMonitor<Signer>, ChannelError>
2492
2499
where
2493
2500
SP::Target: SignerProvider<Signer = Signer>,
@@ -2557,7 +2564,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2557
2564
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);
2558
2565
2559
2566
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
+ }
2561
2572
self.context.cur_holder_commitment_transaction_number -= 1;
2562
2573
self.context.cur_counterparty_commitment_transaction_number -= 1;
2563
2574
@@ -2568,6 +2579,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2568
2579
Ok(channel_monitor)
2569
2580
}
2570
2581
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
+
2571
2589
/// Handles a channel_ready message from our peer. If we've already sent our channel_ready
2572
2590
/// and the channel is now usable (and public), this may generate an announcement_signatures to
2573
2591
/// reply with.
@@ -3656,7 +3674,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3656
3674
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
3657
3675
// first received the funding_signed.
3658
3676
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 {
3660
3678
self.context.funding_transaction.take()
3661
3679
} else { None };
3662
3680
// That said, if the funding transaction is already confirmed (ie we're active with a
@@ -7642,7 +7660,7 @@ mod tests {
7642
7660
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
7643
7661
7644
7662
// 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();
7646
7664
7647
7665
// Put some inbound and outbound HTLCs in A's channel.
7648
7666
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 {
7769
7787
let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
7770
7788
7771
7789
// 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();
7773
7791
7774
7792
// Now disconnect the two nodes and check that the commitment point in
7775
7793
// Node B's channel_reestablish message is sane.
@@ -7957,7 +7975,7 @@ mod tests {
7957
7975
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
7958
7976
7959
7977
// 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();
7961
7979
7962
7980
// Make sure that receiving a channel update will update the Channel as expected.
7963
7981
let update = ChannelUpdate {
0 commit comments