@@ -1944,16 +1944,28 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
19441944 res
19451945 }
19461946
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 {
19491949 if self.channel_state & ChannelState::FundingCreated as u32 != 0 ||
19501950 self.channel_state & ChannelState::WaitingForBatch as u32 != 0 {
1951- self.funding_transaction.clone( )
1951+ self.funding_transaction.as_ref().map(f )
19521952 } else {
19531953 None
19541954 }
19551955 }
19561956
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+
19571969 /// Gets the latest commitment transaction and any dependent transactions for relay (forcing
19581970 /// shutdown of this channel - no more calls into this Channel may be made afterwards except
19591971 /// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
@@ -2613,7 +2625,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
26132625
26142626 let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
26152627
2616- if non_shutdown_state == ChannelState::FundingSent as u32 {
2628+ if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
26172629 self.context.channel_state |= ChannelState::TheirChannelReady as u32;
26182630 } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
26192631 self.context.channel_state = ChannelState::ChannelReady as u32 | (self.context.channel_state & MULTI_STATE_FLAGS);
@@ -4565,7 +4577,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
45654577 pub fn is_awaiting_initial_mon_persist(&self) -> bool {
45664578 if !self.is_awaiting_monitor_update() { return false; }
45674579 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 )
45694581 == ChannelState::FundingSent as u32 {
45704582 // If we're not a 0conf channel, we'll be waiting on a monitor update with only
45714583 // FundingSent set, though our peer could have sent their channel_ready.
@@ -4645,6 +4657,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
46454657 return None;
46464658 }
46474659
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.
46484662 let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
46494663 let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
46504664 self.context.channel_state |= ChannelState::OurChannelReady as u32;
@@ -7477,7 +7491,7 @@ mod tests {
74777491 use crate::ln::PaymentHash;
74787492 use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
74797493 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};
74817495 use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
74827496 use crate::ln::features::ChannelTypeFeatures;
74837497 use crate::ln::msgs::{ChannelUpdate, DecodeError, UnsignedChannelUpdate, MAX_VALUE_MSAT};
@@ -8952,4 +8966,148 @@ mod tests {
89528966 );
89538967 assert!(res.is_err());
89548968 }
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+ }
89559113}
0 commit comments