Skip to content
This repository was archived by the owner on Oct 22, 2026. It is now read-only.

Upgrade bitcoin to upcoming 0.30.0 release - #527

Closed
tcharding wants to merge 1 commit into
rust-bitcoin:masterfrom
tcharding:02-21-upgrade-bitcoin
Closed

tcharding wants to merge 1 commit into
rust-bitcoin:masterfrom
tcharding:02-21-upgrade-bitcoin

Conversation

@tcharding

Copy link
Copy Markdown
Member

This is not a merge candidate

To get a feel for using the code we are about to release I hacked up an upgrade to something resembling the upcoming rust-bitcoin 0.30.0 release.

FTR I found this upgrade painful, some pain points were:

  • API methods missing or hard to use (or I'm being braindead), specifically:

    • PushBytes::to_vec()
    • script::Builder::push_script() (to handle PushBytes)
  • New paths/re-exports don't match with current usage i.e., here in miniscript we tend to include bitcoin:: on types, this upgrade removed a few types from the crate root so now we have long fully qualified paths instead e.g., bitcoin::crypto::ecdsa::Signature instead of bitcoin::EcdsaSig.

  • The "don't implement Ord thing" from absolute::LockTime means we need some way to order absolute lock times, here we use a wrapper type.

Upgrade to something resembling the upcoming `rust-bitcoin` 0.30.0
release.

FTR this upgrade was pretty painful, some pain points were:

- I found PushBytes hard to work with.
- API methods missing or hard to use (or I'm being braindead),
specifically:
  - `PushBytes::to_vec()`
  - `script::Builder::push_script()` (to handle `PushBytes`)

- New paths/re-exports don't match with current usage i.e., here in
miniscript we tend to include `bitcoin::` on types, this upgrade removed
a few types from the crate root so now we have long fully qualified
paths instead e.g., `bitcoin::crypto::ecdsa::Signature` instead of
`bitcoin::EcdsaSig`.

- The "don't implement `Ord` thing" from `absolute::LockTime` means we
need some way to order absolute lock times, here we use a wrapper type.
@tcharding

tcharding commented Feb 22, 2023 •

Copy link
Copy Markdown
Member Author

FYI the rust-bitcoincore-rpc upgrade was trivial :)

@apoelstra

Copy link
Copy Markdown
Member
  • Regarding PushBytes etc, definitely converting a EcdsaSig to Vec then a PushBytes is the wrong thing to do! In some cases maybe we should be more agressive in changing existing slice/vector types to PushBytes at the API boundary rather than converting. In this case I notice we have Witness::push_bitcoin_signature method but not a script::Builder::push_bitcoin_signature method, which is an oversight upstream.
  • For new paths/exports we'll handle that case-by-case. I think we should import bitcoin::crypto::ecdsa and then use ecdsa::Signature. IMHO it's not as nice as using bitcoin:: but having any path at all indicates "this is an import, look up top to find where it comes from".
  • For Ord, we should really be distinguishing between Height and Time by having distinct Terminal fragments. Then we'd get Ord for free.

@tcharding

Copy link
Copy Markdown
Member Author

For Ord, we should really be distinguishing between Height and Time by having distinct Terminal fragments. Then we'd get Ord for free.

Aren't terminal fragments based on "Miniscript fragments" from https://bitcoin.sipa.be/miniscript/?

@tcharding

tcharding commented Feb 23, 2023 •

Copy link
Copy Markdown
Member Author

Regarding PushBytes etc, definitely converting a EcdsaSig to Vec then a PushBytes is the wrong thing to do! In some cases maybe we should be more agressive in changing existing slice/vector types to PushBytes at the API boundary rather than converting. In this case I notice we have Witness::push_bitcoin_signature method but not a script::Builder::push_bitcoin_signature method, which is an oversight upstream.

Ok I finally worked out how to use PushBytes, is there a bug/foot gun hiding in this code?

impl AsRef<PushBytes> for SerializedSignature {
    #[inline]
    fn as_ref(&self) -> &PushBytes {
        &<&PushBytes>::from(&self.data)[..self.len()]
    }
}

This implies that one can do builder.push_slice(sig) but if we look in the witness module we have

    /// Pushes a DER-encoded ECDSA signature with a signature hash type as a new element on the
    /// witness, requires an allocation.
    pub fn push_bitcoin_signature(&mut self, signature: &ecdsa::SerializedSignature, hash_type: EcdsaSighashType) {
        // Note that a maximal length ECDSA signature is 72 bytes, plus the sighash type makes 73
        let mut sig = [0; 73];
        sig[..signature.len()].copy_from_slice(signature);
        sig[signature.len()] = hash_type as u8;
        self.push(&sig[..signature.len() + 1]);
    }

Implying that we need to manually include the sighash type.

cc @Kixunil

@tcharding

tcharding commented Feb 23, 2023 •

Copy link
Copy Markdown
Member Author

And am I still being brain dead or is this how we are expecting downstream to use PushBytes

                let redeem_script = wpkh.script_pubkey();
                script::Builder::new()
                    .push_slice(&<&PushBytes>::try_from(redeem_script.as_bytes()).expect("script is within limit"))
                    .into_script()

@Kixunil

Kixunil commented Feb 23, 2023

Copy link
Copy Markdown

Good idea, I should try to upgrade my projects to see if anything breaks.

  • PushBytes::to_vec - do you actually need Vec<u8> or PushBytesBuf? For the latter it should be to_owned
  • Pushing into script should be done like this: script.push_slice(signature.serialize())
  • The code in Witness wasn't updated, it should be self.push(&signature.serialize()).

I was also thinking we should disallow pushing legacy signatures into Witness and non-legacy signatures into ScriptBuf<Sig> but I don't expect it to happen in this release.

@apoelstra

Copy link
Copy Markdown
Member

For Ord, we should really be distinguishing between Height and Time by having distinct Terminal fragments. Then we'd get Ord for free.

Aren't terminal fragments based on "Miniscript fragments" from https://bitcoin.sipa.be/miniscript/?

Yes, and height-based and time-based olders are unambiguously distinct (although the table doesn't list them separately, and we use the same string-based name older for both...though we spent years telling ourselves that we would change that eventually).

@tcharding

Copy link
Copy Markdown
Member Author

Am I understanding you correctly, are you saying rust-miniscript is miniscript (i.e., its the reference implementation) so we can change the terminal names and such things?

@apoelstra

Copy link
Copy Markdown
Member

@tcharding no, I'm saying that older(time) and older(height) are already serialized as distinct nonoverlapping fragments in bitcoin script, so there is no problem separating them in our Terminal enum.

@tcharding

tcharding commented Feb 23, 2023 •

Copy link
Copy Markdown
Member Author

Good idea, I should try to upgrade my projects to see if anything breaks.

Yeah I want to do bdk and/or electrs as well to see if we break them.

  • PushBytes::to_vec - do you actually need Vec<u8> or PushBytesBuf? For the latter it should be to_owned
  • Pushing into script should be done like this: script.push_slice(signature.serialize())

Wow, I did not realise yesterday that SerializedSignature was a new recently added type, added in #1190, I don't know what happened during my review of #1190 - epic fail.

  • The code in Witness wasn't updated, it should be self.push(&signature.serialize()).

I'll fix it up.

EDIT: I retract that statement, its an API change and a minor can of worms because of the "restrict to non-legacy sigs" thing. Leaving till after release.

I was also thinking we should disallow pushing legacy signatures into Witness and non-legacy signatures into ScriptBuf<Sig> but I don't expect it to happen in this release.

Cool, something to keep in mind when we add script tagging eh, feels like script tagging is going to be quite involved.

@tcharding

Copy link
Copy Markdown
Member Author

@tcharding no, I'm saying that older(time) and older(height) are already serialized as distinct nonoverlapping fragments in bitcoin script, so there is no problem separating them in our Terminal enum.

Ooooh, thanks. I'll have a play when I get a second.

@sanket1729

Copy link
Copy Markdown
Member

Using your changes from this branch to a make a release PR here

@tcharding

Copy link
Copy Markdown
Member Author

Unneeded now, closing.

@tcharding tcharding closed this Mar 28, 2023
@tcharding
tcharding deleted the 02-21-upgrade-bitcoin branch August 7, 2023 07:25
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants