github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/cmd/gathrpctest/main.go (about)

     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  // gathrpctest is a command to run the external RPC tests.
    18  package main
    19  
    20  import (
    21  	"flag"
    22  	"log"
    23  	"os"
    24  	"os/signal"
    25  
    26  	"github.com/atheioschain/go-atheios/accounts/keystore"
    27  	"github.com/atheioschain/go-atheios/crypto"
    28  	"github.com/atheioschain/go-atheios/eth"
    29  	"github.com/atheioschain/go-atheios/ethdb"
    30  	"github.com/atheioschain/go-atheios/logger/glog"
    31  	"github.com/atheioschain/go-atheios/node"
    32  	"github.com/atheioschain/go-atheios/params"
    33  	"github.com/atheioschain/go-atheios/tests"
    34  	whisper "github.com/atheioschain/go-atheios/whisper/whisperv2"
    35  )
    36  
    37  const defaultTestKey = "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"
    38  
    39  var (
    40  	testFile = flag.String("json", "", "Path to the .json test file to load")
    41  	testName = flag.String("test", "", "Name of the test from the .json file to run")
    42  	testKey  = flag.String("key", defaultTestKey, "Private key of a test account to inject")
    43  )
    44  
    45  func main() {
    46  	flag.Parse()
    47  
    48  	// Enable logging errors, we really do want to see those
    49  	glog.SetV(2)
    50  	glog.SetToStderr(true)
    51  
    52  	// Load the test suite to run the RPC against
    53  	tests, err := tests.LoadBlockTests(*testFile)
    54  	if err != nil {
    55  		log.Fatalf("Failed to load test suite: %v", err)
    56  	}
    57  	test, found := tests[*testName]
    58  	if !found {
    59  		log.Fatalf("Requested test (%s) not found within suite", *testName)
    60  	}
    61  
    62  	stack, err := MakeSystemNode(*testKey, test)
    63  	if err != nil {
    64  		log.Fatalf("Failed to assemble test stack: %v", err)
    65  	}
    66  	if err := stack.Start(); err != nil {
    67  		log.Fatalf("Failed to start test node: %v", err)
    68  	}
    69  	defer stack.Stop()
    70  
    71  	log.Println("Test node started...")
    72  
    73  	// Make sure the tests contained within the suite pass
    74  	if err := RunTest(stack, test); err != nil {
    75  		log.Fatalf("Failed to run the pre-configured test: %v", err)
    76  	}
    77  	log.Println("Initial test suite passed...")
    78  
    79  	quit := make(chan os.Signal, 1)
    80  	signal.Notify(quit, os.Interrupt)
    81  	<-quit
    82  }
    83  
    84  // MakeSystemNode configures a protocol stack for the RPC tests based on a given
    85  // keystore path and initial pre-state.
    86  func MakeSystemNode(privkey string, test *tests.BlockTest) (*node.Node, error) {
    87  	// Create a networkless protocol stack
    88  	stack, err := node.New(&node.Config{
    89  		UseLightweightKDF: true,
    90  		IPCPath:           node.DefaultIPCEndpoint(""),
    91  		HTTPHost:          node.DefaultHTTPHost,
    92  		HTTPPort:          node.DefaultHTTPPort,
    93  		HTTPModules:       []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
    94  		WSHost:            node.DefaultWSHost,
    95  		WSPort:            node.DefaultWSPort,
    96  		WSModules:         []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
    97  		NoDiscovery:       true,
    98  	})
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	// Create the keystore and inject an unlocked account if requested
   103  	ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
   104  
   105  	if len(privkey) > 0 {
   106  		key, err := crypto.HexToECDSA(privkey)
   107  		if err != nil {
   108  			return nil, err
   109  		}
   110  		a, err := ks.ImportECDSA(key, "")
   111  		if err != nil {
   112  			return nil, err
   113  		}
   114  		if err := ks.Unlock(a, ""); err != nil {
   115  			return nil, err
   116  		}
   117  	}
   118  	// Initialize and register the Ethereum protocol
   119  	db, _ := ethdb.NewMemDatabase()
   120  	if _, err := test.InsertPreState(db); err != nil {
   121  		return nil, err
   122  	}
   123  	ethConf := &eth.Config{
   124  		TestGenesisState: db,
   125  		TestGenesisBlock: test.Genesis,
   126  		ChainConfig:      &params.ChainConfig{HomesteadBlock: params.MainNetHomesteadBlock},
   127  	}
   128  	if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil {
   129  		return nil, err
   130  	}
   131  	// Initialize and register the Whisper protocol
   132  	if err := stack.Register(func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil {
   133  		return nil, err
   134  	}
   135  	return stack, nil
   136  }
   137  
   138  // RunTest executes the specified test against an already pre-configured protocol
   139  // stack to ensure basic checks pass before running RPC tests.
   140  func RunTest(stack *node.Node, test *tests.BlockTest) error {
   141  	var ethereum *eth.Ethereum
   142  	stack.Service(&ethereum)
   143  	blockchain := ethereum.BlockChain()
   144  
   145  	// Process the blocks and verify the imported headers
   146  	blocks, err := test.TryBlocksInsert(blockchain)
   147  	if err != nil {
   148  		return err
   149  	}
   150  	if err := test.ValidateImportedHeaders(blockchain, blocks); err != nil {
   151  		return err
   152  	}
   153  	// Retrieve the assembled state and validate it
   154  	stateDb, err := blockchain.State()
   155  	if err != nil {
   156  		return err
   157  	}
   158  	if err := test.ValidatePostState(stateDb); err != nil {
   159  		return err
   160  	}
   161  	return nil
   162  }