github.com/codysnider/go-ethereum@v1.10.18-0.20220420071915-14f4ae99222a/cmd/devp2p/internal/ethtest/suite_test.go (about)

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