Skip to content

Commit d60cfd0

Browse files
committed
Review comments
1 parent ba9f4d4 commit d60cfd0

File tree

3 files changed

+258
-58
lines changed

3 files changed

+258
-58
lines changed

lightning/src/ln/channel.rs

+172-7
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ pub(super) struct ChannelContext<Signer: ChannelSigner> {
789789

790790
pub(crate) channel_transaction_parameters: ChannelTransactionParameters,
791791
funding_transaction: Option<Transaction>,
792+
funding_txid: Option<Txid>,
792793

793794
counterparty_cur_commitment_point: Option<PublicKey>,
794795
counterparty_prev_commitment_point: Option<PublicKey>,
@@ -1921,16 +1922,28 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
19211922
res
19221923
}
19231924

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> {
19261927
if self.channel_state & ChannelState::FundingCreated as u32 != 0 ||
19271928
self.channel_state & ChannelState::WaitingForBatch as u32 != 0 {
1928-
self.funding_transaction.clone()
1929+
f()
19291930
} else {
19301931
None
19311932
}
19321933
}
19331934

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+
19341947
/// Gets the latest commitment transaction and any dependent transactions for relay (forcing
19351948
/// shutdown of this channel - no more calls into this Channel may be made afterwards except
19361949
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
@@ -2590,7 +2603,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
25902603

25912604
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
25922605

2593-
if non_shutdown_state == ChannelState::FundingSent as u32 {
2606+
if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
25942607
self.context.channel_state |= ChannelState::TheirChannelReady as u32;
25952608
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
25962609
self.context.channel_state = ChannelState::ChannelReady as u32 | (self.context.channel_state & MULTI_STATE_FLAGS);
@@ -3653,6 +3666,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36533666
// first received the funding_signed.
36543667
let mut funding_broadcastable =
36553668
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();
36563670
self.context.funding_transaction.take()
36573671
} else { None };
36583672
// That said, if the funding transaction is already confirmed (ie we're active with a
@@ -4542,7 +4556,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
45424556
pub fn is_awaiting_initial_mon_persist(&self) -> bool {
45434557
if !self.is_awaiting_monitor_update() { return false; }
45444558
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)
45464560
== ChannelState::FundingSent as u32 {
45474561
// If we're not a 0conf channel, we'll be waiting on a monitor update with only
45484562
// FundingSent set, though our peer could have sent their channel_ready.
@@ -4622,6 +4636,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
46224636
return None;
46234637
}
46244638

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.
46254641
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
46264642
let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
46274643
self.context.channel_state |= ChannelState::OurChannelReady as u32;
@@ -5674,6 +5690,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
56745690
channel_type_features: channel_type.clone()
56755691
},
56765692
funding_transaction: None,
5693+
funding_txid: None,
56775694

56785695
counterparty_cur_commitment_point: None,
56795696
counterparty_prev_commitment_point: None,
@@ -5762,6 +5779,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
57625779
self.context.channel_state = ChannelState::FundingCreated as u32;
57635780
self.context.channel_id = funding_txo.to_channel_id();
57645781
self.context.funding_transaction = Some(funding_transaction);
5782+
self.context.funding_txid = self.context.funding_transaction.as_ref().map(|tx| tx.txid());
57655783

57665784
let channel = Channel {
57675785
context: self.context,
@@ -6312,6 +6330,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
63126330
channel_type_features: channel_type.clone()
63136331
},
63146332
funding_transaction: None,
6333+
funding_txid: None,
63156334

63166335
counterparty_cur_commitment_point: Some(msg.first_per_commitment_point),
63176336
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
71397158
};
71407159

71417160
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());
71437163

71447164
let counterparty_cur_commitment_point = Readable::read(reader)?;
71457165

@@ -7382,6 +7402,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
73827402

73837403
channel_transaction_parameters: channel_parameters,
73847404
funding_transaction,
7405+
funding_txid,
73857406

73867407
counterparty_cur_commitment_point,
73877408
counterparty_prev_commitment_point,
@@ -7435,7 +7456,7 @@ mod tests {
74357456
use crate::ln::PaymentHash;
74367457
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
74377458
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};
74397460
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
74407461
use crate::ln::features::ChannelTypeFeatures;
74417462
use crate::ln::msgs::{ChannelUpdate, DecodeError, UnsignedChannelUpdate, MAX_VALUE_MSAT};
@@ -8910,4 +8931,148 @@ mod tests {
89108931
);
89118932
assert!(res.is_err());
89128933
}
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+
}
89139078
}

0 commit comments

Comments
 (0)