Skip to content

Commit fd6c3aa

Browse files
committed
fix Introduce internal_splice_channel to simplify error handling
1 parent 3536ee3 commit fd6c3aa

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
@@ -8389,7 +8389,7 @@ impl<SP: Deref> FundedChannel<SP> where
83898389
/// Includes the witness weight for this input (e.g. P2WPKH_WITNESS_WEIGHT=109 for typical P2WPKH inputs).
83908390
#[cfg(splicing)]
83918391
pub fn splice_channel(&mut self, our_funding_contribution_satoshis: i64,
8392-
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>,
8392+
our_funding_inputs: &Vec<(TxIn, Transaction, Weight)>,
83938393
funding_feerate_per_kw: u32, locktime: u32,
83948394
) -> Result<msgs::SpliceInit, APIError> {
83958395
// 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
@@ -4281,6 +4281,60 @@ where
42814281
}
42824282
}
42834283

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

0 commit comments

Comments
 (0)