-
Notifications
You must be signed in to change notification settings - Fork 14
Policy fixes #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Policy fixes #170
Changes from all commits
3fcf2f7
dfa267e
36b4ad9
9182897
67dfb83
1bdedd1
d64e3b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ use super::serialize; | |
/// given a witness. | ||
/// | ||
/// Furthermore, the policy can be normalized and is amenable to semantic analysis. | ||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] | ||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub enum Policy<Pk: SimplicityKey> { | ||
/// Unsatisfiable | ||
Unsatisfiable(FailEntropy), | ||
|
@@ -215,6 +215,19 @@ impl<Pk: SimplicityKey> Policy<Pk> { | |
_ => {} | ||
} | ||
} | ||
|
||
/// Return an iterator over the fragments of the policy. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a036b0b: You mention maybe using the dag module in the commit message -- actually we should use the But for now it's fine to have a manual implementation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Miniscript's |
||
pub fn iter(&self) -> PolicyIter<'_, Pk> { | ||
PolicyIter::new(self) | ||
} | ||
|
||
/// Return an iterator over the public keys of the policy. | ||
pub fn iter_pk(&self) -> impl Iterator<Item = Pk> + '_ { | ||
self.iter().filter_map(|fragment| match fragment { | ||
Policy::Key(key) => Some(key.clone()), | ||
_ => None, | ||
}) | ||
} | ||
} | ||
|
||
impl<Pk: SimplicityKey> fmt::Debug for Policy<Pk> { | ||
|
@@ -244,3 +257,39 @@ impl<Pk: SimplicityKey> fmt::Display for Policy<Pk> { | |
fmt::Debug::fmt(self, f) | ||
} | ||
} | ||
|
||
/// Iterator over the fragments of a Simplicity policy. | ||
/// | ||
/// The fragments are visited in preorder: | ||
/// We first visit the parent, then the left subtree, then the right subtree. | ||
pub struct PolicyIter<'a, Pk: SimplicityKey> { | ||
stack: Vec<&'a Policy<Pk>>, | ||
} | ||
|
||
impl<'a, Pk: SimplicityKey> PolicyIter<'a, Pk> { | ||
/// Create an iterator for the given policy. | ||
pub fn new(policy: &'a Policy<Pk>) -> Self { | ||
Self { | ||
stack: vec![policy], | ||
} | ||
} | ||
} | ||
|
||
impl<'a, Pk: SimplicityKey> Iterator for PolicyIter<'a, Pk> { | ||
type Item = &'a Policy<Pk>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let top = self.stack.pop()?; | ||
match top { | ||
Policy::And { left, right } | Policy::Or { left, right } => { | ||
self.stack.push(right); | ||
self.stack.push(left); | ||
} | ||
Policy::Threshold(_, children) => { | ||
self.stack.extend(children.iter().rev()); | ||
} | ||
_ => {} | ||
} | ||
Some(top) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In eb055fa:
I am tempted to change this from
10.0
to a range from 8.0.1 to 10. Later we will just bump the10
. Using a range will minimize the amount of grief that we'll have with coordinating new crate versions. (Though arguably we have no need to support miniscript 8 and 9 since those are in the past.)