Skip to content

Commit f496e64

Browse files
committed
Get next_funding_txid from the funding_outpoint based on state
Instead of having an explicit `ChannelContext::next_funding_txid` to set and read, we can get this value on the fly when it is appropriate to do so.
1 parent 4c43a5b commit f496e64

File tree

3 files changed

+35
-37
lines changed

3 files changed

+35
-37
lines changed

lightning/src/ln/channel.rs

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ use crate::ln::types::ChannelId;
3030
use crate::types::payment::{PaymentPreimage, PaymentHash};
3131
use crate::types::features::{ChannelTypeFeatures, InitFeatures};
3232
use crate::ln::interactivetxs::{
33-
get_output_weight, HandleTxCompleteValue, HandleTxCompleteResult, InteractiveTxConstructor,
34-
InteractiveTxConstructorArgs, InteractiveTxSigningSession, InteractiveTxMessageSendResult,
35-
TX_COMMON_FIELDS_WEIGHT,
33+
get_output_weight, HandleTxCompleteResult, InteractiveTxConstructor, InteractiveTxConstructorArgs,
34+
InteractiveTxSigningSession, InteractiveTxMessageSendResult, TX_COMMON_FIELDS_WEIGHT,
3635
};
3736
use crate::ln::msgs;
3837
use crate::ln::msgs::{ClosingSigned, ClosingSignedFeeRange, DecodeError};
@@ -2006,22 +2005,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
20062005
/// store it here and only release it to the `ChannelManager` once it asks for it.
20072006
blocked_monitor_updates: Vec<PendingChannelMonitorUpdate>,
20082007

2009-
// The `next_funding_txid` field allows peers to finalize the signing steps of an interactive
2010-
// transaction construction, or safely abort that transaction if it was not signed by one of the
2011-
// peers, who has thus already removed it from its state.
2012-
//
2013-
// If we've sent `commtiment_signed` for an interactively constructed transaction
2014-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
2015-
// to the txid of that interactive transaction, else we MUST NOT set it.
2016-
//
2017-
// See the spec for further details on this:
2018-
// * `channel_reestablish`-sending node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2466-L2470
2019-
// * `channel_reestablish`-receiving node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2520-L2531
2020-
//
2021-
// TODO(dual_funding): Persist this when we actually contribute funding inputs. For now we always
2022-
// send an empty witnesses array in `tx_signatures` as a V2 channel acceptor
2023-
next_funding_txid: Option<Txid>,
2024-
20252008
/// Only set when a counterparty `stfu` has been processed to track which node is allowed to
20262009
/// propose "something fundamental" upon becoming quiescent.
20272010
is_holder_quiescence_initiator: Option<bool>,
@@ -2285,10 +2268,6 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22852268
}
22862269
};
22872270

2288-
if let HandleTxCompleteValue::SendTxComplete(_, ref signing_session) = tx_complete {
2289-
self.context.next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
2290-
};
2291-
22922271
HandleTxCompleteResult(Ok(tx_complete))
22932272
}
22942273

@@ -2725,8 +2704,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
27252704

27262705
is_manual_broadcast: false,
27272706

2728-
next_funding_txid: None,
2729-
27302707
is_holder_quiescence_initiator: None,
27312708
};
27322709

@@ -2959,7 +2936,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
29592936
blocked_monitor_updates: Vec::new(),
29602937
local_initiated_shutdown: None,
29612938
is_manual_broadcast: false,
2962-
next_funding_txid: None,
29632939

29642940
is_holder_quiescence_initiator: None,
29652941
};
@@ -6320,7 +6296,6 @@ impl<SP: Deref> FundedChannel<SP> where
63206296
// We have a finalized funding transaction, so we can set the funding transaction and reset the
63216297
// signing session fields.
63226298
self.funding.funding_transaction = funding_tx_opt;
6323-
self.context.next_funding_txid = None;
63246299
self.interactive_tx_signing_session = None;
63256300
}
63266301

