suite_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser 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. // The go-ethereum library 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 Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package ethtest
  17. import (
  18. "os"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/eth"
  21. "github.com/ethereum/go-ethereum/eth/ethconfig"
  22. "github.com/ethereum/go-ethereum/internal/utesting"
  23. "github.com/ethereum/go-ethereum/node"
  24. "github.com/ethereum/go-ethereum/p2p"
  25. )
  26. var (
  27. genesisFile = "./testdata/genesis.json"
  28. halfchainFile = "./testdata/halfchain.rlp"
  29. fullchainFile = "./testdata/chain.rlp"
  30. )
  31. func TestEthSuite(t *testing.T) {
  32. geth, err := runGeth()
  33. if err != nil {
  34. t.Fatalf("could not run geth: %v", err)
  35. }
  36. defer geth.Close()
  37. suite, err := NewSuite(geth.Server().Self(), fullchainFile, genesisFile)
  38. if err != nil {
  39. t.Fatalf("could not create new test suite: %v", err)
  40. }
  41. for _, test := range suite.AllEthTests() {
  42. t.Run(test.Name, func(t *testing.T) {
  43. result := utesting.RunTAP([]utesting.Test{{Name: test.Name, Fn: test.Fn}}, os.Stdout)
  44. if result[0].Failed {
  45. t.Fatal()
  46. }
  47. })
  48. }
  49. }
  50. // runGeth creates and starts a geth node
  51. func runGeth() (*node.Node, error) {
  52. stack, err := node.New(&node.Config{
  53. P2P: p2p.Config{
  54. ListenAddr: "127.0.0.1:0",
  55. NoDiscovery: true,
  56. MaxPeers: 10, // in case a test requires multiple connections, can be changed in the future
  57. NoDial: true,
  58. },
  59. })
  60. if err != nil {
  61. return nil, err
  62. }
  63. err = setupGeth(stack)
  64. if err != nil {
  65. stack.Close()
  66. return nil, err
  67. }
  68. if err = stack.Start(); err != nil {
  69. stack.Close()
  70. return nil, err
  71. }
  72. return stack, nil
  73. }
  74. func setupGeth(stack *node.Node) error {
  75. chain, err := loadChain(halfchainFile, genesisFile)
  76. if err != nil {
  77. return err
  78. }
  79. backend, err := eth.New(stack, &ethconfig.Config{
  80. Genesis: &chain.genesis,
  81. NetworkId: chain.genesis.Config.ChainID.Uint64(), // 19763
  82. })
  83. if err != nil {
  84. return err
  85. }
  86. _, err = backend.BlockChain().InsertChain(chain.blocks[1:])
  87. return err
  88. }