Skip to content

TapTree iterator #901

Merged
apoelstra merged 1 commit intorust-bitcoin:masterfrom
BP-WG:taproot/treeiter
Mar 28, 2022
Merged

TapTree iterator #901
apoelstra merged 1 commit intorust-bitcoin:masterfrom
BP-WG:taproot/treeiter

Conversation

@dr-orlovsky
Copy link
Copy Markdown
Collaborator

Implemented after @sanket1729 suggestion in #895 (comment)

Iterates all scripts present in TapTree in DFS order returning (depth, script) pairs.

I propose to have it as an RC fix since this functionality is really lacking and may be required for many wallets working with Taproot PSBT even outside of the scope where I originally needed it (OP_RETURN tweaks for TapTree described in #895)

@dr-orlovsky dr-orlovsky added this to the 0.28.0 milestone Mar 23, 2022
Copy link
Copy Markdown
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK, but CI is failing on 1.29. This is fairly small and hopefully non-controversial. We can sneak it in as @dr-orlovsky really needs it.

It might sense moving all the TapTree struct and methods inside the taproot module, this is not only related to psbt now. I don't want to scope this further, we can do it next breaking release.

@tcharding
Copy link
Copy Markdown
Member

CI is failing on 1.29

We just need to add a use core statement to src/util/psbt/map/output.rs.

@dr-orlovsky
Copy link
Copy Markdown
Collaborator Author

dr-orlovsky commented Mar 23, 2022

@sanket1729 I'll do a separate PR once this got merged to move TapTree to taproot mod.

@ALL: CI is fixed

Copy link
Copy Markdown
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK e27f8ff.

Follow up notes (post release):

  1. Move TapTree to util/taproot
  2. Use the .node_info() method in the psbt::Serialize impl and in the iterator to remove unreachable! from the code here.

Would be great if you do that in this PR itself, but I would like to prioritize getting this in first.

Copy link
Copy Markdown
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have depth of knowledge in this area, please take my review as shallow only.

/// Returns iterator for a taproot script tree, operating in DFS order over leaf depth and
/// leaf script pairs.
pub fn iter(&self) -> TapTreeIter {
self.into_iter()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we happy that this complies with Rust convention that iter should return an iterator that iterates over &T? I spent some time reading the docs for IntoIterator and I can't work out if the tuple (u8, &'tree Script) is in line with convention or not. Should it by (&u8, &'tree Script)?

Copy link
Copy Markdown
Collaborator Author

@dr-orlovsky dr-orlovsky Mar 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can't be &u8, since u8 is not owned by the TapTree structure and is just retured from len() method which instantiate the value. You technically can't return a reference to it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, thanks.

@JeremyRubin
Copy link
Copy Markdown
Contributor

Would it also make sense to add an API for getting this info out of a TaprootSpendInfo?

@dr-orlovsky
Copy link
Copy Markdown
Collaborator Author

AFAIR TaprootSpendInfo does not have all these information. Anyway this should be a scope of a separate PR/issue...

@sanket1729
Copy link
Copy Markdown
Member

sanket1729 commented Mar 27, 2022

@JeremyRubin Would it also make sense to add an API for getting this info out of a TaprootSpendInfo?

If you can find a compelling use-case, sure. Technically speaking, this information is TaprootSpendInfo, and it can be useful to see the internal tree structure in case the SpendInfo is constructed with_huffman_tree API.

The implementation here might be complicated as it involves inferring a Tree from a BTreeMap of leaf -> merkle_branch. In any case, I think we should leave it out of scope for 0.28.0 .

@dr-orlovsky dr-orlovsky added P-high High priority one ack PRs that have one ACK, so one more can progress them labels Mar 27, 2022
Copy link
Copy Markdown
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK e27f8ff

Agree, let's do the move in another (probably post-release) PR.

@apoelstra apoelstra merged commit 388897b into rust-bitcoin:master Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

one ack PRs that have one ACK, so one more can progress them P-high High priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants