diff --git a/lib/trie/hash.go b/lib/trie/hash.go index f798f594b8d..2030e3906ad 100644 --- a/lib/trie/hash.go +++ b/lib/trie/hash.go @@ -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}) @@ -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 { @@ -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 }