aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/geth/blocktestcmd.go
blob: e0a5becdc07a34ea18ed45d47a1d2dc4e795da50 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2015 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

package main

import (
    "fmt"
    "os"

    "github.com/codegangsta/cli"
    "github.com/ethereum/go-ethereum/cmd/utils"
    "github.com/ethereum/go-ethereum/eth"
    "github.com/ethereum/go-ethereum/ethdb"
    "github.com/ethereum/go-ethereum/tests"
)

var blocktestCommand = cli.Command{
    Action: runBlockTest,
    Name:   "blocktest",
    Usage:  `loads a block test file`,
    Description: `
The first argument should be a block test file.
The second argument is the name of a block test from the file.

The block test will be loaded into an in-memory database.
If loading succeeds, the RPC server is started. Clients will
be able to interact with the chain defined by the test.
`,
}

func runBlockTest(ctx *cli.Context) {
    var (
        file, testname string
        rpc            bool
    )
    args := ctx.Args()
    switch {
    case len(args) == 1:
        file = args[0]
    case len(args) == 2:
        file, testname = args[0], args[1]
    case len(args) == 3:
        file, testname = args[0], args[1]
        rpc = true
    default:
        utils.Fatalf(`Usage: ethereum blocktest <path-to-test-file> [ <test-name> [ "rpc" ] ]`)
    }
    bt, err := tests.LoadBlockTests(file)
    if err != nil {
        utils.Fatalf("%v", err)
    }

    // run all tests if no test name is specified
    if testname == "" {
        ecode := 0
        for name, test := range bt {
            fmt.Printf("----------------- Running Block Test %q\n", name)
            ethereum, err := runOneBlockTest(ctx, test)
            if err != nil {
                fmt.Println(err)
                fmt.Println("FAIL")
                ecode = 1
            }
            if ethereum != nil {
                ethereum.Stop()
                ethereum.WaitForShutdown()
            }
        }
        os.Exit(ecode)
        return
    }
    // otherwise, run the given test
    test, ok := bt[testname]
    if !ok {
        utils.Fatalf("Test file does not contain test named %q", testname)
    }
    ethereum, err := runOneBlockTest(ctx, test)
    if err != nil {
        utils.Fatalf("%v", err)
    }
    if rpc {
        fmt.Println("Block Test post state validated, starting RPC interface.")
        startEth(ctx, ethereum)
        utils.StartRPC(ethereum, ctx)
        ethereum.WaitForShutdown()
    }
}

func runOneBlockTest(ctx *cli.Context, test *tests.BlockTest) (*eth.Ethereum, error) {
    cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
    cfg.NewDB = func(path string) (ethdb.Database, error) { return ethdb.NewMemDatabase() }
    cfg.MaxPeers = 0 // disable network
    cfg.Shh = false  // disable whisper
    cfg.NAT = nil    // disable port mapping
    ethereum, err := eth.New(cfg)
    if err != nil {
        return nil, err
    }

    // import the genesis block
    ethereum.ResetWithGenesisBlock(test.Genesis)
    // import pre accounts
    _, err = test.InsertPreState(ethereum)
    if err != nil {
        return ethereum, fmt.Errorf("InsertPreState: %v", err)
    }

    cm := ethereum.BlockChain()
    validBlocks, err := test.TryBlocksInsert(cm)
    if err != nil {
        return ethereum, fmt.Errorf("Block Test load error: %v", err)
    }
    newDB := cm.State()
    if err := test.ValidatePostState(newDB); err != nil {
        return ethereum, fmt.Errorf("post state validation failed: %v", err)
    }
    return ethereum, test.ValidateImportedHeaders(cm, validBlocks)
}