aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/geth/blocktest.go
blob: f0b6bb1a21c8dac0f066ff279b0b29bfc1fd14a2 (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
package main

import (
    "fmt"

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

var blocktestCmd = 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) {
    if len(ctx.Args()) != 3 {
        utils.Fatalf("Usage: ethereum blocktest <path-to-test-file> <test-name> {rpc, norpc}")
    }
    file, testname, startrpc := ctx.Args()[0], ctx.Args()[1], ctx.Args()[2]

    bt, err := tests.LoadBlockTests(file)
    if err != nil {
        utils.Fatalf("%v", err)
    }
    test, ok := bt[testname]
    if !ok {
        utils.Fatalf("Test file does not contain test named %q", testname)
    }

    cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
    cfg.NewDB = func(path string) (common.Database, error) { return ethdb.NewMemDatabase() }
    cfg.MaxPeers = 0 // disable network
    ethereum, err := eth.New(cfg)
    if err != nil {
        utils.Fatalf("%v", err)
    }

    // import the genesis block
    ethereum.ResetWithGenesisBlock(test.Genesis)

    // import pre accounts
    statedb, err := test.InsertPreState(ethereum.StateDb())
    if err != nil {
        utils.Fatalf("could not insert genesis accounts: %v", err)
    }

    // insert the test blocks, which will execute all transactions
    chain := ethereum.ChainManager()
    if err := chain.InsertChain(test.Blocks); err != nil {
        utils.Fatalf("Block Test load error: %v %T", err, err)
    } else {
        fmt.Println("Block Test chain loaded")
    }

    if err := test.ValidatePostState(statedb); err != nil {
        utils.Fatalf("post state validation failed: %v", err)
    }
    fmt.Println("Block Test post state validated, starting ethereum.")

    if startrpc == "rpc" {
        startEth(ctx, ethereum)
        utils.StartRPC(ethereum, ctx)
        ethereum.WaitForShutdown()
    } else {
        startEth(ctx, ethereum)
    }
}