Skip to content

Commit cffda4b

Browse files
authored
fix: bug under concurrent use of PieceAggregateCommP (#16)
Original code naively used a global hasher without any concurrency control
1 parent f1a31a6 commit cffda4b

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

commd.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package commp
33
import (
44
"errors"
55
"fmt"
6+
"hash"
67
"math/bits"
8+
"sync"
79

810
"github.com/filecoin-project/go-commp-utils/zerocomm"
911
commcid "github.com/filecoin-project/go-fil-commcid"
@@ -110,11 +112,14 @@ func PieceAggregateCommP(proofType abi.RegisteredSealProof, pieceInfos []abi.Pie
110112
return commcid.PieceCommitmentV1ToCID(stack[0].commP)
111113
}
112114

113-
var s256 = sha256simd.New()
115+
var s256pool = sync.Pool{New: func() any { return sha256simd.New() }}
114116

115117
func zeroCommForSize(s uint64) []byte { return zerocomm.PieceComms[bits.TrailingZeros64(s)-7][:] }
116118

117119
func reduceStack(s []stackFrame) []stackFrame {
120+
121+
s256 := s256pool.Get().(hash.Hash)
122+
118123
for len(s) > 1 && s[len(s)-2].size == s[len(s)-1].size {
119124

120125
s256.Reset()
@@ -131,5 +136,7 @@ func reduceStack(s []stackFrame) []stackFrame {
131136
s = s[:len(s)-1]
132137
}
133138

139+
s256pool.Put(s256)
140+
134141
return s
135142
}

0 commit comments

Comments
 (0)