Skip to content

Commit 860a836

Browse files
committed
Preparations for splicing transaction negotiation
1 parent a303b9e commit 860a836

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

lightning/src/ln/channel.rs

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ use crate::ln::interactivetxs::{
3434
InteractiveTxConstructorArgs, InteractiveTxMessageSend, InteractiveTxSigningSession, InteractiveTxMessageSendResult,
3535
OutputOwned, SharedOwnedOutput, TX_COMMON_FIELDS_WEIGHT,
3636
};
37+
#[cfg(splicing)]
38+
use crate::ln::interactivetxs::InteractiveTxMessageSend;
3739
use crate::ln::msgs;
3840
use crate::ln::msgs::{ClosingSigned, ClosingSignedFeeRange, DecodeError};
3941
use crate::ln::script::{self, ShutdownScript};
@@ -1191,6 +1193,10 @@ impl UnfundedChannelContext {
11911193
#[derive(Clone)]
11921194
struct PendingSpliceInfoPre {
11931195
pub our_funding_contribution: i64,
1196+
pub funding_feerate_perkw: u32,
1197+
pub locktime: u32,
1198+
/// The funding inputs that we plan to contributing to the splice.
1199+
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
11941200
}
11951201

11961202
#[cfg(splicing)]
@@ -4268,6 +4274,18 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
42684274
self.get_initial_counterparty_commitment_signature(logger)
42694275
}
42704276

