github.com/daragao/go-ethereum@v1.8.14-0.20180809141559-45eaef243198/consensus/ethash/ethash_test.go (about)

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