Skip to content

Commit 225de7c

Browse files
authored
tests: update tests and implement general state tests (ethereum#14734)
Tests are now included as a submodule. This should make updating easier and removes ~60MB of JSON data from the working copy. State tests are replaced by General State Tests, which run the same test with multiple fork configurations. With the new test runner, consensus tests are run as subtests by walking json files. Many hex issues have been fixed upstream since the last update and most custom parsing code is replaced by existing JSON hex types. Tests can now be marked as 'expected failures', ensuring that fixes for those tests will trigger an update to test configuration. The new test runner also supports parallel execution and the -short flag.
1 parent bd01cd7 commit 225de7c

File tree

903 files changed

+1597
-2505430
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

903 files changed

+1597
-2505430
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "tests"]
2+
path = tests/testdata
3+
url = https://github.com/ethereum/tests

accounts/keystore/keystore_plain_test.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io/ioutil"
2424
"os"
25+
"path/filepath"
2526
"reflect"
2627
"strings"
2728
"testing"
@@ -140,21 +141,32 @@ func TestV3_PBKDF2_1(t *testing.T) {
140141
testDecryptV3(tests["wikipage_test_vector_pbkdf2"], t)
141142
}
142143

144+
var testsSubmodule = filepath.Join("..", "..", "tests", "testdata", "KeyStoreTests")
145+
146+
func skipIfSubmoduleMissing(t *testing.T) {
147+
if !common.FileExist(testsSubmodule) {
148+
t.Skipf("can't find JSON tests from submodule at %s", testsSubmodule)
149+
}
150+
}
151+
143152
func TestV3_PBKDF2_2(t *testing.T) {
153+
skipIfSubmoduleMissing(t)
144154
t.Parallel()
145-
tests := loadKeyStoreTestV3("../../tests/files/KeyStoreTests/basic_tests.json", t)
155+
tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
146156
testDecryptV3(tests["test1"], t)
147157
}
148158

149159
func TestV3_PBKDF2_3(t *testing.T) {
160+
skipIfSubmoduleMissing(t)
150161
t.Parallel()
151-
tests := loadKeyStoreTestV3("../../tests/files/KeyStoreTests/basic_tests.json", t)
162+
tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
152163
testDecryptV3(tests["python_generated_test_with_odd_iv"], t)
153164
}
154165

155166
func TestV3_PBKDF2_4(t *testing.T) {
167+
skipIfSubmoduleMissing(t)
156168
t.Parallel()
157-
tests := loadKeyStoreTestV3("../../tests/files/KeyStoreTests/basic_tests.json", t)
169+
tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
158170
testDecryptV3(tests["evilnonce"], t)
159171
}
160172

@@ -165,8 +177,9 @@ func TestV3_Scrypt_1(t *testing.T) {
165177
}
166178

167179
func TestV3_Scrypt_2(t *testing.T) {
180+
skipIfSubmoduleMissing(t)
168181
t.Parallel()
169-
tests := loadKeyStoreTestV3("../../tests/files/KeyStoreTests/basic_tests.json", t)
182+
tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
170183
testDecryptV3(tests["test2"], t)
171184
}
172185

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ environment:
2121
PATH: C:\msys64\mingw32\bin\;C:\Program Files (x86)\NSIS\;%PATH%
2222

2323
install:
24+
- git submodule update --init
2425
- rmdir C:\go /s /q
2526
- appveyor DownloadFile https://storage.googleapis.com/golang/go1.8.3.windows-%GETH_ARCH%.zip
2627
- 7z x go1.8.3.windows-%GETH_ARCH%.zip -y -oC:\ > NUL

