Skip to content

Commit 5dc002f

Browse files
fix: concurrency crash fixed
1 parent 7ab329b commit 5dc002f

2 files changed

Lines changed: 6 additions & 11 deletions

File tree

dag/dag.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -924,9 +924,7 @@ func (d *Dag) IterateDag(processLeaf func(leaf *DagLeaf, parent *DagLeaf) error)
924924
}
925925

926926
childHashes := []string{}
927-
for _, childHash := range leaf.Links {
928-
childHashes = append(childHashes, childHash)
929-
}
927+
childHashes = append(childHashes, leaf.Links...)
930928

931929
sort.Slice(childHashes, func(i, j int) bool {
932930
numI, _ := strconv.Atoi(strings.Split(childHashes[i], ":")[0])

merkletree/merkletree.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,13 @@ type Proof struct {
149149
Path uint32 // Path variable indicating whether the neighbor is on the left or right.
150150
}
151151

152-
// sha256Digest is the reusable digest for DefaultHashFunc.
153-
// It is used to avoid creating a new hash digest for every call to DefaultHashFunc.
154-
var sha256Digest = sha256.New()
155-
156152
// DefaultHashFunc is the default hash function used when no user-specified hash function is provided.
157-
// It implements the SHA256 hash function and reuses sha256Digest to reduce memory allocations.
153+
// It implements the SHA256 hash function and creates a new hash digest for each call to ensure
154+
// thread-safety in concurrent environments.
158155
func DefaultHashFunc(data []byte) ([]byte, error) {
159-
defer sha256Digest.Reset()
160-
sha256Digest.Write(data)
161-
return sha256Digest.Sum(make([]byte, 0, sha256Digest.Size())), nil
156+
digest := sha256.New()
157+
digest.Write(data)
158+
return digest.Sum(make([]byte, 0, digest.Size())), nil
162159
}
163160

164161
// DefaultHashFuncParallel is the default hash function used by parallel algorithms when no user-specified

0 commit comments

Comments
 (0)