github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/consensus/ethash/ethash_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2017 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package ethash
    26  
    27  import (
    28  	"io/ioutil"
    29  	"math/big"
    30  	"math/rand"
    31  	"os"
    32  	"sync"
    33  	"testing"
    34  	"time"
    35  
    36  	"github.com/ethereum/go-ethereum/common"
    37  	"github.com/ethereum/go-ethereum/common/hexutil"
    38  	"github.com/ethereum/go-ethereum/core/types"
    39  )
    40  
    41  //测试ethash在测试模式下是否正常工作。
    42  func TestTestMode(t *testing.T) {
    43  	header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
    44  
    45  	ethash := NewTester(nil)
    46  	defer ethash.Close()
    47  
    48  	block, err := ethash.Seal(nil, types.NewBlockWithHeader(header), nil)
    49  	if err != nil {
    50  		t.Fatalf("failed to seal block: %v", err)
    51  	}
    52  	header.Nonce = types.EncodeNonce(block.Nonce())
    53  	header.MixDigest = block.MixDigest()
    54  	if err := ethash.VerifySeal(nil, header); err != nil {
    55  		t.Fatalf("unexpected verification error: %v", err)
    56  	}
    57  }
    58  
    59  //此测试检查高速缓存LRU逻辑在负载下是否崩溃。
    60  //它复制了https://github.com/ethereum/go-ethereum/issues/14943
    61  func TestCacheFileEvict(t *testing.T) {
    62  	tmpdir, err := ioutil.TempDir("", "ethash-test")
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	defer os.RemoveAll(tmpdir)
    67  	e := New(Config{CachesInMem: 3, CachesOnDisk: 10, CacheDir: tmpdir, PowMode: ModeTest}, nil)
    68  	defer e.Close()
    69  
    70  	workers := 8
    71  	epochs := 100
    72  	var wg sync.WaitGroup
    73  	wg.Add(workers)
    74  	for i := 0; i < workers; i++ {
    75  		go verifyTest(&wg, e, i, epochs)
    76  	}
    77  	wg.Wait()
    78  }
    79  
    80  func verifyTest(wg *sync.WaitGroup, e *Ethash, workerIndex, epochs int) {
    81  	defer wg.Done()
    82  
    83  	const wiggle = 4 * epochLength
    84  	r := rand.New(rand.NewSource(int64(workerIndex)))
    85  	for epoch := 0; epoch < epochs; epoch++ {
    86  		block := int64(epoch)*epochLength - wiggle/2 + r.Int63n(wiggle)
    87  		if block < 0 {
    88  			block = 0
    89  		}
    90  		header := &types.Header{Number: big.NewInt(block), Difficulty: big.NewInt(100)}
    91  		e.VerifySeal(nil, header)
    92  	}
    93  }
    94  
    95  func TestRemoteSealer(t *testing.T) {
    96  	ethash := NewTester(nil)
    97  	defer ethash.Close()
    98  
    99  	api := &API{ethash}
   100  	if _, err := api.GetWork(); err != errNoMiningWork {
   101  		t.Error("expect to return an error indicate there is no mining work")
   102  	}
   103  	header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
   104  	block := types.NewBlockWithHeader(header)
   105  
   106  //推动新的工作。
   107  	ethash.Seal(nil, block, nil)
   108  
   109  	var (
   110  		work [3]string
   111  		err  error
   112  	)
   113  	if work, err = api.GetWork(); err != nil || work[0] != block.HashNoNonce().Hex() {
   114  		t.Error("expect to return a mining work has same hash")
   115  	}
   116  
   117  	if res := api.SubmitWork(types.BlockNonce{}, block.HashNoNonce(), common.Hash{}); res {
   118  		t.Error("expect to return false when submit a fake solution")
   119  	}
   120  //
   121  	header = &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(1000)}
   122  	block = types.NewBlockWithHeader(header)
   123  	ethash.Seal(nil, block, nil)
   124  
   125  	if work, err = api.GetWork(); err != nil || work[0] != block.HashNoNonce().Hex() {
   126  		t.Error("expect to return the latest pushed work")
   127  	}
   128  //推压块具有更高的块编号。
   129  	newHead := &types.Header{Number: big.NewInt(2), Difficulty: big.NewInt(100)}
   130  	newBlock := types.NewBlockWithHeader(newHead)
   131  	ethash.Seal(nil, newBlock, nil)
   132  
   133  	if res := api.SubmitWork(types.BlockNonce{}, block.HashNoNonce(), common.Hash{}); res {
   134  		t.Error("expect to return false when submit a stale solution")
   135  	}
   136  }
   137  
   138  func TestHashRate(t *testing.T) {
   139  	var (
   140  		hashrate = []hexutil.Uint64{100, 200, 300}
   141  		expect   uint64
   142  		ids      = []common.Hash{common.HexToHash("a"), common.HexToHash("b"), common.HexToHash("c")}
   143  	)
   144  	ethash := NewTester(nil)
   145  	defer ethash.Close()
   146  
   147  	if tot := ethash.Hashrate(); tot != 0 {
   148  		t.Error("expect the result should be zero")
   149  	}
   150  
   151  	api := &API{ethash}
   152  	for i := 0; i < len(hashrate); i += 1 {
   153  		if res := api.SubmitHashRate(hashrate[i], ids[i]); !res {
   154  			t.Error("remote miner submit hashrate failed")
   155  		}
   156  		expect += uint64(hashrate[i])
   157  	}
   158  	if tot := ethash.Hashrate(); tot != float64(expect) {
   159  		t.Error("expect total hashrate should be same")
   160  	}
   161  }
   162  
   163  func TestClosedRemoteSealer(t *testing.T) {
   164  	ethash := NewTester(nil)
   165  time.Sleep(1 * time.Second) //确保出口频道正在收听
   166  	ethash.Close()
   167  
   168  	api := &API{ethash}
   169  	if _, err := api.GetWork(); err != errEthashStopped {
   170  		t.Error("expect to return an error to indicate ethash is stopped")
   171  	}
   172  
   173  	if res := api.SubmitHashRate(hexutil.Uint64(100), common.HexToHash("a")); res {
   174  		t.Error("expect to return false when submit hashrate to a stopped ethash")
   175  	}
   176  }