Skip to content

Commit f2c821e

Browse files
committed
fix Improve persist notifier in splice_channel()
1 parent 4ed8a9b commit f2c821e

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
@@ -4293,46 +4293,64 @@ where
42934293
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>,
42944294
funding_feerate_per_kw: u32, locktime: Option<u32>,
42954295
) -> Result<(), APIError> {
4296-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
4297-
let per_peer_state = self.per_peer_state.read().unwrap();
4296+
let mut res = Ok(());
4297+
PersistenceNotifierGuard::optionally_notify(self, || {
4298+
let per_peer_state = self.per_peer_state.read().unwrap();
42984299

4299-
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
4300-
.ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
4300+
let peer_state_mutex = match per_peer_state.get(counterparty_node_id)
4301+
.ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) }) {
4302+
Ok(p) => p,
4303+
Err(e) => {
4304+
res = Err(e);
4305+
return NotifyOption::SkipPersistNoEvents;
4306+
}
4307+
};
43014308

4302-
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4303-
let peer_state = &mut *peer_state_lock;
4309+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4310+
let peer_state = &mut *peer_state_lock;
43044311

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 = chan.splice_channel(our_funding_contribution_satoshis, our_funding_inputs, funding_feerate_per_kw, locktime)?;
4312+
// Look for the channel
4313+
match peer_state.channel_by_id.entry(*channel_id) {
4314+
hash_map::Entry::Occupied(mut chan_phase_entry) => {
4315+
let locktime = locktime.unwrap_or(self.current_best_block().height);
4316+
if let Some(chan) = chan_phase_entry.get_mut().as_funded_mut() {
4317+
let msg = match chan.splice_channel(our_funding_contribution_satoshis, our_funding_inputs.clone(), funding_feerate_per_kw, locktime) {
4318+
Ok(m) => m,
4319+
Err(e) => {
4320+
res = Err(e);
4321+
return NotifyOption::SkipPersistNoEvents;
4322+
}
4323+
};
43114324

4312-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendSpliceInit {
4313-
node_id: *counterparty_node_id,
4314-
msg,
4315-
});
4325+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendSpliceInit {
4326+
node_id: *counterparty_node_id,
4327+
msg,
4328+
});
43164329

4317-
Ok(())
4318-
} else {
4319-
Err(APIError::ChannelUnavailable {
4330+
res = Ok(());
4331+
NotifyOption::SkipPersistHandleEvents
4332+
} else {
4333+
res = Err(APIError::ChannelUnavailable {
4334+
err: format!(
4335+
"Channel with id {} is not funded, cannot splice it",
4336+
channel_id
4337+
)
4338+
});
4339+
NotifyOption::SkipPersistNoEvents
4340+
}
4341+
},
4342+
hash_map::Entry::Vacant(_) => {
4343+
res = Err(APIError::ChannelUnavailable {
43204344
err: format!(
4321-
"Channel with id {} is not funded, cannot splice it",
4322-
channel_id
4345+
"Channel with id {} not found for the passed counterparty node_id {}",
4346+
channel_id, counterparty_node_id,
43234347
)
4324-
})
4325-
}
4326-
},
4327-
hash_map::Entry::Vacant(_) => {
4328-
return 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-
});
4334-
},
4335-
}
4348+
});
4349+
NotifyOption::SkipPersistNoEvents
4350+
},
4351+
}
4352+
});
4353+
res
43364354
}
43374355

43384356
fn can_forward_htlc_to_outgoing_channel(

0 commit comments

Comments
 (0)