4277+
/// Splice process starting; update state, log, etc.
4278+
#[cfg(splicing)]
4279+
pub(crate) fn splice_start<L: Deref>(&mut self, is_outgoing: bool, logger: &L) where L::Target: Logger {
4280+
// Set state, by this point splice_init/splice_ack handshake is complete
4281+
// TODO(splicing)
4282+
// self.channel_state = ChannelState::NegotiatingFunding(
4283+
// NegotiatingFundingFlags::OUR_INIT_SENT | NegotiatingFundingFlags::THEIR_INIT_SENT
4284+
// );
4285+
log_info!(logger, "Splicing process started, old channel value {}, outgoing {}, channel_id {}",
4286+
self.channel_value_satoshis, is_outgoing, self.channel_id);
4287+
}
4288+
42714289
/// Get the splice message that can be sent during splice initiation.
42724290
#[cfg(splicing)]
42734291
pub fn get_splice_init(&self, our_funding_contribution_satoshis: i64,
@@ -8077,10 +8095,15 @@ impl<SP: Deref> Channel<SP> where
80778095
// Note: post-splice channel value is not yet known at this point, counterpary contribution is not known
80788096
// (Cannot test for miminum required post-splice channel value)
80798097

8098+
// Sum and convert inputs
8099+
let mut sum_input = 0i64;
8100+
let mut funding_inputs = Vec::new();
8101+
for (tx_in, tx) in our_funding_inputs.into_iter() {
8102+
sum_input += tx.output.get(tx_in.previous_output.vout as usize).map(|tx| tx.value.to_sat() as i64).unwrap_or(0);
8103+
let tx16 = TransactionU16LenLimited::new(tx).map_err(|_e| ChannelError::Warn(format!("Too large transaction")))?;
8104+
funding_inputs.push((tx_in, tx16));
8105+
}
80808106
// Check that inputs are sufficient to cover our contribution
8081-
let sum_input: i64 = our_funding_inputs.into_iter().fold(0, |acc, i|
8082-
acc + i.1.output.get(i.0.previous_output.vout as usize).map(|tx| tx.value.to_sat() as i64).unwrap_or(0)
8083-
);
80848107
if sum_input < our_funding_contribution_satoshis {
80858108
return Err(ChannelError::Warn(format!(
80868109
"Provided inputs are insufficient for our contribution, {} {}",
@@ -8090,6 +8113,9 @@ impl<SP: Deref> Channel<SP> where
80908113

80918114
self.pending_splice_pre = Some(PendingSpliceInfoPre {
80928115
our_funding_contribution: our_funding_contribution_satoshis,
8116+
funding_feerate_perkw,
8117+
locktime,
8118+
our_funding_inputs: funding_inputs,
80938119
});
80948120

80958121
let msg = self.context.get_splice_init(our_funding_contribution_satoshis, funding_feerate_perkw, locktime);
@@ -8098,7 +8124,9 @@ impl<SP: Deref> Channel<SP> where
80988124

80998125
/// Handle splice_init
81008126
#[cfg(splicing)]
8101-
pub fn splice_init(&mut self, msg: &msgs::SpliceInit) -> Result<msgs::SpliceAck, ChannelError> {
8127+
pub fn splice_init<ES: Deref, L: Deref>(
8128+
&mut self, msg: &msgs::SpliceInit, _signer_provider: &SP, _entropy_source: &ES, _holder_node_id: PublicKey, logger: &L,
8129+
) -> Result<msgs::SpliceAck, ChannelError> where ES::Target: EntropySource, L::Target: Logger {
81028130
let their_funding_contribution_satoshis = msg.funding_contribution_satoshis;
81038131
// TODO(splicing): Currently not possible to contribute on the splicing-acceptor side
81048132
let our_funding_contribution_satoshis = 0i64;
@@ -8141,16 +8169,24 @@ impl<SP: Deref> Channel<SP> where
81418169
let _res = self.context.check_balance_meets_reserve_requirements(post_balance, post_channel_value)?;
81428170

81438171
// TODO(splicing): Store msg.funding_pubkey
8144-
// TODO(splicing): Apply start of splice (splice_start)
8172+
8173+
// Apply start of splice change in the state
8174+
self.context.splice_start(false, logger);
81458175

81468176
let splice_ack_msg = self.context.get_splice_ack(our_funding_contribution_satoshis);
8177+
81478178
// TODO(splicing): start interactive funding negotiation
8179+
// let _msg = self.begin_interactive_funding_tx_construction(signer_provider, entropy_source, holder_node_id)
8180+
// .map_err(|err| ChannelError::Warn(format!("Failed to start interactive transaction construction, {:?}", err)))?;
8181+
81488182
Ok(splice_ack_msg)
81498183
}
81508184

81518185
/// Handle splice_ack
81528186
#[cfg(splicing)]
8153-
pub fn splice_ack(&mut self, msg: &msgs::SpliceAck) -> Result<(), ChannelError> {
8187+
pub fn splice_ack<ES: Deref, L: Deref>(
8188+
&mut self, msg: &msgs::SpliceAck, _signer_provider: &SP, _entropy_source: &ES, _holder_node_id: PublicKey, logger: &L,
8189+
) -> Result<Option<InteractiveTxMessageSend>, ChannelError> where ES::Target: EntropySource, L::Target: Logger {
81548190
let their_funding_contribution_satoshis = msg.funding_contribution_satoshis;
81558191

81568192
// check if splice is pending
@@ -8168,7 +8204,15 @@ impl<SP: Deref> Channel<SP> where
81688204
// Early check for reserve requirement, assuming maximum balance of full channel value
81698205
// This will also be checked later at tx_complete
81708206
let _res = self.context.check_balance_meets_reserve_requirements(post_balance, post_channel_value)?;
8171-
Ok(())
8207+
8208+
// Apply start of splice change in the state
8209+
self.context.splice_start(true, logger);
8210+
8211+
// TODO(splicing): start interactive funding negotiation
8212+
// let tx_msg_opt = self.begin_interactive_funding_tx_construction(signer_provider, entropy_source, holder_node_id)
8213+
// .map_err(|err| ChannelError::Warn(format!("V2 channel rejected due to sender error, {:?}", err)))?;
8214+
// Ok(tx_msg_opt)
8215+
Ok(None)
81728216
}
81738217

81748218
// 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
@@ -9400,7 +9400,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
94009400
), msg.channel_id)),
94019401
hash_map::Entry::Occupied(mut chan_entry) => {
94029402
if let ChannelPhase::Funded(chan) = chan_entry.get_mut() {
9403-
match chan.splice_init(msg) {
9403+
match chan.splice_init(msg, &self.signer_provider, &self.entropy_source, self.get_our_node_id(), &self.logger) {
94049404
Ok(splice_ack_msg) => {
94059405
peer_state.pending_msg_events.push(events::MessageSendEvent::SendSpliceAck {
94069406
node_id: *counterparty_node_id,
@@ -9445,8 +9445,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
94459445
), msg.channel_id)),
94469446
hash_map::Entry::Occupied(mut chan) => {
94479447
if let ChannelPhase::Funded(chan) = chan.get_mut() {
9448-
match chan.splice_ack(msg) {
9449-
Ok(_) => {}
9448+
match chan.splice_ack(msg, &self.signer_provider, &self.entropy_source, self.get_our_node_id(), &self.logger) {
9449+
Ok(tx_msg_opt) => {
9450+
if let Some(tx_msg_opt) = tx_msg_opt {
9451+
peer_state.pending_msg_events.push(tx_msg_opt.into_msg_send_event(counterparty_node_id.clone()));
9452+
}
9453+
}
94509454
Err(err) => {
94519455
return Err(MsgHandleErrInternal::from_chan_no_close(err, msg.channel_id));
94529456
}

0 commit comments

Comments
 (0)