github.com/theQRL/go-zond@v0.2.1/core/block_validator_test.go (about)

     1  // Copyright 2015 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 core
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/theQRL/go-zond/consensus/beacon"
    24  	"github.com/theQRL/go-zond/core/rawdb"
    25  	"github.com/theQRL/go-zond/core/types"
    26  	"github.com/theQRL/go-zond/core/vm"
    27  	"github.com/theQRL/go-zond/params"
    28  )
    29  
    30  // Tests that simple header verification works, for both good and bad blocks.
    31  func TestHeaderVerification(t *testing.T) {
    32  	testHeaderVerification(t, rawdb.HashScheme)
    33  	testHeaderVerification(t, rawdb.PathScheme)
    34  }
    35  
    36  func testHeaderVerification(t *testing.T, scheme string) {
    37  	// Create a simple chain to verify
    38  	var (
    39  		gspec        = &Genesis{Config: params.TestChainConfig}
    40  		_, blocks, _ = GenerateChainWithGenesis(gspec, beacon.NewFaker(), 8, nil)
    41  	)
    42  	headers := make([]*types.Header, len(blocks))
    43  	for i, block := range blocks {
    44  		headers[i] = block.Header()
    45  	}
    46  	// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
    47  	chain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, beacon.NewFaker(), vm.Config{}, nil)
    48  	defer chain.Stop()
    49  
    50  	for i := 0; i < len(blocks); i++ {
    51  		for j, valid := range []bool{true, false} {
    52  			var results <-chan error
    53  
    54  			if valid {
    55  				engine := beacon.NewFaker()
    56  				_, results = engine.VerifyHeaders(chain, []*types.Header{headers[i]})
    57  			} else {
    58  				engine := beacon.NewFakeFailer(headers[i].Number.Uint64())
    59  				_, results = engine.VerifyHeaders(chain, []*types.Header{headers[i]})
    60  			}
    61  			// Wait for the verification result
    62  			select {
    63  			case result := <-results:
    64  				if (result == nil) != valid {
    65  					t.Errorf("test %d.%d: validity mismatch: have %v, want %v", i, j, result, valid)
    66  				}
    67  			case <-time.After(time.Second):
    68  				t.Fatalf("test %d.%d: verification timeout", i, j)
    69  			}
    70  			// Make sure no more data is returned
    71  			select {
    72  			case result := <-results:
    73  				t.Fatalf("test %d.%d: unexpected result returned: %v", i, j, result)
    74  			case <-time.After(25 * time.Millisecond):
    75  			}
    76  		}
    77  		chain.InsertChain(blocks[i : i+1])
    78  	}
    79  }
    80  
    81  func TestCalcGasLimit(t *testing.T) {
    82  	for i, tc := range []struct {
    83  		pGasLimit uint64
    84  		max       uint64
    85  		min       uint64
    86  	}{
    87  		{20000000, 20019530, 19980470},
    88  		{40000000, 40039061, 39960939},
    89  	} {
    90  		// Increase
    91  		if have, want := CalcGasLimit(tc.pGasLimit, 2*tc.pGasLimit), tc.max; have != want {
    92  			t.Errorf("test %d: have %d want <%d", i, have, want)
    93  		}
    94  		// Decrease
    95  		if have, want := CalcGasLimit(tc.pGasLimit, 0), tc.min; have != want {
    96  			t.Errorf("test %d: have %d want >%d", i, have, want)
    97  		}
    98  		// Small decrease
    99  		if have, want := CalcGasLimit(tc.pGasLimit, tc.pGasLimit-1), tc.pGasLimit-1; have != want {
   100  			t.Errorf("test %d: have %d want %d", i, have, want)
   101  		}
   102  		// Small increase
   103  		if have, want := CalcGasLimit(tc.pGasLimit, tc.pGasLimit+1), tc.pGasLimit+1; have != want {
   104  			t.Errorf("test %d: have %d want %d", i, have, want)
   105  		}
   106  		// No change
   107  		if have, want := CalcGasLimit(tc.pGasLimit, tc.pGasLimit), tc.pGasLimit; have != want {
   108  			t.Errorf("test %d: have %d want %d", i, have, want)
   109  		}
   110  	}
   111  }