@@ -2,6 +2,7 @@ package main
22
33import (
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
1415var 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}
0 commit comments