Skip to content

Commit

Permalink
Unit test mixed up absolute timelocks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tcharding committed May 25, 2022
1 parent d9ffbef commit 9c744be
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/policy/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ mod tests {
assert_eq!(policy.n_keys(), 0);
assert_eq!(policy.minimum_n_keys(), Some(0));

// Block height 1000.
let policy = StringPolicy::from_str("after(1000)").unwrap();
assert_eq!(policy, Policy::After(1000));
assert_eq!(policy.absolute_timelocks(), vec![1000]);
Expand All @@ -773,6 +774,26 @@ mod tests {
assert_eq!(policy.clone().at_height(999), Policy::Unsatisfiable);
assert_eq!(policy.clone().at_height(1000), policy.clone());
assert_eq!(policy.clone().at_height(10000), policy.clone());
// Pass a UNIX timestamp to at_height while policy uses a block height.
assert_eq!(policy.clone().at_height(500_000_001), Policy::Unsatisfiable);
assert_eq!(policy.n_keys(), 0);
assert_eq!(policy.minimum_n_keys(), Some(0));

// UNIX timestamp of 10 seconds after the epoch.
let policy = StringPolicy::from_str("after(500000010)").unwrap();
assert_eq!(policy, Policy::After(500_000_010));
assert_eq!(policy.absolute_timelocks(), vec![500_000_010]);
assert_eq!(policy.relative_timelocks(), vec![]);
// Pass a block height to at_height while policy uses a UNIX timestapm.
assert_eq!(policy.clone().at_height(0), Policy::Unsatisfiable);
assert_eq!(policy.clone().at_height(999), Policy::Unsatisfiable);
assert_eq!(policy.clone().at_height(1000), Policy::Unsatisfiable);
assert_eq!(policy.clone().at_height(10000), Policy::Unsatisfiable);
// And now pass a UNIX timestamp to at_height while policy also uses a timestamp.
assert_eq!(policy.clone().at_height(500_000_000), Policy::Unsatisfiable);
assert_eq!(policy.clone().at_height(500_000_001), Policy::Unsatisfiable);
assert_eq!(policy.clone().at_height(500_000_010), policy.clone());
assert_eq!(policy.clone().at_height(500_000_012), policy.clone());
assert_eq!(policy.n_keys(), 0);
assert_eq!(policy.minimum_n_keys(), Some(0));
}
Expand Down

0 comments on commit 9c744be

Please sign in to comment.