Skip to content

Commit

Permalink
Populate Channel::interactive_tx_signing_session on read
Browse files Browse the repository at this point in the history
We allow persisting `Channel`s in a `ChannelState::FundingNegotiated`
only if `Channel::interactive_tx_signing_session` is Some.

When reading a persisted `Channel` in `ChannelState::FundingNegotiate`,
we populate `Channel::interactive_tx_signing_session` with appropriate
values for accepting dual-funded channels without contributing.
Currently we don't need persistence, but we will need to persist
some information when we allow contributing funds to dual-funded
channels.
  • Loading branch information
dunxen committed Nov 29, 2024
1 parent e1fd19d commit 26fb59f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
40 changes: 36 additions & 4 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ use crate::ln::types::ChannelId;
use crate::types::payment::{PaymentPreimage, PaymentHash};
use crate::types::features::{ChannelTypeFeatures, InitFeatures};
use crate::ln::interactivetxs::{
get_output_weight, HandleTxCompleteResult, InteractiveTxConstructor, InteractiveTxConstructorArgs,
InteractiveTxSigningSession, InteractiveTxMessageSendResult, TX_COMMON_FIELDS_WEIGHT,
ConstructedTransaction, get_output_weight, HandleTxCompleteResult, InteractiveTxConstructor,
InteractiveTxConstructorArgs, InteractiveTxSigningSession, InteractiveTxMessageSendResult,
TX_COMMON_FIELDS_WEIGHT,
};
use crate::ln::msgs;
use crate::ln::msgs::{ClosingSigned, ClosingSignedFeeRange, DecodeError};
Expand Down Expand Up @@ -9244,7 +9245,16 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
self.context.channel_id.write(writer)?;
{
let mut channel_state = self.context.channel_state;
if matches!(channel_state, ChannelState::AwaitingChannelReady(_)|ChannelState::ChannelReady(_)) {
if matches!(channel_state, ChannelState::AwaitingChannelReady(_)|ChannelState::ChannelReady(_))
{
channel_state.set_peer_disconnected();
} else if self.interactive_tx_signing_session.is_some() {
if !matches!(channel_state, ChannelState::FundingNegotiated) {
debug_assert!(false, "V2 channels in pre-signing state should not be written");
}
// This is a V2 session which has an active signing session
// TODO(dual_funding): When we allow contributing funds to dual-funded channels,
// we will need to handle persisting appropriate signing session state.
channel_state.set_peer_disconnected();
} else {
debug_assert!(false, "Pre-funded/shutdown channels should not be written");
Expand Down Expand Up @@ -9898,6 +9908,28 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
let mut is_manual_broadcast = None;

let interactive_tx_signing_session = if matches!(channel_state, ChannelState::FundingNegotiated) {
if let Some(ref funding_tx) = funding_transaction {
// TODO(dual_funding): When we allow contributing funds to dual-funded channels,
// we will need to handle persisting appropriate signing session state.
Some(InteractiveTxSigningSession {
unsigned_tx: ConstructedTransaction::default(),
counterparty_sent_tx_signatures: false,
holder_sends_tx_signatures_first: true, // TODO(dual_funding): Fixed to true as we currently don't contribute inputs.
received_commitment_signed: true, // We only enter the FundingNegotiated state once we have persisted the initial channel monitor.
holder_tx_signatures: Some(msgs::TxSignatures {
channel_id,
tx_hash: funding_tx.compute_txid(),
witnesses: vec![], // TODO(dual_funding): Fixed to true as we currently don't contribute inputs.
shared_input_signature: None, // TODO(splicing): Only relevant to splicing
})
})
} else {
debug_assert!(false, "Tried to read V2 channel with signing session but there is no funding transaction");
None
}
} else { None };

read_tlv_fields!(reader, {
(0, announcement_sigs, option),
(1, minimum_depth, option),
Expand Down Expand Up @@ -10194,7 +10226,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
},
interactive_tx_signing_session: None,
interactive_tx_signing_session,
})
}
}
Expand Down
17 changes: 17 additions & 0 deletions lightning/src/ln/interactivetxs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,23 @@ pub(crate) struct ConstructedTransaction {
holder_sends_tx_signatures_first: bool,
}

impl Default for ConstructedTransaction {
fn default() -> Self {
ConstructedTransaction {
holder_is_initiator: false,
inputs: vec![],
outputs: vec![],
local_inputs_value_satoshis: 0,
local_outputs_value_satoshis: 0,
remote_inputs_value_satoshis: 0,
remote_outputs_value_satoshis: 0,
lock_time: AbsoluteLockTime::from_height(0)
.expect("0 is a valid block height for a locktime"),
holder_sends_tx_signatures_first: true,
}
}
}

impl ConstructedTransaction {
fn new(context: NegotiationContext) -> Self {
let local_inputs_value_satoshis = context
Expand Down

0 comments on commit 26fb59f

Please sign in to comment.