Skip to content

Commit 4abec03

Browse files
committed
fix Introduce internal_splice_channel to simplify error handling
1 parent 2e4406b commit 4abec03

File tree

2 files changed

+60
-55
lines changed

2 files changed

+60
-55
lines changed

lightning/src/ln/channel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8435,7 +8435,7 @@ impl<SP: Deref> FundedChannel<SP> where
84358435
/// Includes the witness weight for this input (e.g. P2WPKH_WITNESS_WEIGHT=109 for typical P2WPKH inputs).
84368436
#[cfg(splicing)]
84378437
pub fn splice_channel(&mut self, our_funding_contribution_satoshis: i64,
8438-
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>,
8438+
our_funding_inputs: &Vec<(TxIn, Transaction, Weight)>,
84398439
funding_feerate_per_kw: u32, locktime: u32,
84408440
) -> Result<msgs::SpliceInit, APIError> {
84418441
// Check if a splice has been initiated already.

lightning/src/ln/channelmanager.rs

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4282,6 +4282,60 @@ where
42824282
}
42834283
}
42844284

4285+
/// See [`splice_channel`]
4286+
#[cfg(splicing)]
4287+
fn internal_splice_channel(
4288+
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, our_funding_contribution_satoshis: i64,
4289+
our_funding_inputs: &Vec<(TxIn, Transaction, Weight)>,
4290+
funding_feerate_per_kw: u32, locktime: Option<u32>,
4291+
) -> (Result<(), APIError>, NotifyOption) {
4292+
let per_peer_state = self.per_peer_state.read().unwrap();
4293+
4294+
let peer_state_mutex = match per_peer_state.get(counterparty_node_id)
4295+
.ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) }) {
4296+
Ok(p) => p,
4297+
Err(e) => return (Err(e), NotifyOption::SkipPersistNoEvents),
4298+
};
4299+
4300+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4301+
let peer_state = &mut *peer_state_lock;
4302+
4303+
// Look for the channel
4304+
match peer_state.channel_by_id.entry(*channel_id) {
4305+
hash_map::Entry::Occupied(mut chan_phase_entry) => {
4306+
let locktime = locktime.unwrap_or(self.current_best_block().height);
4307+
if let Some(chan) = chan_phase_entry.get_mut().as_funded_mut() {
4308+
let msg = match chan.splice_channel(our_funding_contribution_satoshis, our_funding_inputs, funding_feerate_per_kw, locktime) {
4309+
Ok(m) => m,
4310+
Err(e) => return (Err(e), NotifyOption::SkipPersistNoEvents),
4311+
};
4312+
4313+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendSpliceInit {
4314+
node_id: *counterparty_node_id,
4315+
msg,
4316+
});
4317+
4318+
(Ok(()), NotifyOption::SkipPersistHandleEvents)
4319+
} else {
4320+
(Err(APIError::ChannelUnavailable {
4321+
err: format!(
4322+
"Channel with id {} is not funded, cannot splice it",
4323+
channel_id
4324+
)
4325+
}), NotifyOption::SkipPersistNoEvents)
4326+
}
4327+
},
4328+
hash_map::Entry::Vacant(_) => {
4329+
(Err(APIError::ChannelUnavailable {
4330+
err: format!(
4331+
"Channel with id {} not found for the passed counterparty node_id {}",
4332+
channel_id, counterparty_node_id,
4333+
)
4334+
}), NotifyOption::SkipPersistNoEvents)
4335+
},
4336+
}
4337+
}
4338+
42854339
/// Initiate a splice, to change the channel capacity of an existing funded channel.
42864340
/// After completion of splicing, the funding transaction will be replaced by a new one, spending the old funding transaction,
42874341
/// with optional extra inputs (splice-in) and/or extra outputs (splice-out or change).
@@ -4301,60 +4355,11 @@ where
43014355
) -> Result<(), APIError> {
43024356
let mut res = Ok(());
43034357
PersistenceNotifierGuard::optionally_notify(self, || {
4304-
let per_peer_state = self.per_peer_state.read().unwrap();
4305-
4306-
let peer_state_mutex = match per_peer_state.get(counterparty_node_id)
4307-
.ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) }) {
4308-
Ok(p) => p,
4309-
Err(e) => {
4310-
res = Err(e);
4311-
return NotifyOption::SkipPersistNoEvents;
4312-
}
4313-
};
4314-
4315-
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4316-
let peer_state = &mut *peer_state_lock;
4317-
4318-
// Look for the channel
4319-
match peer_state.channel_by_id.entry(*channel_id) {
4320-
hash_map::Entry::Occupied(mut chan_phase_entry) => {
4321-
let locktime = locktime.unwrap_or(self.current_best_block().height);
4322-
if let Some(chan) = chan_phase_entry.get_mut().as_funded_mut() {
4323-
let msg = match chan.splice_channel(our_funding_contribution_satoshis, our_funding_inputs.clone(), funding_feerate_per_kw, locktime) {
4324-
Ok(m) => m,
4325-
Err(e) => {
4326-
res = Err(e);
4327-
return NotifyOption::SkipPersistNoEvents;
4328-
}
4329-
};
4330-
4331-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendSpliceInit {
4332-
node_id: *counterparty_node_id,
4333-
msg,
4334-
});
4335-
4336-
res = Ok(());
4337-
NotifyOption::SkipPersistHandleEvents
4338-
} else {
4339-
res = Err(APIError::ChannelUnavailable {
4340-
err: format!(
4341-
"Channel with id {} is not funded, cannot splice it",
4342-
channel_id
4343-
)
4344-
});
4345-
NotifyOption::SkipPersistNoEvents
4346-
}
4347-
},
4348-
hash_map::Entry::Vacant(_) => {
4349-
res = Err(APIError::ChannelUnavailable {
4350-
err: format!(
4351-
"Channel with id {} not found for the passed counterparty node_id {}",
4352-
channel_id, counterparty_node_id,
4353-
)
4354-
});
4355-
NotifyOption::SkipPersistNoEvents
4356-
},
4357-
}
4358+
let (result, notify_option) = self.internal_splice_channel(
4359+
channel_id, counterparty_node_id, our_funding_contribution_satoshis, &our_funding_inputs, funding_feerate_per_kw, locktime
4360+
);
4361+
res = result;
4362+
notify_option
43584363
});
43594364
res
43604365
}

0 commit comments

Comments
 (0)