Skip to content

Provides a way to encode arbitrary-length integers as XDR. - #100

Merged
Shaptic merged 13 commits into
masterfrom
bigint
Jun 14, 2023
Merged

Shaptic merged 13 commits into
masterfrom
bigint

Conversation

@Shaptic

@Shaptic Shaptic commented Jun 7, 2023 •

Copy link
Copy Markdown
Contributor

This supersedes #96, incorporating #96 (comment) and separating out the 128 and 256-bit integers to stellar-base. This is a dependency of stellar/js-stellar-base#620.

TODO:

  • Is this a breaking change? I think it is, right, since the (Unsigned)Hyper constructor changes?
  • Add code to stellar/dts-xdr to conform to the new type signature

If you have reviewed #96, you can reference this diff to see the changes that this adds on top of that branch: it's just moving files and adding jsdocs.

cc @orbitlens

Comment thread src/bigint-encoder.js Resolved
Comment thread src/large-int.js
* @param {Array<Number|BigInt|String>} parts - Slices to encode
*/
constructor(args) {
super();

@sreuland sreuland Jun 8, 2023 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

to enforce abstraction, should we add the check:

if (this.constructor === LargeInt) {
    throw new Error("cannot create abstract class")
}

here in constructor and on the static methods, read, write?

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.

That's the performance-critical code, so my intention was to reduce the amount of (probably) excessive typechecks in read/write functions. Have no strong objections regarding the constructor.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Had a similar thought re: read/write

@sreuland sreuland left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

minor comments for consideration, great test coverage also!

Comment thread src/bigint-encoder.js Resolved
Comment thread src/bigint-encoder.js Resolved
Comment thread src/bigint-encoder.js Resolved
Comment thread src/bigint-encoder.js Resolved
Comment thread src/bigint-encoder.js Resolved
Comment thread src/bigint-encoder.js Resolved
Comment thread src/bigint-encoder.js Outdated
if (i > 0) {
value >>= shift;
}
result[i] = BigInt.asIntN(sliceSize, value & mask); // clamp value

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Shouldn't this be BigInt.asUintN to get the "bytes"? Incidentally, I think BigInt.asUintN also masks based on the sliceSize, so you don't need both.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right about the mask, but I'm not sure about the "signedness" here... @orbitlens?

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.

Don't remember why exactly I used BigInt.asIntN, but if I'm not mistaken, some tests were failing with BigInt.asUintN in this place. Probably because you can encode, say, u64 from two i32 parts, but other similar serialization libraries throw an error if you try to provide two u32 parts to reconstruct u64. The convention is to use i32, not u32 for constructing large integers. So it makes sense to always deconstruct an integer to signed smaller ints.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fair enough, added a comment to that regard.

we should prooooobably modify slice() to use asUintN for UnsignedHyper, but given that's a new method anyway, it should be fine as-is too

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.

we should prooooobably modify slice() to use asUintN for UnsignedHyper, but given that's a new method anyway, it should be fine as-is too

To my mind, the output of the .slice() method should be uniform for all integers regardless of the size and representation. I'd rather stick to outputting uniform signed integers as a result of this method invocation, otherwise it may result in confusion. So using the asIntN() for all ints looks slightly more appealing to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fine with me! you have a way better perspective given you are likely to be a user of these details so we can keep it as-is

Comment thread src/hyper.js
throw new TypeError(`Invalid i64 string value: ${string}`);
return new Hyper(BigInt(string));
get unsigned() {
return false;

@paulbellamy paulbellamy Jun 9, 2023 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Would these be easier to understand if it was

get signed() {
  true
}

Instead of phrasing it with the double-negative, of not-unsigned.

buuuuut that might break everything, so...

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.

I guess I saw such approach in some other serialization library. For example, Long in bson serializer.

Comment thread test/unit/bigint-encoder_test.js Resolved
@Shaptic

Shaptic commented Jun 13, 2023

Copy link
Copy Markdown
Contributor Author

lol I feel like @orbitlens should reply to the review comments since they're mostly related to code he wrote; I don't feel like I have enough context on his choices to discuss them

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