Skip to content

Commit bf4bb18

Browse files
committed
fix Improve persist notifier in splice_channel()
1 parent f26274a commit bf4bb18

File tree

1 file changed

+51
-33
lines changed

1 file changed

+51
-33
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4246,46 +4246,64 @@ where
42464246
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>,
42474247
funding_feerate_per_kw: u32, locktime: Option<u32>,
42484248
) -> Result<(), APIError> {
4249-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
4250-
let per_peer_state = self.per_peer_state.read().unwrap();
4249+
let mut res = Ok(());
4250+
PersistenceNotifierGuard::optionally_notify(self, || {
4251+
let per_peer_state = self.per_peer_state.read().unwrap();
42514252

4252-
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
4253-
.ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
4253+
let peer_state_mutex = match per_peer_state.get(counterparty_node_id)
4254+
.ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) }) {
4255+
Ok(p) => p,
4256+
Err(e) => {
4257+
res = Err(e);
4258+
return NotifyOption::SkipPersistNoEvents;
4259+
}
4260+
};
42544261

4255-
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4256-
let peer_state = &mut *peer_state_lock;
4262+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4263+
let peer_state = &mut *peer_state_lock;
42574264

4258-
// Look for the channel
4259-
match peer_state.channel_by_id.entry(*channel_id) {
4260-
hash_map::Entry::Occupied(mut chan_phase_entry) => {
4261-
let locktime = locktime.unwrap_or(self.current_best_block().height);
4262-
if let Some(chan) = chan_phase_entry.get_mut().as_funded_mut() {
4263-
let msg = chan.splice_channel(our_funding_contribution_satoshis, our_funding_inputs, funding_feerate_per_kw, locktime)?;
4265+
// Look for the channel
4266+
match peer_state.channel_by_id.entry(*channel_id) {
4267+
hash_map::Entry::Occupied(mut chan_phase_entry) => {
4268+
let locktime = locktime.unwrap_or(self.current_best_block().height);
4269+
if let Some(chan) = chan_phase_entry.get_mut().as_funded_mut() {
4270+
let msg = match chan.splice_channel(our_funding_contribution_satoshis, our_funding_inputs.clone(), funding_feerate_per_kw, locktime) {
4271+
Ok(m) => m,
4272+
Err(e) => {
4273+
res = Err(e);
4274+
return NotifyOption::SkipPersistNoEvents;
4275+
}
4276+
};
42644277

4265-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendSpliceInit {
4266-
node_id: *counterparty_node_id,
4267-
msg,
4268-
});
4278+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendSpliceInit {
4279+
node_id: *counterparty_node_id,
4280+
msg,
4281+
});
42694282

4270-
Ok(())
4271-
} else {
4272-
Err(APIError::ChannelUnavailable {
4283+
res = Ok(());
4284+
NotifyOption::SkipPersistHandleEvents
4285+
} else {
4286+
res = Err(APIError::ChannelUnavailable {
4287+
err: format!(
4288+
"Channel with id {} is not funded, cannot splice it",
4289+
channel_id
4290+
)
4291+
});
4292+
NotifyOption::SkipPersistNoEvents
4293+
}
4294+
},
4295+
hash_map::Entry::Vacant(_) => {
4296+
res = Err(APIError::ChannelUnavailable {
42734297
err: format!(
4274-
"Channel with id {} is not funded, cannot splice it",
4275-
channel_id
4298+
"Channel with id {} not found for the passed counterparty node_id {}",
4299+
channel_id, counterparty_node_id,
42764300
)
4277-
})
4278-
}
4279-
},
4280-
hash_map::Entry::Vacant(_) => {
4281-
return Err(APIError::ChannelUnavailable {
4282-
err: format!(
4283-
"Channel with id {} not found for the passed counterparty node_id {}",
4284-
channel_id, counterparty_node_id,
4285-
)
4286-
});
4287-
},
4288-
}
4301+
});
4302+
NotifyOption::SkipPersistNoEvents
4303+
},
4304+
}
4305+
});
4306+
res
42894307
}
42904308

42914309
fn can_forward_htlc_to_outgoing_channel(

0 commit comments

Comments
 (0)