@@ -789,6 +789,7 @@ pub(super) struct ChannelContext<Signer: ChannelSigner> {
789
789
790
790
pub(crate) channel_transaction_parameters: ChannelTransactionParameters,
791
791
funding_transaction: Option<Transaction>,
792
+ funding_txid: Option<Txid>,
792
793
793
794
counterparty_cur_commitment_point: Option<PublicKey>,
794
795
counterparty_prev_commitment_point: Option<PublicKey>,
@@ -1921,16 +1922,28 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
1921
1922
res
1922
1923
}
1923
1924
1924
- /// Returns transaction if there is pending funding transaction that is yet to broadcast
1925
- pub fn unbroadcasted_funding(&self ) -> Option<Transaction > {
1925
+ fn if_unbroadcasted_funding<F, O>(&self, f: F) -> Option<O>
1926
+ where F: Fn( ) -> Option<O > {
1926
1927
if self.channel_state & ChannelState::FundingCreated as u32 != 0 ||
1927
1928
self.channel_state & ChannelState::WaitingForBatch as u32 != 0 {
1928
- self.funding_transaction.clone ()
1929
+ f ()
1929
1930
} else {
1930
1931
None
1931
1932
}
1932
1933
}
1933
1934
1935
+ /// Returns the transaction if there is a pending funding transaction that is yet to be
1936
+ /// broadcast.
1937
+ pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
1938
+ self.if_unbroadcasted_funding(|| self.funding_transaction.clone())
1939
+ }
1940
+
1941
+ /// Returns the transaction ID if there is a pending funding transaction that is yet to be
1942
+ /// broadcast.
1943
+ pub fn unbroadcasted_funding_txid(&self) -> Option<Txid> {
1944
+ self.if_unbroadcasted_funding(|| self.funding_txid.clone())
1945
+ }
1946
+
1934
1947
/// Gets the latest commitment transaction and any dependent transactions for relay (forcing
1935
1948
/// shutdown of this channel - no more calls into this Channel may be made afterwards except
1936
1949
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
@@ -2590,7 +2603,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2590
2603
2591
2604
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
2592
2605
2593
- if non_shutdown_state == ChannelState::FundingSent as u32 {
2606
+ if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
2594
2607
self.context.channel_state |= ChannelState::TheirChannelReady as u32;
2595
2608
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
2596
2609
self.context.channel_state = ChannelState::ChannelReady as u32 | (self.context.channel_state & MULTI_STATE_FLAGS);
@@ -3653,6 +3666,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3653
3666
// first received the funding_signed.
3654
3667
let mut funding_broadcastable =
3655
3668
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 {
3669
+ self.context.funding_txid.take();
3656
3670
self.context.funding_transaction.take()
3657
3671
} else { None };
3658
3672
// That said, if the funding transaction is already confirmed (ie we're active with a
@@ -4542,7 +4556,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4542
4556
pub fn is_awaiting_initial_mon_persist(&self) -> bool {
4543
4557
if !self.is_awaiting_monitor_update() { return false; }
4544
4558
if self.context.channel_state &
4545
- !(ChannelState::TheirChannelReady as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)
4559
+ !(ChannelState::TheirChannelReady as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32 | ChannelState::WaitingForBatch as u32 )
4546
4560
== ChannelState::FundingSent as u32 {
4547
4561
// If we're not a 0conf channel, we'll be waiting on a monitor update with only
4548
4562
// FundingSent set, though our peer could have sent their channel_ready.
@@ -4622,6 +4636,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4622
4636
return None;
4623
4637
}
4624
4638
4639
+ // Note that we don't include ChannelState::WaitingForBatch as we don't want to send
4640
+ // channel_ready until the entire batch is ready.
4625
4641
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
4626
4642
let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
4627
4643
self.context.channel_state |= ChannelState::OurChannelReady as u32;
@@ -5674,6 +5690,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5674
5690
channel_type_features: channel_type.clone()
5675
5691
},
5676
5692
funding_transaction: None,
5693
+ funding_txid: None,
5677
5694
5678
5695
counterparty_cur_commitment_point: None,
5679
5696
counterparty_prev_commitment_point: None,
@@ -5762,6 +5779,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5762
5779
self.context.channel_state = ChannelState::FundingCreated as u32;
5763
5780
self.context.channel_id = funding_txo.to_channel_id();
5764
5781
self.context.funding_transaction = Some(funding_transaction);
5782
+ self.context.funding_txid = self.context.funding_transaction.as_ref().map(|tx| tx.txid());
5765
5783
5766
5784
let channel = Channel {
5767
5785
context: self.context,
@@ -6312,6 +6330,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6312
6330
channel_type_features: channel_type.clone()
6313
6331
},
6314
6332
funding_transaction: None,
6333
+ funding_txid: None,
6315
6334
6316
6335
counterparty_cur_commitment_point: Some(msg.first_per_commitment_point),
6317
6336
counterparty_prev_commitment_point: None,
@@ -7139,7 +7158,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7139
7158
};
7140
7159
7141
7160
let mut channel_parameters: ChannelTransactionParameters = Readable::read(reader)?;
7142
- let funding_transaction = Readable::read(reader)?;
7161
+ let funding_transaction: Option<Transaction> = Readable::read(reader)?;
7162
+ let funding_txid = funding_transaction.as_ref().map(|tx| tx.txid());
7143
7163
7144
7164
let counterparty_cur_commitment_point = Readable::read(reader)?;
7145
7165
@@ -7382,6 +7402,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7382
7402
7383
7403
channel_transaction_parameters: channel_parameters,
7384
7404
funding_transaction,
7405
+ funding_txid,
7385
7406
7386
7407
counterparty_cur_commitment_point,
7387
7408
counterparty_prev_commitment_point,
@@ -7435,7 +7456,7 @@ mod tests {
7435
7456
use crate::ln::PaymentHash;
7436
7457
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
7437
7458
use crate::ln::channel::InitFeatures;
7438
- use crate::ln::channel::{Channel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
7459
+ use crate::ln::channel::{Channel, ChannelState, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
7439
7460
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
7440
7461
use crate::ln::features::ChannelTypeFeatures;
7441
7462
use crate::ln::msgs::{ChannelUpdate, DecodeError, UnsignedChannelUpdate, MAX_VALUE_MSAT};
@@ -8910,4 +8931,148 @@ mod tests {
8910
8931
);
8911
8932
assert!(res.is_err());
8912
8933
}
8934
+
8935
+ #[test]
8936
+ fn test_waiting_for_batch() {
8937
+ let feeest = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
8938
+ let logger = test_utils::TestLogger::new();
8939
+ let secp_ctx = Secp256k1::new();
8940
+ let seed = [42; 32];
8941
+ let network = Network::Testnet;
8942
+ let best_block = BestBlock::from_network(network);
8943
+ let chain_hash = genesis_block(network).header.block_hash();
8944
+ let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
8945
+
8946
+ let mut config = UserConfig::default();
8947
+ // Set trust_own_funding_0conf while ensuring we don't send channel_ready for a
8948
+ // channel in a batch before all channels are ready.
8949
+ config.channel_handshake_limits.trust_own_funding_0conf = true;
8950
+
8951
+ // Create a channel from node a to node b that will be part of batch funding.
8952
+ let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
8953
+ let mut node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(
8954
+ &feeest,
8955
+ &&keys_provider,
8956
+ &&keys_provider,
8957
+ node_b_node_id,
8958
+ &channelmanager::provided_init_features(&config),
8959
+ 10000000,
8960
+ 100000,
8961
+ 42,
8962
+ &config,
8963
+ 0,
8964
+ 42,
8965
+ ).unwrap();
8966
+
8967
+ let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
8968
+ let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
8969
+ let mut node_b_chan = InboundV1Channel::<EnforcingSigner>::new(
8970
+ &feeest,
8971
+ &&keys_provider,
8972
+ &&keys_provider,
8973
+ node_b_node_id,
8974
+ &channelmanager::provided_channel_type_features(&config),
8975
+ &channelmanager::provided_init_features(&config),
8976
+ &open_channel_msg,
8977
+ 7,
8978
+ &config,
8979
+ 0,
8980
+ &&logger,
8981
+ 42,
8982
+ ).unwrap();
8983
+
8984
+ // Allow node b to send a 0conf channel_ready.
8985
+ node_b_chan.set_0conf();
8986
+
8987
+ let accept_channel_msg = node_b_chan.accept_inbound_channel(0);
8988
+ node_a_chan.accept_channel(
8989
+ &accept_channel_msg,
8990
+ &config.channel_handshake_limits,
8991
+ &channelmanager::provided_init_features(&config),
8992
+ ).unwrap();
8993
+
8994
+ // Fund the channel with a batch funding transaction.
8995
+ let output_script = node_a_chan.context.get_funding_redeemscript();
8996
+ let tx = Transaction {
8997
+ version: 1,
8998
+ lock_time: PackedLockTime::ZERO,
8999
+ input: Vec::new(),
9000
+ output: vec![
9001
+ TxOut {
9002
+ value: 10000000, script_pubkey: output_script.clone(),
9003
+ },
9004
+ TxOut {
9005
+ value: 10000000, script_pubkey: Builder::new().into_script(),
9006
+ },
9007
+ ]};
9008
+ let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
9009
+ let (mut node_a_chan, funding_created_msg) = node_a_chan.get_funding_created(
9010
+ tx.clone(),
9011
+ funding_outpoint,
9012
+ &&logger,
9013
+ ).map_err(|_| ()).unwrap();
9014
+ let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(
9015
+ &funding_created_msg,
9016
+ best_block,
9017
+ &&keys_provider,
9018
+ &&logger,
9019
+ ).map_err(|_| ()).unwrap();
9020
+ let node_b_updates = node_b_chan.monitor_updating_restored(
9021
+ &&logger,
9022
+ &&keys_provider,
9023
+ chain_hash,
9024
+ &config,
9025
+ 0,
9026
+ );
9027
+
9028
+ // Receive funding_signed, but the channel will be configured to hold sending channel_ready and
9029
+ // broadcasting the funding transaction until the batch is ready.
9030
+ let _ = node_a_chan.funding_signed(
9031
+ &funding_signed_msg,
9032
+ best_block,
9033
+ &&keys_provider,
9034
+ true,
9035
+ &&logger,
9036
+ ).unwrap();
9037
+ let node_a_updates = node_a_chan.monitor_updating_restored(
9038
+ &&logger,
9039
+ &&keys_provider,
9040
+ chain_hash,
9041
+ &config,
9042
+ 0,
9043
+ );
9044
+ // Our channel_ready shouldn't be sent yet, even with trust_own_funding_0conf set,
9045
+ // as the funding transaction depends on all channels in the batch becoming ready.
9046
+ assert!(node_a_updates.channel_ready.is_none());
9047
+ assert!(node_a_updates.funding_broadcastable.is_none());
9048
+ assert_eq!(
9049
+ node_a_chan.context.channel_state,
9050
+ ChannelState::FundingSent as u32 |
9051
+ ChannelState::WaitingForBatch as u32,
9052
+ );
9053
+
9054
+ // It is possible to receive a 0conf channel_ready from the remote node.
9055
+ node_a_chan.channel_ready(
9056
+ &node_b_updates.channel_ready.unwrap(),
9057
+ &&keys_provider,
9058
+ chain_hash,
9059
+ &config,
9060
+ &best_block,
9061
+ &&logger,
9062
+ ).unwrap();
9063
+ assert_eq!(
9064
+ node_a_chan.context.channel_state,
9065
+ ChannelState::FundingSent as u32 |
9066
+ ChannelState::WaitingForBatch as u32 |
9067
+ ChannelState::TheirChannelReady as u32,
9068
+ );
9069
+
9070
+ // Clear the ChannelState::WaitingForBatch only when called by ChannelManager.
9071
+ node_a_chan.set_batch_ready();
9072
+ assert_eq!(
9073
+ node_a_chan.context.channel_state,
9074
+ ChannelState::FundingSent as u32 |
9075
+ ChannelState::TheirChannelReady as u32,
9076
+ );
9077
+ }
8913
9078
}
0 commit comments