Skip to content

Fix the PSBT JSON serialization, using serde-derive#508

Merged
sgeisler merged 9 commits intorust-bitcoin:masterfrom
stevenroose:fix-json-psbt
Dec 30, 2020
Merged

Fix the PSBT JSON serialization, using serde-derive#508
sgeisler merged 9 commits intorust-bitcoin:masterfrom
stevenroose:fix-json-psbt

Conversation

@stevenroose
Copy link
Copy Markdown
Collaborator

This re-introduces serde-derive, but adds version pinning instructions to the README.

I hope that's fine. I failed to add hex support for the values of maps (like partial_sigs: BTreeMap<PublicKey, Vec<u8>>). For that I fear we'd need some helper crate like serde_with. Even though, I'm sure in theory it's possible to do manually, but I couldn't figure it out.

dr-orlovsky
dr-orlovsky previously approved these changes Dec 21, 2020
Copy link
Copy Markdown
Collaborator

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

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

This looks good since gets rid of a lot of serde impl macros! utACK

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.

trailing whitespace here

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.

This line also doesn't compile for me

cargo +stable  test   '--features=rand use-serde base64'       # 2020-12-21T16:02:46 / cargo 1.48.0 (65cbdd2dc 2020-10-14)
Command failed: cargo +stable test --features=rand use-serde base64
   Compiling serde_json v1.0.44
   Compiling serde_test v1.0.118
   Compiling bitcoin v0.25.2 (/tmp/tmpwjnlqhmp)
error[E0433]: failed to resolve: use of undeclared crate or module `bip32`
   --> src/util/psbt/mod.rs:356:31
    |
356 |                     let xpub: bip32::ExtendedPubKey = 
    |                               ^^^^^ use of undeclared crate or module `bip32`

error: aborting due to previous error

Instead, implement Debug in impl_bytes_newtype.
README.md Outdated
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.

I don't think there's any reason to downgrade serde. I am testing on 1.29 fine without it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

If I drop that line, I get this:

error: no matching version `= 1.0.118` found for package `serde_derive`
location searched: registry `https://github.com/rust-lang/crates.io-index`
versions found: 1.0.98
required by package `serde v1.0.118`
    ... which is depended on by `bitcoin v0.25.2 (file:///home/steven/code/rust/bitcoin)`

Because a higher serde will require a higher serde_derive. Somehow Cargo is not very smart about all that and it can't realize that it has a pin on _derive and then look for a matching serde.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The script I use is

set -e

MSRV="1.29.0"

CMD="rustup run ${MSRV}"

rm -f Cargo.lock
$CMD cargo generate-lockfile
$CMD cargo update --package "cc" --precise "1.0.41"
$CMD cargo update --package "serde" --precise "1.0.98"
$CMD cargo update --package "serde_derive" --precise "1.0.98"

$CMD cargo test --features "use-serde rand base64"

Perhaps we should commit that.

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.

None of my scripts pin serde and everything works fine for me.

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.

Oh, I'm lying, it's just that my scripts never made any progress on this PR because of the other compilation issue.

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.

I just tried and this worked:
rm -rf target/ && rm Cargo.lock && cargo +1.29.0 test

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.

@elichai you have to add --features=use-serde to see the failure

@stevenroose stevenroose force-pushed the fix-json-psbt branch 2 times, most recently from ca41df6 to e5f285f Compare December 22, 2020 12:46
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One slightly annoying consequence of directly serializing the tuple instead of a wrapper is that the value is serialized as array of u8s instead of hex like everything else.

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.

I think this is more than slightly annoying but it's also really unclear how to fix 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.

It might be worthwhile to duplicate this entire module, changing U to Vec<u8> and doing hex-serialization.

Copy link
Copy Markdown
Contributor

@sgeisler sgeisler Dec 22, 2020

Choose a reason for hiding this comment

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

Could we get away with having a Preferred(De)Serialize trait which is identical to the (De)Serialize traits and has a default impl just passing through to the serde version? But for certain types we can implement default (de)serialization methods.

@stevenroose
Copy link
Copy Markdown
Collaborator Author

Pushed a few more changes:

  • only do the special treatment for human-readable serializers (JSON)
  • add special module for BTreeMap<K, Vec<u8>> where the K is serialized as a string and the value is hex
  • add special module for BTreeMap<K, Vec<u8>> where the K is NOT serialized as a string and the value is hex

}],
};
let encoded = ::serde_json::to_string(&psbt).unwrap();
println!("encoded PSBT: {}", encoded);
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.

debug printf

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Is that a problem? It's in a unit test, so it will only be printed when used with --nocapture. I can remove it if it's a problem. When changing any of the PSBT fields, this helps looking at how the JSON actually looks.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

(F.e. the Transactioin.witness field still is Vec<Vec<u8>> which doesn't print as hex. I tried fixing that too, but I got tired of fighting serde for a while, might do that in another PR.)

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.

Yeah, that's fair, you can leave it in.

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 3fd94ef

Copy link
Copy Markdown
Contributor

@sgeisler sgeisler left a comment

Choose a reason for hiding this comment

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

utACK 3fd94ef

@sgeisler sgeisler merged commit ed9856f into rust-bitcoin:master Dec 30, 2020
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.

5 participants