-
Notifications
You must be signed in to change notification settings - Fork 38.7k
uint256::GetCheapHash bigendian compatibility #7078
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
Conversation
|
eh, use WriteLE64()? |
|
edit: See @laanwj's comment edit2: Conflicted, utACK on the code |
|
This is on purpose! The "hash" is meant to be as fast as possible by using endian-dependent operations. E.g. within one program run it is stable and that is all that matters - it is not supposed to be used with anything that leaves the program. Edit: as for the test failure, have you looked into why this causes a test failure? In principle a different hash function should not break the tests (or at least the correctness of the code), so this error may lie deeper. |
|
The failed test case is addrman_new_collisions and addrman_tried_collisions: https://github.com/bitcoin/bitcoin/blob/master/src/test/addrman_tests.cpp#L141 The testcase make a hash collision, The value of "hash1 % ADDRMAN_BUCKET_SIZE" https://github.com/bitcoin/bitcoin/blob/master/src/addrman.cpp#L29 is always 28 when addr1 is 250.1.1.1 or addr1 is 250.1.1.4 when little endian, but its different in big endian, its the reason that test failed. The test result in big endian should be consistent with the little endian. |
|
Bleh, so the test relies very strongly on the exact outcome of the hash function (as the algorithm is "randomized" by that), even though both results are correct inthemselves? |
|
These addrman "tests" look pretty crappy in general... lots of checking internal implementation details rather than expected external behaviour. :/ |
|
The addrman_new_collisions first call addrman.MakeDeterministic(), it set addrman.nKey is NULL, then the addrman addr placement in new table will be deterministic, it seems try to simulate hash collisions when addrman.add() . |
|
The primary reason I'm protesting here is because GetCheapHash is used in It is possible that adding a (conditional) byte swap has hardly impact on performance, but that would have to be benchmarked. |
src/uint256.h
Outdated
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.
Nit: what you want here is ReadLE64, not WriteLE64:
return ReadLE64(data);
|
I doubt the performance penalty of ReadLE64 is going to matter really on any system. I liked the idea of having an unportable fast function, though, but being able to rely on behaviour does make testing easier too. Unsure. |
Right - in the case of addrman, the overhead is nothing compared to the double-SHA256, in all its uses: So the two usecases of GetCheapHash are wildly different. In the case of sigcache every cycle counts, in the case of addrman we may just as well be using something else. E.g. could also remove the function from uint256 class and have tailored implementations at the call sites |
|
@laanwj Even the signature cache now uses a SHA256 (though that should at some point be replaced with a fast non-cryptographic but highly collision-resistant hash function). A single byteswap (and only on BE platforms...) is not going to be measurable. |
|
OK, just going ahead and merging this then |
c434940 uint256::GetCheapHash bigendian compatibility (daniel)
Tested on PPC/MIPS :
Before change:
After: