Skip to content
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

trie: make stacktrie not mutate input values #22673

Merged
merged 2 commits into from
Apr 16, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
trie: make stacktrie not mutate input values
  • Loading branch information
holiman committed Apr 15, 2021
commit 6179dfaa9f10dedafc222cf3124c828bdca58e44
16 changes: 5 additions & 11 deletions trie/stacktrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,7 @@ func (st *StackTrie) hash() {
panic(err)
}
case emptyNode:
st.val = st.val[:0]
st.val = append(st.val, emptyRoot[:]...)
st.val = emptyRoot.Bytes()
st.key = st.key[:0]
st.nodeType = hashedNode
return
Expand All @@ -357,17 +356,12 @@ func (st *StackTrie) hash() {
st.key = st.key[:0]
st.nodeType = hashedNode
if len(h.tmp) < 32 {
st.val = st.val[:0]
st.val = append(st.val, h.tmp...)
st.val = common.CopyBytes(h.tmp)
return
}
// Going to write the hash to the 'val'. Need to ensure it's properly sized first
// Typically, 'branchNode's will have no 'val', and require this allocation
if required := 32 - len(st.val); required > 0 {
buf := make([]byte, required)
st.val = append(st.val, buf...)
}
st.val = st.val[:32]
// Write the hash to the 'val'. We allocate a new val here to not mutate
// input values
st.val = make([]byte, 32)
h.sha.Reset()
h.sha.Write(h.tmp)
h.sha.Read(st.val)
Expand Down