Skip to content

Commit a67a155

Browse files
committed
Split tests from helper code
1 parent 7b9fbb0 commit a67a155

5 files changed

Lines changed: 170 additions & 555 deletions

File tree

‎tests/block_test.go‎

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
package tests
22

33
import (
4-
"path/filepath"
54
"testing"
6-
7-
"github.com/ethereum/go-ethereum/accounts"
8-
"github.com/ethereum/go-ethereum/common"
9-
"github.com/ethereum/go-ethereum/crypto"
10-
"github.com/ethereum/go-ethereum/eth"
11-
"github.com/ethereum/go-ethereum/ethdb"
125
)
136

147
// TODO: refactor test setup & execution to better align with vm and tx tests
@@ -50,65 +43,3 @@ func TestBcTotalDifficulty(t *testing.T) {
5043
func TestBcWallet(t *testing.T) {
5144
runBlockTestsInFile("files/BlockTests/bcWalletTest.json", []string{}, t)
5245
}
53-
54-
func runBlockTestsInFile(filepath string, snafus []string, t *testing.T) {
55-
bt, err := LoadBlockTests(filepath)
56-
if err != nil {
57-
t.Fatal(err)
58-
}
59-
60-
notWorking := make(map[string]bool, 100)
61-
for _, name := range snafus {
62-
notWorking[name] = true
63-
}
64-
65-
for name, test := range bt {
66-
if !notWorking[name] {
67-
runBlockTest(name, test, t)
68-
}
69-
}
70-
}
71-
72-
func runBlockTest(name string, test *BlockTest, t *testing.T) {
73-
cfg := testEthConfig()
74-
ethereum, err := eth.New(cfg)
75-
if err != nil {
76-
t.Fatalf("%v", err)
77-
}
78-
79-
err = ethereum.Start()
80-
if err != nil {
81-
t.Fatalf("%v", err)
82-
}
83-
84-
// import the genesis block
85-
ethereum.ResetWithGenesisBlock(test.Genesis)
86-
87-
// import pre accounts
88-
statedb, err := test.InsertPreState(ethereum)
89-
if err != nil {
90-
t.Fatalf("InsertPreState: %v", err)
91-
}
92-
93-
err = test.TryBlocksInsert(ethereum.ChainManager())
94-
if err != nil {
95-
t.Fatal(err)
96-
}
97-
98-
if err = test.ValidatePostState(statedb); err != nil {
99-
t.Fatal("post state validation failed: %v", err)
100-
}
101-
t.Log("Test passed: ", name)
102-
}
103-
104-
func testEthConfig() *eth.Config {
105-
ks := crypto.NewKeyStorePassphrase(filepath.Join(common.DefaultDataDir(), "keystore"))
106-
107-
return &eth.Config{
108-
DataDir: common.DefaultDataDir(),
109-
Verbosity: 5,
110-
Etherbase: "primary",
111-
AccountManager: accounts.NewManager(ks),
112-
NewDB: func(path string) (common.Database, error) { return ethdb.NewMemDatabase() },
113-
}
114-
}

‎tests/block_test_util.go‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ import (
77
"fmt"
88
"io/ioutil"
99
"math/big"
10+
"path/filepath"
1011
"runtime"
1112
"strconv"
1213
"strings"
14+
"testing"
1315
"time"
1416

17+
"github.com/ethereum/go-ethereum/accounts"
1518
"github.com/ethereum/go-ethereum/common"
1619
"github.com/ethereum/go-ethereum/core"
1720
"github.com/ethereum/go-ethereum/core/state"
1821
"github.com/ethereum/go-ethereum/core/types"
1922
"github.com/ethereum/go-ethereum/crypto"
2023
"github.com/ethereum/go-ethereum/eth"
24+
"github.com/ethereum/go-ethereum/ethdb"
2125
"github.com/ethereum/go-ethereum/rlp"
2226
)
2327

@@ -83,6 +87,68 @@ type btTransaction struct {
8387
Value string
8488
}
8589

90+
func runBlockTestsInFile(filepath string, snafus []string, t *testing.T) {
91+
bt, err := LoadBlockTests(filepath)
92+
if err != nil {
93+
t.Fatal(err)
94+
}
95+
96+
notWorking := make(map[string]bool, 100)
97+
for _, name := range snafus {
98+
notWorking[name] = true
99+
}
100+
101+
for name, test := range bt {
102+
if !notWorking[name] {
103+
runBlockTest(name, test, t)
104+
}
105+
}
106+
}
107+
108+
func runBlockTest(name string, test *BlockTest, t *testing.T) {
109+
cfg := testEthConfig()
110+
ethereum, err := eth.New(cfg)
111+
if err != nil {
112+
t.Fatalf("%v", err)
113+
}
114+
115+
err = ethereum.Start()
116+
if err != nil {
117+
t.Fatalf("%v", err)
118+
}
119+
120+
// import the genesis block
121+
ethereum.ResetWithGenesisBlock(test.Genesis)
122+
123+
// import pre accounts
124+
statedb, err := test.InsertPreState(ethereum)
125+
if err != nil {
126+
t.Fatalf("InsertPreState: %v", err)
127+
}
128+
129+
err = test.TryBlocksInsert(ethereum.ChainManager())
130+
if err != nil {
131+
t.Fatal(err)
132+
}
133+
134+
if err = test.ValidatePostState(statedb); err != nil {
135+
t.Fatal("post state validation failed: %v", err)
136+
}
137+
t.Log("Test passed: ", name)
138+
}
139+
140+
func testEthConfig() *eth.Config {
141+
ks := crypto.NewKeyStorePassphrase(filepath.Join(common.DefaultDataDir(), "keystore"))
142+
143+
return &eth.Config{
144+
DataDir: common.DefaultDataDir(),
145+
Verbosity: 5,
146+
Etherbase: "primary",
147+
AccountManager: accounts.NewManager(ks),
148+
NewDB: func(path string) (common.Database, error) { return ethdb.NewMemDatabase() },
149+
}
150+
}
151+
86152
// LoadBlockTests loads a block test JSON file.
87153
func LoadBlockTests(file string) (map[string]*BlockTest, error) {
88154
bt := make(map[string]*btJSON)

‎tests/state_test.go‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package tests
2+
3+
import "testing"
4+
5+
func TestStateSystemOperations(t *testing.T) {
6+
const fn = "../files/StateTests/stSystemOperationsTest.json"
7+
RunVmTest(fn, t)
8+
}
9+
10+
func TestStateExample(t *testing.T) {
11+
const fn = "../files/StateTests/stExample.json"
12+
RunVmTest(fn, t)
13+
}
14+
15+
func TestStatePreCompiledContracts(t *testing.T) {
16+
const fn = "../files/StateTests/stPreCompiledContracts.json"
17+
RunVmTest(fn, t)
18+
}
19+
20+
func TestStateRecursiveCreate(t *testing.T) {
21+
const fn = "../files/StateTests/stRecursiveCreate.json"
22+
RunVmTest(fn, t)
23+
}
24+
25+
func TestStateSpecial(t *testing.T) {
26+
const fn = "../files/StateTests/stSpecialTest.json"
27+
RunVmTest(fn, t)
28+
}
29+
30+
func TestStateRefund(t *testing.T) {
31+
const fn = "../files/StateTests/stRefundTest.json"
32+
RunVmTest(fn, t)
33+
}
34+
35+
func TestStateBlockHash(t *testing.T) {
36+
const fn = "../files/StateTests/stBlockHashTest.json"
37+
RunVmTest(fn, t)
38+
}
39+
40+
func TestStateInitCode(t *testing.T) {
41+
const fn = "../files/StateTests/stInitCodeTest.json"
42+
RunVmTest(fn, t)
43+
}
44+
45+
func TestStateLog(t *testing.T) {
46+
const fn = "../files/StateTests/stLogTests.json"
47+
RunVmTest(fn, t)
48+
}
49+
50+
func TestStateTransaction(t *testing.T) {
51+
const fn = "../files/StateTests/stTransactionTest.json"
52+
RunVmTest(fn, t)
53+
}
54+
55+
func TestCallCreateCallCode(t *testing.T) {
56+
const fn = "../files/StateTests/stCallCreateCallCodeTest.json"
57+
RunVmTest(fn, t)
58+
}
59+
60+
func TestMemory(t *testing.T) {
61+
const fn = "../files/StateTests/stMemoryTest.json"
62+
RunVmTest(fn, t)
63+
}
64+
65+
func TestMemoryStress(t *testing.T) {
66+
if os.Getenv("TEST_VM_COMPLEX") == "" {
67+
t.Skip()
68+
}
69+
const fn = "../files/StateTests/stMemoryStressTest.json"
70+
RunVmTest(fn, t)
71+
}
72+
73+
func TestQuadraticComplexity(t *testing.T) {
74+
if os.Getenv("TEST_VM_COMPLEX") == "" {
75+
t.Skip()
76+
}
77+
const fn = "../files/StateTests/stQuadraticComplexityTest.json"
78+
RunVmTest(fn, t)
79+
}
80+
81+
func TestSolidity(t *testing.T) {
82+
const fn = "../files/StateTests/stSolidityTest.json"
83+
RunVmTest(fn, t)
84+
}
85+
86+
func TestWallet(t *testing.T) {
87+
const fn = "../files/StateTests/stWalletTest.json"
88+
RunVmTest(fn, t)
89+
}
90+
91+
func TestStateTestsRandom(t *testing.T) {
92+
fns, _ := filepath.Glob("../files/StateTests/RandomTests/*")
93+
for _, fn := range fns {
94+
RunVmTest(fn, t)
95+
}
96+
}

0 commit comments

Comments
 (0)