Skip to content

Commit

Permalink
chore(lib/trie): Entries implementation writes to map passed as arg…
Browse files Browse the repository at this point in the history
…ument
  • Loading branch information
qdm12 committed Nov 4, 2022
1 parent 5b13219 commit 918435f
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,36 +208,36 @@ func (t *Trie) Hash() (rootHash common.Hash, err error) {

// Entries returns all the key-value pairs in the trie as a map of keys to values
// where the keys are encoded in Little Endian.
func (t *Trie) Entries() map[string][]byte {
return entries(t.root, nil, make(map[string][]byte))
func (t *Trie) Entries() (keyValueMap map[string][]byte) {
keyValueMap = make(map[string][]byte)
entries(t.root, nil, keyValueMap)
return keyValueMap
}

func entries(parent *Node, prefix []byte, kv map[string][]byte) map[string][]byte {
func entries(parent *Node, prefix []byte, kv map[string][]byte) {
if parent == nil {
return kv
return
}

if parent.Kind() == node.Leaf {
parentKey := parent.Key
fullKeyNibbles := concatenateSlices(prefix, parentKey)
keyLE := string(codec.NibblesToKeyLE(fullKeyNibbles))
kv[keyLE] = parent.SubValue
return kv
return
}

branch := parent
if branch.SubValue != nil {
fullKeyNibbles := concatenateSlices(prefix, branch.Key)
keyLE := string(codec.NibblesToKeyLE(fullKeyNibbles))
kv[keyLE] = branch.SubValue
kv[keyLE] = parent.SubValue
}

for i, child := range branch.Children {
childPrefix := concatenateSlices(prefix, branch.Key, intToByteSlice(i))
entries(child, childPrefix, kv)
}

return kv
}

// NextKey returns the next key in the trie in lexicographic order.
Expand Down

0 comments on commit 918435f

Please sign in to comment.