2015-11-17 11:39:18 +00:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
// gethrpctest is a command to run the external RPC tests.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2015-12-16 09:58:01 +00:00
|
|
|
|
2017-01-24 09:49:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
2015-11-17 11:39:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2015-10-15 14:07:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
2015-11-17 11:39:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
2016-04-12 13:44:42 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2015-11-17 11:39:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/tests"
|
2016-10-29 12:11:37 +00:00
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv2"
|
2015-11-17 11:39:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultTestKey = "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"
|
|
|
|
|
|
|
|
var (
|
|
|
|
testFile = flag.String("json", "", "Path to the .json test file to load")
|
|
|
|
testName = flag.String("test", "", "Name of the test from the .json file to run")
|
|
|
|
testKey = flag.String("key", defaultTestKey, "Private key of a test account to inject")
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2016-01-27 16:01:30 +00:00
|
|
|
// Enable logging errors, we really do want to see those
|
|
|
|
glog.SetV(2)
|
|
|
|
glog.SetToStderr(true)
|
|
|
|
|
2015-11-17 11:39:18 +00:00
|
|
|
// Load the test suite to run the RPC against
|
|
|
|
tests, err := tests.LoadBlockTests(*testFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to load test suite: %v", err)
|
|
|
|
}
|
|
|
|
test, found := tests[*testName]
|
|
|
|
if !found {
|
|
|
|
log.Fatalf("Requested test (%s) not found within suite", *testName)
|
|
|
|
}
|
|
|
|
|
2016-08-15 16:38:32 +00:00
|
|
|
stack, err := MakeSystemNode(*testKey, test)
|
2015-11-17 11:39:18 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to assemble test stack: %v", err)
|
|
|
|
}
|
|
|
|
if err := stack.Start(); err != nil {
|
|
|
|
log.Fatalf("Failed to start test node: %v", err)
|
|
|
|
}
|
|
|
|
defer stack.Stop()
|
|
|
|
|
|
|
|
log.Println("Test node started...")
|
|
|
|
|
|
|
|
// Make sure the tests contained within the suite pass
|
|
|
|
if err := RunTest(stack, test); err != nil {
|
|
|
|
log.Fatalf("Failed to run the pre-configured test: %v", err)
|
|
|
|
}
|
|
|
|
log.Println("Initial test suite passed...")
|
|
|
|
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, os.Interrupt)
|
|
|
|
<-quit
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeSystemNode configures a protocol stack for the RPC tests based on a given
|
|
|
|
// keystore path and initial pre-state.
|
2016-08-15 16:38:32 +00:00
|
|
|
func MakeSystemNode(privkey string, test *tests.BlockTest) (*node.Node, error) {
|
2015-11-17 11:39:18 +00:00
|
|
|
// Create a networkless protocol stack
|
2016-02-05 11:45:36 +00:00
|
|
|
stack, err := node.New(&node.Config{
|
2016-08-15 16:38:32 +00:00
|
|
|
UseLightweightKDF: true,
|
2016-08-18 11:28:17 +00:00
|
|
|
IPCPath: node.DefaultIPCEndpoint(""),
|
2016-09-16 09:53:50 +00:00
|
|
|
HTTPHost: node.DefaultHTTPHost,
|
|
|
|
HTTPPort: node.DefaultHTTPPort,
|
2016-08-15 16:38:32 +00:00
|
|
|
HTTPModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
|
2016-09-16 09:53:50 +00:00
|
|
|
WSHost: node.DefaultWSHost,
|
|
|
|
WSPort: node.DefaultWSPort,
|
2016-08-15 16:38:32 +00:00
|
|
|
WSModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
|
|
|
|
NoDiscovery: true,
|
2016-02-05 11:45:36 +00:00
|
|
|
})
|
2015-11-17 11:39:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Create the keystore and inject an unlocked account if requested
|
2017-02-07 10:47:34 +00:00
|
|
|
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
|
2017-01-24 09:49:20 +00:00
|
|
|
|
2015-11-17 11:39:18 +00:00
|
|
|
if len(privkey) > 0 {
|
|
|
|
key, err := crypto.HexToECDSA(privkey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-24 09:49:20 +00:00
|
|
|
a, err := ks.ImportECDSA(key, "")
|
2016-03-02 12:57:15 +00:00
|
|
|
if err != nil {
|
2015-11-17 11:39:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-24 09:49:20 +00:00
|
|
|
if err := ks.Unlock(a, ""); err != nil {
|
2015-11-17 11:39:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Initialize and register the Ethereum protocol
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2016-03-21 22:00:39 +00:00
|
|
|
if _, err := test.InsertPreState(db); err != nil {
|
2015-11-17 11:39:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ethConf := ð.Config{
|
|
|
|
TestGenesisState: db,
|
|
|
|
TestGenesisBlock: test.Genesis,
|
2016-10-20 11:36:29 +00:00
|
|
|
ChainConfig: ¶ms.ChainConfig{HomesteadBlock: params.MainNetHomesteadBlock},
|
2015-11-17 11:39:18 +00:00
|
|
|
}
|
2015-11-26 16:35:44 +00:00
|
|
|
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil {
|
2015-11-17 11:39:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Initialize and register the Whisper protocol
|
2015-11-26 16:35:44 +00:00
|
|
|
if err := stack.Register(func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil {
|
2015-11-17 11:39:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return stack, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunTest executes the specified test against an already pre-configured protocol
|
|
|
|
// stack to ensure basic checks pass before running RPC tests.
|
|
|
|
func RunTest(stack *node.Node, test *tests.BlockTest) error {
|
2016-06-30 10:03:26 +00:00
|
|
|
var ethereum *eth.Ethereum
|
2015-11-26 16:35:44 +00:00
|
|
|
stack.Service(ðereum)
|
2015-11-17 16:33:25 +00:00
|
|
|
blockchain := ethereum.BlockChain()
|
2015-11-17 11:39:18 +00:00
|
|
|
|
|
|
|
// Process the blocks and verify the imported headers
|
|
|
|
blocks, err := test.TryBlocksInsert(blockchain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := test.ValidateImportedHeaders(blockchain, blocks); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Retrieve the assembled state and validate it
|
|
|
|
stateDb, err := blockchain.State()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := test.ValidatePostState(stateDb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|