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

feat(primitive-types, serde): deserialize hex strings without 0x prefix #598

Merged

Conversation

shekhirin
Copy link
Contributor

Support hex strings which are not prefixed with 0x.

Example from geth's debug_traceTransaction (storage key):

> debug.traceTransaction("0x2059dd53ecac9827faad14d364f9e04b1d5fe5b506e3acc886eff7a6f88a696a")
{
  gas: 85301,
  returnValue: "",
  structLogs: [{
      depth: 1,
      error: "",
      gas: 100000,
      gasCost: 0,
      memory: ["0000000000000000000000000000000000000000000000000000000000000006", "0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000060"],
      op: "STOP",
      pc: 120,
      stack: ["00000000000000000000000000000000000000000000000000000000d67cbec9"],
      storage: {
        0000000000000000000000000000000000000000000000000000000000000004: "8241fa522772837f0d05511f20caa6da1d5a3209000000000000000400000001",
        0000000000000000000000000000000000000000000000000000000000000006: "0000000000000000000000000000000000000000000000000000000000000001",
        f652222313e28459528d920b65115c16c04f3efc82aaedc97be59f3f377c0d3f: "00000000000000000000000002e816afc1b5c0f39852131959d946eb3b07b5ad"
      }
  }]

@shekhirin shekhirin marked this pull request as ready for review November 9, 2021 18:01
@shekhirin shekhirin changed the title feat(primitive-types): deserialize hex strings without 0x prefix feat(primitive-types, serde): deserialize hex strings without 0x prefix Nov 9, 2021
@shekhirin shekhirin force-pushed the feat/primitive-types-deserialize-hex-wo-0x branch from 49a4773 to 9574a9b Compare November 9, 2021 19:15
@shekhirin
Copy link
Contributor Author

@ordian fixed the remaining tests

Copy link
Member

@ordian ordian left a comment

Choose a reason for hiding this comment

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

@tomusdrw is this going to break anything? We're less restrictive here, but also
assert_eq!(serialize(deserialize(s)?), s) is broken in some cases now.

Other than that, LGTM.

primitive-types/impls/serde/src/serialize.rs Outdated Show resolved Hide resolved
primitive-types/impls/serde/src/serialize.rs Outdated Show resolved Hide resolved
@tomusdrw
Copy link
Contributor

tomusdrw commented Nov 9, 2021

We've been trying to be strict to avoid ambiguity between hex and decimal encoding. Also it used to be part of the Ethereum JSON-RPC spec to always prefix hex values with 0x. I guess it's not the case any more. I don't see a reason why it would break anything.

@shekhirin
Copy link
Contributor Author

sounds good. Merge?

Copy link
Member

@niklasad1 niklasad1 left a comment

Choose a reason for hiding this comment

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

LGTM, the changelog needs to be updated not sure if any project is using these types in the ethereum JSON-RPC interface anymore but still...


let mut bytes = vec![0u8; (v.len() - 1) / 2];
from_hex_raw(v, &mut bytes)?;
let mut bytes = vec![0u8; (v.len() + 1) / 2];
Copy link
Member

Choose a reason for hiding this comment

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

dq: is (v.len() + 1) / 2 used to work in case the hex string is not divisible by 2?

when it's even we would allocate one extra byte but yeah it was like this before too..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, there're some tests like

assert_eq!(from_hex("102"), Ok(vec![1, 2]));

which wouldn't pass if bytes.len() would be 1 instead of 2, so we need to round up. It was - 1 previously because we were always expecting 0x prefix to present.

@shekhirin
Copy link
Contributor Author

@niklasad1 I updated the changelog

@ordian
Copy link
Member

ordian commented Nov 10, 2021

Ok, thanks. I'll merge and publish it today.

@ordian ordian merged commit aa58883 into paritytech:master Nov 10, 2021
@ordian
Copy link
Member

ordian commented Nov 10, 2021

Published.

@shekhirin
Copy link
Contributor Author

@ordian I forgot to update ethereum-types version since I've changed used impl-serde version inside it. Bumped here: #599

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants