Skip to content

Conversation

insipx
Copy link
Contributor

@insipx insipx commented Dec 3, 2020

Latest PolkadotJS includes two new types UInt<size, type> and Int<size, type>. There were a few ways to handle this:

  • with nothing done, desub recognizes this as a generic and tries to parse/decode it as such, but runs into name resolution issues because it won't be able to resolve UInt/Int (those definitions are internal to polkadot.js and not in the JSON). So we can instead include a special case to match on UInt and Int in the generic matching code and redirect it to instead decode this as a primitive integer
  • create new regex to parse UInt and Int as u{8-128} and Byte-arrays

I went with the second option as it seem the most flexible with future changes, and requires less code added to decoder.rs (which could be confusing as a generic redirects to a primitive)

Includes changes to update to latest master from #33

Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

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

LGTM modulo nits

insipx and others added 3 commits December 4, 2020 15:40
Co-authored-by: David <dvdplm@gmail.com>
@@ -823,6 +814,49 @@ impl Decoder {
}
}

/// Decodes old address pre-refactor (https://github.com/paritytech/substrate/pull/7380)
/// and converts it to a MultiAddress, where "old" here means anything before v0.8.26 or 26/2026/46 on polkadot/kusama/westend respectively.
fn decode_old_address(data: &[u8], cursor: &mut usize) -> Result<substrate_types::Address, Error> {
Copy link
Contributor

@niklasad1 niklasad1 Dec 6, 2020

Choose a reason for hiding this comment

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

AFAIU there's nothing checks that cursor and cursor + 1 are valid indexes into data slice but maybe checked somewhere else?

It may panic then.

Copy link
Contributor Author

@insipx insipx Dec 7, 2020

Choose a reason for hiding this comment

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

Yeah,

This is part of the larger issue that the function decode_single doesn't do any bounds checking on the cursor at all, and panics if anything goes wrong. I'd rather address that in it's own PR though, since it might be a bit of a larger/medium sized refactor so I opened #35 to track that

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, cool

@@ -90,6 +94,10 @@ fn rust_array_decl_struct() -> Regex {
Regex::new(r"^\[ *?([\w><]+) *?; *?(\d+) *?\]").expect("Primitive Regex expression invalid")
}

pub fn rust_bit_size() -> Regex {
Regex::new(r"^(Int|UInt)<([\w\d]+), ?([\w\d]+)>").expect("Regex expression should be infallible; qed")
Copy link
Contributor

@niklasad1 niklasad1 Dec 6, 2020

Choose a reason for hiding this comment

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

I guess this regex is for Int<size, type>, I'm not a regex export but...

I'm surprised that [\d\w]+ is used for the size, it would match beef999bibimbap for example. Thus, if size is in decimal just use \d+ then.

Why , ?([\w\d]+) it would only work for Int<999,bar> and Int<999, bar> but not for arbitrary number of spaces :)

I suggest , *(\w+) instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the capture groups are necessary for the type params, i.e. this should be fine: (Int|UInt)<[\w\d]+, *[\w\d]+> and like niklas I'm not sure I understand what the input to this looks like: can both params contain both letters and number? A test that exercise this case would be good.

Copy link
Contributor Author

@insipx insipx Dec 7, 2020

Choose a reason for hiding this comment

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

Here's a sample of some of the input from polkadot.js:

      "Fixed64": "Int<64, Fixed64>",
      "FixedI64": "Int<64, FixedI64>",
      "FixedU64": "UInt<64, FixedU64>",
      "Fixed128": "Int<128, Fixed128>",
      "FixedI128": "Int<128, FixedI128>",
      "FixedU128": "UInt<128, FixedU128>",
      "I32F32": "Int<64, I32F32>",
      "U32F32": "UInt<64, U32F32>",
      "PerU16": "UInt<16, PerU16>",
      "Perbill": "UInt<32, Perbill>",
      "Percent": "UInt<8, Percent>",
      "Permill": "UInt<32, Permill>",
      "Perquintill": "UInt<64, Perquintill>",
      "Balance": "UInt<128, Balance>",

So the second type parameter is unnecessary, since it's already a key in our de-serialized JSON, but the first type parameter is needed to delegate the size of the type we are dealing with. This also leads me to believe that the first type parameter will always be a digit and the second just a repeat of the JSON key (I haven't found anything in the definitions that contradicts this). I'm not 100% on what the rationale of having the type repeat is in Polkadot.JS, but in the PR's I found like here they use the Type<BitLength> in that style.

I agree with @niklasad1 , though, arbitrary number of spaces is definitely preferable, fixes for these/and above incoming

Copy link
Contributor

@niklasad1 niklasad1 Dec 8, 2020

Choose a reason for hiding this comment

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

All right, I see

We could do (Int|UInt)<[\d]+(, *[\w\d]+)?> instead to make the second type parameter optional then I guess

insipx and others added 4 commits December 6, 2020 23:27
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
…h/desub into insipx/update-type-definitions
@dvdplm dvdplm merged commit bee6388 into master Dec 8, 2020
@dvdplm dvdplm deleted the insipx/update-type-definitions branch December 8, 2020 10:17
@insipx insipx mentioned this pull request Dec 14, 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.

3 participants