Skip to content
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

upgrade to bitcoin 0.32 #209

Merged
merged 2 commits into from
Aug 26, 2024
Merged

upgrade to bitcoin 0.32 #209

merged 2 commits into from
Aug 26, 2024

Conversation

RCasatta
Copy link
Collaborator

@RCasatta RCasatta commented Aug 14, 2024

#208

    fn consensus_encode<W: bitcoin::io::Write + ?Sized>(&self, e: W) -> Result<usize, crate::encode::Error> {}
    fn consensus_decode<R: bitcoin::io::BufRead + ?Sized>(reader: &mut R) -> Result<Self, crate::encode::Error> {}

We went for not depending on bitcoin::Encodable instead

@RCasatta RCasatta changed the title upgrade to bitcoin 0.32 WIP upgrade to bitcoin 0.32 Aug 14, 2024
@RCasatta RCasatta mentioned this pull request Aug 14, 2024
@apoelstra
Copy link
Member

The policy in this crate is "do whatever works" to get Encodable/Decodable working. Properly speaking, we should have our own io dependency and copy all the clever nostd stuff that rust-bitcoin does. In practice this crate requires std and has direct references to std/alloc stuff everywhere. So we wind up with "performative" nostd stuff like gating our functions on bitcoin::io::Write instead of std::io::Write even though the crate won't actually work without std.

So yes, your proposal sounds good to me.

If it turns out to be unusable downstream for some reason then we can iterate a bit.

@apoelstra
Copy link
Member

For readers who aren't familiar with the gory details of this codebase,

  • We have our own elements::Encodable/Decodable traits which are copies of the ones in bitcoin because bitcoin took away some impl that we needed (maybe on &[u8]?)
  • However, because we directly deal with bitcoin types in this crate (e.g. in pegin witnesses where we have bitcoin::BlockHashes and merkle proofs and what not), we need elements::Encodable to be implemented on bitcoin types, in terms of bitcoin::Encodable.
  • In bitcoin 0.32 we changed bitcoin::Encodable to gate on our bespoke io::Write trait instead of the std::io::Write trait.
  • We did not put a blanket bitcoin::io::Write impl on T: std::io::Write even though this is what users expect and need, because the Rust language is stupid and limiting and prevents us from doing so
  • Therefore, in order for us to implement elements::Encodable as a passthrough to bitcoin::Encodable, we need to copy the bespoke bounds on bitcoin::Encodable onto elements::Encodable.

That last bullet point isn't quite true: alternately, we could drop the impl_upstream! macro and stop implementing elements::Encodable directly in terms of bitcoin::Encodable. In fact most of these impls would be one-liners (if we had a utility function to serialize a &[u8] as a length prefix I think they all would be, except for btcenc::VarInt which would be 4 or 5 lines) (though VarInt we should really drop in favor of utility functions on some sort of WriteExt/ReadExt traits, as we are doing upstream rust-bitcoin/rust-bitcoin#2931) .

Actually maybe @RCasatta you want to try that? Replacing impl_upstream and getting rid of the btcenc import in src/encode.rs entirely? When I started typing I thought it would be a giant pain in the ass, but actually I think it's fairly straightforward, and we could even do it in a PR separately to the upgrade.

@RCasatta
Copy link
Collaborator Author

Actually maybe @RCasatta you want to try that?

I am going to try

apoelstra added a commit that referenced this pull request Aug 14, 2024
4269728 apply tomlfmt to Cargo.toml (Riccardo Casatta)
e9a90cc apply rustfmt to encode.rs (Riccardo Casatta)
6789492 apply rustfmt to pset/raw.rs (Riccardo Casatta)
8ae550b apply rustfmt to src/blind.rs (Riccardo Casatta)
084f7d4 Avoid instantiating base58 errors in Address::from_base58 (Riccardo Casatta)
5eefe85 apply rustfmt to address.rs (Riccardo Casatta)

Pull request description:

  This is propedeutic to upgrade to bitcoin 0.32 #209
  where the base58 error variant specific to address are removed

  Also it applies formatting around on which I think we agreed upon, but also on the toml file which I had doubt, let me know if I have to remove it

