-
Notifications
You must be signed in to change notification settings - Fork 144
fix(trie): decode inline child nodes #2369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fee33f3
3dc3fd5
cb21042
ff5ba1e
130487c
54d9b8a
db64597
3612b6c
02a3e67
cbe4b73
657b624
c94e83c
5154e16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,18 @@ func decodeBranch(reader io.Reader, header byte) (branch *Branch, err error) { | |
ErrDecodeChildHash, i, err) | ||
} | ||
|
||
// Handle inlined leaf nodes. | ||
const hashLength = 32 | ||
if Type(hash[0]>>6) == LeafType && len(hash) < hashLength { | ||
leaf, err := decodeLeaf(bytes.NewReader(hash[1:]), hash[0]) | ||
if err != nil { | ||
return nil, fmt.Errorf("%w: at index %d: %s", | ||
ErrDecodeValue, i, err) | ||
} | ||
branch.Children[i] = leaf | ||
continue | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quick question, I guess this inline format cannot be used for branches right? Only for leaves? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah so a few things here I think. Technically there could be, my impression of the logic is that any child node < 32 bytes can be in-lined leaf or branch. The child parsing seems to be wrong in general? It looks like its currently parsing every child as a leaf node. Although I'm not sure it semantically makes a difference when its just a hash pointer. Also, there are a few header types that are missing from the impl: impl Decode for NodeHeader {
fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
let i = input.read_byte()?;
if i == trie_constants::EMPTY_TRIE {
return Ok(NodeHeader::Null);
}
println!("header byte {:b} {:b}", i, i & (0b11 << 6));
match i & (0b11 << 6) {
trie_constants::LEAF_PREFIX_MASK => Ok(NodeHeader::Leaf(decode_size(i, input, 2)?)),
trie_constants::BRANCH_WITH_MASK => {
Ok(NodeHeader::Branch(true, decode_size(i, input, 2)?))
}
trie_constants::BRANCH_WITHOUT_MASK => {
Ok(NodeHeader::Branch(false, decode_size(i, input, 2)?))
}
trie_constants::EMPTY_TRIE => {
if i & (0b111 << 5) == trie_constants::ALT_HASHING_LEAF_PREFIX_MASK {
Ok(NodeHeader::HashedValueLeaf(decode_size(i, input, 3)?))
} else if i & (0b1111 << 4) == trie_constants::ALT_HASHING_BRANCH_WITH_MASK {
Ok(NodeHeader::HashedValueBranch(decode_size(i, input, 4)?))
} else {
// do not allow any special encoding
Err("Unallowed encoding".into())
}
}
_ => unreachable!(),
}
}
} The mask size for hashed leaves and branches are different but after digging through the scale decoder implementation it looks like it handles that internally? I think it's probably worth looking into some of the other issues but outside the scope of this PR? Maybe just have some open issues for now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
As the // Note that since the encoded branch stores the hash of the children nodes, we are not
// reconstructing the child nodes from the encoding. This function instead stubs where the
// children are known to be with an empty leaf. The children nodes hashes are then used to
// find other values using the persistent database. In another PR, it would be interesting to refactor this to load from database as it decodes, especially since you can have in-lined encoded nodes, otherwise it's rather confusing. Created #2375.
That might be the case, I opened #2374, thanks so much for your investigation!
Are you sure? Can you point to some code you've seen? @timwu20 maybe do you have a clue on this?
Totally 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It looks like that way based on this section of the header parsing code I shared above.
Leaf hash mask: Inisde of |
||
branch.Children[i] = &Leaf{ | ||
HashDigest: hash, | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.