@@ -299,6 +299,10 @@ enum ChannelState {
299
299
/// We've successfully negotiated a closing_signed dance. At this point ChannelManager is about
300
300
/// to drop us, but we store this anyway.
301
301
ShutdownComplete = 4096,
302
+ /// Flag which is set on `FundingSent` to indicate this channel is funded in a batch and the
303
+ /// broadcasting of the funding transaction is being held until all channels in the batch
304
+ /// have received funding_signed and have their monitors persisted.
305
+ WaitingForBatch = 1 << 13,
302
306
}
303
307
const BOTH_SIDES_SHUTDOWN_MASK: u32 = ChannelState::LocalShutdownSent as u32 | ChannelState::RemoteShutdownSent as u32;
304
308
const MULTI_STATE_FLAGS: u32 = BOTH_SIDES_SHUTDOWN_MASK | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32;
@@ -1184,9 +1188,11 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
1184
1188
did_channel_update
1185
1189
}
1186
1190
1187
- /// Returns true if funding_created was sent/received.
1191
+ /// Returns true if funding_signed was sent/received and the
1192
+ /// funding transaction has been broadcast if necessary.
1188
1193
pub fn is_funding_initiated(&self) -> bool {
1189
- self.channel_state >= ChannelState::FundingSent as u32
1194
+ self.channel_state >= ChannelState::FundingSent as u32 &&
1195
+ self.channel_state & ChannelState::WaitingForBatch as u32 == 0
1190
1196
}
1191
1197
1192
1198
/// Transaction nomenclature is somewhat confusing here as there are many different cases - a
@@ -1917,7 +1923,8 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
1917
1923
1918
1924
/// Returns transaction if there is pending funding transaction that is yet to broadcast
1919
1925
pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
1920
- if self.channel_state & (ChannelState::FundingCreated as u32) != 0 {
1926
+ if self.channel_state & ChannelState::FundingCreated as u32 != 0 ||
1927
+ self.channel_state & ChannelState::WaitingForBatch as u32 != 0 {
1921
1928
self.funding_transaction.clone()
1922
1929
} else {
1923
1930
None
@@ -2464,7 +2471,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2464
2471
/// Handles a funding_signed message from the remote end.
2465
2472
/// If this call is successful, broadcast the funding transaction (and not before!)
2466
2473
pub fn funding_signed<SP: Deref, L: Deref>(
2467
- &mut self, msg: &msgs::FundingSigned, best_block: BestBlock, signer_provider: &SP, logger: &L
2474
+ &mut self, msg: &msgs::FundingSigned, best_block: BestBlock, signer_provider: &SP, is_batch_funding: bool, logger: &L
2468
2475
) -> Result<ChannelMonitor<Signer>, ChannelError>
2469
2476
where
2470
2477
SP::Target: SignerProvider<Signer = Signer>,
@@ -2534,7 +2541,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2534
2541
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);
2535
2542
2536
2543
assert_eq!(self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32), 0); // We have no had any monitor(s) yet to fail update!
2537
- self.context.channel_state = ChannelState::FundingSent as u32;
2544
+ if is_batch_funding {
2545
+ self.context.channel_state = ChannelState::FundingSent as u32 | ChannelState::WaitingForBatch as u32;
2546
+ } else {
2547
+ self.context.channel_state = ChannelState::FundingSent as u32;
2548
+ }
2538
2549
self.context.cur_holder_commitment_transaction_number -= 1;
2539
2550
self.context.cur_counterparty_commitment_transaction_number -= 1;
2540
2551
@@ -2545,6 +2556,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2545
2556
Ok(channel_monitor)
2546
2557
}
2547
2558
2559
+ /// Updates the state of the channel to indicate that all channels in the batch
2560
+ /// have received funding_signed and persisted their monitors.
2561
+ /// The funding transaction is consequently allowed to be broadcast.
2562
+ pub fn set_batch_ready(&mut self) {
2563
+ self.context.channel_state &= !(ChannelState::WaitingForBatch as u32);
2564
+ }
2565
+
2548
2566
/// Handles a channel_ready message from our peer. If we've already sent our channel_ready
2549
2567
/// and the channel is now usable (and public), this may generate an announcement_signatures to
2550
2568
/// reply with.
@@ -3634,7 +3652,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3634
3652
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
3635
3653
// first received the funding_signed.
3636
3654
let mut funding_broadcastable =
3637
- if self.context.is_outbound() && self.context.channel_state & !MULTI_STATE_FLAGS >= ChannelState::FundingSent as u32 {
3655
+ 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 {
3638
3656
self.context.funding_transaction.take()
3639
3657
} else { None };
3640
3658
// That said, if the funding transaction is already confirmed (ie we're active with a
@@ -7601,7 +7619,7 @@ mod tests {
7601
7619
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
7602
7620
7603
7621
// Node B --> Node A: funding signed
7604
- let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
7622
+ let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
7605
7623
7606
7624
// Put some inbound and outbound HTLCs in A's channel.
7607
7625
let htlc_amount_msat = 11_092_000; // put an amount below A's effective dust limit but above B's.
@@ -7728,7 +7746,7 @@ mod tests {
7728
7746
let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
7729
7747
7730
7748
// Node B --> Node A: funding signed
7731
- let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
7749
+ let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
7732
7750
7733
7751
// Now disconnect the two nodes and check that the commitment point in
7734
7752
// Node B's channel_reestablish message is sane.
@@ -7916,7 +7934,7 @@ mod tests {
7916
7934
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
7917
7935
7918
7936
// Node B --> Node A: funding signed
7919
- let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
7937
+ let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, false, &&logger).unwrap();
7920
7938
7921
7939
// Make sure that receiving a channel update will update the Channel as expected.
7922
7940
let update = ChannelUpdate {
0 commit comments