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

Improve full & compressed inner node deserialization #4004

Closed
wants to merge 2 commits into from
Closed

Improve full & compressed inner node deserialization #4004

wants to merge 2 commits into from

Conversation

nbougalis
Copy link
Contributor

Details

There are two serialization formats for inner nodes: "full" and "compressed". A full node is serialized as 16 32-byte hashes, for a total of 512 bytes. An inner node is serialized as a number of {position, hash} pairs, between 1 and 16, where position is stored as 1 byte and a hash is stored as 32 bytes.

This change removes a redundant copy by using SerialIter instead of Serializer and makes the buffer size checks more stringent.

No change in functionality is expected.

@HowardHinnant is there any reason to keep SHAMapHash as a class instead of converting it to something like: using SHAMapHash = base_uint<256, SHAMap>, other than then fact that we need to pass such hashes to functions which accept a raw uint256 and we have no easy easy to "bend" the type system enough to cast a base_uint<N, X> to base_uint<N, Y> without copying?

Type

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (non-breaking change that only restructures code)
  • Tests (You added tests for code that already exists, or your new feature included in this PR)
  • Documentation Updates
  • Release

Copy link
Contributor

@HowardHinnant HowardHinnant left a comment

Choose a reason for hiding this comment

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

I find this to be a significant improvement in readability.

The only reason I can see for the separate SHAMapHash class is that it has an API that is a subset of base_uint. That being said, I don't think it would be especially error-prone to make the API of SHAMapHash and base_uint identical using a type alias.

Copy link
Collaborator

@seelabs seelabs left a comment

Choose a reason for hiding this comment

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

Nice use of SeriaIter to clean up the code. LGTM, except I get a new warning from gcc that we should resolve. Thumbs up after that's fixed.

while (!si.empty())
{
auto const hash = si.getBitString<256>();
auto const pos = si.get8();

if ((pos < 0) || (pos >= branchFactor))
Copy link
Collaborator

Choose a reason for hiding this comment

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

gcc warns here:

warning: comparison is always false due to limited range of data type [-Wtype-limits]

Yes, I know this isn't new code; I'm not sure why it didn't use to warn, but we probably want to fix the warning anyway. (pos is unsigned, so it can't be less than 0).

@nbougalis
Copy link
Contributor Author

The only reason I can see for the separate SHAMapHash class is that it has an API that is a subset of base_uint. That being said, I don't think it would be especially error-prone to make the API of SHAMapHash and base_uint identical using a type alias.

It's a real pity that we don't have strongly typed alias support in C++. I really do think it would help make a LOT of code cleaner, safer and more maintainable.

This was referenced Dec 15, 2021
@nbougalis nbougalis deleted the shamapserdes branch October 16, 2023 06:00
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