mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 17:33:47 +00:00
consensus/ethash: add maxEpoch constant
This commit is contained in:
parent
b06e20bc7c
commit
6abad68f82
5 changed files with 23 additions and 23 deletions
|
|
@ -355,9 +355,11 @@ func hashimotoFull(dataset []uint32, hash []byte, nonce uint64) ([]byte, []byte)
|
||||||
return hashimoto(hash, nonce, uint64(len(dataset))*4, lookup)
|
return hashimoto(hash, nonce, uint64(len(dataset))*4, lookup)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const maxEpoch = 2048
|
||||||
|
|
||||||
// datasetSizes is a lookup table for the ethash dataset size for the first 2048
|
// datasetSizes is a lookup table for the ethash dataset size for the first 2048
|
||||||
// epochs (i.e. 61440000 blocks).
|
// epochs (i.e. 61440000 blocks).
|
||||||
var datasetSizes = []uint64{
|
var datasetSizes = [maxEpoch]uint64{
|
||||||
1073739904, 1082130304, 1090514816, 1098906752, 1107293056,
|
1073739904, 1082130304, 1090514816, 1098906752, 1107293056,
|
||||||
1115684224, 1124070016, 1132461952, 1140849536, 1149232768,
|
1115684224, 1124070016, 1132461952, 1140849536, 1149232768,
|
||||||
1157627776, 1166013824, 1174404736, 1182786944, 1191180416,
|
1157627776, 1166013824, 1174404736, 1182786944, 1191180416,
|
||||||
|
|
@ -771,7 +773,7 @@ var datasetSizes = []uint64{
|
||||||
|
|
||||||
// cacheSizes is a lookup table for the ethash verification cache size for the
|
// cacheSizes is a lookup table for the ethash verification cache size for the
|
||||||
// first 2048 epochs (i.e. 61440000 blocks).
|
// first 2048 epochs (i.e. 61440000 blocks).
|
||||||
var cacheSizes = []uint64{
|
var cacheSizes = [maxEpoch]uint64{
|
||||||
16776896, 16907456, 17039296, 17170112, 17301056, 17432512, 17563072,
|
16776896, 16907456, 17039296, 17170112, 17301056, 17432512, 17563072,
|
||||||
17693888, 17824192, 17955904, 18087488, 18218176, 18349504, 18481088,
|
17693888, 17824192, 17955904, 18087488, 18218176, 18349504, 18481088,
|
||||||
18611392, 18742336, 18874304, 19004224, 19135936, 19267264, 19398208,
|
18611392, 18742336, 18874304, 19004224, 19135936, 19267264, 19398208,
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ package ethash
|
||||||
func cacheSize(block uint64) uint64 {
|
func cacheSize(block uint64) uint64 {
|
||||||
// If we have a pre-generated value, use that
|
// If we have a pre-generated value, use that
|
||||||
epoch := int(block / epochLength)
|
epoch := int(block / epochLength)
|
||||||
if epoch < len(cacheSizes) {
|
if epoch < maxEpoch {
|
||||||
return cacheSizes[epoch]
|
return cacheSizes[epoch]
|
||||||
}
|
}
|
||||||
// We don't have a way to verify primes fast before Go 1.8
|
// We don't have a way to verify primes fast before Go 1.8
|
||||||
|
|
@ -39,7 +39,7 @@ func cacheSize(block uint64) uint64 {
|
||||||
func datasetSize(block uint64) uint64 {
|
func datasetSize(block uint64) uint64 {
|
||||||
// If we have a pre-generated value, use that
|
// If we have a pre-generated value, use that
|
||||||
epoch := int(block / epochLength)
|
epoch := int(block / epochLength)
|
||||||
if epoch < len(datasetSizes) {
|
if epoch < maxEpoch {
|
||||||
return datasetSizes[epoch]
|
return datasetSizes[epoch]
|
||||||
}
|
}
|
||||||
// We don't have a way to verify primes fast before Go 1.8
|
// We don't have a way to verify primes fast before Go 1.8
|
||||||
|
|
|
||||||
|
|
@ -27,10 +27,14 @@ import "math/big"
|
||||||
func cacheSize(block uint64) uint64 {
|
func cacheSize(block uint64) uint64 {
|
||||||
// If we have a pre-generated value, use that
|
// If we have a pre-generated value, use that
|
||||||
epoch := int(block / epochLength)
|
epoch := int(block / epochLength)
|
||||||
if epoch < len(cacheSizes) {
|
if epoch < maxEpoch {
|
||||||
return cacheSizes[epoch]
|
return cacheSizes[epoch]
|
||||||
}
|
}
|
||||||
// No known cache size, calculate manually (sanity branch only)
|
// No known cache size, calculate manually (sanity branch only)
|
||||||
|
return calcCacheSize(epoch)
|
||||||
|
}
|
||||||
|
|
||||||
|
func calcCacheSize(epoch int) uint64 {
|
||||||
size := cacheInitBytes + cacheGrowthBytes*uint64(epoch) - hashBytes
|
size := cacheInitBytes + cacheGrowthBytes*uint64(epoch) - hashBytes
|
||||||
for !new(big.Int).SetUint64(size / hashBytes).ProbablyPrime(1) { // Always accurate for n < 2^64
|
for !new(big.Int).SetUint64(size / hashBytes).ProbablyPrime(1) { // Always accurate for n < 2^64
|
||||||
size -= 2 * hashBytes
|
size -= 2 * hashBytes
|
||||||
|
|
@ -48,7 +52,10 @@ func datasetSize(block uint64) uint64 {
|
||||||
if epoch < len(datasetSizes) {
|
if epoch < len(datasetSizes) {
|
||||||
return datasetSizes[epoch]
|
return datasetSizes[epoch]
|
||||||
}
|
}
|
||||||
// No known dataset size, calculate manually (sanity branch only)
|
return calcDatasetSize(epoch)
|
||||||
|
}
|
||||||
|
|
||||||
|
func calcDatasetSize(epoch int) uint64 {
|
||||||
size := datasetInitBytes + datasetGrowthBytes*uint64(epoch) - mixBytes
|
size := datasetInitBytes + datasetGrowthBytes*uint64(epoch) - mixBytes
|
||||||
for !new(big.Int).SetUint64(size / mixBytes).ProbablyPrime(1) { // Always accurate for n < 2^64
|
for !new(big.Int).SetUint64(size / mixBytes).ProbablyPrime(1) { // Always accurate for n < 2^64
|
||||||
size -= 2 * mixBytes
|
size -= 2 * mixBytes
|
||||||
|
|
|
||||||
|
|
@ -23,24 +23,15 @@ import "testing"
|
||||||
// Tests whether the dataset size calculator works correctly by cross checking the
|
// Tests whether the dataset size calculator works correctly by cross checking the
|
||||||
// hard coded lookup table with the value generated by it.
|
// hard coded lookup table with the value generated by it.
|
||||||
func TestSizeCalculations(t *testing.T) {
|
func TestSizeCalculations(t *testing.T) {
|
||||||
var tests []uint64
|
// Verify all the cache and dataset sizes from the lookup table.
|
||||||
|
for epoch, want := range cacheSizes {
|
||||||
// Verify all the cache sizes from the lookup table
|
if size := calcCacheSize(epoch); size != want {
|
||||||
defer func(sizes []uint64) { cacheSizes = sizes }(cacheSizes)
|
t.Errorf("cache %d: cache size mismatch: have %d, want %d", epoch, size, want)
|
||||||
tests, cacheSizes = cacheSizes, []uint64{}
|
|
||||||
|
|
||||||
for i, test := range tests {
|
|
||||||
if size := cacheSize(uint64(i*epochLength) + 1); size != test {
|
|
||||||
t.Errorf("cache %d: cache size mismatch: have %d, want %d", i, size, test)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Verify all the dataset sizes from the lookup table
|
for epoch, want := range datasetSizes {
|
||||||
defer func(sizes []uint64) { datasetSizes = sizes }(datasetSizes)
|
if size := calcDatasetSize(epoch); size != want {
|
||||||
tests, datasetSizes = datasetSizes, []uint64{}
|
t.Errorf("dataset %d: dataset size mismatch: have %d, want %d", epoch, size, want)
|
||||||
|
|
||||||
for i, test := range tests {
|
|
||||||
if size := datasetSize(uint64(i*epochLength) + 1); size != test {
|
|
||||||
t.Errorf("dataset %d: dataset size mismatch: have %d, want %d", i, size, test)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
// Sanity check that the block number is below the lookup table size (60M blocks)
|
||||||
number := header.Number.Uint64()
|
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)
|
// Go < 1.7 cannot calculate new cache/dataset sizes (no fast prime check)
|
||||||
return errNonceOutOfRange
|
return errNonceOutOfRange
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue