Skip to content

Commit 2d1cbed

Browse files
s7v7nislandsshekhirin
authored andcommitted
consensus/ethash: use atomic type (ethereum#27068)
1 parent d6a04de commit 2d1cbed

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

consensus/ethash/algorithm.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) {
163163
rows := int(size) / hashBytes
164164

165165
// Start a monitoring goroutine to report progress on low end devices
166-
var progress uint32
166+
var progress atomic.Uint32
167167

168168
done := make(chan struct{})
169169
defer close(done)
@@ -174,7 +174,7 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) {
174174
case <-done:
175175
return
176176
case <-time.After(3 * time.Second):
177-
logger.Info("Generating ethash verification cache", "percentage", atomic.LoadUint32(&progress)*100/uint32(rows)/(cacheRounds+1), "elapsed", common.PrettyDuration(time.Since(start)))
177+
logger.Info("Generating ethash verification cache", "percentage", progress.Load()*100/uint32(rows)/(cacheRounds+1), "elapsed", common.PrettyDuration(time.Since(start)))
178178
}
179179
}
180180
}()
@@ -185,7 +185,7 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) {
185185
keccak512(cache, seed)
186186
for offset := uint64(hashBytes); offset < size; offset += hashBytes {
187187
keccak512(cache[offset:], cache[offset-hashBytes:offset])
188-
atomic.AddUint32(&progress, 1)
188+
progress.Add(1)
189189
}
190190
// Use a low-round version of randmemohash
191191
temp := make([]byte, hashBytes)
@@ -200,7 +200,7 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) {
200200
bitutil.XORBytes(temp, cache[srcOff:srcOff+hashBytes], cache[xorOff:xorOff+hashBytes])
201201
keccak512(cache[dstOff:], temp)
202202

203-
atomic.AddUint32(&progress, 1)
203+
progress.Add(1)
204204
}
205205
}
206206
// Swap the byte order on big endian systems and return
@@ -299,7 +299,7 @@ func generateDataset(dest []uint32, epoch uint64, cache []uint32) {
299299
var pend sync.WaitGroup
300300
pend.Add(threads)
301301

302-
var progress uint64
302+
var progress atomic.Uint64
303303
for i := 0; i < threads; i++ {
304304
go func(id int) {
305305
defer pend.Done()
@@ -323,7 +323,7 @@ func generateDataset(dest []uint32, epoch uint64, cache []uint32) {
323323
}
324324
copy(dataset[index*hashBytes:], item)
325325

326-
if status := atomic.AddUint64(&progress, 1); status%percent == 0 {
326+
if status := progress.Add(1); status%percent == 0 {
327327
logger.Info("Generating DAG in progress", "percentage", (status*100)/(size/hashBytes), "elapsed", common.PrettyDuration(time.Since(start)))
328328
}
329329
}

consensus/ethash/ethash.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,12 @@ func (c *cache) finalizer() {
308308

309309
// dataset wraps an ethash dataset with some metadata to allow easier concurrent use.
310310
type dataset struct {
311-
epoch uint64 // Epoch for which this cache is relevant
312-
dump *os.File // File descriptor of the memory mapped cache
313-
mmap mmap.MMap // Memory map itself to unmap before releasing
314-
dataset []uint32 // The actual cache data content
315-
once sync.Once // Ensures the cache is generated only once
316-
done uint32 // Atomic flag to determine generation status
311+
epoch uint64 // Epoch for which this cache is relevant
312+
dump *os.File // File descriptor of the memory mapped cache
313+
mmap mmap.MMap // Memory map itself to unmap before releasing
314+
dataset []uint32 // The actual cache data content
315+
once sync.Once // Ensures the cache is generated only once
316+
done atomic.Bool // Atomic flag to determine generation status
317317
}
318318

319319
// newDataset creates a new ethash mining dataset and returns it as a plain Go
@@ -326,7 +326,7 @@ func newDataset(epoch uint64) *dataset {
326326
func (d *dataset) generate(dir string, limit int, lock bool, test bool) {
327327
d.once.Do(func() {
328328
// Mark the dataset generated after we're done. This is needed for remote
329-
defer atomic.StoreUint32(&d.done, 1)
329+
defer d.done.Store(true)
330330

331331
csize := cacheSize(d.epoch*epochLength + 1)
332332
dsize := datasetSize(d.epoch*epochLength + 1)
@@ -390,7 +390,7 @@ func (d *dataset) generate(dir string, limit int, lock bool, test bool) {
390390
// or not (it may not have been started at all). This is useful for remote miners
391391
// to default to verification caches instead of blocking on DAG generations.
392392
func (d *dataset) generated() bool {
393-
return atomic.LoadUint32(&d.done) == 1
393+
return d.done.Load()
394394
}
395395

396396
// finalizer closes any file handlers and memory maps open.

0 commit comments

Comments
 (0)