@@ -34,11 +34,11 @@ import (
3434 "unsafe"
3535
3636 "github.com/edsrzf/mmap-go"
37+ lrupkg "github.com/ethereum/go-ethereum/common/lru"
3738 "github.com/ethereum/go-ethereum/consensus"
3839 "github.com/ethereum/go-ethereum/log"
3940 "github.com/ethereum/go-ethereum/metrics"
4041 "github.com/ethereum/go-ethereum/rpc"
41- "github.com/hashicorp/golang-lru/simplelru"
4242)
4343
4444var ErrInvalidDumpMagic = errors .New ("invalid dump magic" )
@@ -165,34 +165,45 @@ func memoryMapAndGenerate(path string, size uint64, lock bool, generator func(bu
165165 return memoryMap (path , lock )
166166}
167167
168+ type cacheOrDataset interface {
169+ * cache | * dataset
170+ }
171+
168172// lru tracks caches or datasets by their last use time, keeping at most N of them.
169- type lru struct {
173+ type lru [ T cacheOrDataset ] struct {
170174 what string
171- new func (epoch uint64 ) interface {}
175+ new func (epoch uint64 ) T
172176 mu sync.Mutex
173177 // Items are kept in a LRU cache, but there is a special case:
174178 // We always keep an item for (highest seen epoch) + 1 as the 'future item'.
175- cache * simplelru. LRU
179+ cache lrupkg. BasicLRU [ uint64 , T ]
176180 future uint64
177- futureItem interface {}
181+ futureItem T
178182}
179183
180184// newlru create a new least-recently-used cache for either the verification caches
181185// or the mining datasets.
182- func newlru (what string , maxItems int , new func (epoch uint64 ) interface {}) * lru {
183- if maxItems <= 0 {
184- maxItems = 1
186+ func newlru [T cacheOrDataset ](maxItems int , new func (epoch uint64 ) T ) * lru [T ] {
187+ var what string
188+ switch any (T (nil )).(type ) {
189+ case * cache :
190+ what = "cache"
191+ case * dataset :
192+ what = "dataset"
193+ default :
194+ panic ("unknown type" )
195+ }
196+ return & lru [T ]{
197+ what : what ,
198+ new : new ,
199+ cache : lrupkg.NewBasicLRU [uint64 , T ](maxItems ),
185200 }
186- cache , _ := simplelru .NewLRU (maxItems , func (key , value interface {}) {
187- log .Trace ("Evicted ethash " + what , "epoch" , key )
188- })
189- return & lru {what : what , new : new , cache : cache }
190201}
191202
192203// get retrieves or creates an item for the given epoch. The first return value is always
193204// non-nil. The second return value is non-nil if lru thinks that an item will be useful in
194205// the near future.
195- func (lru * lru ) get (epoch uint64 ) (item , future interface {} ) {
206+ func (lru * lru [ T ] ) get (epoch uint64 ) (item , future T ) {
196207 lru .mu .Lock ()
197208 defer lru .mu .Unlock ()
198209
@@ -226,9 +237,8 @@ type cache struct {
226237 once sync.Once // Ensures the cache is generated only once
227238}
228239
229- // newCache creates a new ethash verification cache and returns it as a plain Go
230- // interface to be usable in an LRU cache.
231- func newCache (epoch uint64 ) interface {} {
240+ // newCache creates a new ethash verification cache.
241+ func newCache (epoch uint64 ) * cache {
232242 return & cache {epoch : epoch }
233243}
234244
@@ -308,7 +318,7 @@ type dataset struct {
308318
309319// newDataset creates a new ethash mining dataset and returns it as a plain Go
310320// interface to be usable in an LRU cache.
311- func newDataset (epoch uint64 ) interface {} {
321+ func newDataset (epoch uint64 ) * dataset {
312322 return & dataset {epoch : epoch }
313323}
314324
@@ -439,8 +449,8 @@ type Config struct {
439449type Ethash struct {
440450 config Config
441451
442- caches * lru // In memory caches to avoid regenerating too often
443- datasets * lru // In memory datasets to avoid regenerating too often
452+ caches * lru [ * cache ] // In memory caches to avoid regenerating too often
453+ datasets * lru [ * dataset ] // In memory datasets to avoid regenerating too often
444454
445455 // Mining related fields
446456 rand * rand.Rand // Properly seeded random source for nonces
@@ -477,8 +487,8 @@ func New(config Config, notify []string, noverify bool) *Ethash {
477487 }
478488 ethash := & Ethash {
479489 config : config ,
480- caches : newlru ("cache" , config .CachesInMem , newCache ),
481- datasets : newlru ("dataset" , config .DatasetsInMem , newDataset ),
490+ caches : newlru (config .CachesInMem , newCache ),
491+ datasets : newlru (config .DatasetsInMem , newDataset ),
482492 update : make (chan struct {}),
483493 hashrate : metrics .NewMeterForced (),
484494 }
@@ -573,15 +583,13 @@ func (ethash *Ethash) StopRemoteSealer() error {
573583// stored on disk, and finally generating one if none can be found.
574584func (ethash * Ethash ) cache (block uint64 ) * cache {
575585 epoch := block / epochLength
576- currentI , futureI := ethash .caches .get (epoch )
577- current := currentI .(* cache )
586+ current , future := ethash .caches .get (epoch )
578587
579588 // Wait for generation finish.
580589 current .generate (ethash .config .CacheDir , ethash .config .CachesOnDisk , ethash .config .CachesLockMmap , ethash .config .PowMode == ModeTest )
581590
582591 // If we need a new future cache, now's a good time to regenerate it.
583- if futureI != nil {
584- future := futureI .(* cache )
592+ if future != nil {
585593 go future .generate (ethash .config .CacheDir , ethash .config .CachesOnDisk , ethash .config .CachesLockMmap , ethash .config .PowMode == ModeTest )
586594 }
587595 return current
@@ -596,25 +604,20 @@ func (ethash *Ethash) cache(block uint64) *cache {
596604func (ethash * Ethash ) dataset (block uint64 , async bool ) * dataset {
597605 // Retrieve the requested ethash dataset
598606 epoch := block / epochLength
599- currentI , futureI := ethash .datasets .get (epoch )
600- current := currentI .(* dataset )
607+ current , future := ethash .datasets .get (epoch )
601608
602609 // If async is specified, generate everything in a background thread
603610 if async && ! current .generated () {
604611 go func () {
605612 current .generate (ethash .config .DatasetDir , ethash .config .DatasetsOnDisk , ethash .config .DatasetsLockMmap , ethash .config .PowMode == ModeTest )
606-
607- if futureI != nil {
608- future := futureI .(* dataset )
613+ if future != nil {
609614 future .generate (ethash .config .DatasetDir , ethash .config .DatasetsOnDisk , ethash .config .DatasetsLockMmap , ethash .config .PowMode == ModeTest )
610615 }
611616 }()
612617 } else {
613618 // Either blocking generation was requested, or already done
614619 current .generate (ethash .config .DatasetDir , ethash .config .DatasetsOnDisk , ethash .config .DatasetsLockMmap , ethash .config .PowMode == ModeTest )
615-
616- if futureI != nil {
617- future := futureI .(* dataset )
620+ if future != nil {
618621 go future .generate (ethash .config .DatasetDir , ethash .config .DatasetsOnDisk , ethash .config .DatasetsLockMmap , ethash .config .PowMode == ModeTest )
619622 }
620623 }
0 commit comments