Skip to content

Commit df72e20

Browse files
karalabefjl
authored andcommitted
pow: only support prime calculations on Go 1.8 and above
1 parent 023670f commit df72e20

6 files changed

Lines changed: 157 additions & 63 deletions

File tree

‎pow/ethash.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
)
3636

3737
var (
38+
ErrNonceOutOfRange = errors.New("nonce out of range")
3839
ErrInvalidDifficulty = errors.New("non-positive difficulty")
3940
ErrInvalidMixDigest = errors.New("invalid mix digest")
4041
ErrInvalidPoW = errors.New("pow difficulty invalid")
@@ -174,13 +175,18 @@ func NewSharedEthash() PoW {
174175
// Verify implements PoW, checking whether the given block satisfies the PoW
175176
// difficulty requirements.
176177
func (ethash *Ethash) Verify(block Block) error {
178+
// Sanity check that the block number is below the lookup table size (60M blocks)
179+
number := block.NumberU64()
180+
if number/epochLength >= uint64(len(cacheSizes)) {
181+
// Go < 1.7 cannot calculate new cache/dataset sizes (no fast prime check)
182+
return ErrNonceOutOfRange
183+
}
177184
// Ensure twe have a valid difficulty for the block
178185
difficulty := block.Difficulty()
179186
if difficulty.Sign() <= 0 {
180187
return ErrInvalidDifficulty
181188
}
182189
// Recompute the digest and PoW value and verify against the block
183-
number := block.NumberU64()
184190
cache := ethash.cache(number)
185191

186192
size := datasetSize(number)

‎pow/ethash_algo.go‎

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package pow
1919
import (
2020
"encoding/binary"
2121
"io"
22-
"math/big"
2322
"runtime"
2423
"sync"
2524
"sync/atomic"
@@ -45,42 +44,6 @@ const (
4544
loopAccesses = 64 // Number of accesses in hashimoto loop
4645
)
4746

48-
// cacheSize calculates and returns the size of the ethash verification cache that
49-
// belongs to a certain block number. The cache size grows linearly, however, we
50-
// always take the highest prime below the linearly growing threshold in order to
51-
// reduce the risk of accidental regularities leading to cyclic behavior.
52-
func cacheSize(block uint64) uint64 {
53-
// If we have a pre-generated value, use that
54-
epoch := int(block / epochLength)
55-
if epoch < len(cacheSizes) {
56-
return cacheSizes[epoch]
57-
}
58-
// No known cache size, calculate manually (sanity branch only)
59-
size := uint64(cacheInitBytes + cacheGrowthBytes*epoch - hashBytes)
60-
for !new(big.Int).SetUint64(size / hashBytes).ProbablyPrime(1) { // Always accurate for n < 2^64
61-
size -= 2 * hashBytes
62-
}
63-
return size
64-
}
65-
66-
// datasetSize calculates and returns the size of the ethash mining dataset that
67-
// belongs to a certain block number. The dataset size grows linearly, however, we
68-
// always take the highest prime below the linearly growing threshold in order to
69-
// reduce the risk of accidental regularities leading to cyclic behavior.
70-
func datasetSize(block uint64) uint64 {
71-
// If we have a pre-generated value, use that
72-
epoch := int(block / epochLength)
73-
if epoch < len(datasetSizes) {
74-
return datasetSizes[epoch]
75-
}
76-
// No known dataset size, calculate manually (sanity branch only)
77-
size := uint64(datasetInitBytes + datasetGrowthBytes*epoch - mixBytes)
78-
for !new(big.Int).SetUint64(size / mixBytes).ProbablyPrime(1) { // Always accurate for n < 2^64
79-
size -= 2 * mixBytes
80-
}
81-
return size
82-
}
83-
8447
// seedHash is the seed to use for generating a verification cache and the mining
8548
// dataset.
8649
func seedHash(block uint64) []byte {

‎pow/ethash_algo_go1.7.go‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2017 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// +build !go1.8
18+
19+
package pow
20+
21+
// cacheSize calculates and returns the size of the ethash verification cache that
22+
// belongs to a certain block number. The cache size grows linearly, however, we
23+
// always take the highest prime below the linearly growing threshold in order to
24+
// reduce the risk of accidental regularities leading to cyclic behavior.
25+
func cacheSize(block uint64) uint64 {
26+
// If we have a pre-generated value, use that
27+
epoch := int(block / epochLength)
28+
if epoch < len(cacheSizes) {
29+
return cacheSizes[epoch]
30+
}
31+
// We don't have a way to verify primes fast before Go 1.8
32+
panic("fast prime testing unsupported in Go < 1.8")
33+
}
34+
35+
// datasetSize calculates and returns the size of the ethash mining dataset that
36+
// belongs to a certain block number. The dataset size grows linearly, however, we
37+
// always take the highest prime below the linearly growing threshold in order to
38+
// reduce the risk of accidental regularities leading to cyclic behavior.
39+
func datasetSize(block uint64) uint64 {
40+
// If we have a pre-generated value, use that
41+
epoch := int(block / epochLength)
42+
if epoch < len(datasetSizes) {
43+
return datasetSizes[epoch]
44+
}
45+
// We don't have a way to verify primes fast before Go 1.8
46+
panic("fast prime testing unsupported in Go < 1.8")
47+
}

‎pow/ethash_algo_go1.8.go‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2017 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// +build go1.8
18+
19+
package pow
20+
21+
import "math/big"
22+
23+
// cacheSize calculates and returns the size of the ethash verification cache that
24+
// belongs to a certain block number. The cache size grows linearly, however, we
25+
// always take the highest prime below the linearly growing threshold in order to
26+
// reduce the risk of accidental regularities leading to cyclic behavior.
27+
func cacheSize(block uint64) uint64 {
28+
// If we have a pre-generated value, use that
29+
epoch := int(block / epochLength)
30+
if epoch < len(cacheSizes) {
31+
return cacheSizes[epoch]
32+
}
33+
// No known cache size, calculate manually (sanity branch only)
34+
size := uint64(cacheInitBytes + cacheGrowthBytes*uint64(epoch) - hashBytes)
35+
for !new(big.Int).SetUint64(size / hashBytes).ProbablyPrime(1) { // Always accurate for n < 2^64
36+
size -= 2 * hashBytes
37+
}
38+
return size
39+
}
40+
41+
// datasetSize calculates and returns the size of the ethash mining dataset that
42+
// belongs to a certain block number. The dataset size grows linearly, however, we
43+
// always take the highest prime below the linearly growing threshold in order to
44+
// reduce the risk of accidental regularities leading to cyclic behavior.
45+
func datasetSize(block uint64) uint64 {
46+
// If we have a pre-generated value, use that
47+
epoch := int(block / epochLength)
48+
if epoch < len(datasetSizes) {
49+
return datasetSizes[epoch]
50+
}
51+
// No known dataset size, calculate manually (sanity branch only)
52+
size := uint64(datasetInitBytes + datasetGrowthBytes*uint64(epoch) - mixBytes)
53+
for !new(big.Int).SetUint64(size / mixBytes).ProbablyPrime(1) { // Always accurate for n < 2^64
54+
size -= 2 * mixBytes
55+
}
56+
return size
57+
}

‎pow/ethash_algo_go1.8_test.go‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2017 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// +build go1.8
18+
19+
package pow
20+
21+
import "testing"
22+
23+
// Tests whether the dataset size calculator work correctly by cross checking the
24+
// hard coded lookup table with the value generated by it.
25+
func TestSizeCalculations(t *testing.T) {
26+
var tests []uint64
27+
28+
// Verify all the cache sizes from the lookup table
29+
defer func(sizes []uint64) { cacheSizes = sizes }(cacheSizes)
30+
tests, cacheSizes = cacheSizes, []uint64{}
31+
32+
for i, test := range tests {
33+
if size := cacheSize(uint64(i*epochLength) + 1); size != test {
34+
t.Errorf("cache %d: cache size mismatch: have %d, want %d", i, size, test)
35+
}
36+
}
37+
// Verify all the dataset sizes from the lookup table
38+
defer func(sizes []uint64) { datasetSizes = sizes }(datasetSizes)
39+
tests, datasetSizes = datasetSizes, []uint64{}
40+
41+
for i, test := range tests {
42+
if size := datasetSize(uint64(i*epochLength) + 1); size != test {
43+
t.Errorf("dataset %d: dataset size mismatch: have %d, want %d", i, size, test)
44+
}
45+
}
46+
}

‎pow/ethash_algo_test.go‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,6 @@ import (
2323
"github.com/ethereum/go-ethereum/common/hexutil"
2424
)
2525

26-
// Tests whether the dataset size calculator work correctly by cross checking the
27-
// hard coded lookup table with the value generated by it.
28-
func TestSizeCalculations(t *testing.T) {
29-
var tests []uint64
30-
31-
// Verify all the cache sizes from the lookup table
32-
defer func(sizes []uint64) { cacheSizes = sizes }(cacheSizes)
33-
tests, cacheSizes = cacheSizes, []uint64{}
34-
35-
for i, test := range tests {
36-
if size := cacheSize(uint64(i*epochLength) + 1); size != test {
37-
t.Errorf("cache %d: cache size mismatch: have %d, want %d", i, size, test)
38-
}
39-
}
40-
// Verify all the dataset sizes from the lookup table
41-
defer func(sizes []uint64) { datasetSizes = sizes }(datasetSizes)
42-
tests, datasetSizes = datasetSizes, []uint64{}
43-
44-
for i, test := range tests {
45-
if size := datasetSize(uint64(i*epochLength) + 1); size != test {
46-
t.Errorf("dataset %d: dataset size mismatch: have %d, want %d", i, size, test)
47-
}
48-
}
49-
}
50-
5126
// Tests that verification caches can be correctly generated.
5227
func TestCacheGeneration(t *testing.T) {
5328
tests := []struct {

0 commit comments

Comments
 (0)