Skip to content

Commit

Permalink
In splice_channel() check if provided inputs are sufficient
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Nov 26, 2024
1 parent 4972b25 commit 9ffe4b1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
13 changes: 12 additions & 1 deletion lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7895,7 +7895,7 @@ impl<SP: Deref> Channel<SP> where
/// Initiate splicing
#[cfg(splicing)]
pub fn splice_channel(&mut self, our_funding_contribution_satoshis: i64,
funding_feerate_perkw: u32, locktime: u32,
our_funding_inputs: Vec<(TxIn, Transaction)>, funding_feerate_perkw: u32, locktime: u32,
) -> Result<msgs::SpliceInit, ChannelError> {
// Check if a splice has been initiated already.
// Note: this could be handled more nicely, and support multiple outstanding splice's, the incoming splice_ack matters anyways.
Expand Down Expand Up @@ -7928,6 +7928,17 @@ impl<SP: Deref> Channel<SP> where
// Note: post-splice channel value is not yet known at this point, counterpary contribution is not known
// (Cannot test for miminum required post-splice channel value)

// Check that inputs are sufficient to cover our contribution
let sum_input: i64 = our_funding_inputs.into_iter().fold(0, |acc, i|
acc + i.1.output.get(i.0.previous_output.vout as usize).map(|tx| tx.value.to_sat() as i64).unwrap_or(0)
);
if sum_input < our_funding_contribution_satoshis {
return Err(ChannelError::Warn(format!(
"Provided inputs are insufficient for our contribution, {} {}",
sum_input, our_funding_contribution_satoshis,
)));
}

self.pending_splice_pre = Some(PendingSpliceInfoPre {
our_funding_contribution: our_funding_contribution_satoshis,
});
Expand Down
4 changes: 2 additions & 2 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4128,7 +4128,7 @@ where
#[cfg(splicing)]
pub fn splice_channel(
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, our_funding_contribution_satoshis: i64,
_our_funding_inputs: Vec<(TxIn, Transaction)>, funding_feerate_perkw: u32, locktime: u32,
our_funding_inputs: Vec<(TxIn, Transaction)>, funding_feerate_perkw: u32, locktime: u32,
) -> Result<(), APIError> {
let per_peer_state = self.per_peer_state.read().unwrap();

Expand All @@ -4142,7 +4142,7 @@ where
match peer_state.channel_by_id.entry(*channel_id) {
hash_map::Entry::Occupied(mut chan_phase_entry) => {
if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
let msg = chan.splice_channel(our_funding_contribution_satoshis, funding_feerate_perkw, locktime)
let msg = chan.splice_channel(our_funding_contribution_satoshis, our_funding_inputs, funding_feerate_perkw, locktime)
.map_err(|err| APIError::APIMisuseError {
err: format!(
"Cannot initiate Splicing, {}, channel ID {}", err, channel_id
Expand Down
8 changes: 7 additions & 1 deletion lightning/src/ln/functional_tests_splice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,20 @@ fn test_v1_splice_in() {
let funding_feerate_perkw = 1024; // TODO
let locktime = 0; // TODO

// Create additional inputs
let extra_splice_funding_input_sats = 35_000;
let funding_inputs = create_dual_funding_utxos_with_prev_txs(
&initiator_node,
&[extra_splice_funding_input_sats],
);
// Initiate splice-in (on initiator_node)
let _res = initiator_node
.node
.splice_channel(
&channel_id2,
&acceptor_node.node.get_our_node_id(),
splice_in_sats as i64,
Vec::new(),
funding_inputs,
funding_feerate_perkw,
locktime,
)
Expand Down

0 comments on commit 9ffe4b1

Please sign in to comment.