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