ACKs for top commit:
  apoelstra:
    ACK 4269728 successfully ran local tests

Tree-SHA512: 0cdc7077d4ec4550a4df6f5e6eb244c7a8c0793b507c9a4327b30bdaf9f597df0dee7c72e33414df0a87cb10f9a9be12588c9a564d71cb308564cf0226b7dec1
@RCasatta
Copy link
Collaborator Author

I did some initial work here 8ce9c35

But we still need something similar to impl_upstream because of the bitcoin types in pset right?

@apoelstra
Copy link
Member

PSET has its own Deserialize/Serialize trait that mostly passes through to Encodable/Decodable. So there should be minimal changes required to PSET once you have Encodable/Decodable working.

src/blech32/mod.rs Outdated Show resolved Hide resolved
@RCasatta RCasatta force-pushed the bitcoin_032 branch 3 times, most recently from 2d52f75 to 0ff64eb Compare August 19, 2024 13:57
apoelstra added a commit that referenced this pull request Aug 20, 2024
…able

c08382c Copy ReadExt/WriteExt from bitcoin (Riccardo Casatta)
4aad862 Stop implementing elements::Encodable with bitcoin::Encodable (Riccardo Casatta)

Pull request description:

  Following bitcoin versions change `io` with `bitcoin_io`, upgrading would then require changing also elements::Encodable to match.

  Instead, we are re-implementing what is needed.

  Makes #209 easier

ACKs for top commit:
  apoelstra:
    ACK c08382c successfully ran local tests; a bit redundant but I think this is the right way to go

Tree-SHA512: e849dc141ab412f1db3539a132688b28e2770befb8fc46e64d3396f16c3b9035bb7d76a0813d4d7bf65ff85df418cb42e8d75e01d1db7879c5853dd40214e053
@RCasatta RCasatta marked this pull request as ready for review August 22, 2024 08:43
@RCasatta RCasatta changed the title WIP upgrade to bitcoin 0.32 upgrade to bitcoin 0.32 Aug 22, 2024
@RCasatta RCasatta marked this pull request as draft August 22, 2024 08:55
@RCasatta
Copy link
Collaborator Author

still needing something along the lines of ElementsProject/elements-miniscript#88

@RCasatta RCasatta marked this pull request as ready for review August 22, 2024 10:56
@RCasatta
Copy link
Collaborator Author

Integration testing is now passing but testing locally with elements Elements Core version v23.2.1 I hit an error:

[nix-shell:~/git/rust-elements/elementsd-tests]$ elementsd --version
Elements Core version v23.2.1
[nix-shell:~/git/rust-elements/elementsd-tests]$ cargo test -- tx_issuance
    Finished test [unoptimized + debuginfo] target(s) in 0.16s
     Running unittests src/lib.rs (/home/casatta/git/rust-elements/target/debug/deps/elementsd_tests-534535ebc6e90ec9)

running 1 test
test pset::tx_issuance ... FAILED

failures:

---- pset::tx_issuance stdout ----
thread 'pset::tx_issuance' panicked at elementsd-tests/src/lib.rs:37:23:
error JSON-RPC error: RPC error response: RpcError { code: -4, message: "Unable to create an asset surjection proof", data: None } while calling walletprocesspsbt with [String("cHNldP8BAgQCAAAAAQMEAAAAAAEEAQEBBQEEAfsEAgAAAAABDiAIe20YE7qmV3wszCrvpMzUqdI8fWWYRhP0/w9AOl/MkQEPBAAAAAABEAT/////B/wEcHNldAAIAOh2SBcAAAAH/ARwc2V0CggA4fUFAAAAAAABAwgA6HZIFwAAAAEEFgAUIG3HsMlrrmBZ2Z/fkew6jvzw8cQH/ARwc2V0AiCMtTvaweODNVnCoPh80OQHJ7w4kxJ5N65WAfoFAJK0eAf8BHBzZXQGIQNwqIVVbZjBP3WbfM2Q8rAf+UgZ18FXTFz63sjjROxbSwf8BHBzZXQIBAAAAAAAAQMIAOH1BQAAAAABBBYAFOQA+B8RK4Xe3KZuDBdXMUH8TdKBB/wEcHNldAIg+wI9yyBNBZ8YSEWrxZOS/GiW+fnJqyUNM5YkNyoWVXYH/ARwc2V0BiECsxS+LIBwaCdgZNH/FvEcvBsFUfRkMB7K4CviLIwcRUsH/ARwc2V0CAQAAAAAAAEDCIDeknwAAAAAAQQWABTCiuEQ1wJ5ohJXRzHqxwXhaEAAMwf8BHBzZXQCICWyUQcOKcoZBDzzPM1zJOLdqwPsxK4LXnfE/A5c9slaB/wEcHNldAYhAweZZvsyOP39XpkGG0xIH5QZsB2o3GC4rAhnpG4dF7ffB/wEcHNldAgEAAAAAAABAwiAlpgAAAAAAAEEAAf8BHBzZXQCICWyUQcOKcoZBDzzPM1zJOLdqwPsxK4LXnfE/A5c9slaAA==")]
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

