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