-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Conversation
There was a problem hiding this 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.
There was a problem hiding this 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)) |
There was a problem hiding this comment.
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).
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. |
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 ofSerializer
and makes the buffer size checks more stringent.No change in functionality is expected.
@HowardHinnant is there any reason to keep
SHAMapHash
as aclass
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 rawuint256
and we have no easy easy to "bend" the type system enough to cast abase_uint<N, X>
tobase_uint<N, Y>
without copying?Type