Skip to content

Commit 476c5dd

Browse files
committed
Review comments
1 parent ec6cabd commit 476c5dd

File tree

3 files changed

+257
-57
lines changed

3 files changed

+257
-57
lines changed

lightning/src/ln/channel.rs

Lines changed: 172 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
793793

794794
pub(crate) channel_transaction_parameters: ChannelTransactionParameters,
795795
funding_transaction: Option<Transaction>,
796+
funding_txid: Option<Txid>,
796797

797798
counterparty_cur_commitment_point: Option<PublicKey>,
798799
counterparty_prev_commitment_point: Option<PublicKey>,
@@ -1926,16 +1927,28 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19261927
res
19271928
}
19281929

1929-
/// Returns transaction if there is pending funding transaction that is yet to broadcast
1930-
pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
1930+
fn if_unbroadcasted_funding<F, O>(&self, f: F) -> Option<O>
1931+
where F: Fn() -> Option<O> {
19311932
if self.channel_state & ChannelState::FundingCreated as u32 != 0 ||
19321933
self.channel_state & ChannelState::WaitingForBatch as u32 != 0 {
1933-
self.funding_transaction.clone()
1934+
f()
19341935
} else {
19351936
None
19361937
}
19371938
}
19381939

1940+
/// Returns the transaction if there is a pending funding transaction that is yet to be
1941+
/// broadcast.
1942+
pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
1943+
self.if_unbroadcasted_funding(|| self.funding_transaction.clone())
1944+
}
1945+
1946+
/// Returns the transaction ID if there is a pending funding transaction that is yet to be
1947+
/// broadcast.
1948+
pub fn unbroadcasted_funding_txid(&self) -> Option<Txid> {
1949+
self.if_unbroadcasted_funding(|| self.funding_txid.clone())
1950+
}
1951+
19391952
/// Gets the latest commitment transaction and any dependent transactions for relay (forcing
19401953
/// shutdown of this channel - no more calls into this Channel may be made afterwards except
19411954
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
@@ -2603,7 +2616,7 @@ impl<SP: Deref> Channel<SP> where
26032616

26042617
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
26052618

2606-
if non_shutdown_state == ChannelState::FundingSent as u32 {
2619+
if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
26072620
self.context.channel_state |= ChannelState::TheirChannelReady as u32;
26082621
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
26092622
self.context.channel_state = ChannelState::ChannelReady as u32 | (self.context.channel_state & MULTI_STATE_FLAGS);
@@ -3693,6 +3706,7 @@ impl<SP: Deref> Channel<SP> where
36933706
// first received the funding_signed.
36943707
let mut funding_broadcastable =
36953708
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 {
3709+
self.context.funding_txid.take();
36963710
self.context.funding_transaction.take()
36973711
} else { None };
36983712
// That said, if the funding transaction is already confirmed (ie we're active with a
@@ -4594,7 +4608,7 @@ impl<SP: Deref> Channel<SP> where
45944608
pub fn is_awaiting_initial_mon_persist(&self) -> bool {
45954609
if !self.is_awaiting_monitor_update() { return false; }
45964610
if self.context.channel_state &
4597-
!(ChannelState::TheirChannelReady as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)
4611+
!(ChannelState::TheirChannelReady as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32 | ChannelState::WaitingForBatch as u32)
45984612
== ChannelState::FundingSent as u32 {
45994613
// If we're not a 0conf channel, we'll be waiting on a monitor update with only
46004614
// FundingSent set, though our peer could have sent their channel_ready.
@@ -4674,6 +4688,8 @@ impl<SP: Deref> Channel<SP> where
46744688
return None;
46754689
}
46764690

4691+
// Note that we don't include ChannelState::WaitingForBatch as we don't want to send
4692+
// channel_ready until the entire batch is ready.
46774693
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
46784694
let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
46794695
self.context.channel_state |= ChannelState::OurChannelReady as u32;
@@ -5747,6 +5763,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
57475763
channel_type_features: channel_type.clone()
57485764
},
57495765
funding_transaction: None,
5766+
funding_txid: None,
57505767

57515768
counterparty_cur_commitment_point: None,
57525769
counterparty_prev_commitment_point: None,
@@ -5840,6 +5857,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
58405857
self.context.channel_state = ChannelState::FundingCreated as u32;
58415858
self.context.channel_id = funding_txo.to_channel_id();
58425859
self.context.funding_transaction = Some(funding_transaction);
5860+
self.context.funding_txid = self.context.funding_transaction.as_ref().map(|tx| tx.txid());
58435861

58445862
let channel = Channel {
58455863
context: self.context,
@@ -6389,6 +6407,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
63896407
channel_type_features: channel_type.clone()
63906408
},
63916409
funding_transaction: None,
6410+
funding_txid: None,
63926411

63936412
counterparty_cur_commitment_point: Some(msg.first_per_commitment_point),
63946413
counterparty_prev_commitment_point: None,
@@ -7226,7 +7245,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
72267245
};
72277246

