Skip to content

Commit 786b2f5

Browse files
committed
Preparations for splicing transaction negotiation
1 parent 36b68cc commit 786b2f5

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

lightning/src/ln/channel.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,10 @@ impl UnfundedChannelContext {
15451545
#[derive(Clone)]
15461546
struct PendingSpliceInfoPre {
15471547
pub our_funding_contribution: i64,
1548+
pub funding_feerate_perkw: u32,
1549+
pub locktime: u32,
1550+
/// The funding inputs that we plan to contributing to the splice.
1551+
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
15481552
}
15491553

15501554
#[cfg(splicing)]
@@ -4566,6 +4570,18 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
45664570
self.get_initial_counterparty_commitment_signature(logger)
45674571
}
45684572

4573+
/// Splice process starting; update state, log, etc.
4574+
#[cfg(splicing)]
4575+
pub(crate) fn splice_start<L: Deref>(&mut self, is_outgoing: bool, logger: &L) where L::Target: Logger {
4576+
// Set state, by this point splice_init/splice_ack handshake is complete
4577+
// TODO(splicing)
4578+
// self.channel_state = ChannelState::NegotiatingFunding(
4579+
// NegotiatingFundingFlags::OUR_INIT_SENT | NegotiatingFundingFlags::THEIR_INIT_SENT
4580+
// );
4581+
log_info!(logger, "Splicing process started, old channel value {}, outgoing {}, channel_id {}",
4582+
self.channel_value_satoshis, is_outgoing, self.channel_id);
4583+
}
4584+
45694585
/// Get the splice message that can be sent during splice initiation.
45704586
#[cfg(splicing)]
45714587
pub fn get_splice_init(&self, our_funding_contribution_satoshis: i64,
@@ -8343,10 +8359,15 @@ impl<SP: Deref> FundedChannel<SP> where
83438359
// Note: post-splice channel value is not yet known at this point, counterpary contribution is not known
83448360
// (Cannot test for miminum required post-splice channel value)
83458361

8362+
// Sum and convert inputs
8363+
let mut sum_input = 0i64;
8364+
let mut funding_inputs = Vec::new();
8365+
for (tx_in, tx) in our_funding_inputs.into_iter() {
8366+
sum_input += tx.output.get(tx_in.previous_output.vout as usize).map(|tx| tx.value.to_sat() as i64).unwrap_or(0);
8367+
let tx16 = TransactionU16LenLimited::new(tx).map_err(|_e| ChannelError::Warn(format!("Too large transaction")))?;
8368+
funding_inputs.push((tx_in, tx16));
8369+
}
83468370
// Check that inputs are sufficient to cover our contribution
8347-
let sum_input: i64 = our_funding_inputs.into_iter().fold(0, |acc, i|
8348-
acc + i.1.output.get(i.0.previous_output.vout as usize).map(|tx| tx.value.to_sat() as i64).unwrap_or(0)
8349-
);
83508371
if sum_input < our_funding_contribution_satoshis {
83518372
return Err(ChannelError::Warn(format!(
83528373
"Provided inputs are insufficient for our contribution, {} {}",
@@ -8356,6 +8377,9 @@ impl<SP: Deref> FundedChannel<SP> where
83568377

83578378
self.pending_splice_pre = Some(PendingSpliceInfoPre {
83588379
our_funding_contribution: our_funding_contribution_satoshis,
8380+
funding_feerate_perkw,
8381+
locktime,
8382+
our_funding_inputs: funding_inputs,
83598383
});
83608384

83618385
let msg = self.context.get_splice_init(our_funding_contribution_satoshis, funding_feerate_perkw, locktime);
@@ -8364,7 +8388,9 @@ impl<SP: Deref> FundedChannel<SP> where
83648388

83658389
/// Handle splice_init
83668390
#[cfg(splicing)]
8367-
pub fn splice_init(&mut self, msg: &msgs::SpliceInit) -> Result<msgs::SpliceAck, ChannelError> {
8391+
pub fn splice_init<ES: Deref, L: Deref>(
8392+
&mut self, msg: &msgs::SpliceInit, _signer_provider: &SP, _entropy_source: &ES, _holder_node_id: PublicKey, logger: &L,
8393+
) -> Result<msgs::SpliceAck, ChannelError> where ES::Target: EntropySource, L::Target: Logger {
83688394
let their_funding_contribution_satoshis = msg.funding_contribution_satoshis;
83698395
// TODO(splicing): Currently not possible to contribute on the splicing-acceptor side
83708396
let our_funding_contribution_satoshis = 0i64;
@@ -8407,16 +8433,24 @@ impl<SP: Deref> FundedChannel<SP> where
84078433
let _res = self.context.check_balance_meets_reserve_requirements(post_balance, post_channel_value)?;
84088434

84098435
// TODO(splicing): Store msg.funding_pubkey
8410-
// TODO(splicing): Apply start of splice (splice_start)
8436+
8437+
// Apply start of splice change in the state
8438+
self.context.splice_start(false, logger);
84118439

84128440
let splice_ack_msg = self.context.get_splice_ack(our_funding_contribution_satoshis);
8441+
84138442
// TODO(splicing): start interactive funding negotiation
8443+
// let _msg = self.begin_interactive_funding_tx_construction(signer_provider, entropy_source, holder_node_id)
8444+
// .map_err(|err| ChannelError::Warn(format!("Failed to start interactive transaction construction, {:?}", err)))?;
8445+
84148446
Ok(splice_ack_msg)
84158447
}
84168448

84178449
/// Handle splice_ack
84188450
#[cfg(splicing)]
8419-
pub fn splice_ack(&mut self, msg: &msgs::SpliceAck) -> Result<(), ChannelError> {
8451+
pub fn splice_ack<ES: Deref, L: Deref>(
8452+
&mut self, msg: &msgs::SpliceAck, _signer_provider: &SP, _entropy_source: &ES, _holder_node_id: PublicKey, logger: &L,
8453+
) -> Result<Option<InteractiveTxMessageSend>, ChannelError> where ES::Target: EntropySource, L::Target: Logger {
84208454
let their_funding_contribution_satoshis = msg.funding_contribution_satoshis;
84218455

84228456
// check if splice is pending
@@ -8434,7 +8468,15 @@ impl<SP: Deref> FundedChannel<SP> where
84348468
// Early check for reserve requirement, assuming maximum balance of full channel value
84358469
// This will also be checked later at tx_complete
84368470
let _res = self.context.check_balance_meets_reserve_requirements(post_balance, post_channel_value)?;
8437-
Ok(())
8471+
8472+
// Apply start of splice change in the state
8473+
self.context.splice_start(true, logger);
8474+
8475+
// TODO(splicing): start interactive funding negotiation
8476+
// let tx_msg_opt = self.begin_interactive_funding_tx_construction(signer_provider, entropy_source, holder_node_id)
8477+
// .map_err(|err| ChannelError::Warn(format!("V2 channel rejected due to sender error, {:?}", err)))?;
8478+
// Ok(tx_msg_opt)
8479+
Ok(None)
84388480
}
84398481

84408482
// Send stuff to our remote peers:

lightning/src/ln/channelmanager.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9377,7 +9377,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
93779377
), msg.channel_id)),
93789378
hash_map::Entry::Occupied(mut chan_entry) => {
93799379
if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
9380-
match chan.splice_init(msg) {
9380+
match chan.splice_init(msg, &self.signer_provider, &self.entropy_source, self.get_our_node_id(), &self.logger) {
93819381
Ok(splice_ack_msg) => {
93829382
peer_state.pending_msg_events.push(events::MessageSendEvent::SendSpliceAck {
93839383
node_id: *counterparty_node_id,
@@ -9422,8 +9422,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
94229422
), msg.channel_id)),
94239423
hash_map::Entry::Occupied(mut chan) => {
94249424
if let Some(chan) = chan.get_mut().as_funded_mut() {
9425-
match chan.splice_ack(msg) {
9426-
Ok(_) => {}
9425+
match chan.splice_ack(msg, &self.signer_provider, &self.entropy_source, self.get_our_node_id(), &self.logger) {
9426+
Ok(tx_msg_opt) => {
9427+
if let Some(tx_msg_opt) = tx_msg_opt {
9428+
peer_state.pending_msg_events.push(tx_msg_opt.into_msg_send_event(counterparty_node_id.clone()));
9429+
}
9430+
}
94279431
Err(err) => {
94289432
return Err(MsgHandleErrInternal::from_chan_no_close(err, msg.channel_id));
94299433
}

0 commit comments

Comments
 (0)