gitee.com/liu-zhao234568/cntest@v1.0.0/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  	"gitee.com/liu-zhao234568/cntest/eth"
    25  	"gitee.com/liu-zhao234568/cntest/eth/ethconfig"
    26  	"gitee.com/liu-zhao234568/cntest/internal/utesting"
    27  	"gitee.com/liu-zhao234568/cntest/node"
    28  	"gitee.com/liu-zhao234568/cntest/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.AllEthTests() {
    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  // runGeth creates and starts a geth node
    59  func runGeth() (*node.Node, error) {
    60  	stack, err := node.New(&node.Config{
    61  		P2P: p2p.Config{
    62  			ListenAddr:  "127.0.0.1:0",
    63  			NoDiscovery: true,
    64  			MaxPeers:    10, // in case a test requires multiple connections, can be changed in the future
    65  			NoDial:      true,
    66  		},
    67  	})
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	err = setupGeth(stack)
    73  	if err != nil {
    74  		stack.Close()
    75  		return nil, err
    76  	}
    77  	if err = stack.Start(); err != nil {
    78  		stack.Close()
    79  		return nil, err
    80  	}
    81  	return stack, nil
    82  }
    83  
    84  func setupGeth(stack *node.Node) error {
    85  	chain, err := loadChain(halfchainFile, genesisFile)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	backend, err := eth.New(stack, &ethconfig.Config{
    91  		Genesis:                 &chain.genesis,
    92  		NetworkId:               chain.genesis.Config.ChainID.Uint64(), // 19763
    93  		DatabaseCache:           10,
    94  		TrieCleanCache:          10,
    95  		TrieCleanCacheJournal:   "",
    96  		TrieCleanCacheRejournal: 60 * time.Minute,
    97  		TrieDirtyCache:          16,
    98  		TrieTimeout:             60 * time.Minute,
    99  		SnapshotCache:           10,
   100  	})
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	_, err = backend.BlockChain().InsertChain(chain.blocks[1:])
   106  	return err
   107  }