lru.get¶
| Source (upstream pre-purge) | Current | |
|---|---|---|
| File | consensus/ethash/ethash.go |
ethash.go |
| Symbol | lru.get |
lru.get |
| Ref | dde2da0ef~1 |
etc/v1.17.3-full-node |
ECIP-1099 epoch length parameter added
3-way merge — purge → getc ← upstream¶
pre-purgecore-geth
↗// get retrieves or creates an item for the given epoch. The first return value is always
// non-nil. The second return value is non-nil if lru thinks that an item will be useful in
// the near future.
↗func (lru *lru[T]) get(epoch uint64, epochLength uint64, ecip1099FBlock *uint64) (item, future T) {
↗ lru.mu.Lock()
defer lru.mu.Unlock()
↗ // Use the sum of epoch and epochLength as the cache key.
// This is not perfectly safe, but it's good enough (at least for the first 30000 epochs, or the first 427 years).
cacheKey := epochLength + epoch
↗ // Get or create the item for the requested epoch.
↗ item, ok := lru.cache.Get(cacheKey)
↗ if !ok {
if lru.future > 0 && lru.future == epoch {
item = lru.futureItem
} else {
log.Trace("Requiring new ethash "+lru.what, "epoch", epoch)
↗ item = lru.new(epoch, epochLength)
↗ }
↗ lru.cache.Add(cacheKey, item)
↗ }
↗ // Ensure pre-generation handles ecip-1099 changeover correctly
var nextEpoch = epoch + 1
var nextEpochLength = epochLength
if ecip1099FBlock != nil {
nextEpochBlock := nextEpoch * epochLength
// Note that == demands that the ECIP1099 activation block is situated
// at the beginning of an epoch.
// https://github.com/ethereumclassic/ECIPs/blob/master/_specs/ecip-1099.md#implementation
if nextEpochBlock == *ecip1099FBlock && epochLength == epochLengthDefault {
nextEpoch = nextEpoch / 2
nextEpochLength = epochLengthECIP1099
}
}
↗ // Update the 'future item' if epoch is larger than previously seen.
↗ // Last conditional clause ('lru.future > nextEpoch') handles the ECIP1099 case where
// the next epoch is expected to be LESSER THAN that of the previous state's future epoch number.
if epoch < maxEpoch-1 && lru.future != nextEpoch {
log.Trace("Requiring new future ethash "+lru.what, "epoch", nextEpoch)
future = lru.new(nextEpoch, nextEpochLength)
lru.future = nextEpoch
↗ lru.futureItem = future
}
return item, future
}