@@ -793,6 +793,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
793
793
794
794
pub(crate) channel_transaction_parameters: ChannelTransactionParameters,
795
795
funding_transaction: Option<Transaction>,
796
+ funding_txid: Option<Txid>,
796
797
797
798
counterparty_cur_commitment_point: Option<PublicKey>,
798
799
counterparty_prev_commitment_point: Option<PublicKey>,
@@ -1926,16 +1927,28 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1926
1927
res
1927
1928
}
1928
1929
1929
- /// Returns transaction if there is pending funding transaction that is yet to broadcast
1930
- pub fn unbroadcasted_funding(&self ) -> Option<Transaction > {
1930
+ fn if_unbroadcasted_funding<F, O>(&self, f: F) -> Option<O>
1931
+ where F: Fn( ) -> Option<O > {
1931
1932
if self.channel_state & ChannelState::FundingCreated as u32 != 0 ||
1932
1933
self.channel_state & ChannelState::WaitingForBatch as u32 != 0 {
1933
- self.funding_transaction.clone ()
1934
+ f ()
1934
1935
} else {
1935
1936
None
1936
1937
}
1937
1938
}
1938
1939
1940
+ /// Returns the transaction if there is a pending funding transaction that is yet to be
1941
+ /// broadcast.
1942
+ pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
1943
+ self.if_unbroadcasted_funding(|| self.funding_transaction.clone())
1944
+ }
1945
+
1946
+ /// Returns the transaction ID if there is a pending funding transaction that is yet to be
1947
+ /// broadcast.
1948
+ pub fn unbroadcasted_funding_txid(&self) -> Option<Txid> {
1949
+ self.if_unbroadcasted_funding(|| self.funding_txid.clone())
1950
+ }
1951
+
1939
1952
/// Gets the latest commitment transaction and any dependent transactions for relay (forcing
1940
1953
/// shutdown of this channel - no more calls into this Channel may be made afterwards except
1941
1954
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
@@ -2603,7 +2616,7 @@ impl<SP: Deref> Channel<SP> where
2603
2616
2604
2617
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
2605
2618
2606
- if non_shutdown_state == ChannelState::FundingSent as u32 {
2619
+ if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
2607
2620
self.context.channel_state |= ChannelState::TheirChannelReady as u32;
2608
2621
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
2609
2622
self.context.channel_state = ChannelState::ChannelReady as u32 | (self.context.channel_state & MULTI_STATE_FLAGS);
@@ -3693,6 +3706,7 @@ impl<SP: Deref> Channel<SP> where
3693
3706
// first received the funding_signed.
3694
3707
let mut funding_broadcastable =
3695
3708
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 {
3709
+ self.context.funding_txid.take();
3696
3710
self.context.funding_transaction.take()
3697
3711
} else { None };
3698
3712
// That said, if the funding transaction is already confirmed (ie we're active with a
@@ -4594,7 +4608,7 @@ impl<SP: Deref> Channel<SP> where
4594
4608
pub fn is_awaiting_initial_mon_persist(&self) -> bool {
4595
4609
if !self.is_awaiting_monitor_update() { return false; }
4596
4610
if self.context.channel_state &
4597
- !(ChannelState::TheirChannelReady as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)
4611
+ !(ChannelState::TheirChannelReady as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32 | ChannelState::WaitingForBatch as u32 )
4598
4612
== ChannelState::FundingSent as u32 {
4599
4613
// If we're not a 0conf channel, we'll be waiting on a monitor update with only
4600
4614
// FundingSent set, though our peer could have sent their channel_ready.
@@ -4674,6 +4688,8 @@ impl<SP: Deref> Channel<SP> where
4674
4688
return None;
4675
4689
}
4676
4690
4691
+ // Note that we don't include ChannelState::WaitingForBatch as we don't want to send
4692
+ // channel_ready until the entire batch is ready.
4677
4693
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
4678
4694
let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
4679
4695
self.context.channel_state |= ChannelState::OurChannelReady as u32;
@@ -5747,6 +5763,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
5747
5763
channel_type_features: channel_type.clone()
5748
5764
},
5749
5765
funding_transaction: None,
5766
+ funding_txid: None,
5750
5767
5751
5768
counterparty_cur_commitment_point: None,
5752
5769
counterparty_prev_commitment_point: None,
@@ -5840,6 +5857,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
5840
5857
self.context.channel_state = ChannelState::FundingCreated as u32;
5841
5858
self.context.channel_id = funding_txo.to_channel_id();
5842
5859
self.context.funding_transaction = Some(funding_transaction);
5860
+ self.context.funding_txid = self.context.funding_transaction.as_ref().map(|tx| tx.txid());
5843
5861
5844
5862
let channel = Channel {
5845
5863
context: self.context,
@@ -6389,6 +6407,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6389
6407
channel_type_features: channel_type.clone()
6390
6408
},
6391
6409
funding_transaction: None,
6410
+ funding_txid: None,
6392
6411
6393
6412
counterparty_cur_commitment_point: Some(msg.first_per_commitment_point),
6394
6413
counterparty_prev_commitment_point: None,
@@ -7226,7 +7245,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7226
7245
};
7227
7246
7228
7247
let mut channel_parameters: ChannelTransactionParameters = Readable::read(reader)?;
7229
- let funding_transaction = Readable::read(reader)?;
7248
+ let funding_transaction: Option<Transaction> = Readable::read(reader)?;
7249
+ let funding_txid = funding_transaction.as_ref().map(|tx| tx.txid());
7230
7250
7231
7251
let counterparty_cur_commitment_point = Readable::read(reader)?;
7232
7252
@@ -7469,6 +7489,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7469
7489
7470
7490
channel_transaction_parameters: channel_parameters,
7471
7491
funding_transaction,
7492
+ funding_txid,
7472
7493
7473
7494
counterparty_cur_commitment_point,
7474
7495
counterparty_prev_commitment_point,
@@ -7522,7 +7543,7 @@ mod tests {
7522
7543
use crate::ln::PaymentHash;
7523
7544
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
7524
7545
use crate::ln::channel::InitFeatures;
7525
- use crate::ln::channel::{Channel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
7546
+ use crate::ln::channel::{Channel, ChannelState, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
7526
7547
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
7527
7548
use crate::ln::features::ChannelTypeFeatures;
7528
7549
use crate::ln::msgs::{ChannelUpdate, DecodeError, UnsignedChannelUpdate, MAX_VALUE_MSAT};
@@ -8996,4 +9017,148 @@ mod tests {
8996
9017
);
8997
9018
assert!(res.is_err());
8998
9019
}
9020
+
9021
+ #[test]
9022
+ fn test_waiting_for_batch() {
9023
+ let feeest = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
9024
+ let logger = test_utils::TestLogger::new();
9025
+ let secp_ctx = Secp256k1::new();
9026
+ let seed = [42; 32];
9027
+ let network = Network::Testnet;
9028
+ let best_block = BestBlock::from_network(network);
9029
+ let chain_hash = genesis_block(network).header.block_hash();
9030
+ let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
9031
+
9032
+ let mut config = UserConfig::default();
9033
+ // Set trust_own_funding_0conf while ensuring we don't send channel_ready for a
9034
+ // channel in a batch before all channels are ready.
9035
+ config.channel_handshake_limits.trust_own_funding_0conf = true;
9036
+
9037
+ // Create a channel from node a to node b that will be part of batch funding.
9038
+ let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
9039
+ let mut node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(
9040
+ &feeest,
9041
+ &&keys_provider,
9042
+ &&keys_provider,
9043
+ node_b_node_id,
9044
+ &channelmanager::provided_init_features(&config),
9045
+ 10000000,
9046
+ 100000,
9047
+ 42,
9048
+ &config,
9049
+ 0,
9050
+ 42,
9051
+ ).unwrap();
9052
+
9053
+ let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
9054
+ let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
9055
+ let mut node_b_chan = InboundV1Channel::<EnforcingSigner>::new(
9056
+ &feeest,
9057
+ &&keys_provider,
9058
+ &&keys_provider,
9059
+ node_b_node_id,
9060
+ &channelmanager::provided_channel_type_features(&config),
9061
+ &channelmanager::provided_init_features(&config),
9062
+ &open_channel_msg,
9063
+ 7,
9064
+ &config,
9065
+ 0,
9066
+ &&logger,
9067
+ 42,
9068
+ ).unwrap();
9069
+
9070
+ // Allow node b to send a 0conf channel_ready.
9071
+ node_b_chan.set_0conf();
9072
+
9073
+ let accept_channel_msg = node_b_chan.accept_inbound_channel(0);
9074
+ node_a_chan.accept_channel(
9075
+ &accept_channel_msg,
9076
+ &config.channel_handshake_limits,
9077
+ &channelmanager::provided_init_features(&config),
9078
+ ).unwrap();
9079
+
9080
+ // Fund the channel with a batch funding transaction.
9081
+ let output_script = node_a_chan.context.get_funding_redeemscript();
9082
+ let tx = Transaction {
9083
+ version: 1,
9084
+ lock_time: PackedLockTime::ZERO,
9085
+ input: Vec::new(),
9086
+ output: vec![
9087
+ TxOut {
9088
+ value: 10000000, script_pubkey: output_script.clone(),
9089
+ },
9090
+ TxOut {
9091
+ value: 10000000, script_pubkey: Builder::new().into_script(),
9092
+ },
9093
+ ]};
9094
+ let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
9095
+ let (mut node_a_chan, funding_created_msg) = node_a_chan.get_funding_created(
9096
+ tx.clone(),
9097
+ funding_outpoint,
9098
+ &&logger,
9099
+ ).map_err(|_| ()).unwrap();
9100
+ let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(
9101
+ &funding_created_msg,
9102
+ best_block,
9103
+ &&keys_provider,
9104
+ &&logger,
9105
+ ).map_err(|_| ()).unwrap();
9106
+ let node_b_updates = node_b_chan.monitor_updating_restored(
9107
+ &&logger,
9108
+ &&keys_provider,
9109
+ chain_hash,
9110
+ &config,
9111
+ 0,
9112
+ );
9113
+
9114
+ // Receive funding_signed, but the channel will be configured to hold sending channel_ready and
9115
+ // broadcasting the funding transaction until the batch is ready.
9116
+ let _ = node_a_chan.funding_signed(
9117
+ &funding_signed_msg,
9118
+ best_block,
9119
+ &&keys_provider,
9120
+ true,
9121
+ &&logger,
9122
+ ).unwrap();
9123
+ let node_a_updates = node_a_chan.monitor_updating_restored(
9124
+ &&logger,
9125
+ &&keys_provider,
9126
+ chain_hash,
9127
+ &config,
9128
+ 0,
9129
+ );
9130
+ // Our channel_ready shouldn't be sent yet, even with trust_own_funding_0conf set,
9131
+ // as the funding transaction depends on all channels in the batch becoming ready.
9132
+ assert!(node_a_updates.channel_ready.is_none());
9133
+ assert!(node_a_updates.funding_broadcastable.is_none());
9134
+ assert_eq!(
9135
+ node_a_chan.context.channel_state,
9136
+ ChannelState::FundingSent as u32 |
9137
+ ChannelState::WaitingForBatch as u32,
9138
+ );
9139
+
9140
+ // It is possible to receive a 0conf channel_ready from the remote node.
9141
+ node_a_chan.channel_ready(
9142
+ &node_b_updates.channel_ready.unwrap(),
9143
+ &&keys_provider,
9144
+ chain_hash,
9145
+ &config,
9146
+ &best_block,
9147
+ &&logger,
9148
+ ).unwrap();
9149
+ assert_eq!(
9150
+ node_a_chan.context.channel_state,
9151
+ ChannelState::FundingSent as u32 |
9152
+ ChannelState::WaitingForBatch as u32 |
9153
+ ChannelState::TheirChannelReady as u32,
9154
+ );
9155
+
9156
+ // Clear the ChannelState::WaitingForBatch only when called by ChannelManager.
9157
+ node_a_chan.set_batch_ready();
9158
+ assert_eq!(
9159
+ node_a_chan.context.channel_state,
9160
+ ChannelState::FundingSent as u32 |
9161
+ ChannelState::TheirChannelReady as u32,
9162
+ );
9163
+ }
8999
9164
}
0 commit comments