github.com/theQRL/go-zond@v0.1.1/cmd/devp2p/internal/ethtest/suite_test.go (about)

     1  // Copyright 2021 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  
    17  package ethtest
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/theQRL/go-zond/zond"
    25  	"github.com/theQRL/go-zond/zond/ethconfig"
    26  	"github.com/theQRL/go-zond/internal/utesting"
    27  	"github.com/theQRL/go-zond/node"
    28  	"github.com/theQRL/go-zond/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.EthTests() {
    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 := zond.New(stack, &ethconfig.Config{
   112  		Genesis:        &chain.genesis,
   113  		NetworkId:      chain.genesis.Config.ChainID.Uint64(), // 19763
   114  		DatabaseCache:  10,
   115  		TrieCleanCache: 10,
   116  		TrieDirtyCache: 16,
   117  		TrieTimeout:    60 * time.Minute,
   118  		SnapshotCache:  10,
   119  	})
   120  	if err != nil {
   121  		return err
   122  	}
   123  
   124  	_, err = backend.BlockChain().InsertChain(chain.blocks[1:])
   125  	return err
   126  }