github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/consensus/ethash/ethash_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:33</date> 10 //</624342612110741504> 11 12 13 package ethash 14 15 import ( 16 "io/ioutil" 17 "math/big" 18 "math/rand" 19 "os" 20 "sync" 21 "testing" 22 "time" 23 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/common/hexutil" 26 "github.com/ethereum/go-ethereum/core/types" 27 ) 28 29 //测试ethash在测试模式下是否正常工作。 30 func TestTestMode(t *testing.T) { 31 header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)} 32 33 ethash := NewTester(nil) 34 defer ethash.Close() 35 36 block, err := ethash.Seal(nil, types.NewBlockWithHeader(header), nil) 37 if err != nil { 38 t.Fatalf("failed to seal block: %v", err) 39 } 40 header.Nonce = types.EncodeNonce(block.Nonce()) 41 header.MixDigest = block.MixDigest() 42 if err := ethash.VerifySeal(nil, header); err != nil { 43 t.Fatalf("unexpected verification error: %v", err) 44 } 45 } 46 47 //此测试检查高速缓存LRU逻辑在负载下是否崩溃。 48 //它复制了https://github.com/ethereum/go-ethereum/issues/14943 49 func TestCacheFileEvict(t *testing.T) { 50 tmpdir, err := ioutil.TempDir("", "ethash-test") 51 if err != nil { 52 t.Fatal(err) 53 } 54 defer os.RemoveAll(tmpdir) 55 e := New(Config{CachesInMem: 3, CachesOnDisk: 10, CacheDir: tmpdir, PowMode: ModeTest}, nil) 56 defer e.Close() 57 58 workers := 8 59 epochs := 100 60 var wg sync.WaitGroup 61 wg.Add(workers) 62 for i := 0; i < workers; i++ { 63 go verifyTest(&wg, e, i, epochs) 64 } 65 wg.Wait() 66 } 67 68 func verifyTest(wg *sync.WaitGroup, e *Ethash, workerIndex, epochs int) { 69 defer wg.Done() 70 71 const wiggle = 4 * epochLength 72 r := rand.New(rand.NewSource(int64(workerIndex))) 73 for epoch := 0; epoch < epochs; epoch++ { 74 block := int64(epoch)*epochLength - wiggle/2 + r.Int63n(wiggle) 75 if block < 0 { 76 block = 0 77 } 78 header := &types.Header{Number: big.NewInt(block), Difficulty: big.NewInt(100)} 79 e.VerifySeal(nil, header) 80 } 81 } 82 83 func TestRemoteSealer(t *testing.T) { 84 ethash := NewTester(nil) 85 defer ethash.Close() 86 87 api := &API{ethash} 88 if _, err := api.GetWork(); err != errNoMiningWork { 89 t.Error("expect to return an error indicate there is no mining work") 90 } 91 header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)} 92 block := types.NewBlockWithHeader(header) 93 94 //推动新的工作。 95 ethash.Seal(nil, block, nil) 96 97 var ( 98 work [3]string 99 err error 100 ) 101 if work, err = api.GetWork(); err != nil || work[0] != block.HashNoNonce().Hex() { 102 t.Error("expect to return a mining work has same hash") 103 } 104 105 if res := api.SubmitWork(types.BlockNonce{}, block.HashNoNonce(), common.Hash{}); res { 106 t.Error("expect to return false when submit a fake solution") 107 } 108 // 109 header = &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(1000)} 110 block = types.NewBlockWithHeader(header) 111 ethash.Seal(nil, block, nil) 112 113 if work, err = api.GetWork(); err != nil || work[0] != block.HashNoNonce().Hex() { 114 t.Error("expect to return the latest pushed work") 115 } 116 //推压块具有更高的块编号。 117 newHead := &types.Header{Number: big.NewInt(2), Difficulty: big.NewInt(100)} 118 newBlock := types.NewBlockWithHeader(newHead) 119 ethash.Seal(nil, newBlock, nil) 120 121 if res := api.SubmitWork(types.BlockNonce{}, block.HashNoNonce(), common.Hash{}); res { 122 t.Error("expect to return false when submit a stale solution") 123 } 124 } 125 126 func TestHashRate(t *testing.T) { 127 var ( 128 hashrate = []hexutil.Uint64{100, 200, 300} 129 expect uint64 130 ids = []common.Hash{common.HexToHash("a"), common.HexToHash("b"), common.HexToHash("c")} 131 ) 132 ethash := NewTester(nil) 133 defer ethash.Close() 134 135 if tot := ethash.Hashrate(); tot != 0 { 136 t.Error("expect the result should be zero") 137 } 138 139 api := &API{ethash} 140 for i := 0; i < len(hashrate); i += 1 { 141 if res := api.SubmitHashRate(hashrate[i], ids[i]); !res { 142 t.Error("remote miner submit hashrate failed") 143 } 144 expect += uint64(hashrate[i]) 145 } 146 if tot := ethash.Hashrate(); tot != float64(expect) { 147 t.Error("expect total hashrate should be same") 148 } 149 } 150 151 func TestClosedRemoteSealer(t *testing.T) { 152 ethash := NewTester(nil) 153 time.Sleep(1 * time.Second) //确保出口频道正在收听 154 ethash.Close() 155 156 api := &API{ethash} 157 if _, err := api.GetWork(); err != errEthashStopped { 158 t.Error("expect to return an error to indicate ethash is stopped") 159 } 160 161 if res := api.SubmitHashRate(hexutil.Uint64(100), common.HexToHash("a")); res { 162 t.Error("expect to return false when submit hashrate to a stopped ethash") 163 } 164 } 165