Skip to content

Commit

Permalink
Write leaf encoding field in encodeLeaf
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Nov 22, 2021
1 parent ba58bd5 commit 42f83f1
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/trie/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ func encodeNode(n node, buffer *bytes.Buffer, parallel bool) (err error) {
if err != nil {
return fmt.Errorf("cannot encode leaf: %w", err)
}

n.encodingMu.Lock()
defer n.encodingMu.Unlock()

n.encoding = make([]byte, buffer.Len())
copy(n.encoding, buffer.Bytes())
return nil
case nil:
buffer.Write([]byte{0})
Expand Down Expand Up @@ -273,7 +267,7 @@ func encodeChild(child node, buffer io.Writer) (err error) {

// encodeLeaf encodes a leaf to the buffer given, with the encoding
// specified at the top of this package.
func encodeLeaf(l *leaf, buffer io.Writer) (err error) {
func encodeLeaf(l *leaf, buffer *bytes.Buffer) (err error) {
l.encodingMu.RLock()
defer l.encodingMu.RUnlock()
if !l.dirty && l.encoding != nil {
Expand Down Expand Up @@ -309,5 +303,11 @@ func encodeLeaf(l *leaf, buffer io.Writer) (err error) {
return fmt.Errorf("cannot write scale encoded value to buffer: %w", err)
}

l.encodingMu.Lock()
defer l.encodingMu.Unlock()

l.encoding = make([]byte, buffer.Len())
copy(l.encoding, buffer.Bytes())

return nil
}

0 comments on commit 42f83f1

Please sign in to comment.