@@ -804,6 +804,7 @@ pub(super) struct ChannelContext<Signer: ChannelSigner> {
804
804
805
805
pub(crate) channel_transaction_parameters: ChannelTransactionParameters,
806
806
funding_transaction: Option<Transaction>,
807
+ funding_txid: Option<Txid>,
807
808
808
809
counterparty_cur_commitment_point: Option<PublicKey>,
809
810
counterparty_prev_commitment_point: Option<PublicKey>,
@@ -1944,16 +1945,28 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
1944
1945
res
1945
1946
}
1946
1947
1947
- /// Returns transaction if there is pending funding transaction that is yet to broadcast
1948
- pub fn unbroadcasted_funding(&self ) -> Option<Transaction > {
1948
+ fn if_unbroadcasted_funding<F, O>(&self, f: F) -> Option<O>
1949
+ where F: Fn( ) -> Option<O > {
1949
1950
if self.channel_state & ChannelState::FundingCreated as u32 != 0 ||
1950
1951
self.channel_state & ChannelState::WaitingForBatch as u32 != 0 {
1951
- self.funding_transaction.clone ()
1952
+ f ()
1952
1953
} else {
1953
1954
None
1954
1955
}
1955
1956
}
1956
1957
1958
+ /// Returns the transaction if there is a pending funding transaction that is yet to be
1959
+ /// broadcast.
1960
+ pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
1961
+ self.if_unbroadcasted_funding(|| self.funding_transaction.clone())
1962
+ }
1963
+
1964
+ /// Returns the transaction ID if there is a pending funding transaction that is yet to be
1965
+ /// broadcast.
1966
+ pub fn unbroadcasted_funding_txid(&self) -> Option<Txid> {
1967
+ self.if_unbroadcasted_funding(|| self.funding_txid.clone())
1968
+ }
1969
+
1957
1970
/// Gets the latest commitment transaction and any dependent transactions for relay (forcing
1958
1971
/// shutdown of this channel - no more calls into this Channel may be made afterwards except
1959
1972
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
@@ -2613,7 +2626,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2613
2626
2614
2627
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
2615
2628
2616
- if non_shutdown_state == ChannelState::FundingSent as u32 {
2629
+ if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
2617
2630
self.context.channel_state |= ChannelState::TheirChannelReady as u32;
2618
2631
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
2619
2632
self.context.channel_state = ChannelState::ChannelReady as u32 | (self.context.channel_state & MULTI_STATE_FLAGS);
@@ -3676,6 +3689,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3676
3689
// first received the funding_signed.
3677
3690
let mut funding_broadcastable =
3678
3691
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 {
3692
+ self.context.funding_txid.take();
3679
3693
self.context.funding_transaction.take()
3680
3694
} else { None };
3681
3695
// That said, if the funding transaction is already confirmed (ie we're active with a
@@ -4565,7 +4579,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4565
4579
pub fn is_awaiting_initial_mon_persist(&self) -> bool {
4566
4580
if !self.is_awaiting_monitor_update() { return false; }
4567
4581
if self.context.channel_state &
4568
- !(ChannelState::TheirChannelReady as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)
4582
+ !(ChannelState::TheirChannelReady as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32 | ChannelState::WaitingForBatch as u32 )
4569
4583
== ChannelState::FundingSent as u32 {
4570
4584
// If we're not a 0conf channel, we'll be waiting on a monitor update with only
4571
4585
// FundingSent set, though our peer could have sent their channel_ready.
@@ -4645,6 +4659,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4645
4659
return None;
4646
4660
}
4647
4661
4662
+ // Note that we don't include ChannelState::WaitingForBatch as we don't want to send
4663
+ // channel_ready until the entire batch is ready.
4648
4664
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
4649
4665
let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
4650
4666
self.context.channel_state |= ChannelState::OurChannelReady as u32;
@@ -5699,6 +5715,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5699
5715
channel_type_features: channel_type.clone()
5700
5716
},
5701
5717
funding_transaction: None,
5718
+ funding_txid: None,
5702
5719
5703
5720
counterparty_cur_commitment_point: None,
5704
5721
counterparty_prev_commitment_point: None,
@@ -5787,6 +5804,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5787
5804
self.context.channel_state = ChannelState::FundingCreated as u32;
5788
5805
self.context.channel_id = funding_txo.to_channel_id();
5789
5806
self.context.funding_transaction = Some(funding_transaction);
5807
+ self.context.funding_txid = self.context.funding_transaction.as_ref().map(|tx| tx.txid());
5790
5808
5791
5809
let channel = Channel {
5792
5810
context: self.context,
@@ -6333,6 +6351,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6333
6351
channel_type_features: channel_type.clone()
6334
6352
},
6335
6353
funding_transaction: None,
6354
+ funding_txid: None,
6336
6355
6337
6356
counterparty_cur_commitment_point: Some(msg.first_per_commitment_point),
6338
6357
counterparty_prev_commitment_point: None,
@@ -7179,7 +7198,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7179
7198
};
7180
7199
7181
7200
let mut channel_parameters: ChannelTransactionParameters = Readable::read(reader)?;
7182
- let funding_transaction = Readable::read(reader)?;
7201
+ let funding_transaction: Option<Transaction> = Readable::read(reader)?;
7202
+ let funding_txid = funding_transaction.as_ref().map(|tx| tx.txid());
7183
7203
7184
7204
let counterparty_cur_commitment_point = Readable::read(reader)?;
7185
7205
@@ -7424,6 +7444,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7424
7444
7425
7445
channel_transaction_parameters: channel_parameters,
7426
7446
funding_transaction,
7447
+ funding_txid,
7427
7448
7428
7449
counterparty_cur_commitment_point,
7429
7450
counterparty_prev_commitment_point,
@@ -7477,7 +7498,7 @@ mod tests {
7477
7498
use crate::ln::PaymentHash;
7478
7499
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
7479
7500
use crate::ln::channel::InitFeatures;
7480
- use crate::ln::channel::{Channel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
7501
+ use crate::ln::channel::{Channel, ChannelState, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
7481
7502
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
7482
7503
use crate::ln::features::ChannelTypeFeatures;
7483
7504
use crate::ln::msgs::{ChannelUpdate, DecodeError, UnsignedChannelUpdate, MAX_VALUE_MSAT};
@@ -8952,4 +8973,148 @@ mod tests {
8952
8973
);
8953
8974
assert!(res.is_err());
8954
8975
}
8976
+
8977
+ #[test]
8978
+ fn test_waiting_for_batch() {
8979
+ let feeest = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
8980
+ let logger = test_utils::TestLogger::new();
8981
+ let secp_ctx = Secp256k1::new();
8982
+ let seed = [42; 32];
8983
+ let network = Network::Testnet;
8984
+ let best_block = BestBlock::from_network(network);
8985
+ let chain_hash = genesis_block(network).header.block_hash();
8986
+ let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
8987
+
8988
+ let mut config = UserConfig::default();
8989
+ // Set trust_own_funding_0conf while ensuring we don't send channel_ready for a
8990
+ // channel in a batch before all channels are ready.
8991
+ config.channel_handshake_limits.trust_own_funding_0conf = true;
8992
+
8993
+ // Create a channel from node a to node b that will be part of batch funding.
8994
+ let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
8995
+ let mut node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(
8996
+ &feeest,
8997
+ &&keys_provider,
8998
+ &&keys_provider,
8999
+ node_b_node_id,
9000
+ &channelmanager::provided_init_features(&config),
9001
+ 10000000,
9002
+ 100000,
9003
+ 42,
9004
+ &config,
9005
+ 0,
9006
+ 42,
9007
+ ).unwrap();
9008
+
9009
+ let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
9010
+ let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
9011
+ let mut node_b_chan = InboundV1Channel::<EnforcingSigner>::new(
9012
+ &feeest,
9013
+ &&keys_provider,
9014
+ &&keys_provider,
9015
+ node_b_node_id,
9016
+ &channelmanager::provided_channel_type_features(&config),
9017
+ &channelmanager::provided_init_features(&config),
9018
+ &open_channel_msg,
9019
+ 7,
9020
+ &config,
9021
+ 0,
9022
+ &&logger,
9023
+ 42,
9024
+ ).unwrap();
9025
+
9026
+ // Allow node b to send a 0conf channel_ready.
9027
+ node_b_chan.set_0conf();
9028
+
9029
+ let accept_channel_msg = node_b_chan.accept_inbound_channel(0);
9030
+ node_a_chan.accept_channel(
9031
+ &accept_channel_msg,
9032
+ &config.channel_handshake_limits,
9033
+ &channelmanager::provided_init_features(&config),
9034
+ ).unwrap();
9035
+
9036
+ // Fund the channel with a batch funding transaction.
9037
+ let output_script = node_a_chan.context.get_funding_redeemscript();
9038
+ let tx = Transaction {
9039
+ version: 1,
9040
+ lock_time: PackedLockTime::ZERO,
9041
+ input: Vec::new(),
9042
+ output: vec![
9043
+ TxOut {
9044
+ value: 10000000, script_pubkey: output_script.clone(),
9045
+ },
9046
+ TxOut {
9047
+ value: 10000000, script_pubkey: Builder::new().into_script(),
9048
+ },
9049
+ ]};
9050
+ let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
9051
+ let (mut node_a_chan, funding_created_msg) = node_a_chan.get_funding_created(
9052
+ tx.clone(),
9053
+ funding_outpoint,
9054
+ &&logger,
9055
+ ).map_err(|_| ()).unwrap();
9056
+ let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(
9057
+ &funding_created_msg,
9058
+ best_block,
9059
+ &&keys_provider,
9060
+ &&logger,
9061
+ ).map_err(|_| ()).unwrap();
9062
+ let node_b_updates = node_b_chan.monitor_updating_restored(
9063
+ &&logger,
9064
+ &&keys_provider,
9065
+ chain_hash,
9066
+ &config,
9067
+ 0,
9068
+ );
9069
+
9070
+ // Receive funding_signed, but the channel will be configured to hold sending channel_ready and
9071
+ // broadcasting the funding transaction until the batch is ready.
9072
+ let _ = node_a_chan.funding_signed(
9073
+ &funding_signed_msg,
9074
+ best_block,
9075
+ &&keys_provider,
9076
+ true,
9077
+ &&logger,
9078
+ ).unwrap();
9079
+ let node_a_updates = node_a_chan.monitor_updating_restored(
9080
+ &&logger,
9081
+ &&keys_provider,
9082
+ chain_hash,
9083
+ &config,
9084
+ 0,
9085
+ );
9086
+ // Our channel_ready shouldn't be sent yet, even with trust_own_funding_0conf set,
9087
+ // as the funding transaction depends on all channels in the batch becoming ready.
9088
+ assert!(node_a_updates.channel_ready.is_none());
9089
+ assert!(node_a_updates.funding_broadcastable.is_none());
9090
+ assert_eq!(
9091
+ node_a_chan.context.channel_state,
9092
+ ChannelState::FundingSent as u32 |
9093
+ ChannelState::WaitingForBatch as u32,
9094
+ );
9095
+
9096
+ // It is possible to receive a 0conf channel_ready from the remote node.
9097
+ node_a_chan.channel_ready(
9098
+ &node_b_updates.channel_ready.unwrap(),
9099
+ &&keys_provider,
9100
+ chain_hash,
9101
+ &config,
9102
+ &best_block,
9103
+ &&logger,
9104
+ ).unwrap();
9105
+ assert_eq!(
9106
+ node_a_chan.context.channel_state,
9107
+ ChannelState::FundingSent as u32 |
9108
+ ChannelState::WaitingForBatch as u32 |
9109
+ ChannelState::TheirChannelReady as u32,
9110
+ );
9111
+
9112
+ // Clear the ChannelState::WaitingForBatch only when called by ChannelManager.
9113
+ node_a_chan.set_batch_ready();
9114
+ assert_eq!(
9115
+ node_a_chan.context.channel_state,
9116
+ ChannelState::FundingSent as u32 |
9117
+ ChannelState::TheirChannelReady as u32,
9118
+ );
9119
+ }
8955
9120
}
0 commit comments