Skip to content

Commit 4d9cfd8

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 36ba27a commit 4d9cfd8

File tree

3 files changed

+35
-37
lines changed

3 files changed

+35
-37
lines changed

lightning/src/ln/channel.rs

+22-36
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ use crate::ln::types::ChannelId;
3131
use crate::types::payment::{PaymentPreimage, PaymentHash};
3232
use crate::types::features::{ChannelTypeFeatures, InitFeatures};
3333
use crate::ln::interactivetxs::{
34-
get_output_weight, HandleTxCompleteValue, HandleTxCompleteResult, InteractiveTxConstructor,
35-
InteractiveTxConstructorArgs, InteractiveTxSigningSession, InteractiveTxMessageSendResult,
36-
TX_COMMON_FIELDS_WEIGHT,
34+
get_output_weight, HandleTxCompleteResult, InteractiveTxConstructor, InteractiveTxConstructorArgs,
35+
InteractiveTxSigningSession, InteractiveTxMessageSendResult, TX_COMMON_FIELDS_WEIGHT,
3736
};
3837
use crate::ln::msgs;
3938
use crate::ln::msgs::{ClosingSigned, ClosingSignedFeeRange, DecodeError};
@@ -1996,22 +1995,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
19961995
/// store it here and only release it to the `ChannelManager` once it asks for it.
19971996
blocked_monitor_updates: Vec<PendingChannelMonitorUpdate>,
19981997

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

2278-
if let HandleTxCompleteValue::SendTxComplete(_, ref signing_session) = tx_complete {
2279-
self.context.next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
2280-
};
2281-
22822261
HandleTxCompleteResult(Ok(tx_complete))
22832262
}
22842263

@@ -2713,8 +2692,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
27132692

27142693
is_manual_broadcast: false,
27152694

2716-
next_funding_txid: None,
2717-
27182695
is_holder_quiescence_initiator: None,
27192696
};
27202697

@@ -2945,7 +2922,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
29452922
blocked_monitor_updates: Vec::new(),
29462923
local_initiated_shutdown: None,
29472924
is_manual_broadcast: false,
2948-
next_funding_txid: None,
29492925

29502926
is_holder_quiescence_initiator: None,
29512927
};
@@ -6220,7 +6196,6 @@ impl<SP: Deref> FundedChannel<SP> where
62206196
// We have a finalized funding transaction, so we can set the funding transaction and reset the
62216197
// signing session fields.
62226198
self.funding.funding_transaction = funding_tx_opt;
6223-
self.context.next_funding_txid = None;
62246199
self.interactive_tx_signing_session = None;
62256200
}
62266201

@@ -8269,6 +8244,25 @@ impl<SP: Deref> FundedChannel<SP> where
82698244
}
82708245
}
82718246

8247+
fn maybe_get_next_funding_txid(&self) -> Option<Txid> {
8248+
// If we've sent `commtiment_signed` for an interactively constructed transaction
8249+
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
8250+
// to the txid of that interactive transaction, else we MUST NOT set it.
8251+
if let Some(signing_session) = &self.interactive_tx_signing_session {
8252+
// Since we have a signing_session, this implies we've sent an initial `commitment_signed`...
8253+
if !signing_session.counterparty_sent_tx_signatures {
8254+
// ...but we didn't receive a `tx_signatures` from the counterparty yet.
8255+
Some(self.funding_outpoint().txid)
8256+
} else {
8257+
// ...and we received a `tx_signatures` from the counterparty.
8258+
None
8259+
}
8260+
} else {
8261+
// We don't have an active signing session.
8262+
None
8263+
}
8264+
}
8265+
82728266
/// May panic if called on a channel that wasn't immediately-previously
82738267
/// self.remove_uncommitted_htlcs_and_mark_paused()'d
82748268
fn get_channel_reestablish<L: Deref>(&mut self, logger: &L) -> msgs::ChannelReestablish where L::Target: Logger {
@@ -8318,7 +8312,7 @@ impl<SP: Deref> FundedChannel<SP> where
83188312
next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number - 1,
83198313
your_last_per_commitment_secret: remote_last_secret,
83208314
my_current_per_commitment_point: dummy_pubkey,
8321-
next_funding_txid: self.context.next_funding_txid,
8315+
next_funding_txid: self.maybe_get_next_funding_txid(),
83228316
}
83238317
}
83248318

@@ -10872,14 +10866,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1087210866
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
1087310867
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
1087410868

10875-
// TODO(dual_funding): Instead of getting this from persisted value, figure it out based on the
10876-
// funding transaction and other channel state.
10877-
//
10878-
// If we've sent `commtiment_signed` for an interactively constructed transaction
10879-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
10880-
// to the txid of that interactive transaction, else we MUST NOT set it.
10881-
next_funding_txid: None,
10882-
1088310869
is_holder_quiescence_initiator: None,
1088410870
},
1088510871
interactive_tx_signing_session: None,

lightning/src/ln/interactivetxs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,10 @@ impl ConstructedTransaction {
290290
#[derive(Debug, Clone, PartialEq)]
291291
pub(crate) struct InteractiveTxSigningSession {
292292
pub unsigned_tx: ConstructedTransaction,
293+
pub counterparty_sent_tx_signatures: bool,
293294
holder_sends_tx_signatures_first: bool,
294295
received_commitment_signed: bool,
295296
holder_tx_signatures: Option<TxSignatures>,
296-
counterparty_sent_tx_signatures: bool,
297297
}
298298

299299
impl InteractiveTxSigningSession {

lightning/src/ln/msgs.rs

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

0 commit comments

Comments
 (0)