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

Incorrect decoding of signed integers i8/i16 #5702

Closed
5 of 10 tasks
peetzweg opened this issue Aug 17, 2023 · 3 comments
Closed
5 of 10 tasks

Incorrect decoding of signed integers i8/i16 #5702

peetzweg opened this issue Aug 17, 2023 · 3 comments

Comments

@peetzweg
Copy link
Contributor

peetzweg commented Aug 17, 2023

The decoding of some scale encoded signed integers values result in the wrong representation in js.
Generally scale encoded i8 and i16 value seem to work fine, however the edge cases, the value of 1 and i8::MAX and i16::MAX are decoded to the wrong representation in javascript.

This issue bubbles up in one form or another, see related issue section. How can we resolve this once and for all?

I've created a PR adding testcases for the broken decoding cases #5703.

  • I'm submitting a ...

    • Bug report
    • Feature request
    • Support request
    • Other
  • What is the current behavior and expected behavior?

// Scale encoded signed integers of value `1`
const bytes_1i8 = new Uint8Array([1]);
const bytes_1i16 = new Uint8Array([1, 0]);
const bytes_1i32 = new Uint8Array([1, 0, 0, 0]);
const bytes_1i128 = new Uint8Array([
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  ]);


api.createType("i8", bytes_1i8).toNumber(); // expected 1, actual -255 ❌
api.createType("i16", bytes_1i16).toNumber(); // expected 1, actual -65535 ❌
api.createType("i32", bytes_1i32).toNumber(); // expected 1, actual 1 ✅
api.createType("i128", bytes_1i128).toNumber(); // expected 1, actual 1 ✅


// Decoding MAX values for signed integers
const bytes_maxi8 = new Uint8Array([127]);
const bytes_maxi16 = new Uint8Array([255, 127]);
api.createType("i8", bytes_maxi8).toNumber(); // expected 127, actual -129 ❌
api.createType("i16", bytes_maxi16).toNumber(); // expected 32767, actual -32769 ❌


// SCALE encoded `0` value of `i8` and `i16`
const bytes_0i8 = new Uint8Array([0]);
const bytes_0i16 = new Uint8Array([0, 0]);
api.createType("i8", bytes_0i8).toNumber(); // expected 0, actual `Input too large. Found input with 9 bits, expected 8`  ❌
api.createType("i16", bytes_0i16).toNumber(); // expected 0, actual `Input too large. Found input with 17 bits, expected 16` ❌

Codesandbox Link

  • Please tell us about your environment:

    • Version: @polkadot/[email protected]

      • Node.js
      • Browser
      • Other (limited support for other environments)
    • Language:

      • JavaScript
      • TypeScript (include tsc --version)
      • Other

Related Issues

@peetzweg
Copy link
Contributor Author

I've probably identified the issues and written about it here: #5703 (comment)

jacogr added a commit that referenced this issue Aug 29, 2023
* adds tests cases describe in #5702

* fix wrong test outputs

* Update packages/types-codec/src/base/Int.spec.ts

Co-authored-by: Jaco <[email protected]>

* Apply suggestions from code review

---------

Co-authored-by: Jaco <[email protected]>
@jacogr
Copy link
Member

jacogr commented Aug 30, 2023

Closing, issues addressed at the util layer (test cases merged)

@jacogr jacogr closed this as completed Aug 30, 2023
@polkadot-js-bot
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Sep 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants