Skip to content

Commit 66da893

Browse files
committed
xhash/asert: add mutex on initASERTAnchor
1 parent b52870f commit 66da893

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

consensus/xhash/consensus.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ func (xhash *XHash) CalcDifficulty(chain consensus.ChainHeaderReader, time uint6
304304
asertActivationHeight := xcfg.AsertActivationHeight
305305
asertAnchorHeight := asertActivationHeight - 1
306306

307+
// Protect anchor cache with a mutex. CalcDifficulty can be called concurrently
308+
xhash.asertMu.Lock()
307309
if !xhash.asertAnchorInit {
308310
// First ASERT use: init anchor from current chain view
309311
xhash.initASERTAnchor(chain, asertAnchorHeight, parent)
@@ -315,6 +317,7 @@ func (xhash *XHash) CalcDifficulty(chain consensus.ChainHeaderReader, time uint6
315317
xhash.initASERTAnchor(chain, asertAnchorHeight, parent)
316318
}
317319
}
320+
xhash.asertMu.Unlock()
318321

319322
evalHeight := int64(parent.Number.Uint64())
320323
evalTime := int64(parent.Time)

consensus/xhash/xhash.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ type XHash struct {
446446
lock sync.Mutex // Ensures thread safety for the in-memory caches and mining fields
447447
closeOnce sync.Once // Ensures exit channel will not be closed twice.
448448

449+
// ASERT anchor cache (protected by asertMu)
450+
asertMu sync.Mutex
449451
asertAnchorInit bool
450452
asertAnchorHeight int64
451453
asertAnchorParentTime int64

0 commit comments

Comments
 (0)