@@ -8372,6 +8347,25 @@ impl<SP: Deref> FundedChannel<SP> where
83728347
}
83738348
}
83748349

8350+
fn maybe_get_next_funding_txid(&self) -> Option<Txid> {
8351+
// If we've sent `commtiment_signed` for an interactively constructed transaction
8352+
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
8353+
// to the txid of that interactive transaction, else we MUST NOT set it.
8354+
if let Some(signing_session) = &self.interactive_tx_signing_session {
8355+
// Since we have a signing_session, this implies we've sent an initial `commitment_signed`...
8356+
if !signing_session.counterparty_sent_tx_signatures {
8357+
// ...but we didn't receive a `tx_signatures` from the counterparty yet.
8358+
Some(self.funding_outpoint().txid)
8359+
} else {
8360+
// ...and we received a `tx_signatures` from the counterparty.
8361+
None
8362+
}
8363+
} else {
8364+
// We don't have an active signing session.
8365+
None
8366+
}
8367+
}
8368+
83758369
/// May panic if called on a channel that wasn't immediately-previously
83768370
/// self.remove_uncommitted_htlcs_and_mark_paused()'d
83778371
fn get_channel_reestablish<L: Deref>(&mut self, logger: &L) -> msgs::ChannelReestablish where L::Target: Logger {
@@ -8421,7 +8415,7 @@ impl<SP: Deref> FundedChannel<SP> where
84218415
next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number - 1,
84228416
your_last_per_commitment_secret: remote_last_secret,
84238417
my_current_per_commitment_point: dummy_pubkey,
8424-
next_funding_txid: self.context.next_funding_txid,
8418+
next_funding_txid: self.maybe_get_next_funding_txid(),
84258419
}
84268420
}
84278421

@@ -11106,14 +11100,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1110611100
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
1110711101
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
1110811102

11109-
// TODO(dual_funding): Instead of getting this from persisted value, figure it out based on the
11110-
// funding transaction and other channel state.
11111-
//
11112-
// If we've sent `commtiment_signed` for an interactively constructed transaction
11113-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
11114-
// to the txid of that interactive transaction, else we MUST NOT set it.
11115-
next_funding_txid: None,
11116-
1111711103
is_holder_quiescence_initiator: None,
1111811104
},
1111911105
interactive_tx_signing_session: None,

lightning/src/ln/interactivetxs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,10 @@ impl ConstructedTransaction {
289289
#[derive(Debug, Clone, PartialEq)]
290290
pub(crate) struct InteractiveTxSigningSession {
291291
pub unsigned_tx: ConstructedTransaction,
292+
pub counterparty_sent_tx_signatures: bool,
292293
holder_sends_tx_signatures_first: bool,
293294
received_commitment_signed: bool,
294295
holder_tx_signatures: Option<TxSignatures>,
295-
counterparty_sent_tx_signatures: bool,
296296
}
297297

298298
impl InteractiveTxSigningSession {

lightning/src/ln/msgs.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,18 @@ pub struct ChannelReestablish {
854854
/// The sender's per-commitment point for their current commitment transaction
855855
pub my_current_per_commitment_point: PublicKey,
856856
/// The next funding transaction ID
857+
///
858+
/// Allows peers to finalize the signing steps of an interactive transaction construction, or
859+
/// safely abort that transaction if it was not signed by one of the peers, who has thus already
860+
/// removed it from its state.
861+
///
862+
/// If we've sent `commtiment_signed` for an interactively constructed transaction
863+
/// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
864+
/// to the txid of that interactive transaction, else we MUST NOT set it.
865+
///
866+
/// See the spec for further details on this:
867+
/// * `channel_reestablish`-sending node: https:///github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2466-L2470
868+
/// * `channel_reestablish`-receiving node: https:///github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2520-L2531
857869
pub next_funding_txid: Option<Txid>,
858870
}
859871

0 commit comments

Comments
 (0)