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

Fix serde #2559

Merged
merged 3 commits into from
Nov 12, 2024
Merged

Fix serde #2559

merged 3 commits into from
Nov 12, 2024

Conversation

ghostant-1017
Copy link
Contributor

@ghostant-1017 ghostant-1017 commented Oct 16, 2024

Motivation

Fix the bug in Block serialize and deserialize

Copy link
Contributor

@vicsn vicsn left a comment

Choose a reason for hiding this comment

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

Nice find. Why did test_serde_json below not catch any issues? Can any of the underlying test logic be adjusted so it would have failed earlier? And as a result, would we need to re-evaluate any other test_serde_json functions in snarkVM?

@ghostant-1017
Copy link
Contributor Author

@vicsn
In current test cases, we sample a couple of genesis blocks which's cumulative_weight is 0 to make tests on serialize and deserialize.

I don't think any adjustments are necessary, but we need to check if the types used in snarkVM are unsupported by JSON

@d0cd
Copy link
Contributor

d0cd commented Oct 17, 2024

@ghostant-1017 great find!
Would it be possible to add some of the failing test cases to this PR?
Also curious what are the implications in rolling out the fix? Would it break any existing code/infra/services?

@ghostant-1017
Copy link
Contributor Author

ghostant-1017 commented Oct 18, 2024

@d0cd
In JSON, the largest numeric type is f64, so any values in the u128 range that exceed f64 will cause test case failures. However, existing test cases do not cover this scenario.

To my knowledge, only the RPC interfaces related to /block/ have been affected by serialization.
Affect:
The json block returned after height 1203484 on mainnet can no longer be deserialized by the Block provided by snarkVM.

@vicsn
Copy link
Contributor

vicsn commented Oct 18, 2024

In current test cases, we sample a couple of genesis blocks which's cumulative_weight is 0 to make tests on serialize and deserialize.

I think a clean approach to testing could be to add a function similar to sample_genesis_block(, which uses Block::from_unchecked to construct a random invalid block to test serialization/deserialization on.

If you're not interested to add the test, just let us know and I'll close this PR, adding the test in a new one.

@iamalwaysuncomfortable
Copy link
Contributor

iamalwaysuncomfortable commented Oct 18, 2024

Nice find. Why did test_serde_json below not catch any issues? Can any of the underlying test logic be adjusted so it would have failed earlier? And as a result, would we need to re-evaluate any other test_serde_json functions in snarkVM?

@vicsn

serde-json by default deserializes directly to an f64 if it finds an integer that can only be represented by the u128 type.

The Number enum in serde-json without the arbitraryprecision flag can hold a u64, i64, or f64. Thus without the arbitraryprecision flag, Number isn't capable of holding a u128 directly and defaults to storing it as an f64. The arbitraryprecision flag creates an alternative Number enum that represents higher precision with two u64s, i64s, or f64s. I don't think it would be wise to rely on that flag because it would change how every single type is represented during the json Deserialization process and we're unaware the side effects that would have. It would also make everyone else using serde-json in Rust projects surrounding Aleo to use it.

The reason we didn't likely see it in testing, is because we didn't end up testing with a number above u64::MAX. If we had we might've caught the error, so test cases that use integers that require more than 8 bits should be added.

@ghostant-1017
Copy link
Contributor Author

@vicsn I will make some changes according to @iamalwaysuncomfortable , and add the test case sir.

Copy link
Contributor

@vicsn vicsn left a comment

Choose a reason for hiding this comment

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

Amazing!

Copy link
Collaborator

@zkxuerb zkxuerb left a comment

Choose a reason for hiding this comment

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

LGTM

@zosorock zosorock added bug Something isn't working v1.1.4 Canary release v1.1.4 labels Nov 12, 2024
Copy link
Collaborator

@ljedrz ljedrz left a comment

Choose a reason for hiding this comment

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

LGTM 👌

@zosorock
Copy link
Contributor

check-clippy finishes fine, in 34s. It is just CircleCI being its usual garbage.

@zosorock zosorock merged commit 60e4602 into AleoNet:staging Nov 12, 2024
83 of 84 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working v1.1.4 Canary release v1.1.4
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants