Skip to content

Commit 04416c3

Browse files
committed
Review comments
1 parent edb1ccf commit 04416c3

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
@@ -790,6 +790,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
790790

791791
pub(crate) channel_transaction_parameters: ChannelTransactionParameters,
792792
funding_transaction: Option<Transaction>,
793+
funding_txid: Option<Txid>,
793794

794795
counterparty_cur_commitment_point: Option<PublicKey>,
795796
counterparty_prev_commitment_point: Option<PublicKey>,
@@ -1922,16 +1923,28 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19221923
res
19231924
}
19241925

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

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

26002613
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
26012614

2602-
if non_shutdown_state == ChannelState::FundingSent as u32 {
2615+
if non_shutdown_state & !(ChannelState::WaitingForBatch as u32) == ChannelState::FundingSent as u32 {
26032616
self.context.channel_state |= ChannelState::TheirChannelReady as u32;
26042617
} else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) {
26052618
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
36903703
// first received the funding_signed.
36913704
let mut funding_broadcastable =
36923705
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();
36933707
self.context.funding_transaction.take()
36943708
} else { None };
36953709
// 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
45914605
pub fn is_awaiting_initial_mon_persist(&self) -> bool {
45924606
if !self.is_awaiting_monitor_update() { return false; }
45934607
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)
45954609
== ChannelState::FundingSent as u32 {
45964610
// If we're not a 0conf channel, we'll be waiting on a monitor update with only
45974611
// FundingSent set, though our peer could have sent their channel_ready.
@@ -4671,6 +4685,8 @@ impl<SP: Deref> Channel<SP> where
46714685
return None;
46724686
}
46734687

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.
46744690
let non_shutdown_state = self.context.channel_state & (!MULTI_STATE_FLAGS);
46754691
let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
46764692
self.context.channel_state |= ChannelState::OurChannelReady as u32;
@@ -5744,6 +5760,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
57445760
channel_type_features: channel_type.clone()
57455761
},
57465762
funding_transaction: None,
5763+
funding_txid: None,
57475764

57485765
counterparty_cur_commitment_point: None,
57495766
counterparty_prev_commitment_point: None,
@@ -5837,6 +5854,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
58375854
self.context.channel_state = ChannelState::FundingCreated as u32;
58385855
self.context.channel_id = funding_txo.to_channel_id();
58395856
self.context.funding_transaction = Some(funding_transaction);
5857+
self.context.funding_txid = self.context.funding_transaction.as_ref().map(|tx| tx.txid());
58405858

58415859
let channel = Channel {
58425860
context: self.context,
@@ -6386,6 +6404,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
63866404
channel_type_features: channel_type.clone()
63876405
},
63886406
funding_transaction: None,
6407+
funding_txid: None,
63896408

63906409
counterparty_cur_commitment_point: Some(msg.first_per_commitment_point),
63916410
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
72237242
};
72247243

72257244
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());
72277247

72287248
let counterparty_cur_commitment_point = Readable::read(reader)?;
72297249

@@ -7466,6 +7486,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
74667486

74677487
channel_transaction_parameters: channel_parameters,
74687488
funding_transaction,
7489+
funding_txid,
74697490

74707491
counterparty_cur_commitment_point,
74717492
counterparty_prev_commitment_point,
@@ -7519,7 +7540,7 @@ mod tests {
75197540
use crate::ln::PaymentHash;
75207541
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
75217542
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};
75237544
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
75247545
use crate::ln::features::ChannelTypeFeatures;
75257546
use crate::ln::msgs::{ChannelUpdate, DecodeError, UnsignedChannelUpdate, MAX_VALUE_MSAT};
@@ -8993,4 +9014,148 @@ mod tests {
89939014
);
89949015
assert!(res.is_err());
89959016
}
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+
}
89969161
}

0 commit comments

Comments
 (0)