Fix Digest hex encoding to preserve numeric ordering#277
Conversation
|
Do we want this? In isolation I think it makes sense. I'm just not sure it's worth it dealing with the fallout. I'm leading towards a yes. But maybe, for downstream compatibility there should be an option to have the small-endian version also? Edit: Seems like more than just endianness is changing. I think it has to do with how the number are divided and doesn't necessarily poiint to an error in this PR. |
I think your edit is right: this is not only an endianness change. The old implementation encoded the byte representation of the underlying digest elements. This PR instead makes the hex string represent the same numeric value used by My motivation was to address #214 as written: make I agree that the compatibility concern is the main tradeoff here. If downstream users may rely on the previous byte-oriented encoding, I can add explicit legacy helpers, for example |
|
I say we go with this and deal with the downstream problems when they appear. I foresee some problems downstream in @haoshengzhen Do you think you can sign your commits such that this can be merged? |
|
@haoshengzhen Can you sign your commits? Then we can merge. |
Signed-off-by: haoshengzhen <haoshengzhen@outlook.com>
@Sword-Smith Sorry for the late reply.
|
|
@haoshengzhen Just had a talk about this in the office. @aszepieniec will have the final say since he invented the TIP5 hashing algorithm. I'm sorry for the confusion. |
|
If I understand correctly, the only changes are user-facing displays (nothing mechanical); and these display changes are divisible into two separate points:
I actually oppose both but for different reasons. Big integer representationThere are many ways to embed arrays of 5 Converting base Converting base Most-significant digit firstWe inherit the digit order we use from numbers in prose from Hindu scribes who did not know about arrays and indexing. Given that the I am aware of the motivation for block hashes to appear small when they satisfy the proof-of-work threshold digest < target. The number of leading zeros (as opposed to the number of trailing zeros) gives an idea of how much work a block represents. IMHO this application is rather artificial. It is equally possible to define Nakamoto consensus by counting leading (or trailing) ones instead. That's not how we do it in Neptune Cash, but that's a design choice that one validates by reading If we want block hashes of blocks that satisfy the threshold inequality to be displayed with leading zeros instead of trailing zeros (I am indifferent to this particular motivation) then the right place to write that display implementation is ConclusionI'm obviously grateful that a volunteer chose to donate time and effort to make a PR and I certainly don't want to be discouraging. That said, I do not think these particular changes improve the code base. If you want to keep focus on the feature you are targeting, small block hashes, then I recommend making a PR to |

Summary
Fixes #214.
This changes
Digesthex formatting/parsing to use the same numeric representation asBigUintconversion andOrd, padded to the fixed digest byte width.Previously, small digests such as
Digest([14, 0, 0, 0, 0])formatted with the non-zero byte at the beginning of the hex string. With this change, the hex representation has leading zeroes and matches the digest's numeric value.Changes
Digestas fixed-width big-endian hex viaBigUint::from(digest).Digest.BigUintDigest::Ordto_hex/try_from_hexDigest::to_hex.Verification
cargo test --workspace --lib --testscargo test --doccargo clippy --workspace --all-targets -- -D warningscargo fmt --all -- --check