|
| 1 | +// Copyright 2015 The go-ethereum Authors |
| 2 | +// This file is part of go-ethereum. |
| 3 | +// |
| 4 | +// go-ethereum is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// go-ethereum is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +// gethrpctest is a command to run the external RPC tests. |
| 18 | +package main |
| 19 | + |
| 20 | +import ( |
| 21 | + "flag" |
| 22 | + "io/ioutil" |
| 23 | + "log" |
| 24 | + "os" |
| 25 | + "os/signal" |
| 26 | + |
| 27 | + "github.com/ethereum/go-ethereum/accounts" |
| 28 | + "github.com/ethereum/go-ethereum/crypto" |
| 29 | + "github.com/ethereum/go-ethereum/eth" |
| 30 | + "github.com/ethereum/go-ethereum/ethdb" |
| 31 | + "github.com/ethereum/go-ethereum/node" |
| 32 | + "github.com/ethereum/go-ethereum/rpc/api" |
| 33 | + "github.com/ethereum/go-ethereum/rpc/codec" |
| 34 | + "github.com/ethereum/go-ethereum/rpc/comms" |
| 35 | + "github.com/ethereum/go-ethereum/tests" |
| 36 | + "github.com/ethereum/go-ethereum/whisper" |
| 37 | + "github.com/ethereum/go-ethereum/xeth" |
| 38 | +) |
| 39 | + |
| 40 | +const defaultTestKey = "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" |
| 41 | + |
| 42 | +var ( |
| 43 | + testFile = flag.String("json", "", "Path to the .json test file to load") |
| 44 | + testName = flag.String("test", "", "Name of the test from the .json file to run") |
| 45 | + testKey = flag.String("key", defaultTestKey, "Private key of a test account to inject") |
| 46 | +) |
| 47 | + |
| 48 | +var ( |
| 49 | + ethereumServiceId = "ethereum" |
| 50 | + whisperServiceId = "whisper" |
| 51 | +) |
| 52 | + |
| 53 | +func main() { |
| 54 | + flag.Parse() |
| 55 | + |
| 56 | + // Load the test suite to run the RPC against |
| 57 | + tests, err := tests.LoadBlockTests(*testFile) |
| 58 | + if err != nil { |
| 59 | + log.Fatalf("Failed to load test suite: %v", err) |
| 60 | + } |
| 61 | + test, found := tests[*testName] |
| 62 | + if !found { |
| 63 | + log.Fatalf("Requested test (%s) not found within suite", *testName) |
| 64 | + } |
| 65 | + // Create the protocol stack to run the test with |
| 66 | + keydir, err := ioutil.TempDir("", "") |
| 67 | + if err != nil { |
| 68 | + log.Fatalf("Failed to create temporary keystore directory: %v", err) |
| 69 | + } |
| 70 | + defer os.RemoveAll(keydir) |
| 71 | + |
| 72 | + stack, err := MakeSystemNode(keydir, *testKey, test) |
| 73 | + if err != nil { |
| 74 | + log.Fatalf("Failed to assemble test stack: %v", err) |
| 75 | + } |
| 76 | + if err := stack.Start(); err != nil { |
| 77 | + log.Fatalf("Failed to start test node: %v", err) |
| 78 | + } |
| 79 | + defer stack.Stop() |
| 80 | + |
| 81 | + log.Println("Test node started...") |
| 82 | + |
| 83 | + // Make sure the tests contained within the suite pass |
| 84 | + if err := RunTest(stack, test); err != nil { |
| 85 | + log.Fatalf("Failed to run the pre-configured test: %v", err) |
| 86 | + } |
| 87 | + log.Println("Initial test suite passed...") |
| 88 | + |
| 89 | + // Start the RPC interface and wait until terminated |
| 90 | + if err := StartRPC(stack); err != nil { |
| 91 | + log.Fatalf("Failed to start RPC instarface: %v", err) |
| 92 | + } |
| 93 | + log.Println("RPC Interface started, accepting requests...") |
| 94 | + |
| 95 | + quit := make(chan os.Signal, 1) |
| 96 | + signal.Notify(quit, os.Interrupt) |
| 97 | + <-quit |
| 98 | +} |
| 99 | + |
| 100 | +// MakeSystemNode configures a protocol stack for the RPC tests based on a given |
| 101 | +// keystore path and initial pre-state. |
| 102 | +func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node.Node, error) { |
| 103 | + // Create a networkless protocol stack |
| 104 | + stack, err := node.New(&node.Config{NoDiscovery: true}) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + // Create the keystore and inject an unlocked account if requested |
| 109 | + keystore := crypto.NewKeyStorePassphrase(keydir, crypto.StandardScryptN, crypto.StandardScryptP) |
| 110 | + accman := accounts.NewManager(keystore) |
| 111 | + |
| 112 | + if len(privkey) > 0 { |
| 113 | + key, err := crypto.HexToECDSA(privkey) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + if err := keystore.StoreKey(crypto.NewKeyFromECDSA(key), ""); err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + if err := accman.Unlock(crypto.NewKeyFromECDSA(key).Address, ""); err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + } |
| 124 | + // Initialize and register the Ethereum protocol |
| 125 | + db, _ := ethdb.NewMemDatabase() |
| 126 | + if _, err := test.InsertPreState(db, accman); err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + ethConf := ð.Config{ |
| 130 | + TestGenesisState: db, |
| 131 | + TestGenesisBlock: test.Genesis, |
| 132 | + AccountManager: accman, |
| 133 | + } |
| 134 | + if err := stack.Register(ethereumServiceId, func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + // Initialize and register the Whisper protocol |
| 138 | + if err := stack.Register(whisperServiceId, func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + return stack, nil |
| 142 | +} |
| 143 | + |
| 144 | +// RunTest executes the specified test against an already pre-configured protocol |
| 145 | +// stack to ensure basic checks pass before running RPC tests. |
| 146 | +func RunTest(stack *node.Node, test *tests.BlockTest) error { |
| 147 | + blockchain := stack.Service(ethereumServiceId).(*eth.Ethereum).BlockChain() |
| 148 | + |
| 149 | + // Process the blocks and verify the imported headers |
| 150 | + blocks, err := test.TryBlocksInsert(blockchain) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + if err := test.ValidateImportedHeaders(blockchain, blocks); err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + // Retrieve the assembled state and validate it |
| 158 | + stateDb, err := blockchain.State() |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + if err := test.ValidatePostState(stateDb); err != nil { |
| 163 | + return err |
| 164 | + } |
| 165 | + return nil |
| 166 | +} |
| 167 | + |
| 168 | +// StartRPC initializes an RPC interface to the given protocol stack. |
| 169 | +func StartRPC(stack *node.Node) error { |
| 170 | + config := comms.HttpConfig{ |
| 171 | + ListenAddress: "127.0.0.1", |
| 172 | + ListenPort: 8545, |
| 173 | + } |
| 174 | + xeth := xeth.New(stack, nil) |
| 175 | + codec := codec.JSON |
| 176 | + |
| 177 | + apis, err := api.ParseApiString(comms.DefaultHttpRpcApis, codec, xeth, stack) |
| 178 | + if err != nil { |
| 179 | + return err |
| 180 | + } |
| 181 | + return comms.StartHttp(config, codec, api.Merge(apis...)) |
| 182 | +} |
0 commit comments