aboutsummaryrefslogtreecommitdiffstats
path: root/consensus/ethash/consensus.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@users.noreply.github.com>2018-01-23 18:05:30 +0800
committerPéter Szilágyi <peterke@gmail.com>2018-01-23 18:05:30 +0800
commit924065e19d08cc7e6af0b3a5b5b1ef3785b79bd4 (patch)
tree9aefabb6b4c375971570555486ce5b172660661d /consensus/ethash/consensus.go
parent5d4267911a7791bfa60f275a97347372fbf0ce99 (diff)
downloaddexon-924065e19d08cc7e6af0b3a5b5b1ef3785b79bd4.tar.gz
dexon-924065e19d08cc7e6af0b3a5b5b1ef3785b79bd4.tar.zst
dexon-924065e19d08cc7e6af0b3a5b5b1ef3785b79bd4.zip
consensus/ethash: improve cache/dataset handling (#15864)
* consensus/ethash: add maxEpoch constant * consensus/ethash: improve cache/dataset handling There are two fixes in this commit: Unmap the memory through a finalizer like the libethash wrapper did. The release logic was incorrect and freed the memory while it was being used, leading to crashes like in #14495 or #14943. Track caches and datasets using simplelru instead of reinventing LRU logic. This should make it easier to see whether it's correct. * consensus/ethash: restore 'future item' logic in lru * consensus/ethash: use mmap even in test mode This makes it possible to shorten the time taken for TestCacheFileEvict. * consensus/ethash: shuffle func calc*Size comments around * consensus/ethash: ensure future cache/dataset is in the lru cache * consensus/ethash: add issue link to the new test * consensus/ethash: fix vet * consensus/ethash: fix test * consensus: tiny issue + nitpick fixes
Diffstat (limited to 'consensus/ethash/consensus.go')
-rw-r--r--consensus/ethash/consensus.go10
1 files changed, 7 insertions, 3 deletions
diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go
index 82d23c92b..92a23d4a4 100644
--- a/consensus/ethash/consensus.go
+++ b/consensus/ethash/consensus.go
@@ -476,7 +476,7 @@ func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Head
}
// Sanity check that the block number is below the lookup table size (60M blocks)
number := header.Number.Uint64()
- if number/epochLength >= uint64(len(cacheSizes)) {
+ if number/epochLength >= maxEpoch {
// Go < 1.7 cannot calculate new cache/dataset sizes (no fast prime check)
return errNonceOutOfRange
}
@@ -484,14 +484,18 @@ func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Head
if header.Difficulty.Sign() <= 0 {
return errInvalidDifficulty
}
+
// Recompute the digest and PoW value and verify against the header
cache := ethash.cache(number)
-
size := datasetSize(number)
if ethash.config.PowMode == ModeTest {
size = 32 * 1024
}
- digest, result := hashimotoLight(size, cache, header.HashNoNonce().Bytes(), header.Nonce.Uint64())
+ digest, result := hashimotoLight(size, cache.cache, header.HashNoNonce().Bytes(), header.Nonce.Uint64())
+ // Caches are unmapped in a finalizer. Ensure that the cache stays live
+ // until after the call to hashimotoLight so it's not unmapped while being used.
+ runtime.KeepAlive(cache)
+
if !bytes.Equal(header.MixDigest[:], digest) {
return errInvalidMixDigest
}