trie/bintrie: fix tree key hashing to match spec (#33694)

Based on [EIP-7864](https://eips.ethereum.org/EIPS/eip-7864), the tree
index should be 32 bytes instead of 31 bytes.
```
def get_tree_key(address: Address32, tree_index: int, sub_index: int):
    # Assumes STEM_SUBTREE_WIDTH = 256
    return tree_hash(address + tree_index.to_bytes(32, "little"))[:31] + bytes(
        [sub_index]
    )
```
This commit is contained in:
Ng Wei Han 2026-01-28 18:51:02 +08:00 committed by GitHub
parent 344d01e2be
commit 3d05284928
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 2 additions and 1 deletions

View file

@ -308,7 +308,7 @@ func TestVerkleGenesisCommit(t *testing.T) {
},
}
expected := common.FromHex("19056b480530799a4fdaa9fd9407043b965a3a5c37b4d2a1a9a4f3395a327561")
expected := common.FromHex("b94812c1674dcf4f2bc98f4503d15f4cc674265135bcf3be6e4417b60881042a")
got := genesis.ToBlock().Root().Bytes()
if !bytes.Equal(got, expected) {
t.Fatalf("invalid genesis state root, expected %x, got %x", expected, got)

View file

@ -51,6 +51,7 @@ func GetBinaryTreeKey(addr common.Address, key []byte) []byte {
hasher.Write(zeroHash[:12])
hasher.Write(addr[:])
hasher.Write(key[:31])
hasher.Write([]byte{0})
k := hasher.Sum(nil)
k[31] = key[31]
return k