Skip to content

Commit a660685

Browse files
holimanfjl
authored andcommitted
tests: add ethash difficulty tests (#15191)
1 parent 2ab2a9f commit a660685

File tree

6 files changed

+235
-9
lines changed

6 files changed

+235
-9
lines changed

tests/difficulty_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
18+
package tests
19+
20+
import (
21+
"testing"
22+
23+
"math/big"
24+
25+
"github.com/ethereum/go-ethereum/common"
26+
"github.com/ethereum/go-ethereum/params"
27+
)
28+
29+
var (
30+
mainnetChainConfig = params.ChainConfig{
31+
ChainId: big.NewInt(1),
32+
HomesteadBlock: big.NewInt(1150000),
33+
DAOForkBlock: big.NewInt(1920000),
34+
DAOForkSupport: true,
35+
EIP150Block: big.NewInt(2463000),
36+
EIP150Hash: common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"),
37+
EIP155Block: big.NewInt(2675000),
38+
EIP158Block: big.NewInt(2675000),
39+
ByzantiumBlock: big.NewInt(4370000),
40+
}
41+
)
42+
43+
func TestDifficulty(t *testing.T) {
44+
t.Parallel()
45+
46+
dt := new(testMatcher)
47+
// Not difficulty-tests
48+
dt.skipLoad("hexencodetest.*")
49+
dt.skipLoad("crypto.*")
50+
dt.skipLoad("blockgenesistest\\.json")
51+
dt.skipLoad("genesishashestest\\.json")
52+
dt.skipLoad("keyaddrtest\\.json")
53+
dt.skipLoad("txtest\\.json")
54+
55+
// files are 2 years old, contains strange values
56+
dt.skipLoad("difficultyCustomHomestead\\.json")
57+
dt.skipLoad("difficultyMorden\\.json")
58+
dt.skipLoad("difficultyOlimpic\\.json")
59+
60+
dt.config("Ropsten", *params.TestnetChainConfig)
61+
dt.config("Morden", *params.TestnetChainConfig)
62+
dt.config("Frontier", params.ChainConfig{})
63+
64+
dt.config("Homestead", params.ChainConfig{
65+
HomesteadBlock: big.NewInt(0),
66+
})
67+
68+
dt.config("Byzantium", params.ChainConfig{
69+
ByzantiumBlock: big.NewInt(0),
70+
})
71+
72+
dt.config("Frontier", *params.TestnetChainConfig)
73+
dt.config("MainNetwork", mainnetChainConfig)
74+
dt.config("CustomMainNetwork", mainnetChainConfig)
75+
dt.config("difficulty.json", mainnetChainConfig)
76+
77+
dt.walk(t, difficultyTestDir, func(t *testing.T, name string, test *DifficultyTest) {
78+
cfg := dt.findConfig(name)
79+
if test.ParentDifficulty.Cmp(params.MinimumDifficulty) < 0 {
80+
t.Skip("difficulty below minimum")
81+
return
82+
}
83+
if err := dt.checkFailure(t, name, test.Run(cfg)); err != nil {
84+
t.Error(err)
85+
}
86+
})
87+
}

tests/difficulty_test_util.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
18+
package tests
19+
20+
import (
21+
"fmt"
22+
"math/big"
23+
24+
"github.com/ethereum/go-ethereum/common"
25+
"github.com/ethereum/go-ethereum/common/math"
26+
"github.com/ethereum/go-ethereum/consensus/ethash"
27+
"github.com/ethereum/go-ethereum/core/types"
28+
"github.com/ethereum/go-ethereum/params"
29+
)
30+
31+
//go:generate gencodec -type DifficultyTest -field-override difficultyTestMarshaling -out gen_difficultytest.go
32+
33+
type DifficultyTest struct {
34+
ParentTimestamp *big.Int `json:"parentTimestamp"`
35+
ParentDifficulty *big.Int `json:"parentDifficulty"`
36+
UncleHash common.Hash `json:"parentUncles"`
37+
CurrentTimestamp *big.Int `json:"currentTimestamp"`
38+
CurrentBlockNumber uint64 `json:"currentBlockNumber"`
39+
CurrentDifficulty *big.Int `json:"currentDifficulty"`
40+
}
41+
42+
type difficultyTestMarshaling struct {
43+
ParentTimestamp *math.HexOrDecimal256
44+
ParentDifficulty *math.HexOrDecimal256
45+
CurrentTimestamp *math.HexOrDecimal256
46+
CurrentDifficulty *math.HexOrDecimal256
47+
UncleHash common.Hash
48+
CurrentBlockNumber math.HexOrDecimal64
49+
}
50+
51+
func (test *DifficultyTest) Run(config *params.ChainConfig) error {
52+
parentNumber := big.NewInt(int64(test.CurrentBlockNumber - 1))
53+
parent := &types.Header{
54+
Difficulty: test.ParentDifficulty,
55+
Time: test.ParentTimestamp,
56+
Number: parentNumber,
57+
UncleHash: test.UncleHash,
58+
}
59+
60+
actual := ethash.CalcDifficulty(config, test.CurrentTimestamp.Uint64(), parent)
61+
exp := test.CurrentDifficulty
62+
63+
if actual.Cmp(exp) != 0 {
64+
return fmt.Errorf("parent[time %v diff %v unclehash:%x] child[time %v number %v] diff %v != expected %v",
65+
test.ParentTimestamp, test.ParentDifficulty, test.UncleHash,
66+
test.CurrentTimestamp, test.CurrentBlockNumber, actual, exp)
67+
}
68+
return nil
69+
70+
}

tests/gen_difficultytest.go

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/init_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var (
3939
transactionTestDir = filepath.Join(baseDir, "TransactionTests")
4040
vmTestDir = filepath.Join(baseDir, "VMTests")
4141
rlpTestDir = filepath.Join(baseDir, "RLPTests")
42+
difficultyTestDir = filepath.Join(baseDir, "BasicTests")
4243
)
4344

4445
func readJson(reader io.Reader, value interface{}) error {

tests/state_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ func TestState(t *testing.T) {
3939
st.fails(`^stRevertTest/RevertPrefoundEmptyOOG\.json/EIP158`, "bug in test")
4040
st.fails(`^stRevertTest/RevertPrecompiledTouch\.json/Byzantium`, "bug in test")
4141
st.fails(`^stRevertTest/RevertPrefoundEmptyOOG\.json/Byzantium`, "bug in test")
42-
st.fails( `^stRandom/randomStatetest645\.json/EIP150/.*`, "known bug #15119")
43-
st.fails( `^stRandom/randomStatetest645\.json/Frontier/.*`, "known bug #15119")
44-
st.fails( `^stRandom/randomStatetest645\.json/Homestead/.*`, "known bug #15119")
45-
st.fails( `^stRandom/randomStatetest644\.json/EIP150/.*`, "known bug #15119")
46-
st.fails( `^stRandom/randomStatetest644\.json/Frontier/.*`, "known bug #15119")
47-
st.fails( `^stRandom/randomStatetest644\.json/Homestead/.*`, "known bug #15119")
48-
49-
42+
st.fails(`^stRandom/randomStatetest645\.json/EIP150/.*`, "known bug #15119")
43+
st.fails(`^stRandom/randomStatetest645\.json/Frontier/.*`, "known bug #15119")
44+
st.fails(`^stRandom/randomStatetest645\.json/Homestead/.*`, "known bug #15119")
45+
st.fails(`^stRandom/randomStatetest644\.json/EIP150/.*`, "known bug #15119")
46+
st.fails(`^stRandom/randomStatetest644\.json/Frontier/.*`, "known bug #15119")
47+
st.fails(`^stRandom/randomStatetest644\.json/Homestead/.*`, "known bug #15119")
48+
st.fails(`^stCreateTest/TransactionCollisionToEmpty\.json/EIP158/2`, "known bug ")
49+
st.fails(`^stCreateTest/TransactionCollisionToEmpty\.json/EIP158/3`, "known bug ")
50+
st.fails(`^stCreateTest/TransactionCollisionToEmpty\.json/Byzantium/2`, "known bug ")
51+
st.fails(`^stCreateTest/TransactionCollisionToEmpty\.json/Byzantium/3`, "known bug ")
5052
st.walk(t, stateTestDir, func(t *testing.T, name string, test *StateTest) {
5153
for _, subtest := range test.Subtests() {
5254
subtest := subtest

tests/testdata

Submodule testdata updated 835 files

0 commit comments

Comments
 (0)