suite_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "time"
  21. "github.com/ethereum/go-ethereum/eth"
  22. "github.com/ethereum/go-ethereum/eth/ethconfig"
  23. "github.com/ethereum/go-ethereum/internal/utesting"
  24. "github.com/ethereum/go-ethereum/node"
  25. "github.com/ethereum/go-ethereum/p2p"
  26. )
  27. var (
  28. genesisFile = "./testdata/genesis.json"
  29. halfchainFile = "./testdata/halfchain.rlp"
  30. fullchainFile = "./testdata/chain.rlp"
  31. )
  32. func TestEthSuite(t *testing.T) {
  33. geth, err := runGeth()
  34. if err != nil {
  35. t.Fatalf("could not run geth: %v", err)
  36. }
  37. defer geth.Close()
  38. suite, err := NewSuite(geth.Server().Self(), fullchainFile, genesisFile)
  39. if err != nil {
  40. t.Fatalf("could not create new test suite: %v", err)
  41. }
  42. for _, test := range suite.AllEthTests() {
  43. t.Run(test.Name, func(t *testing.T) {
  44. result := utesting.RunTAP([]utesting.Test{{Name: test.Name, Fn: test.Fn}}, os.Stdout)
  45. if result[0].Failed {
  46. t.Fatal()
  47. }
  48. })
  49. }
  50. }
  51. // runGeth creates and starts a geth node
  52. func runGeth() (*node.Node, error) {
  53. stack, err := node.New(&node.Config{
  54. P2P: p2p.Config{
  55. ListenAddr: "127.0.0.1:0",
  56. NoDiscovery: true,
  57. MaxPeers: 10, // in case a test requires multiple connections, can be changed in the future
  58. NoDial: true,
  59. },
  60. })
  61. if err != nil {
  62. return nil, err
  63. }
  64. err = setupGeth(stack)
  65. if err != nil {
  66. stack.Close()
  67. return nil, err
  68. }
  69. if err = stack.Start(); err != nil {
  70. stack.Close()
  71. return nil, err
  72. }
  73. return stack, nil
  74. }
  75. func setupGeth(stack *node.Node) error {
  76. chain, err := loadChain(halfchainFile, genesisFile)
  77. if err != nil {
  78. return err
  79. }
  80. backend, err := eth.New(stack, &ethconfig.Config{
  81. Genesis: &chain.genesis,
  82. NetworkId: chain.genesis.Config.ChainID.Uint64(), // 19763
  83. DatabaseCache: 10,
  84. TrieCleanCache: 10,
  85. TrieCleanCacheJournal: "",
  86. TrieCleanCacheRejournal: 60 * time.Minute,
  87. TrieDirtyCache: 16,
  88. TrieTimeout: 60 * time.Minute,
  89. SnapshotCache: 10,
  90. })
  91. if err != nil {
  92. return err
  93. }
  94. _, err = backend.BlockChain().InsertChain(chain.blocks[1:])
  95. return err
  96. }