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