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

Extra validation of data for encoding #86

Merged
merged 2 commits into from
Nov 24, 2021
Merged

Extra validation of data for encoding #86

merged 2 commits into from
Nov 24, 2021

Conversation

davidyuk
Copy link
Member

No description provided.

@davidyuk davidyuk requested a review from dincho November 10, 2021 13:26
@davidyuk davidyuk changed the title Extra validation of encoded data Extra validation of data for encoding Nov 10, 2021
@@ -85,6 +85,9 @@ class DataFactory {
}

if (type.name === 'list') {
if (!Array.isArray(value)) {
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason to not validate it in the type class ? DataFactory should be general

Copy link
Member Author

Choose a reason for hiding this comment

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

Because later we are calling the map method of value, should we move it to FateList as well?

Copy link
Member

Choose a reason for hiding this comment

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

Fair point, I've overlooked that.

However there is non-sense tautology in the error message "list must be an array", same for tuple: "Tuple must be an array". Please try to find a better error message.

Something like "[Fate] List constructor expects an array of values, got X instead"

And slight note on formatting, please have a new line after each block of statements, if that not the last statement in another block. I know I've to commit the rules and it's not obvious from the codebase .. soon

Copy link
Member Author

Choose a reason for hiding this comment

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

[Fate] List constructor expects an array of values, got X instead

I'm not sure about the exact message

  • won't "Sophia" be more clear than "Fate" for third-party users?
  • the constructor seems to be too internal part to mention it
  • I would drop "of values" because this is the same for any array, technically we can generate the exact type instead (like array of numbers)
  • would be better to name Array from the capital letter to highlight that this is a JS type?
  • should we do JSON.stringify of X? should we handle the "Do not know how to serialize a BigInt" exception then?

Proposing to change it to "Sophia list must be an Array, got X instead"

Copy link
Member

Choose a reason for hiding this comment

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

My message was just an example, feel free to change it to whatever feel better. However, again on users .. this library has nothing to do with Sophia programming language, but FATE virtual machine :) So please don't use the term sophia, you don't have to use the term fate as well if you don't think it's a good fit

Copy link
Member

@dincho dincho Nov 23, 2021

Choose a reason for hiding this comment

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

I don’t know really, AEVM is deprecated, so the only VM now is FATE. We also don’t know if there would be any other VM in future so let’s keep it simple? I’m more lean to drop off the calldata from the name as I plan to add full serializaion support anytime soon, however we might just split the serializer and encoder 🤔🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

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

also when looking into the readme that might be confusing. just want to raise that topic now :-)

Would you propose readme changes?

Copy link
Contributor

@marc0olo marc0olo Nov 23, 2021

Choose a reason for hiding this comment

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

I shouldn't have interferred here 😅 - actually I currently don't have concrete proposals. I am fine with leaving like is for the moment in regards to the readme.

We also don’t know if there would be any other VM in future so let’s keep it simple?

👍

I’m more lean to drop off the calldata from the name as I plan to add full serializaion support anytime soon

might make sense in that case to do this better earlier than later. we should have some reliable release (& npm package) in the near future 🤔

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we should rush about that, the only user of the lib is the SDK so we should be fine for quite some time.

The lib already have NPM packages and release process in place, once we all agree on it's stability we should tag 1.0.0

Copy link
Member

Choose a reason for hiding this comment

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

1.0.0-rc1 perhaps

Copy link
Member

@dincho dincho left a comment

Choose a reason for hiding this comment

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

please keep the DataFactory general - knowing nothing about types specifics, but only how to create types

try {
this._value = BigInt(value)
} catch (error) {
if (/^Cannot convert .* to a BigInt$/.test(error.message)) {
Copy link
Member

Choose a reason for hiding this comment

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

what's the reason changing the error message ?!

Copy link
Member Author

Choose a reason for hiding this comment

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

Using calldata the developer may not use BigInts explicitly, so it may be unclear why an argument should be convertible to BigInt. "number" is more general than "BigInt" so the developer would guess that the problem is rather on his side than in the internals of calldata.

But I'm not 100% sure about it and ok to revert this if @marc0olo @mradkov agrees that this is not needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Mhhhh, when thinking about this I don't think we really need this. I mean every experienced dev should know how to handle this or whats the problem, right? Personally I favor the original error message.

Copy link
Member

Choose a reason for hiding this comment

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

That reason, don't hold for me. Please revert.

@@ -12,7 +12,13 @@ const toByteArray = (value) => {
return HexStringToByteArray(value)
}

return Int2ByteArray(BigInt(value))
try {
Copy link
Member

@dincho dincho Nov 22, 2021

Choose a reason for hiding this comment

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

shouldn't this be reverted as well ? I didn't feel it was necessary to comment on each error message rewrite

Copy link
Member Author

@davidyuk davidyuk Nov 23, 2021

Choose a reason for hiding this comment

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

The general idea is to show complete error message in case if we wasn't able to convert value to byte array.

Number is the less obvious representation of byte array, can we replace this validation with Array.isArray to show error like "is not an array" instead "can't convert to BigIng" in case we remove the complete error message?

We need an agreement on developer friendlies. SDK before was too developer friendly, I'm just trying to make the difference not so significant (on calldata side).

Copy link
Member

Choose a reason for hiding this comment

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

I’m more or less against changing of error messages. Additional validation should be fine. It’s also fine if SDK strive to be developer friendly, however this lib is more low level and doesn’t target non experienced devs. It should be focused on specification conformance, clean code, maintability (that includes minimal solid API) and performance

@marc0olo
Copy link
Contributor

according to @davidyuk this is related to #79 (kind of the same issue) and could probably be merged now, too? :-)

@@ -12,7 +12,11 @@ const toByteArray = (value) => {
return HexStringToByteArray(value)
}

return Int2ByteArray(BigInt(value))
Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, value converted to BigInt at the first line of Int2ByteArray

@dincho dincho merged commit 39b4212 into master Nov 24, 2021
@dincho dincho deleted the extra-validation branch November 24, 2021 09:12
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