Skip to content

Commit 338eac2

Browse files
committed
fix Introduce internal_splice_channel to simplify error handling
1 parent 3fb8b5f commit 338eac2

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
@@ -8259,7 +8259,7 @@ impl<SP: Deref> FundedChannel<SP> where
82598259
/// Includes the witness weight for this input (e.g. P2WPKH_WITNESS_WEIGHT=109 for typical P2WPKH inputs).
82608260
#[cfg(splicing)]
82618261
pub fn splice_channel(&mut self, our_funding_contribution_satoshis: i64,
8262-
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>,
8262+
our_funding_inputs: &Vec<(TxIn, Transaction, Weight)>,
82638263
funding_feerate_per_kw: u32, locktime: u32,
82648264
) -> Result<msgs::SpliceInit, APIError> {
82658265
// 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
@@ -4269,6 +4269,60 @@ where
42694269
}
42704270
}
42714271

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

0 commit comments

Comments
 (0)