Skip to content

Commit b0b8f25

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 37be726 commit b0b8f25

File tree

3 files changed

+35
-35
lines changed

3 files changed

+35
-35
lines changed

lightning/src/ln/channel.rs

+22-34
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};
@@ -1845,21 +1844,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
18451844
/// If we can't release a [`ChannelMonitorUpdate`] until some external action completes, we
18461845
/// store it here and only release it to the `ChannelManager` once it asks for it.
18471846
blocked_monitor_updates: Vec<PendingChannelMonitorUpdate>,
1848-
// The `next_funding_txid` field allows peers to finalize the signing steps of an interactive
1849-
// transaction construction, or safely abort that transaction if it was not signed by one of the
1850-
// peers, who has thus already removed it from its state.
1851-
//
1852-
// If we've sent `commtiment_signed` for an interactively constructed transaction
1853-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
1854-
// to the txid of that interactive transaction, else we MUST NOT set it.
1855-
//
1856-
// See the spec for further details on this:
1857-
// * `channel_reestablish`-sending node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2466-L2470
1858-
// * `channel_reestablish`-receiving node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2520-L2531
1859-
//
1860-
// TODO(dual_funding): Persist this when we actually contribute funding inputs. For now we always
1861-
// send an empty witnesses array in `tx_signatures` as a V2 channel acceptor
1862-
next_funding_txid: Option<Txid>,
18631847
}
18641848

18651849
/// A channel struct implementing this trait can receive an initial counterparty commitment
@@ -2090,10 +2074,6 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20902074
}
20912075
};
20922076

2093-
if let HandleTxCompleteValue::SendTxComplete(_, ref signing_session) = tx_complete {
2094-
self.context.next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
2095-
};
2096-
20972077
HandleTxCompleteResult(Ok(tx_complete))
20982078
}
20992079

@@ -2526,8 +2506,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25262506
blocked_monitor_updates: Vec::new(),
25272507

25282508
is_manual_broadcast: false,
2529-
2530-
next_funding_txid: None,
25312509
};
25322510

25332511
Ok(channel_context)
@@ -2754,7 +2732,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
27542732
blocked_monitor_updates: Vec::new(),
27552733
local_initiated_shutdown: None,
27562734
is_manual_broadcast: false,
2757-
next_funding_txid: None,
27582735
})
27592736
}
27602737

@@ -6024,7 +6001,6 @@ impl<SP: Deref> FundedChannel<SP> where
60246001
// We have a persisted channel monitor and and a finalized funding transaction, so we can move
60256002
// the channel state forward, set the funding transaction and reset the signing session fields.
60266003
self.context.funding_transaction = funding_tx_opt;
6027-
self.context.next_funding_txid = None;
60286004
self.interactive_tx_signing_session = None;
60296005
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
60306006
}
@@ -8045,6 +8021,25 @@ impl<SP: Deref> FundedChannel<SP> where
80458021
}
80468022
}
80478023

8024+
fn maybe_get_next_funding_txid(&self) -> Option<Txid> {
8025+
// If we've sent `commtiment_signed` for an interactively constructed transaction
8026+
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
8027+
// to the txid of that interactive transaction, else we MUST NOT set it.
8028+
if let Some(signing_session) = &self.interactive_tx_signing_session {
8029+
// Since we have a signing_session, this implies we've sent an initial `commitment_signed`...
8030+
if !signing_session.counterparty_sent_tx_signatures {
8031+
// ...but we didn't receive a `tx_signatures` from the counterparty yet.
8032+
Some(self.funding_outpoint().txid)
8033+
} else {
8034+
// ...and we received a `tx_signatures` from the counterparty.
8035+
None
8036+
}
8037+
} else {
8038+
// We don't have an active signing session.
8039+
None
8040+
}
8041+
}
8042+
80488043
/// May panic if called on a channel that wasn't immediately-previously
80498044
/// self.remove_uncommitted_htlcs_and_mark_paused()'d
80508045
fn get_channel_reestablish<L: Deref>(&mut self, logger: &L) -> msgs::ChannelReestablish where L::Target: Logger {
@@ -8095,7 +8090,7 @@ impl<SP: Deref> FundedChannel<SP> where
80958090
next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number - 1,
80968091
your_last_per_commitment_secret: remote_last_secret,
80978092
my_current_per_commitment_point: dummy_pubkey,
8098-
next_funding_txid: self.context.next_funding_txid,
8093+
next_funding_txid: self.maybe_get_next_funding_txid(),
80998094
}
81008095
}
81018096

@@ -10455,13 +10450,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1045510450

1045610451
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
1045710452
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
10458-
// TODO(dual_funding): Instead of getting this from persisted value, figure it out based on the
10459-
// funding transaction and other channel state.
10460-
//
10461-
// If we've sent `commtiment_signed` for an interactively constructed transaction
10462-
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
10463-
// to the txid of that interactive transaction, else we MUST NOT set it.
10464-
next_funding_txid: None,
1046510453
},
1046610454
interactive_tx_signing_session: None,
1046710455
is_v2_established,

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)