Skip to content

Commit bbacca8

Browse files
committed
chore: use errgroup
1 parent 00e0cf4 commit bbacca8

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

datasquare.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"math"
77
"sync"
8+
9+
"golang.org/x/sync/errgroup"
810
)
911

1012
// ErrUnevenChunks is thrown when non-nil chunks are not all of equal size.
@@ -187,37 +189,41 @@ func (ds *dataSquare) resetRoots() {
187189
}
188190
}
189191

190-
func (ds *dataSquare) computeRoots() {
191-
var wg sync.WaitGroup
192+
func (ds *dataSquare) computeRoots() error {
193+
g := new(errgroup.Group)
192194

193195
rowRoots := make([][]byte, ds.width)
194196
colRoots := make([][]byte, ds.width)
195197

196198
for i := uint(0); i < ds.width; i++ {
197-
wg.Add(2)
198-
199-
go func(i uint) {
200-
defer wg.Done()
199+
i := i // https://go.dev/doc/faq#closures_and_goroutines
200+
g.Go(func() error {
201201
rowRoot, err := ds.getRowRoot(i)
202202
if err != nil {
203-
panic(err)
203+
return err
204204
}
205205
rowRoots[i] = rowRoot
206-
}(i)
206+
return nil
207+
})
207208

208-
go func(i uint) {
209-
defer wg.Done()
209+
g.Go(func() error {
210210
colRoot, err := ds.getColRoot(i)
211211
if err != nil {
212-
panic(err)
212+
return err
213213
}
214214
colRoots[i] = colRoot
215-
}(i)
215+
return nil
216+
})
217+
}
218+
219+
err := g.Wait()
220+
if err != nil {
221+
return err
216222
}
217223

218-
wg.Wait()
219224
ds.rowRoots = rowRoots
220225
ds.colRoots = colRoots
226+
return nil
221227
}
222228

223229
// getRowRoots returns the Merkle roots of all the rows in the square.

datasquare_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ func TestLazyRootGeneration(t *testing.T) {
146146
colRoots = append(colRoots, colRoot)
147147
}
148148

149-
square.computeRoots()
149+
err = square.computeRoots()
150+
assert.NoError(t, err)
150151

151152
if !reflect.DeepEqual(square.rowRoots, rowRoots) && !reflect.DeepEqual(square.colRoots, colRoots) {
152153
t.Error("getRowRoot or getColRoot did not produce identical roots to computeRoots")
@@ -213,7 +214,8 @@ func BenchmarkEDSRoots(b *testing.B) {
213214
func(b *testing.B) {
214215
for n := 0; n < b.N; n++ {
215216
square.resetRoots()
216-
square.computeRoots()
217+
err := square.computeRoots()
218+
assert.NoError(b, err)
217219
}
218220
},
219221
)

0 commit comments

Comments
 (0)