feat(core): add zero-copy Deref bridge to bitcoin::Script#186
Open
alexanderwiederin wants to merge 6 commits into
Open
feat(core): add zero-copy Deref bridge to bitcoin::Script#186alexanderwiederin wants to merge 6 commits into
Deref bridge to bitcoin::Script#186alexanderwiederin wants to merge 6 commits into
Conversation
21b7ae6 to
b4ad130
Compare
Implements `Deref<Target = bitcoin::Script>` for `ScriptPubkey` and `ScriptPubkeyRef` behind the `bitcoin` feature flag. `btck_script_pubkey_to_bytes` calls its writer callback synchronously with a direct pointer into the `CScript` buffer, allowing a zero-copy `&[u8]` slice. `Script::from_bytes` is then a zero-cost cast, giving kernel script types the full `bitcoin::Script` API transparently.
Separates the CI MSRV run with and without the bitcoin feature to improve localization of issues.
b4ad130 to
6ced318
Compare
Deref bridge to bitcoin::Script
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
closes #20
Summary
Implements
Deref<Target = bitcoin::Script>forScriptPubkeyandScriptPubkeyRef, behind a newbitcoinfeature flag. TheDerefimplementation uses a new function,script_as_bytes, which captures the raw pointer passed to thebtck_script_pubkey_to_bytescallback and casts it to a&[u8]slice, avoiding any allocation.Motivation
Silent payments block scanning requires script introspection such as
is_p2tr(reference). Delegating this functionality torust-bitcoinkeeps the kernel C API focused on consensus, avoiding scope creep. Further, integratingScriptPubkeywith rust-bitcoin'sScripttype, provides a suite of new functionality with Bitcoin protocol awareness.Usage
What you should know
Given that
Derefis intended to be used for wrappers and their underlying type, the use here may be considered misplaced -bitcoin::Scriptis not an underlying type, but a peer type from a separate crate that shares the same underlying representation. The alternative would be to add aScriptPubkey::as_bitcoin_script()method to make the relationship explicit, at the cost of call-site ergonomics. I recognise this is a design decision that requires careful consideration. I'm happy to go either way.See the Rust documentation on when to implement
Deref.