Skip to content

Commit 828932f

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 43de15e commit 828932f

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
@@ -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};
@@ -1997,22 +1996,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
19971996
/// store it here and only release it to the `ChannelManager` once it asks for it.
19981997
blocked_monitor_updates: Vec<PendingChannelMonitorUpdate>,
19991998

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

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

@@ -2716,8 +2695,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
27162695

27172696
is_manual_broadcast: false,
27182697

2719-
next_funding_txid: None,
2720-
27212698
is_holder_quiescence_initiator: None,
27222699
};
27232700

@@ -2950,7 +2927,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
29502927
blocked_monitor_updates: Vec::new(),
29512928
local_initiated_shutdown: None,
29522929
is_manual_broadcast: false,
2953-
next_funding_txid: None,
29542930

29552931
is_holder_quiescence_initiator: None,
29562932
};
@@ -6225,7 +6201,6 @@ impl<SP: Deref> FundedChannel<SP> where
62256201
// We have a finalized funding transaction, so we can set the funding transaction and reset the
62266202
// signing session fields.
62276203
self.funding.funding_transaction = funding_tx_opt;
6228-
self.context.next_funding_txid = None;
62296204
self.interactive_tx_signing_session = None;
62306205
}
62316206

@@ -8278,6 +8253,25 @@ impl<SP: Deref> FundedChannel<SP> where
82788253
}
82798254
}
82808255

8256+
fn maybe_get_next_funding_txid(&self) -> Option<Txid> {
8257+
// If we've sent `commtiment_signed` for an interactively constructed transaction
8258+
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
8259+
// to the txid of that interactive transaction, else we MUST NOT set it.
8260+
if let Some(signing_session) = &self.interactive_tx_signing_session {
8261+
// Since we have a signing_session, this implies we've sent an initial `commitment_signed`...
8262+
if !signing_session.counterparty_sent_tx_signatures {
8263+
// ...but we didn't receive a `tx_signatures` from the counterparty yet.
8264+
Some(self.funding_outpoint().txid)
8265+
} else {
8266+
// ...and we received a `tx_signatures` from the counterparty.
8267+
None
8268+
}
8269+
} else {
8270+
// We don't have an active signing session.
8271+
None
8272+
}
8273+
}
8274+
82818275
/// May panic if called on a channel that wasn't immediately-previously
82828276
/// self.remove_uncommitted_htlcs_and_mark_paused()'d
82838277
fn get_channel_reestablish<L: Deref>(&mut self, logger: &L) -> msgs::ChannelReestablish where L::Target: Logger {
@@ -8327,7 +8321,7 @@ impl<SP: Deref> FundedChannel<SP> where
83278321
next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number - 1,
83288322
your_last_per_commitment_secret: remote_last_secret,
83298323
my_current_per_commitment_point: dummy_pubkey,
8330-
next_funding_txid: self.context.next_funding_txid,
8324+
next_funding_txid: self.maybe_get_next_funding_txid(),
83318325
}
83328326
}
83338327

@@ -10867,14 +10861,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1086710861
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
1086810862
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
1086910863

10870-
// TODO(dual_funding): Instead of getting this from persisted value, figure it out based on the
10871-
// funding transaction and other channel state.
10872-
//
10873-
// If we've sent `commtiment_signed` for an interactively constructed transaction
10874-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
10875-
// to the txid of that interactive transaction, else we MUST NOT set it.
10876-
next_funding_txid: None,
10877-
1087810864
is_holder_quiescence_initiator: None,
1087910865
},
1088010866
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
@@ -852,6 +852,18 @@ pub struct ChannelReestablish {
852852
/// The sender's per-commitment point for their current commitment transaction
853853
pub my_current_per_commitment_point: PublicKey,
854854
/// The next funding transaction ID
855+
///
856+
/// Allows peers to finalize the signing steps of an interactive transaction construction, or
857+
/// safely abort that transaction if it was not signed by one of the peers, who has thus already
858+
/// removed it from its state.
859+
///
860+
/// If we've sent `commtiment_signed` for an interactively constructed transaction
861+
/// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
862+
/// to the txid of that interactive transaction, else we MUST NOT set it.
863+
///
864+
/// See the spec for further details on this:
865+
/// * `channel_reestablish`-sending node: https:///github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2466-L2470
866+
/// * `channel_reestablish`-receiving node: https:///github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2520-L2531
855867
pub next_funding_txid: Option<Txid>,
856868
}
857869

0 commit comments

Comments
 (0)