gossamer icon indicating copy to clipboard operation
gossamer copied to clipboard

fix(trie): use direct Merkle value for database keys

Open qdm12 opened this issue 3 years ago • 1 comments

Changes

The problem is as follows in this example.

For a node A encoding to {1, 2}, its database key is {0, 0, 0, ..., 1, 2}. Another node B with > 32B encoding could (unlikely but still) hash to the same value {0, 0, 0, ..., 1, 2} as for A and that would conflict in the database.

The solution is to NOT force the database key to be 32B always, and to keep it plainly as the Merkle value of the node (32B or less). This should also occupy less space in the database eventually.

  • Storage KV database node key has now the length of the Merkle value and is no longer forced to 32B
  • Renaming XXXHashesSet to XXXMerkleValues since it's not just hashes
  • Renaming deletedKeys to deletedMerkleValueToBlockNumber in dot/state
  • Renaming methods XXXNodeHashes to XXXMerkleValues

Tests

go test -tags integration github.com/ChainSafe/gossamer

Issues

Primary Reviewer

@timwu20

qdm12 avatar Aug 04 '22 15:08 qdm12

Codecov Report

Merging #2725 (c304159) into development (9dc11e4) will decrease coverage by 0.10%. The diff coverage is 50.00%.

Additional details and impacted files
@@               Coverage Diff               @@
##           development    #2725      +/-   ##
===============================================
- Coverage        63.45%   63.34%   -0.11%     
===============================================
  Files              217      217              
  Lines            27059    27058       -1     
===============================================
- Hits             17170    17141      -29     
- Misses            8327     8352      +25     
- Partials          1562     1565       +3     

codecov[bot] avatar Aug 04 '22 15:08 codecov[bot]

It's enforced that the node value <= 32B yes? Seems like but just wanted to double check

jimjbrettj avatar Sep 21 '22 20:09 jimjbrettj

It's enforced that the node value <= 32B yes? Seems like but just wanted to double check

The Merkle value is <= 32B (node scale encoding if it is smaller than 32B, otherwise the 32B blake2b hash digest of the node scale encoding)

qdm12 avatar Sep 22 '22 12:09 qdm12

Just looked at this. Great work!

timwu20 avatar Sep 22 '22 18:09 timwu20

:tada: This PR is included in version 0.7.0 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Nov 23 '22 18:11 github-actions[bot]