Skip to content

Commit 54e22f9

Browse files
committed
Correct maximum_pending_updates of 0 in MonitorUpdatingPersister
Though users maybe shouldn't use `MonitorUpdatingPersister` if they don't actually want to persist `ChannelMonitorUpdate`s, we also shouldn't panic if `maximum_pending_updates` is set to zero.
1 parent ecce859 commit 54e22f9

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

lightning/src/util/persist.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ where
796796
const LEGACY_CLOSED_CHANNEL_UPDATE_ID: u64 = u64::MAX;
797797
if let Some(update) = update {
798798
let persist_update = update.update_id != LEGACY_CLOSED_CHANNEL_UPDATE_ID
799+
&& self.maximum_pending_updates != 0
799800
&& update.update_id % self.maximum_pending_updates != 0;
800801
if persist_update {
801802
let monitor_key = monitor_name.to_string();
@@ -1188,12 +1189,9 @@ mod tests {
11881189
}
11891190

11901191
// Exercise the `MonitorUpdatingPersister` with real channels and payments.
1191-
#[test]
1192-
fn persister_with_real_monitors() {
1193-
// This value is used later to limit how many iterations we perform.
1194-
let persister_0_max_pending_updates = 7;
1195-
// Intentionally set this to a smaller value to test a different alignment.
1196-
let persister_1_max_pending_updates = 3;
1192+
fn do_persister_with_real_monitors(persisters_max_pending_updates: (u64, u64)) {
1193+
let persister_0_max_pending_updates = persisters_max_pending_updates.0;
1194+
let persister_1_max_pending_updates = persisters_max_pending_updates.1;
11971195
let chanmon_cfgs = create_chanmon_cfgs(4);
11981196
let persister_0 = MonitorUpdatingPersister {
11991197
kv_store: &TestStore::new(false),
@@ -1256,6 +1254,11 @@ mod tests {
12561254
assert_eq!(mon.get_latest_update_id(), $expected_update_id);
12571255

12581256
let monitor_name = mon.persistence_key();
1257+
let expected_updates = if persister_0_max_pending_updates == 0 {
1258+
0
1259+
} else {
1260+
mon.get_latest_update_id() % persister_0_max_pending_updates
1261+
};
12591262
assert_eq!(
12601263
persister_0
12611264
.kv_store
@@ -1265,7 +1268,7 @@ mod tests {
12651268
)
12661269
.unwrap()
12671270
.len() as u64,
1268-
mon.get_latest_update_id() % persister_0_max_pending_updates,
1271+
expected_updates,
12691272
"Wrong number of updates stored in persister 0",
12701273
);
12711274
}
@@ -1275,6 +1278,11 @@ mod tests {
12751278
for (_, mon) in persisted_chan_data_1.iter() {
12761279
assert_eq!(mon.get_latest_update_id(), $expected_update_id);
12771280
let monitor_name = mon.persistence_key();
1281+
let expected_updates = if persister_1_max_pending_updates == 0 {
1282+
0
1283+
} else {
1284+
mon.get_latest_update_id() % persister_1_max_pending_updates
1285+
};
12781286
assert_eq!(
12791287
persister_1
12801288
.kv_store
@@ -1284,7 +1292,7 @@ mod tests {
12841292
)
12851293
.unwrap()
12861294
.len() as u64,
1287-
mon.get_latest_update_id() % persister_1_max_pending_updates,
1295+
expected_updates,
12881296
"Wrong number of updates stored in persister 1",
12891297
);
12901298
}
@@ -1348,10 +1356,18 @@ mod tests {
13481356

13491357
// Make sure everything is persisted as expected after close.
13501358
check_persisted_data!(
1351-
persister_0_max_pending_updates * 2 * EXPECTED_UPDATES_PER_PAYMENT + 1
1359+
cmp::max(2, persister_0_max_pending_updates * 2) * EXPECTED_UPDATES_PER_PAYMENT + 1
13521360
);
13531361
}
13541362

1363+
#[test]
1364+
fn persister_with_real_monitors() {
1365+
// Test various alignments
1366+
do_persister_with_real_monitors((7, 3));
1367+
do_persister_with_real_monitors((0, 1));
1368+
do_persister_with_real_monitors((4, 2));
1369+
}
1370+
13551371
// Test that if the `MonitorUpdatingPersister`'s can't actually write, trying to persist a
13561372
// monitor or update with it results in the persister returning an UnrecoverableError status.
13571373
#[test]

0 commit comments

Comments
 (0)