@delta1
Copy link
Member

delta1 commented Aug 22, 2024

reproduced the asset surjection proof error with elementsd v22.1.1 and v23.2.2

it does not happen with elementsd v0.21.0.3

@LeoComandini
Copy link
Contributor

@RCasatta

Making the issuance unblinded fixes the issue

diff --git a/elementsd-tests/src/pset.rs b/elementsd-tests/src/pset.rs
index dd2d92b..6c475b0 100644
--- a/elementsd-tests/src/pset.rs
+++ b/elementsd-tests/src/pset.rs
@@ -55,12 +55,12 @@ fn tx_issuance() {
     let contract_hash = ContractHash::from_byte_array([0u8; 32]);
     let entropy = AssetId::generate_asset_entropy(prevout, contract_hash);
     let asset_id = AssetId::from_entropy(entropy.clone());
-    let reissuance_id = AssetId::reissuance_token_from_entropy(entropy, true);
+    let reissuance_id = AssetId::reissuance_token_from_entropy(entropy, false);
 
     let value = elementsd.call(
             "createpsbt",
             &[
-                json!([{ "txid": prevout.txid.to_string(), "vout": prevout.vout, "issuance_amount": 1000, "issuance_tokens": 1}]),
+                json!([{ "txid": prevout.txid.to_string(), "vout": prevout.vout, "issuance_amount": 1000, "issuance_tokens": 1, "blind_reissuance": false}]),
                 json!([
                     {address_asset: "1000", "asset": asset_id.to_string(), "blinder_index": 0},
                     {address_reissuance: "1", "asset": reissuance_id.to_string(), "blinder_index": 0},

In the end this is a elementsd issue: PSET is created by elementsd and the walletprocesspsbt fails.
IMHO this should be addressed in the elements repo, or if we want to do it here at least in a separate PR.

@LeoComandini
Copy link
Contributor

@RCasatta

The diff does not work with elements 21... anyway I think this is a elementsd issue, so we can't do much on the rust-elements side.
Raised #214 for tracking

@LeoComandini
Copy link
Contributor

utACK 61091a7, code review

@apoelstra
Copy link
Member

Can you squash the first two commits together? The test crate doesn't compile with the first one due to version mismatches.

avoid setting {BITCOIND,ELEMENTSD}_EXE in setup

The logic of setting the env var inside the setup prevents user of other
OS like nixos to run the tests (because the included binary are not
working on nixos).
Setting those env vars is meant to be done in the dev environment not in
the code.
With this users with `bitcoind` and `elementsd` in their PATH will work
too.
The CI script will set the variables only if they are not already set.
(Allowing Nixos users to override and run it locally)
@RCasatta
Copy link
Collaborator Author

First two commits squashed

Copy link
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 f889525 successfully ran local tests

@apoelstra
Copy link
Member

Edited PR description to remove crossed out text, since it didn't seem essential and because it @-referenced me, which the merge script did not like.

@apoelstra apoelstra merged commit 85604b0 into master Aug 26, 2024
3 of 5 checks passed
@apoelstra apoelstra deleted the bitcoin_032 branch August 26, 2024 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants