main.go 5.0 KB

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