erigontech / erigontech/erigon

Commitment: fix encoding of `Root Cell` and regular `Cell`

Open
#17,612 3 comments 0 reactions 1 assignee Claimed by @awskii View on GitHub
tech debt reduction
Dominant language
Go
Stars
3.6k
Forks
1.5k
Avg merge
1d 16h
Merged PRs (30d)
455

Description

Within `EncodeCurrentState` we use `hph.root.Encode()` which does binary serialization of the node.
Does in following order:
- flags
- accountAddr
- storageAddr
- hashedExtension
- extension

notice that we do not encode cell hash or state hash (memoised hash)

While we encode `BranchData` via `EncodeBranch` we encode each cell differently:
- flags
- extension
- accountAddr
- storageAddr
- hash (aka branchHash)
- stateHash (aka memoisedHash)

A.T.M. i implemented trie state save+restore, i did not understood that hashedExtension (DownHash back then) and extension are actually 2 sides of one coin.

In real state files hashed extension should be empty due to prefixes divergence straight from nibble 0 so no extension after root possible.
Same is true for account/storage addresses - they are just empty because account trie is deep already. In other words, IRL root encoding is almost empty all the time but we still encode it to cover strange edge cases (which easy to build test for but hard to meet in a wild)

Hash is not encoded there because easily computable from top branch (before which we do not have any extension) and state hash - well, it was not the case as well (memoisation was not implemented).

To skip encoding of some fields, we could just set their length to zero but use same encoding.
When we decode `BranchData` we use method `cell.fillFromFields` which intentionally was made to operate over single slice of bytes by different cells but signature could be easily changed to have just a slice of bytes (starting with a field bitmap) and able to decode just a single cell, returning latest position (to move ptr and allow next cell to decode forward)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.