Skip to content

Commit 235ed7e

Browse files
fjlGustav Simonsson
authored andcommitted
cmd/geth, tests: enable running multiple tests from a single file
This commit also changes the block test loading so tests containing invalid RLP blocks can be loaded and return an error only when they are run. (cherry picked from commit 898ba87984791249586b97c9ce340dd087b79d67)
1 parent 2ce21ce commit 235ed7e

2 files changed

Lines changed: 105 additions & 33 deletions

File tree

‎cmd/geth/blocktest.go‎

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/codegangsta/cli"
78
"github.com/ethereum/go-ethereum/cmd/utils"
@@ -12,7 +13,7 @@ import (
1213
)
1314

1415
var blocktestCmd = cli.Command{
15-
Action: runblocktest,
16+
Action: runBlockTest,
1617
Name: "blocktest",
1718
Usage: `loads a block test file`,
1819
Description: `
@@ -25,27 +26,78 @@ be able to interact with the chain defined by the test.
2526
`,
2627
}
2728

28-
func runblocktest(ctx *cli.Context) {
29-
if len(ctx.Args()) != 3 {
30-
utils.Fatalf("Usage: ethereum blocktest <path-to-test-file> <test-name> {rpc, norpc}")
29+
func runBlockTest(ctx *cli.Context) {
30+
var (
31+
file, testname string
32+
rpc bool
33+
)
34+
args := ctx.Args()
35+
switch {
36+
case len(args) == 1:
37+
file = args[0]
38+
case len(args) == 2:
39+
file, testname = args[0], args[1]
40+
case len(args) == 3:
41+
file, testname = args[0], args[1]
42+
rpc = true
43+
default:
44+
utils.Fatalf(`Usage: ethereum blocktest <path-to-test-file> [ <test-name> [ "rpc" ] ]`)
3145
}
32-
file, testname, startrpc := ctx.Args()[0], ctx.Args()[1], ctx.Args()[2]
33-
3446
bt, err := tests.LoadBlockTests(file)
3547
if err != nil {
3648
utils.Fatalf("%v", err)
3749
}
50+
51+
// run all tests if no test name is specified
52+
if testname == "" {
53+
ecode := 0
54+
for name, test := range bt {
55+
fmt.Printf("----------------- Running Block Test %q\n", name)
56+
ethereum, err := runOneBlockTest(ctx, test)
57+
if err != nil {
58+
fmt.Println(err)
59+
fmt.Println("FAIL")
60+
ecode = 1
61+
}
62+
if ethereum != nil {
63+
ethereum.Stop()
64+
ethereum.WaitForShutdown()
65+
}
66+
}
67+
os.Exit(ecode)
68+
return
69+
}
70+
// otherwise, run the given test
3871
test, ok := bt[testname]
3972
if !ok {
4073
utils.Fatalf("Test file does not contain test named %q", testname)
4174
}
75+
ethereum, err := runOneBlockTest(ctx, test)
76+
if err != nil {
77+
utils.Fatalf("%v", err)
78+
}
79+
defer ethereum.Stop()
80+
if rpc {
81+
fmt.Println("Block Test post state validated, starting RPC interface.")
82+
startEth(ctx, ethereum)
83+
utils.StartRPC(ethereum, ctx)
84+
ethereum.WaitForShutdown()
85+
}
86+
}
4287

88+
func runOneBlockTest(ctx *cli.Context, test *tests.BlockTest) (*eth.Ethereum, error) {
4389
cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
4490
cfg.NewDB = func(path string) (common.Database, error) { return ethdb.NewMemDatabase() }
4591
cfg.MaxPeers = 0 // disable network
92+
cfg.Shh = false // disable whisper
93+
cfg.NAT = nil // disable port mapping
94+
4695
ethereum, err := eth.New(cfg)
4796
if err != nil {
48-
utils.Fatalf("%v", err)
97+
return nil, err
98+
}
99+
if err := ethereum.Start(); err != nil {
100+
return nil, err
49101
}
50102

51103
// import the genesis block
@@ -54,27 +106,16 @@ func runblocktest(ctx *cli.Context) {
54106
// import pre accounts
55107
statedb, err := test.InsertPreState(ethereum.StateDb())
56108
if err != nil {
57-
utils.Fatalf("could not insert genesis accounts: %v", err)
109+
return ethereum, fmt.Errorf("InsertPreState: %v", err)
58110
}
59111

60112
// insert the test blocks, which will execute all transactions
61-
chain := ethereum.ChainManager()
62-
if err := chain.InsertChain(test.Blocks); err != nil {
63-
utils.Fatalf("Block Test load error: %v %T", err, err)
64-
} else {
65-
fmt.Println("Block Test chain loaded")
113+
if err := test.InsertBlocks(ethereum.ChainManager()); err != nil {
114+
return ethereum, fmt.Errorf("Block Test load error: %v %T", err, err)
66115
}
67-
116+
fmt.Println("chain loaded")
68117
if err := test.ValidatePostState(statedb); err != nil {
69-
utils.Fatalf("post state validation failed: %v", err)
70-
}
71-
fmt.Println("Block Test post state validated, starting ethereum.")
72-
73-
if startrpc == "rpc" {
74-
startEth(ctx, ethereum)
75-
utils.StartRPC(ethereum, ctx)
76-
ethereum.WaitForShutdown()
77-
} else {
78-
startEth(ctx, ethereum)
118+
return ethereum, fmt.Errorf("post state validation failed: %v", err)
79119
}
120+
return ethereum, nil
80121
}

‎tests/blocktest.go‎

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"github.com/ethereum/go-ethereum/common"
15+
"github.com/ethereum/go-ethereum/core"
1516
"github.com/ethereum/go-ethereum/core/state"
1617
"github.com/ethereum/go-ethereum/core/types"
1718
"github.com/ethereum/go-ethereum/rlp"
@@ -73,8 +74,8 @@ type btBlock struct {
7374

7475
type BlockTest struct {
7576
Genesis *types.Block
76-
Blocks []*types.Block
7777

78+
json *btJSON
7879
preAccounts map[string]btAccount
7980
}
8081

@@ -88,7 +89,7 @@ func LoadBlockTests(file string) (map[string]*BlockTest, error) {
8889
for name, in := range bt {
8990
var err error
9091
if out[name], err = convertTest(in); err != nil {
91-
return nil, fmt.Errorf("bad test %q: %v", err)
92+
return out, fmt.Errorf("bad test %q: %v", name, err)
9293
}
9394
}
9495
return out, nil
@@ -124,6 +125,15 @@ func (t *BlockTest) InsertPreState(db common.Database) (*state.StateDB, error) {
124125
return statedb, nil
125126
}
126127

128+
// InsertBlocks loads the test's blocks into the given chain.
129+
func (t *BlockTest) InsertBlocks(chain *core.ChainManager) error {
130+
blocks, err := t.convertBlocks()
131+
if err != nil {
132+
return err
133+
}
134+
return chain.InsertChain(blocks)
135+
}
136+
127137
func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error {
128138
for addrString, acct := range t.preAccounts {
129139
// XXX: is is worth it checking for errors here?
@@ -149,6 +159,21 @@ func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error {
149159
return nil
150160
}
151161

162+
func (t *BlockTest) convertBlocks() (blocks []*types.Block, err error) {
163+
// the conversion handles errors by catching panics.
164+
// you might consider this ugly, but the alternative (passing errors)
165+
// would be much harder to read.
166+
defer func() {
167+
if recovered := recover(); recovered != nil {
168+
buf := make([]byte, 64<<10)
169+
buf = buf[:runtime.Stack(buf, false)]
170+
err = fmt.Errorf("%v\n%s", recovered, buf)
171+
}
172+
}()
173+
blocks = mustConvertBlocks(t.json.Blocks)
174+
return blocks, nil
175+
}
176+
152177
func convertTest(in *btJSON) (out *BlockTest, err error) {
153178
// the conversion handles errors by catching panics.
154179
// you might consider this ugly, but the alternative (passing errors)
@@ -160,9 +185,8 @@ func convertTest(in *btJSON) (out *BlockTest, err error) {
160185
err = fmt.Errorf("%v\n%s", recovered, buf)
161186
}
162187
}()
163-
out = &BlockTest{preAccounts: in.Pre}
188+
out = &BlockTest{preAccounts: in.Pre, json: in}
164189
out.Genesis = mustConvertGenesis(in.GenesisBlockHeader)
165-
out.Blocks = mustConvertBlocks(in.Blocks)
166190
return out, err
167191
}
168192

@@ -203,7 +227,7 @@ func mustConvertBlocks(testBlocks []btBlock) []*types.Block {
203227
var b types.Block
204228
r := bytes.NewReader(mustConvertBytes(inb.Rlp))
205229
if err := rlp.Decode(r, &b); err != nil {
206-
panic(fmt.Errorf("invalid block %d: %q", i, inb.Rlp))
230+
panic(fmt.Errorf("invalid block %d: %q\nerror: %v", i, inb.Rlp, err))
207231
}
208232
out = append(out, &b)
209233
}
@@ -293,11 +317,18 @@ func findLine(data []byte, offset int64) (line int) {
293317
}
294318

295319
func unfuckCPPHexInts(s string) string {
296-
if s == "0x" { // no respect for the empty value :(
320+
switch {
321+
case s == "0x":
322+
// no respect for the empty value :(
297323
return "0x00"
298-
}
299-
if (len(s) % 2) != 0 { // motherfucking nibbles
324+
case len(s) == 0:
325+
return "0x00"
326+
case len(s) == 1:
327+
return "0x0" + s[:1]
328+
case len(s)%2 != 0:
329+
// motherfucking nibbles
300330
return "0x0" + s[2:]
331+
default:
332+
return s
301333
}
302-
return s
303334
}

0 commit comments

Comments
 (0)