Skip to content

Commit

Permalink
trie: fixes to also support storage tries (#16)
Browse files Browse the repository at this point in the history
* fix issues when using storage trie

* bugfix: 0 as first key byte would produce invalid results
  • Loading branch information
gballet committed May 6, 2020
1 parent f4b7ec6 commit ddda96e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions trie/stacktrie.go
Expand Up @@ -314,7 +314,7 @@ func (st *ReStackTrie) getDiffIndex(key []byte) int {
func (st *ReStackTrie) insert(key, value []byte) {
switch st.nodeType {
case branchNode: /* Branch */
idx := key[st.keyOffset]
idx := int(key[st.keyOffset])
if st.children[idx] == nil {
st.children[idx] = NewReStackTrie()
st.children[idx].keyOffset = st.keyOffset + 1
Expand Down Expand Up @@ -514,6 +514,11 @@ func writeHPRLP(writer io.Writer, key, val []byte, leaf bool) {
// value part will be two bytes as the leaf is more than 56 bytes
// long.
valHeaderLen := 1
if len(val) == 1 && val[0] < 128 {
// Don't reserve space for the header if this
// is an integer < 128
valHeaderLen = 0
}
if len(val) > 56 {
valHeaderLen = 2
}
Expand Down Expand Up @@ -550,9 +555,10 @@ func writeHPRLP(writer io.Writer, key, val []byte, leaf bool) {
}
}

if leaf {
// Write the RLP prefix to the value if needed
if len(val) > 56 {
writer.Write([]byte{0xb8, byte(len(val))})
} else {
} else if len(val) > 1 || val[0] >= 128 {
writer.Write([]byte{0x80 + byte(len(val))})
}
writer.Write(val)
Expand Down

0 comments on commit ddda96e

Please sign in to comment.