72287247
let mut channel_parameters: ChannelTransactionParameters = Readable::read(reader)?;
7229-
let funding_transaction = Readable::read(reader)?;
7248+
let funding_transaction: Option<Transaction> = Readable::read(reader)?;
7249+
let funding_txid = funding_transaction.as_ref().map(|tx| tx.txid());
72307250

72317251
let counterparty_cur_commitment_point = Readable::read(reader)?;
72327252

@@ -7469,6 +7489,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
74697489

74707490
channel_transaction_parameters: channel_parameters,
74717491
funding_transaction,
7492+
funding_txid,
74727493

74737494
counterparty_cur_commitment_point,
74747495
counterparty_prev_commitment_point,
@@ -7522,7 +7543,7 @@ mod tests {
75227543
use crate::ln::PaymentHash;
75237544
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
75247545
use crate::ln::channel::InitFeatures;
7525-
use crate::ln::channel::{Channel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
7546+
use crate::ln::channel::{Channel, ChannelState, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
75267547
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
75277548
use crate::ln::features::ChannelTypeFeatures;
75287549
use crate::ln::msgs::{ChannelUpdate, DecodeError, UnsignedChannelUpdate, MAX_VALUE_MSAT};
@@ -8996,4 +9017,148 @@ mod tests {
89969017
);
89979018
assert!(res.is_err());
89989019
}
9020+
9021+
#[test]
9022+
fn test_waiting_for_batch() {
9023+
let feeest = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
9024+
let logger = test_utils::TestLogger::new();
9025+
let secp_ctx = Secp256k1::new();
9026+
let seed = [42; 32];
9027+
let network = Network::Testnet;
9028+
let best_block = BestBlock::from_network(network);
9029+
let chain_hash = genesis_block(network).header.block_hash();
9030+
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
9031+
9032+
let mut config = UserConfig::default();
9033+
// Set trust_own_funding_0conf while ensuring we don't send channel_ready for a
9034+
// channel in a batch before all channels are ready.
9035+
config.channel_handshake_limits.trust_own_funding_0conf = true;
9036+
9037+
// Create a channel from node a to node b that will be part of batch funding.
9038+
let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
9039+
let mut node_a_chan = OutboundV1Channel::<EnforcingSigner>::new(
9040+
&feeest,
9041+
&&keys_provider,
9042+
&&keys_provider,
9043+
node_b_node_id,
9044+
&channelmanager::provided_init_features(&config),
9045+
10000000,
9046+
100000,
9047+
42,
9048+
&config,
9049+
0,
9050+
42,
9051+
).unwrap();
9052+
9053+
let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
9054+
let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
9055+
let mut node_b_chan = InboundV1Channel::<EnforcingSigner>::new(
9056+
&feeest,
9057+
&&keys_provider,
9058+
&&keys_provider,
9059+
node_b_node_id,
9060+
&channelmanager::provided_channel_type_features(&config),
9061+
&channelmanager::provided_init_features(&config),
9062+
&open_channel_msg,
9063+
7,
9064+
&config,
9065+
0,
9066+
&&logger,
9067+
42,
9068+
).unwrap();
9069+
9070+
// Allow node b to send a 0conf channel_ready.
9071+
node_b_chan.set_0conf();
9072+
9073+
let accept_channel_msg = node_b_chan.accept_inbound_channel(0);
9074+
node_a_chan.accept_channel(
9075+
&accept_channel_msg,
9076+
&config.channel_handshake_limits,
9077+
&channelmanager::provided_init_features(&config),
9078+
).unwrap();
9079+
9080+
// Fund the channel with a batch funding transaction.
9081+
let output_script = node_a_chan.context.get_funding_redeemscript();
9082+
let tx = Transaction {
9083+
version: 1,
9084+
lock_time: PackedLockTime::ZERO,
9085+
input: Vec::new(),
9086+
output: vec![
9087+
TxOut {
9088+
value: 10000000, script_pubkey: output_script.clone(),
9089+
},
9090+
TxOut {
9091+
value: 10000000, script_pubkey: Builder::new().into_script(),
9092+
},
9093+
]};
9094+
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
9095+
let (mut node_a_chan, funding_created_msg) = node_a_chan.get_funding_created(
9096+
tx.clone(),
9097+
funding_outpoint,
9098+
&&logger,
9099+
).map_err(|_| ()).unwrap();
9100+
let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(
9101+
&funding_created_msg,
9102+
best_block,
9103+
&&keys_provider,
9104+
&&logger,
9105+
).map_err(|_| ()).unwrap();
9106+
let node_b_updates = node_b_chan.monitor_updating_restored(
9107+
&&logger,
9108+
&&keys_provider,
9109+
chain_hash,
9110+
&config,
9111+
0,
9112+
);
9113+
9114+
// Receive funding_signed, but the channel will be configured to hold sending channel_ready and
9115+
// broadcasting the funding transaction until the batch is ready.
9116+
let _ = node_a_chan.funding_signed(
9117+
&funding_signed_msg,
9118+
best_block,
9119+
&&keys_provider,
9120+
true,
9121+
&&logger,
9122+
).unwrap();
9123+
let node_a_updates = node_a_chan.monitor_updating_restored(
9124+
&&logger,
9125+
&&keys_provider,
9126+
chain_hash,
9127+
&config,
9128+
0,
9129+
);
9130+
// Our channel_ready shouldn't be sent yet, even with trust_own_funding_0conf set,
9131+
// as the funding transaction depends on all channels in the batch becoming ready.
9132+
assert!(node_a_updates.channel_ready.is_none());
9133+
assert!(node_a_updates.funding_broadcastable.is_none());
9134+
assert_eq!(
9135+
node_a_chan.context.channel_state,
9136+
ChannelState::FundingSent as u32 |
9137+
ChannelState::WaitingForBatch as u32,
9138+
);
9139+
9140+
// It is possible to receive a 0conf channel_ready from the remote node.
9141+
node_a_chan.channel_ready(
9142+
&node_b_updates.channel_ready.unwrap(),
9143+
&&keys_provider,
9144+
chain_hash,
9145+
&config,
9146+
&best_block,
9147+
&&logger,
9148+
).unwrap();
9149+
assert_eq!(
9150+
node_a_chan.context.channel_state,
9151+
ChannelState::FundingSent as u32 |
9152+
ChannelState::WaitingForBatch as u32 |
9153+
ChannelState::TheirChannelReady as u32,
9154+
);
9155+
9156+
// Clear the ChannelState::WaitingForBatch only when called by ChannelManager.
9157+
node_a_chan.set_batch_ready();
9158+
assert_eq!(
9159+
node_a_chan.context.channel_state,
9160+
ChannelState::FundingSent as u32 |
9161+
ChannelState::TheirChannelReady as u32,
9162+
);
9163+
}
89999164
}

0 commit comments

Comments
 (0)