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