Skip to content

Commit 508f160

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 aaef672 commit 508f160

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, OnionErrorPacket};
@@ -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>,
@@ -2278,10 +2261,6 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22782261
}
22792262
};
22802263

2281-
if let HandleTxCompleteValue::SendTxComplete(_, ref signing_session) = tx_complete {
2282-
self.context.next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
2283-
};
2284-
22852264
HandleTxCompleteResult(Ok(tx_complete))
22862265
}
22872266

@@ -2718,8 +2697,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
27182697

27192698
is_manual_broadcast: false,
27202699

2721-
next_funding_txid: None,
2722-
27232700
is_holder_quiescence_initiator: None,
27242701
};
27252702

@@ -2952,7 +2929,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
29522929
blocked_monitor_updates: Vec::new(),
29532930
local_initiated_shutdown: None,
29542931
is_manual_broadcast: false,
2955-
next_funding_txid: None,
29562932

29572933
is_holder_quiescence_initiator: None,
29582934
};
@@ -6315,7 +6291,6 @@ impl<SP: Deref> FundedChannel<SP> where
63156291
// We have a finalized funding transaction, so we can set the funding transaction and reset the
63166292
// signing session fields.
63176293
self.funding.funding_transaction = funding_tx_opt;
6318-
self.context.next_funding_txid = None;
63196294
self.interactive_tx_signing_session = None;
63206295
}
63216296

@@ -8367,6 +8342,25 @@ impl<SP: Deref> FundedChannel<SP> where
83678342
}
83688343
}
83698344

8345+
fn maybe_get_next_funding_txid(&self) -> Option<Txid> {
8346+
// If we've sent `commtiment_signed` for an interactively constructed transaction
8347+
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
8348+
// to the txid of that interactive transaction, else we MUST NOT set it.
8349+
if let Some(signing_session) = &self.interactive_tx_signing_session {
8350+
// Since we have a signing_session, this implies we've sent an initial `commitment_signed`...
8351+
if !signing_session.counterparty_sent_tx_signatures {
8352+
// ...but we didn't receive a `tx_signatures` from the counterparty yet.
8353+
Some(self.funding_outpoint().txid)
8354+
} else {
8355+
// ...and we received a `tx_signatures` from the counterparty.
8356+
None
8357+
}
8358+
} else {
8359+
// We don't have an active signing session.
8360+
None
8361+
}
8362+
}
8363+
83708364
/// May panic if called on a channel that wasn't immediately-previously
83718365
/// self.remove_uncommitted_htlcs_and_mark_paused()'d
83728366
fn get_channel_reestablish<L: Deref>(&mut self, logger: &L) -> msgs::ChannelReestablish where L::Target: Logger {
@@ -8416,7 +8410,7 @@ impl<SP: Deref> FundedChannel<SP> where
84168410
next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number - 1,
84178411
your_last_per_commitment_secret: remote_last_secret,
84188412
my_current_per_commitment_point: dummy_pubkey,
8419-
next_funding_txid: self.context.next_funding_txid,
8413+
next_funding_txid: self.maybe_get_next_funding_txid(),
84208414
}
84218415
}
84228416

@@ -11120,14 +11114,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1112011114
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
1112111115
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
1112211116

11123-
// TODO(dual_funding): Instead of getting this from persisted value, figure it out based on the
11124-
// funding transaction and other channel state.
11125-
//
11126-
// If we've sent `commtiment_signed` for an interactively constructed transaction
11127-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
11128-
// to the txid of that interactive transaction, else we MUST NOT set it.
11129-
next_funding_txid: None,
11130-
1113111117
is_holder_quiescence_initiator: None,
1113211118
},
1113311119
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)