-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(trie): refactor header encoding to prepare for upgrades (#2530)
- Declare node variants as bits+bit mask instead of just bits or enums - `encodeHeader` encodes the first header byte + extra partial key length bytes instead of the convoluted/not-modular `encodeHeader` + `encodeKeyLength` - `decodeHeader` gets the variant + partial key length, instead of convoluted in-line header code in `Decode` + further header decoding in `decodeLeaf` and `decodeBranch` - Find the node variant header partial key length mask dynamically from the node variant header bit mask, instead of using a constant - Use `uint16` for `partialKeyLength` as specified in the specification - Clarify codec documentation in `internal/trie/node/README.md` - Fixes: - `decodeKey` last byte maximum value check fixed - `decodeKey` accepts a key of length up to `65535` - Allow to encode partial key of length `65535` - Other refactoring: - Panic for programming errors cases - Revise some of the error wrapping on errors changed in this commit - Remove single byte sync pool (slower than stack byte slice of length 1)
- Loading branch information
Showing
14 changed files
with
626 additions
and
468 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Trie node | ||
|
||
Package node defines the `Node` structure with methods to be used in the modified Merkle-Patricia Radix-16 trie. | ||
|
||
## Codec | ||
|
||
The following sub-sections precise the encoding of a node. | ||
This encoding is formally described in [the Polkadot specification](https://spec.polkadot.network/#sect-state-storage). | ||
|
||
### Header | ||
|
||
Each node encoding has a header of one or more bytes. | ||
The first byte contains the node variant and some or all of the partial key length of the node. | ||
If the partial key length cannot fit in the first byte, additional bytes are added to the header to represent the total partial key length. | ||
|
||
### Partial key | ||
|
||
The header is then concatenated with the partial key of the node, encoded as Little Endian bytes. | ||
|
||
### Remaining bytes | ||
|
||
The remaining bytes appended depend on the node variant. | ||
|
||
- For leaves, the SCALE-encoded leaf value is appended. | ||
- For branches, the following elements are concatenated in this order and appended to the previous header+partial key: | ||
- Children bitmap (2 bytes) | ||
- SCALE-encoded node value | ||
- Hash(Encoding(Child[0])) | ||
- Hash(Encoding(Child[1])) | ||
- ... | ||
- Hash(Encoding(Child[15])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.