-
Notifications
You must be signed in to change notification settings - Fork 154
Add Descriptor::iter_pk
#823
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
Open
apoelstra
wants to merge
2
commits into
master
Choose a base branch
from
push-slnpvpouumpw
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// SPDX-License-Identifier: CC0-1.0 | ||
|
||
//! Iterators over descriptors | ||
|
||
use crate::descriptor::{TapTreeIter, Tr}; | ||
use crate::miniscript::context::{BareCtx, Legacy, Segwitv0, Tap}; | ||
use crate::{miniscript, Miniscript, MiniscriptKey}; | ||
|
||
/// Iterator over all the keys in a descriptor. | ||
pub struct PkIter<'desc, Pk: MiniscriptKey> { | ||
single_key: Option<Pk>, | ||
taptree_iter: Option<TapTreeIter<'desc, Pk>>, | ||
ms_iter_bare: Option<miniscript::iter::PkIter<'desc, Pk, BareCtx>>, | ||
ms_iter_legacy: Option<miniscript::iter::PkIter<'desc, Pk, Legacy>>, | ||
ms_iter_segwit: Option<miniscript::iter::PkIter<'desc, Pk, Segwitv0>>, | ||
ms_iter_taproot: Option<miniscript::iter::PkIter<'desc, Pk, Tap>>, | ||
sorted_multi: Option<core::slice::Iter<'desc, Pk>>, | ||
} | ||
|
||
impl<'desc, Pk: MiniscriptKey> PkIter<'desc, Pk> { | ||
pub(super) fn from_key(pk: Pk) -> Self { | ||
Self { | ||
single_key: Some(pk), | ||
taptree_iter: None, | ||
ms_iter_bare: None, | ||
ms_iter_legacy: None, | ||
ms_iter_segwit: None, | ||
ms_iter_taproot: None, | ||
sorted_multi: None, | ||
} | ||
} | ||
|
||
pub(super) fn from_miniscript_bare(ms: &'desc Miniscript<Pk, BareCtx>) -> Self { | ||
Self { | ||
single_key: None, | ||
taptree_iter: None, | ||
ms_iter_bare: Some(ms.iter_pk()), | ||
ms_iter_legacy: None, | ||
ms_iter_segwit: None, | ||
ms_iter_taproot: None, | ||
sorted_multi: None, | ||
} | ||
} | ||
|
||
pub(super) fn from_miniscript_legacy(ms: &'desc Miniscript<Pk, Legacy>) -> Self { | ||
Self { | ||
single_key: None, | ||
taptree_iter: None, | ||
ms_iter_bare: None, | ||
ms_iter_legacy: Some(ms.iter_pk()), | ||
ms_iter_segwit: None, | ||
ms_iter_taproot: None, | ||
sorted_multi: None, | ||
} | ||
} | ||
|
||
pub(super) fn from_miniscript_segwit(ms: &'desc Miniscript<Pk, Segwitv0>) -> Self { | ||
Self { | ||
single_key: None, | ||
taptree_iter: None, | ||
ms_iter_bare: None, | ||
ms_iter_legacy: None, | ||
ms_iter_segwit: Some(ms.iter_pk()), | ||
ms_iter_taproot: None, | ||
sorted_multi: None, | ||
} | ||
} | ||
|
||
pub(super) fn from_sortedmulti(sm: &'desc [Pk]) -> Self { | ||
Self { | ||
single_key: None, | ||
taptree_iter: None, | ||
ms_iter_bare: None, | ||
ms_iter_legacy: None, | ||
ms_iter_segwit: None, | ||
ms_iter_taproot: None, | ||
sorted_multi: Some(sm.iter()), | ||
} | ||
} | ||
|
||
pub(super) fn from_tr(tr: &'desc Tr<Pk>) -> Self { | ||
Self { | ||
single_key: Some(tr.internal_key().clone()), | ||
taptree_iter: Some(tr.leaves()), | ||
ms_iter_bare: None, | ||
ms_iter_legacy: None, | ||
ms_iter_segwit: None, | ||
ms_iter_taproot: None, | ||
sorted_multi: None, | ||
} | ||
} | ||
} | ||
|
||
impl<'desc, Pk: MiniscriptKey> Iterator for PkIter<'desc, Pk> { | ||
type Item = Pk; | ||
|
||
#[rustfmt::skip] // the tower of .or_elses looks good as is | ||
fn next(&mut self) -> Option<Self::Item> { | ||
// If there is a single key, return it first. (This will be the case | ||
// for all single-key-only iterators but also for Taproot, where the | ||
// single key is the root key.) | ||
if let Some(k) = self.single_key.take() { | ||
return Some(k.clone()); | ||
} | ||
|
||
// Then attempt to yield something from the Taptree iterator. | ||
loop { | ||
if let Some(item) = self.ms_iter_taproot.as_mut().and_then(Iterator::next) { | ||
return Some(item); | ||
} | ||
if let Some(iter) = self.taptree_iter.as_mut().and_then(Iterator::next) { | ||
self.ms_iter_taproot = Some(iter.miniscript().iter_pk()); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
// Finally run through the train of other iterators. | ||
self.ms_iter_bare.as_mut().and_then(Iterator::next).or_else( | ||
|| self.ms_iter_legacy.as_mut().and_then(Iterator::next).or_else( | ||
|| self.ms_iter_segwit.as_mut().and_then(Iterator::next).or_else( | ||
|| self.sorted_multi.as_mut().and_then(Iterator::next).cloned() | ||
) | ||
) | ||
) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is fine. But Ideally, we want this to be a sum type instead of a product type? But then we get whole loads of matches everywhere?
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.
The main reason to use a product type was that Taproot descriptors have both single keys and (optionally) a taptree iterator.
And in
PkIter::next
I have a construction where the taptree iterator yields new Miniscript pkiters.I can try to refactor this to use a sum type but I think it would result in more repeated/redundant code.