Refactor Taproot to use `LeafScript` instead of tuple in APIs

Open
#7,070 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Refactor
Clarity
Needs clarification
Activity status
Active
Tech stack
rust
Domain
backend

Research direction

Start by reading the taproot module APIs that use (TapScriptBuf, LeafVersion) and the TaprootSpendInfo map type. The issue asks maintainers to choose among several API and storage designs, including unsafe layout changes; no implementation path or tests are specified, so done means reaching a design decision before making the refactor.

Written by the indexing model from the issue text.

Description

The taproot module contains a bunch of things that use (TapScriptBuf, LeafVersion) (to-be-renamed to TaprootLeaf) instead of LeafScript. The dedicated type would be nicer as it could provide convenience methods, easier remembering of which field is which, shorter name...

I plan to do this but I have also figured out a way to bypass the annoying limitation of BTreeMap (and HashMap) around Borrow, so that we could access the map using LeafScript<&TaprootLeaf> instead of forcing owned keys! (Technically, it needs to go through conversion that is explicit but internally no-op) However this requires (reasonable) unsafe internally and specific layout of LeafScript. We have these options:

  • Force using LeafScript<Box<TaprootLeaf>> instead of LeafScript<TaprootLeafBuf> as the key stored in the map - this drops capacity which could be a good thing, if the keys are generally produced as fixed (likely) and have len == capacity (I don't think likely) but could also hurt performance if the capacity needs to shrink (and even worse if it needs to regrow but I don't see people taking out the scripts and then appending something to them as realistic)
  • Add and publicly expose an additional LeafScriptBuf wrapper type that hacks this (it needs to store LeafScript<NonNull<TaprootLeaf>> and capacity separately). This increases our API surface, and may store capacity uselessly.
  • We can also support both (if we do the second, the first is trivial to add) but we need to choose one for TaprootSpendInfo unless we want to complicate the API by adding a generic parameter to it, which seems annoying although it could also allow using HashMap instead of BTreeMap which appears interesting.
  • We can instead wrap the map, so that ScriptMerkleProofMap becomes a proper newtype and delegate all the API. This could even select HashMap internally in std builds, keeps the door open for changes but decreases interoperability with std maps. We could have conversions but those might or might not be costly and we wouldn't be able to guarantee performance here. And we would need to write a lot of boilerplate to delegate everything. (Though LLMs should be able to do this.)

So which solution do we want?

I personally think first seems the best but the last is also a bit tempting.

Dominant language
Rust
Stars
2.7k
Forks
1.1k
Avg merge
3d 8h
Merged PRs (30d)
41

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rust-bitcoin/rust-bitcoin

All issues in rust-bitcoin/rust-bitcoin

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.