Skip to content

Commit bf5988d

Browse files
committed
Preparations for splicing transaction negotiation
1 parent ee46987 commit bf5988d

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, InteractiveTxSigningSession, InteractiveTxMessageSendResult,
3535
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};
@@ -1186,6 +1188,10 @@ impl UnfundedChannelContext {
11861188
#[derive(Clone)]
11871189
struct PendingSpliceInfoPre {
11881190
pub our_funding_contribution: i64,
1191+
pub funding_feerate_perkw: u32,
1192+
pub locktime: u32,
1193+
/// The funding inputs that we plan to contributing to the splice.
1194+
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
11891195
}
11901196

11911197
#[cfg(splicing)]
@@ -4144,6 +4150,18 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
41444150
self.get_initial_counterparty_commitment_signature(logger)
41454151
}
41464152

4153+
/// Splice process starting; update state, log, etc.
4154+
#[cfg(splicing)]
4155+
pub(crate) fn splice_start<L: Deref>(&mut self, is_outgoing: bool, logger: &L) where L::Target: Logger {
4156+
// Set state, by this point splice_init/splice_ack handshake is complete
4157+
// TODO(splicing)
4158+
// self.channel_state = ChannelState::NegotiatingFunding(
4159+
// NegotiatingFundingFlags::OUR_INIT_SENT | NegotiatingFundingFlags::THEIR_INIT_SENT
4160+
// );
4161+
log_info!(logger, "Splicing process started, old channel value {}, outgoing {}, channel_id {}",
4162+
self.channel_value_satoshis, is_outgoing, self.channel_id);
4163+
}
4164+
41474165
/// Get the splice message that can be sent during splice initiation.
41484166
#[cfg(splicing)]
41494167
pub fn get_splice_init(&self, our_funding_contribution_satoshis: i64,
@@ -7936,10 +7954,15 @@ impl<SP: Deref> Channel<SP> where
79367954
// Note: post-splice channel value is not yet known at this point, counterpary contribution is not known
79377955
// (Cannot test for miminum required post-splice channel value)
79387956

7957+
// Sum and convert inputs
7958+
let mut sum_input = 0i64;
7959+
let mut funding_inputs = Vec::new();
7960+
for (tx_in, tx) in our_funding_inputs.into_iter() {
7961+
sum_input += tx.output.get(tx_in.previous_output.vout as usize).map(|tx| tx.value.to_sat() as i64).unwrap_or(0);
7962+
let tx16 = TransactionU16LenLimited::new(tx).map_err(|_e| ChannelError::Warn(format!("Too large transaction")))?;
7963+
funding_inputs.push((tx_in, tx16));
7964+
}
79397965
// Check that inputs are sufficient to cover our contribution
7940-
let sum_input: i64 = our_funding_inputs.into_iter().fold(0, |acc, i|
7941-
acc + i.1.output.get(i.0.previous_output.vout as usize).map(|tx| tx.value.to_sat() as i64).unwrap_or(0)
7942-
);
79437966
if sum_input < our_funding_contribution_satoshis {
79447967
return Err(ChannelError::Warn(format!(
79457968
"Provided inputs are insufficient for our contribution, {} {}",
@@ -7949,6 +7972,9 @@ impl<SP: Deref> Channel<SP> where
79497972

79507973
self.pending_splice_pre = Some(PendingSpliceInfoPre {
79517974
our_funding_contribution: our_funding_contribution_satoshis,
7975+
funding_feerate_perkw,
7976+
locktime,
7977+
our_funding_inputs: funding_inputs,
79527978
});
79537979

79547980
let msg = self.context.get_splice_init(our_funding_contribution_satoshis, funding_feerate_perkw, locktime);
@@ -7957,7 +7983,9 @@ impl<SP: Deref> Channel<SP> where
79577983

79587984
/// Handle splice_init
79597985
#[cfg(splicing)]
7960-
pub fn splice_init(&mut self, msg: &msgs::SpliceInit) -> Result<msgs::SpliceAck, ChannelError> {
7986+
pub fn splice_init<ES: Deref, L: Deref>(
7987+
&mut self, msg: &msgs::SpliceInit, _signer_provider: &SP, _entropy_source: &ES, _holder_node_id: PublicKey, logger: &L,
7988+
) -> Result<msgs::SpliceAck, ChannelError> where ES::Target: EntropySource, L::Target: Logger {
79617989
let their_funding_contribution_satoshis = msg.funding_contribution_satoshis;
79627990
// TODO(splicing): Currently not possible to contribute on the splicing-acceptor side
79637991
let our_funding_contribution_satoshis = 0i64;
@@ -8000,16 +8028,24 @@ impl<SP: Deref> Channel<SP> where
80008028
let _res = self.context.check_balance_meets_reserve_requirements(post_balance, post_channel_value)?;
80018029

80028030
// TODO(splicing): Store msg.funding_pubkey
8003-
// TODO(splicing): Apply start of splice (splice_start)
8031+
8032+
// Apply start of splice change in the state
8033+
self.context.splice_start(false, logger);
80048034

80058035
let splice_ack_msg = self.context.get_splice_ack(our_funding_contribution_satoshis);
8036+
80068037
// TODO(splicing): start interactive funding negotiation
8038+
// let _msg = self.begin_interactive_funding_tx_construction(signer_provider, entropy_source, holder_node_id)
8039+
// .map_err(|err| ChannelError::Warn(format!("Failed to start interactive transaction construction, {:?}", err)))?;
8040+
80078041
Ok(splice_ack_msg)
80088042
}
80098043

80108044
/// Handle splice_ack
80118045
#[cfg(splicing)]
8012-
pub fn splice_ack(&mut self, msg: &msgs::SpliceAck) -> Result<(), ChannelError> {
8046+
pub fn splice_ack<ES: Deref, L: Deref>(
8047+
&mut self, msg: &msgs::SpliceAck, _signer_provider: &SP, _entropy_source: &ES, _holder_node_id: PublicKey, logger: &L,
8048+
) -> Result<Option<InteractiveTxMessageSend>, ChannelError> where ES::Target: EntropySource, L::Target: Logger {
80138049
let their_funding_contribution_satoshis = msg.funding_contribution_satoshis;
80148050

80158051
// check if splice is pending
@@ -8027,7 +8063,15 @@ impl<SP: Deref> Channel<SP> where
80278063
// Early check for reserve requirement, assuming maximum balance of full channel value
80288064
// This will also be checked later at tx_complete
80298065
let _res = self.context.check_balance_meets_reserve_requirements(post_balance, post_channel_value)?;
8030-
Ok(())
8066+
8067+
// Apply start of splice change in the state
8068+
self.context.splice_start(true, logger);
8069+
8070+
// TODO(splicing): start interactive funding negotiation
8071+
// let tx_msg_opt = self.begin_interactive_funding_tx_construction(signer_provider, entropy_source, holder_node_id)
8072+
// .map_err(|err| ChannelError::Warn(format!("V2 channel rejected due to sender error, {:?}", err)))?;
8073+
// Ok(tx_msg_opt)
8074+
Ok(None)
80318075
}
80328076

80338077
// 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
@@ -9322,7 +9322,7 @@ where
93229322
), msg.channel_id)),
93239323
hash_map::Entry::Occupied(mut chan_entry) => {
93249324
if let ChannelPhase::Funded(chan) = chan_entry.get_mut() {
9325-
match chan.splice_init(msg) {
9325+
match chan.splice_init(msg, &self.signer_provider, &self.entropy_source, self.get_our_node_id(), &self.logger) {
93269326
Ok(splice_ack_msg) => {
93279327
peer_state.pending_msg_events.push(events::MessageSendEvent::SendSpliceAck {
93289328
node_id: *counterparty_node_id,
@@ -9367,8 +9367,12 @@ where
93679367
), msg.channel_id)),
93689368
hash_map::Entry::Occupied(mut chan) => {
93699369
if let ChannelPhase::Funded(chan) = chan.get_mut() {
9370-
match chan.splice_ack(msg) {
9371-
Ok(_) => {}
9370+
match chan.splice_ack(msg, &self.signer_provider, &self.entropy_source, self.get_our_node_id(), &self.logger) {
9371+
Ok(tx_msg_opt) => {
9372+
if let Some(tx_msg_opt) = tx_msg_opt {
9373+
peer_state.pending_msg_events.push(tx_msg_opt.into_msg_send_event(counterparty_node_id.clone()));
9374+
}
9375+
}
93729376
Err(err) => {
93739377
return Err(MsgHandleErrInternal::from_chan_no_close(err, msg.channel_id));
93749378
}

0 commit comments

Comments
 (0)