Skip to content

Commit af488b4

Browse files
blindchaseryzang2019
authored andcommitted
fix: lthash worker loop break; remove unreachable digest.Read fallback (#2698)
## Describe your changes and provide context - Since `start := w * chunkSize` is monotonic, we switched both loops from continue to break to avoid unnecessary iterations. - remove unreachable digest.Read fallback ## Testing performed to validate your change
1 parent b576e84 commit af488b4

2 files changed

Lines changed: 3 additions & 30 deletions

File tree

sei-db/state_db/sc/flatkv/lthash/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func computeDelta(kvPairs []KVPairWithLastValue, numWorkers int) (*LtHash, *LtHa
9999
for w := 0; w < numWorkers; w++ {
100100
start := w * chunkSize
101101
if start >= len(kvPairs) {
102-
continue
102+
break
103103
}
104104
end := start + chunkSize
105105
if end > len(kvPairs) {
@@ -130,7 +130,7 @@ func computeDelta(kvPairs []KVPairWithLastValue, numWorkers int) (*LtHash, *LtHa
130130
for w := 0; w < numWorkers; w++ {
131131
start := w * chunkSize
132132
if start >= len(kvPairs) {
133-
continue
133+
break
134134
}
135135
end := start + chunkSize
136136
if end > len(kvPairs) {

sei-db/state_db/sc/flatkv/lthash/lthash.go

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,9 @@ func hash(data []byte) *LtHash {
137137

138138
bufPtr := xofBufferPool.Get().(*[]byte)
139139
output := *bufPtr
140-
n, err := digest.Read(output)
140+
_, _ = digest.Read(output) // Blake3 XOF never errors and always fills buffer
141141
blake3HasherPool.Put(hasher)
142142

143-
if err != nil || n != LtHashBytes {
144-
xofBufferPool.Put(bufPtr)
145-
h := blake3.Sum256(data)
146-
output = extendTo2048Bytes(h[:])
147-
148-
lth := ltHashPool.Get().(*LtHash)
149-
for i := 0; i < LtHashSize; i++ {
150-
lth.limbs[i] = binary.LittleEndian.Uint16(output[i*2:])
151-
}
152-
return lth
153-
}
154-
155143
lth := ltHashPool.Get().(*LtHash)
156144
for i := 0; i < LtHashSize; i++ {
157145
lth.limbs[i] = binary.LittleEndian.Uint16(output[i*2 : (i+1)*2])
@@ -224,18 +212,3 @@ func putLtHashToPool(lth *LtHash) {
224212
ltHashPool.Put(lth)
225213
}
226214
}
227-
228-
func extendTo2048Bytes(seed []byte) []byte {
229-
result := make([]byte, LtHashBytes)
230-
copy(result[:32], seed)
231-
for i := 32; i < LtHashBytes; i += 32 {
232-
chunk := result[i-32 : i]
233-
h := blake3.Sum256(chunk)
234-
copy(result[i:], h[:])
235-
if i+32 > LtHashBytes {
236-
copy(result[i:], h[:LtHashBytes-i])
237-
break
238-
}
239-
}
240-
return result
241-
}

0 commit comments

Comments
 (0)