Skip to content

Commit 42e1216

Browse files
committed
Unit test mixed up absolute timelocks
Currently if we mix up height/time absolute timelocks when filtering policies the result is incorrect. Add a bunch of assertions that verify the bug.
1 parent e4eb285 commit 42e1216

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/policy/semantic.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ mod tests {
764764
assert_eq!(policy.n_keys(), 0);
765765
assert_eq!(policy.minimum_n_keys(), Some(0));
766766

767+
// Block height 1000.
767768
let policy = StringPolicy::from_str("after(1000)").unwrap();
768769
assert_eq!(policy, Policy::After(1000));
769770
assert_eq!(policy.absolute_timelocks(), vec![1000]);
@@ -772,6 +773,26 @@ mod tests {
772773
assert_eq!(policy.clone().at_height(999), Policy::Unsatisfiable);
773774
assert_eq!(policy.clone().at_height(1000), policy.clone());
774775
assert_eq!(policy.clone().at_height(10000), policy.clone());
776+
// Pass a UNIX timestamp to at_height while policy uses a block height.
777+
assert_eq!(policy.clone().at_height(500_000_001), Policy::Unsatisfiable);
778+
assert_eq!(policy.n_keys(), 0);
779+
assert_eq!(policy.minimum_n_keys(), Some(0));
780+
781+
// UNIX timestamp of 10 seconds after the epoch.
782+
let policy = StringPolicy::from_str("after(500000010)").unwrap();
783+
assert_eq!(policy, Policy::After(500_000_010));
784+
assert_eq!(policy.absolute_timelocks(), vec![500_000_010]);
785+
assert_eq!(policy.relative_timelocks(), vec![]);
786+
// Pass a block height to at_height while policy uses a UNIX timestapm.
787+
assert_eq!(policy.clone().at_height(0), Policy::Unsatisfiable);
788+
assert_eq!(policy.clone().at_height(999), Policy::Unsatisfiable);
789+
assert_eq!(policy.clone().at_height(1000), Policy::Unsatisfiable);
790+
assert_eq!(policy.clone().at_height(10000), Policy::Unsatisfiable);
791+
// And now pass a UNIX timestamp to at_height while policy also uses a timestamp.
792+
assert_eq!(policy.clone().at_height(500_000_000), Policy::Unsatisfiable);
793+
assert_eq!(policy.clone().at_height(500_000_001), Policy::Unsatisfiable);
794+
assert_eq!(policy.clone().at_height(500_000_010), policy.clone());
795+
assert_eq!(policy.clone().at_height(500_000_012), policy.clone());
775796
assert_eq!(policy.n_keys(), 0);
776797
assert_eq!(policy.minimum_n_keys(), Some(0));
777798
}

0 commit comments

Comments
 (0)