Skip to content

Commit 04e31f1

Browse files
Test utils: allow queueing >2 persistence update results
1 parent 647d70a commit 04e31f1

File tree

3 files changed

+18
-24
lines changed

3 files changed

+18
-24
lines changed

lightning/src/chain/chainmonitor.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ mod tests {
815815

816816
chanmon_cfgs[1].persister.offchain_monitor_updates.lock().unwrap().clear();
817817
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
818+
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
818819

819820
nodes[1].node.claim_funds(payment_preimage_1);
820821
check_added_monitors!(nodes[1], 1);
@@ -823,8 +824,6 @@ mod tests {
823824
check_added_monitors!(nodes[1], 1);
824825
expect_payment_claimed!(nodes[1], payment_hash_2, 1_000_000);
825826

826-
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
827-
828827
let persistences = chanmon_cfgs[1].persister.offchain_monitor_updates.lock().unwrap().clone();
829828
assert_eq!(persistences.len(), 1);
830829
let (funding_txo, updates) = persistences.iter().next().unwrap();

lightning/src/ln/chanmon_update_fail_tests.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ fn test_monitor_update_raa_while_paused() {
782782
check_added_monitors!(nodes[1], 1);
783783
let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
784784

785+
chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
785786
chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
786787
nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_event_2.msgs[0]);
787788
nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_event_2.commitment_msg);
@@ -793,7 +794,6 @@ fn test_monitor_update_raa_while_paused() {
793794
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
794795
check_added_monitors!(nodes[0], 1);
795796

796-
chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
797797
let (outpoint, latest_update, _) = nodes[0].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
798798
nodes[0].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
799799
check_added_monitors!(nodes[0], 0);
@@ -1223,6 +1223,7 @@ fn raa_no_response_awaiting_raa_state() {
12231223
// nodes[1]) followed by an RAA. Fail the monitor updating prior to the CS, deliver the RAA,
12241224
// then restore channel monitor updates.
12251225
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
1226+
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
12261227
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
12271228
nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
12281229
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
@@ -1233,7 +1234,6 @@ fn raa_no_response_awaiting_raa_state() {
12331234
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
12341235
check_added_monitors!(nodes[1], 1);
12351236

1236-
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
12371237
let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
12381238
nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
12391239
// nodes[1] should be AwaitingRAA here!
@@ -1959,7 +1959,7 @@ fn test_path_paused_mpp() {
19591959
// Set it so that the first monitor update (for the path 0 -> 1 -> 3) succeeds, but the second
19601960
// (for the path 0 -> 2 -> 3) fails.
19611961
chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
1962-
chanmon_cfgs[0].persister.set_next_update_ret(Some(ChannelMonitorUpdateStatus::InProgress));
1962+
chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
19631963

19641964
// Now check that we get the right return value, indicating that the first path succeeded but
19651965
// the second got a MonitorUpdateInProgress err. This implies
@@ -2268,6 +2268,7 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
22682268
nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
22692269
check_added_monitors!(nodes[0], 0);
22702270

2271+
chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
22712272
chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
22722273
nodes[0].node.claim_funds(payment_preimage_0);
22732274
check_added_monitors!(nodes[0], 1);

lightning/src/util/test_utils.rs

+13-19
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,9 @@ impl<'a> chain::Watch<EnforcingSigner> for TestChainMonitor<'a> {
224224
}
225225

226226
pub struct TestPersister {
227-
pub update_ret: Mutex<chain::ChannelMonitorUpdateStatus>,
228-
/// If this is set to Some(), after the next return, we'll always return this until update_ret
229-
/// is changed:
230-
pub next_update_ret: Mutex<Option<chain::ChannelMonitorUpdateStatus>>,
227+
/// The queue of update statuses we'll return. If none are queued, ::Completed will always be
228+
/// returned.
229+
pub update_rets: Mutex<VecDeque<chain::ChannelMonitorUpdateStatus>>,
231230
/// When we get an update_persisted_channel call with no ChannelMonitorUpdate, we insert the
232231
/// MonitorUpdateId here.
233232
pub chain_sync_monitor_persistences: Mutex<HashMap<OutPoint, HashSet<MonitorUpdateId>>>,
@@ -238,34 +237,29 @@ pub struct TestPersister {
238237
impl TestPersister {
239238
pub fn new() -> Self {
240239
Self {
241-
update_ret: Mutex::new(chain::ChannelMonitorUpdateStatus::Completed),
242-
next_update_ret: Mutex::new(None),
240+
update_rets: Mutex::new(VecDeque::new()),
243241
chain_sync_monitor_persistences: Mutex::new(HashMap::new()),
244242
offchain_monitor_updates: Mutex::new(HashMap::new()),
245243
}
246244
}
247245

248-
pub fn set_update_ret(&self, ret: chain::ChannelMonitorUpdateStatus) {
249-
*self.update_ret.lock().unwrap() = ret;
250-
}
251-
252-
pub fn set_next_update_ret(&self, next_ret: Option<chain::ChannelMonitorUpdateStatus>) {
253-
*self.next_update_ret.lock().unwrap() = next_ret;
246+
/// Queue an update status to return.
247+
pub fn set_update_ret(&self, next_ret: chain::ChannelMonitorUpdateStatus) {
248+
self.update_rets.lock().unwrap().push_back(next_ret);
254249
}
255250
}
256251
impl<Signer: keysinterface::Sign> chainmonitor::Persist<Signer> for TestPersister {
257252
fn persist_new_channel(&self, _funding_txo: OutPoint, _data: &channelmonitor::ChannelMonitor<Signer>, _id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
258-
let ret = self.update_ret.lock().unwrap().clone();
259-
if let Some(next_ret) = self.next_update_ret.lock().unwrap().take() {
260-
*self.update_ret.lock().unwrap() = next_ret;
253+
if let Some(update_ret) = self.update_rets.lock().unwrap().pop_front() {
254+
return update_ret
261255
}
262-
ret
256+
chain::ChannelMonitorUpdateStatus::Completed
263257
}
264258

265259
fn update_persisted_channel(&self, funding_txo: OutPoint, update: &Option<channelmonitor::ChannelMonitorUpdate>, _data: &channelmonitor::ChannelMonitor<Signer>, update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
266-
let ret = self.update_ret.lock().unwrap().clone();
267-
if let Some(next_ret) = self.next_update_ret.lock().unwrap().take() {
268-
*self.update_ret.lock().unwrap() = next_ret;
260+
let mut ret = chain::ChannelMonitorUpdateStatus::Completed;
261+
if let Some(update_ret) = self.update_rets.lock().unwrap().pop_front() {
262+
ret = update_ret;
269263
}
270264
if update.is_none() {
271265
self.chain_sync_monitor_persistences.lock().unwrap().entry(funding_txo).or_insert(HashSet::new()).insert(update_id);

0 commit comments

Comments
 (0)