Skip to content

Commit 9c744be

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 d9ffbef commit 9c744be

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
@@ -765,6 +765,7 @@ mod tests {
765765
assert_eq!(policy.n_keys(), 0);
766766
assert_eq!(policy.minimum_n_keys(), Some(0));
767767

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

0 commit comments

Comments
 (0)