github.com/ebakus/go-ebakus@v1.0.5-0.20200520105415-dbccef9ec421/consensus/ethash/ethash_test.go (about)

     1  // Copyright 2019 The ebakus/go-ebakus Authors
     2  // This file is part of the ebakus/go-ebakus library.
     3  //
     4  // The ebakus/go-ebakus 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 ebakus/go-ebakus 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 ebakus/go-ebakus library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package ethash
    18  
    19  import (
    20  	"io/ioutil"
    21  	"math/big"
    22  	"math/rand"
    23  	"os"
    24  	"sync"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/ebakus/go-ebakus/common"
    29  	"github.com/ebakus/go-ebakus/common/hexutil"
    30  	"github.com/ebakus/go-ebakus/core/types"
    31  )
    32  
    33  // Tests that ethash works correctly in test mode.
    34  func TestTestMode(t *testing.T) {
    35  	header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
    36  
    37  	ethash := NewTester(nil, false)
    38  	defer ethash.Close()
    39  
    40  	results := make(chan *types.Block)
    41  	err := ethash.Seal(nil, types.NewBlockWithHeader(header), results, nil)
    42  	if err != nil {
    43  		t.Fatalf("failed to seal block: %v", err)
    44  	}
    45  	select {
    46  	case block := <-results:
    47  		if err := ethash.VerifySeal(nil, header); err != nil {
    48  			t.Fatalf("unexpected verification error: %v", err)
    49  		}
    50  	case <-time.NewTimer(time.Second).C:
    51  		t.Error("sealing result timeout")
    52  	}
    53  }
    54  
    55  // This test checks that cache lru logic doesn't crash under load.
    56  // It reproduces https://github.com/ebakus/go-ebakus/issues/14943
    57  func TestCacheFileEvict(t *testing.T) {
    58  	tmpdir, err := ioutil.TempDir("", "ethash-test")
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	defer os.RemoveAll(tmpdir)
    63  	e := New(Config{CachesInMem: 3, CachesOnDisk: 10, CacheDir: tmpdir, PowMode: ModeTest}, nil, false)
    64  	defer e.Close()
    65  
    66  	workers := 8
    67  	epochs := 100
    68  	var wg sync.WaitGroup
    69  	wg.Add(workers)
    70  	for i := 0; i < workers; i++ {
    71  		go verifyTest(&wg, e, i, epochs)
    72  	}
    73  	wg.Wait()
    74  }
    75  
    76  func verifyTest(wg *sync.WaitGroup, e *Ethash, workerIndex, epochs int) {
    77  	defer wg.Done()
    78  
    79  	const wiggle = 4 * epochLength
    80  	r := rand.New(rand.NewSource(int64(workerIndex)))
    81  	for epoch := 0; epoch < epochs; epoch++ {
    82  		block := int64(epoch)*epochLength - wiggle/2 + r.Int63n(wiggle)
    83  		if block < 0 {
    84  			block = 0
    85  		}
    86  		header := &types.Header{Number: big.NewInt(block), Difficulty: big.NewInt(100)}
    87  		e.VerifySeal(nil, header)
    88  	}
    89  }
    90  
    91  func TestRemoteSealer(t *testing.T) {
    92  	ethash := NewTester(nil, false)
    93  	defer ethash.Close()
    94  
    95  	api := &API{ethash}
    96  	if _, err := api.GetWork(); err != errNoMiningWork {
    97  		t.Error("expect to return an error indicate there is no mining work")
    98  	}
    99  	header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
   100  	block := types.NewBlockWithHeader(header)
   101  	sealhash := ethash.SealHash(header)
   102  
   103  	// Push new work.
   104  	results := make(chan *types.Block)
   105  	ethash.Seal(nil, block, results, nil)
   106  
   107  	var (
   108  		work [4]string
   109  		err  error
   110  	)
   111  	if work, err = api.GetWork(); err != nil || work[0] != sealhash.Hex() {
   112  		t.Error("expect to return a mining work has same hash")
   113  	}
   114  
   115  	if res := api.SubmitWork(types.BlockNonce{}, sealhash, common.Hash{}); res {
   116  		t.Error("expect to return false when submit a fake solution")
   117  	}
   118  	// Push new block with same block number to replace the original one.
   119  	header = &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(1000)}
   120  	block = types.NewBlockWithHeader(header)
   121  	sealhash = ethash.SealHash(header)
   122  	ethash.Seal(nil, block, results, nil)
   123  
   124  	if work, err = api.GetWork(); err != nil || work[0] != sealhash.Hex() {
   125  		t.Error("expect to return the latest pushed work")
   126  	}
   127  }
   128  
   129  func TestHashRate(t *testing.T) {
   130  	var (
   131  		hashrate = []hexutil.Uint64{100, 200, 300}
   132  		expect   uint64
   133  		ids      = []common.Hash{common.HexToHash("a"), common.HexToHash("b"), common.HexToHash("c")}
   134  	)
   135  	ethash := NewTester(nil, false)
   136  	defer ethash.Close()
   137  
   138  	if tot := ethash.Hashrate(); tot != 0 {
   139  		t.Error("expect the result should be zero")
   140  	}
   141  
   142  	api := &API{ethash}
   143  	for i := 0; i < len(hashrate); i += 1 {
   144  		if res := api.SubmitHashRate(hashrate[i], ids[i]); !res {
   145  			t.Error("remote miner submit hashrate failed")
   146  		}
   147  		expect += uint64(hashrate[i])
   148  	}
   149  	if tot := ethash.Hashrate(); tot != float64(expect) {
   150  		t.Error("expect total hashrate should be same")
   151  	}
   152  }
   153  
   154  func TestClosedRemoteSealer(t *testing.T) {
   155  	ethash := NewTester(nil, false)
   156  	time.Sleep(1 * time.Second) // ensure exit channel is listening
   157  	ethash.Close()
   158  
   159  	api := &API{ethash}
   160  	if _, err := api.GetWork(); err != errEthashStopped {
   161  		t.Error("expect to return an error to indicate ethash is stopped")
   162  	}
   163  
   164  	if res := api.SubmitHashRate(hexutil.Uint64(100), common.HexToHash("a")); res {
   165  		t.Error("expect to return false when submit hashrate to a stopped ethash")
   166  	}
   167  }