@@ -1944,16 +1944,28 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
1944
1944
res
1945
1945
}
1946
1946
1947
- /// Returns transaction if there is pending funding transaction that is yet to broadcast
1948
- pub fn unbroadcasted_funding(&self ) -> Option<Transaction> {
1947
+ fn map_unbroadcasted_funding<F, R>(&self, f: F) -> Option<R>
1948
+ where F: Fn(&Transaction ) -> R {
1949
1949
if self.channel_state & ChannelState::FundingCreated as u32 != 0 ||
1950
1950
self.channel_state & ChannelState::WaitingForBatch as u32 != 0 {
1951
- self.funding_transaction.clone( )
1951
+ self.funding_transaction.as_ref().map(f )
1952
1952
} else {
1953
1953
None
1954
1954
}
1955
1955
}
1956
1956
1957
+ /// Returns the transaction if there is a pending funding transaction that is yet to be
1958
+ /// broadcast.
1959
+ pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
1960
+ self.map_unbroadcasted_funding(|tx| tx.clone())
1961
+ }
1962
+
1963
+ /// Returns the transaction ID if there is a pending funding transaction that is yet to be
1964
+ /// broadcast.
1965
+ pub fn unbroadcasted_funding_txid(&self) -> Option<Txid> {
1966
+ self.map_unbroadcasted_funding(|tx| tx.txid())
1967
+ }
1968
+
1957
1969
/// Gets the latest commitment transaction and any dependent transactions for relay (forcing
1958
1970
/// shutdown of this channel - no more calls into this Channel may be made afterwards except
1959
1971
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
@@ -2613,7 +2625,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2613
2625
2614
2626
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
2615
2627
2616
- if non_shutdown_state == ChannelState::FundingSent as u32 {
2628
+ if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
2617
2629
self.context.channel_state |= ChannelState::TheirChannelReady as u32;
2618
2630
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
2619
2631
self.context.channel_state = ChannelState::ChannelReady as u32 | (self.context.channel_state & MULTI_STATE_FLAGS);
@@ -4565,7 +4577,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4565
4577
pub fn is_awaiting_initial_mon_persist(&self) -> bool {
4566
4578
if !self.is_awaiting_monitor_update() { return false; }
4567
4579
if self.context.channel_state &
4568
- !(ChannelState::TheirChannelReady as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)
4580
+ !(ChannelState::TheirChannelReady as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32 | ChannelState::WaitingForBatch as u32 )
4569
4581
== ChannelState::FundingSent as u32 {
4570
4582
// If we're not a 0conf channel, we'll be waiting on a monitor update with only
4571
4583
// FundingSent set, though our peer could have sent their channel_ready.
@@ -4645,6 +4657,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4645
4657
return None;
4646
4658
}
4647
4659
4660
+ // Note that we don't include ChannelState::WaitingForBatch as we don't want to send
4661
+ // channel_ready until the entire batch is ready.
4648
4662
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
4649
4663
let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
4650
4664
self.context.channel_state |= ChannelState::OurChannelReady as u32;
@@ -7477,7 +7491,7 @@ mod tests {
7477
7491
use crate::ln::PaymentHash;
7478
7492
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
7479
7493
use crate::ln::channel::InitFeatures;
7480
- use crate::ln::channel::{Channel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
7494
+ use crate::ln::channel::{Channel, ChannelState, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
7481
7495
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
7482
7496
use crate::ln::features::ChannelTypeFeatures;
7483
7497
use crate::ln::msgs::{ChannelUpdate, DecodeError, UnsignedChannelUpdate, MAX_VALUE_MSAT};
@@ -8952,4 +8966,148 @@ mod tests {
8952
8966
);
8953
8967
assert!(res.is_err());
8954
8968
}
8969
+
8970
+ #[test]
8971
+ fn test_waiting_for_batch() {
8972
+ let feeest = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
8973
+ let logger = test_utils::TestLogger::new();
8974
+ let secp_ctx = Secp256k1::new();
8975
+ let seed = [42; 32];
8976
+ let network = Network::Testnet;
8977
+ let best_block = BestBlock::from_network(network);
8978
+ let chain_hash = genesis_block(network).header.block_hash();
8979
+ let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
8980
+
8981
+ let mut config = UserConfig::default();
8982
+ // Set trust_own_funding_0conf while ensuring we don't send channel_ready for a
8983
+ // channel in a batch before all channels are ready.
8984
+ config.channel_handshake_limits.trust_own_funding_0conf = true;
8985
+
8986
+ // Create a channel from node a to node b that will be part of batch funding.
8987
+ let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
8988
+ let mut node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(
8989
+ &feeest,
8990
+ &&keys_provider,
8991
+ &&keys_provider,
8992
+ node_b_node_id,
8993
+ &channelmanager::provided_init_features(&config),
8994
+ 10000000,
8995
+ 100000,
8996
+ 42,
8997
+ &config,
8998
+ 0,
8999
+ 42,
9000
+ ).unwrap();
9001
+
9002
+ let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
9003
+ let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
9004
+ let mut node_b_chan = InboundV1Channel::<EnforcingSigner>::new(
9005
+ &feeest,
9006
+ &&keys_provider,
9007
+ &&keys_provider,
9008
+ node_b_node_id,
9009
+ &channelmanager::provided_channel_type_features(&config),
9010
+ &channelmanager::provided_init_features(&config),
9011
+ &open_channel_msg,
9012
+ 7,
9013
+ &config,
9014
+ 0,
9015
+ &&logger,
9016
+ 42,
9017
+ ).unwrap();
9018
+
9019
+ // Allow node b to send a 0conf channel_ready.
9020
+ node_b_chan.set_0conf();
9021
+
9022
+ let accept_channel_msg = node_b_chan.accept_inbound_channel(0);
9023
+ node_a_chan.accept_channel(
9024
+ &accept_channel_msg,
9025
+ &config.channel_handshake_limits,
9026
+ &channelmanager::provided_init_features(&config),
9027
+ ).unwrap();
9028
+
9029
+ // Fund the channel with a batch funding transaction.
9030
+ let output_script = node_a_chan.context.get_funding_redeemscript();
9031
+ let tx = Transaction {
9032
+ version: 1,
9033
+ lock_time: PackedLockTime::ZERO,
9034
+ input: Vec::new(),
9035
+ output: vec![
9036
+ TxOut {
9037
+ value: 10000000, script_pubkey: output_script.clone(),
9038
+ },
9039
+ TxOut {
9040
+ value: 10000000, script_pubkey: Builder::new().into_script(),
9041
+ },
9042
+ ]};
9043
+ let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
9044
+ let (mut node_a_chan, funding_created_msg) = node_a_chan.get_funding_created(
9045
+ tx.clone(),
9046
+ funding_outpoint,
9047
+ &&logger,
9048
+ ).map_err(|_| ()).unwrap();
9049
+ let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(
9050
+ &funding_created_msg,
9051
+ best_block,
9052
+ &&keys_provider,
9053
+ &&logger,
9054
+ ).map_err(|_| ()).unwrap();
9055
+ let node_b_updates = node_b_chan.monitor_updating_restored(
9056
+ &&logger,
9057
+ &&keys_provider,
9058
+ chain_hash,
9059
+ &config,
9060
+ 0,
9061
+ );
9062
+
9063
+ // Receive funding_signed, but the channel will be configured to hold sending channel_ready and
9064
+ // broadcasting the funding transaction until the batch is ready.
9065
+ let _ = node_a_chan.funding_signed(
9066
+ &funding_signed_msg,
9067
+ best_block,
9068
+ &&keys_provider,
9069
+ true,
9070
+ &&logger,
9071
+ ).unwrap();
9072
+ let node_a_updates = node_a_chan.monitor_updating_restored(
9073
+ &&logger,
9074
+ &&keys_provider,
9075
+ chain_hash,
9076
+ &config,
9077
+ 0,
9078
+ );
9079
+ // Our channel_ready shouldn't be sent yet, even with trust_own_funding_0conf set,
9080
+ // as the funding transaction depends on all channels in the batch becoming ready.
9081
+ assert!(node_a_updates.channel_ready.is_none());
9082
+ assert!(node_a_updates.funding_broadcastable.is_none());
9083
+ assert_eq!(
9084
+ node_a_chan.context.channel_state,
9085
+ ChannelState::FundingSent as u32 |
9086
+ ChannelState::WaitingForBatch as u32,
9087
+ );
9088
+
9089
+ // It is possible to receive a 0conf channel_ready from the remote node.
9090
+ node_a_chan.channel_ready(
9091
+ &node_b_updates.channel_ready.unwrap(),
9092
+ &&keys_provider,
9093
+ chain_hash,
9094
+ &config,
9095
+ &best_block,
9096
+ &&logger,
9097
+ ).unwrap();
9098
+ assert_eq!(
9099
+ node_a_chan.context.channel_state,
9100
+ ChannelState::FundingSent as u32 |
9101
+ ChannelState::WaitingForBatch as u32 |
9102
+ ChannelState::TheirChannelReady as u32,
9103
+ );
9104
+
9105
+ // Clear the ChannelState::WaitingForBatch only when called by ChannelManager.
9106
+ node_a_chan.set_batch_ready();
9107
+ assert_eq!(
9108
+ node_a_chan.context.channel_state,
9109
+ ChannelState::FundingSent as u32 |
9110
+ ChannelState::TheirChannelReady as u32,
9111
+ );
9112
+ }
8955
9113
}
0 commit comments