build/update-license.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var (
4545
// paths with any of these prefixes will be skipped
4646
skipPrefixes = []string{
4747
// boring stuff
48-
"vendor/", "tests/files/", "build/",
48+
"vendor/", "tests/testdata/", "build/",
4949
// don't relicense vendored sources
5050
"cmd/internal/browser",
5151
"consensus/ethash/xor.go",

consensus/ethash/consensus_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/json"
2121
"math/big"
2222
"os"
23+
"path/filepath"
2324
"testing"
2425

2526
"github.com/ethereum/go-ethereum/common/math"
@@ -57,9 +58,9 @@ func (d *diffTest) UnmarshalJSON(b []byte) (err error) {
5758
}
5859

5960
func TestCalcDifficulty(t *testing.T) {
60-
file, err := os.Open("../../tests/files/BasicTests/difficulty.json")
61+
file, err := os.Open(filepath.Join("..", "..", "tests", "testdata", "BasicTests", "difficulty.json"))
6162
if err != nil {
62-
t.Fatal(err)
63+
t.Skip(err)
6364
}
6465
defer file.Close()
6566

core/gen_genesis.go

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

core/gen_genesis_account.go

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

core/genesis.go

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package core
1818

1919
import (
20+
"bytes"
21+
"encoding/hex"
22+
"encoding/json"
2023
"errors"
2124
"fmt"
2225
"math/big"
@@ -44,24 +47,42 @@ type Genesis struct {
4447
Config *params.ChainConfig `json:"config"`
4548
Nonce uint64 `json:"nonce"`
4649
Timestamp uint64 `json:"timestamp"`
47-
ParentHash common.Hash `json:"parentHash"`
4850
ExtraData []byte `json:"extraData"`
4951
GasLimit uint64 `json:"gasLimit" gencodec:"required"`
5052
Difficulty *big.Int `json:"difficulty" gencodec:"required"`
5153
Mixhash common.Hash `json:"mixHash"`
5254
Coinbase common.Address `json:"coinbase"`
5355
Alloc GenesisAlloc `json:"alloc" gencodec:"required"`
56+
57+
// These fields are used for consensus tests. Please don't use them
58+
// in actual genesis blocks.
59+
Number uint64 `json:"number"`
60+
GasUsed uint64 `json:"gasUsed"`
61+
ParentHash common.Hash `json:"parentHash"`
5462
}
5563

5664
// GenesisAlloc specifies the initial state that is part of the genesis block.
5765
type GenesisAlloc map[common.Address]GenesisAccount
5866

67+
func (ga *GenesisAlloc) UnmarshalJSON(data []byte) error {
68+
m := make(map[common.UnprefixedAddress]GenesisAccount)
69+
if err := json.Unmarshal(data, &m); err != nil {
70+
return err
71+
}
72+
*ga = make(GenesisAlloc)
73+
for addr, a := range m {
74+
(*ga)[common.Address(addr)] = a
75+
}
76+
return nil
77+
}
78+
5979
// GenesisAccount is an account in the state of the genesis block.
6080
type GenesisAccount struct {
61-
Code []byte `json:"code,omitempty"`
62-
Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
63-
Balance *big.Int `json:"balance" gencodec:"required"`
64-
Nonce uint64 `json:"nonce,omitempty"`
81+
Code []byte `json:"code,omitempty"`
82+
Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
83+
Balance *big.Int `json:"balance" gencodec:"required"`
84+
Nonce uint64 `json:"nonce,omitempty"`
85+
PrivateKey []byte `json:"secretKey,omitempty"` // for tests
6586
}
6687

6788
// field type overrides for gencodec
@@ -70,13 +91,38 @@ type genesisSpecMarshaling struct {
7091
Timestamp math.HexOrDecimal64
7192
ExtraData hexutil.Bytes
7293
GasLimit math.HexOrDecimal64
94+
GasUsed math.HexOrDecimal64
7395
Difficulty *math.HexOrDecimal256
7496
Alloc map[common.UnprefixedAddress]GenesisAccount
7597
}
98+
7699
type genesisAccountMarshaling struct {
77-
Code hexutil.Bytes
78-
Balance *math.HexOrDecimal256
79-
Nonce math.HexOrDecimal64
100+
Code hexutil.Bytes
101+
Balance *math.HexOrDecimal256
102+
Nonce math.HexOrDecimal64
103+
Storage map[storageJSON]storageJSON
104+
PrivateKey hexutil.Bytes
105+
}
106+
107+
// storageJSON represents a 256 bit byte array, but allows less than 256 bits when
108+
// unmarshaling from hex.
109+
type storageJSON common.Hash
110+
111+
func (h *storageJSON) UnmarshalText(text []byte) error {
112+
text = bytes.TrimPrefix(text, []byte("0x"))
113+
if len(text) > 64 {
114+
return fmt.Errorf("too many hex characters in storage key/value %q", text)
115+
}
116+
offset := len(h) - len(text)/2 // pad on the left
117+
if _, err := hex.Decode(h[offset:], text); err != nil {
118+
fmt.Println(err)
119+
return fmt.Errorf("invalid hex storage key/value %q", text)
120+
}
121+
return nil
122+
}
123+
124+
func (h storageJSON) MarshalText() ([]byte, error) {
125+
return hexutil.Bytes(h[:]).MarshalText()
80126
}
81127

82128
// GenesisMismatchError is raised when trying to overwrite an existing
@@ -187,11 +233,13 @@ func (g *Genesis) ToBlock() (*types.Block, *state.StateDB) {
187233
}
188234
root := statedb.IntermediateRoot(false)
189235
head := &types.Header{
236+
Number: new(big.Int).SetUint64(g.Number),
190237
Nonce: types.EncodeNonce(g.Nonce),
191238
Time: new(big.Int).SetUint64(g.Timestamp),
192239
ParentHash: g.ParentHash,
193240
Extra: g.ExtraData,
194241
GasLimit: new(big.Int).SetUint64(g.GasLimit),
242+
GasUsed: new(big.Int).SetUint64(g.GasUsed),
195243
Difficulty: g.Difficulty,
196244
MixDigest: g.Mixhash,
197245
Coinbase: g.Coinbase,
@@ -210,6 +258,9 @@ func (g *Genesis) ToBlock() (*types.Block, *state.StateDB) {
210258
// The block is committed as the canonical head block.
211259
func (g *Genesis) Commit(db ethdb.Database) (*types.Block, error) {
212260
block, statedb := g.ToBlock()
261+
if block.Number().Sign() != 0 {
262+
return nil, fmt.Errorf("can't commit genesis block with number > 0")
263+
}
213264
if _, err := statedb.CommitTo(db, false); err != nil {
214265
return nil, fmt.Errorf("cannot write state: %v", err)
215266
}

0 commit comments

Comments
 (0)