github.com/klaytn/klaytn@v1.12.1/consensus/gxhash/gxhash_test.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2017 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from consensus/ethash/ethash_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package gxhash 22 23 import ( 24 "math/big" 25 "math/rand" 26 "os" 27 "sync" 28 "testing" 29 30 "github.com/klaytn/klaytn/blockchain/types" 31 ) 32 33 // Tests that gxhash works correctly in test mode. 34 func TestTestMode(t *testing.T) { 35 head := &types.Header{Number: big.NewInt(1), BlockScore: big.NewInt(100)} 36 37 gxhash := NewTester() 38 39 if err := gxhash.VerifySeal(nil, head); err != nil { 40 t.Fatalf("unexpected verification error: %v", err) 41 } 42 } 43 44 // This test checks that cache lru logic doesn't crash under load. 45 // It reproduces https://github.com/ethereum/go-ethereum/issues/14943 46 func TestCacheFileEvict(t *testing.T) { 47 tmpdir, err := os.MkdirTemp("", "gxhash-test") 48 if err != nil { 49 t.Fatal(err) 50 } 51 defer os.RemoveAll(tmpdir) 52 e := New(Config{CachesInMem: 3, CachesOnDisk: 10, CacheDir: tmpdir, PowMode: ModeTest}) 53 54 workers := 8 55 epochs := 100 56 var wg sync.WaitGroup 57 wg.Add(workers) 58 for i := 0; i < workers; i++ { 59 go verifyTest(&wg, e, i, epochs) 60 } 61 wg.Wait() 62 } 63 64 func verifyTest(wg *sync.WaitGroup, e *Gxhash, workerIndex, epochs int) { 65 defer wg.Done() 66 67 const wiggle = 4 * epochLength 68 r := rand.New(rand.NewSource(int64(workerIndex))) 69 for epoch := 0; epoch < epochs; epoch++ { 70 block := int64(epoch)*epochLength - wiggle/2 + r.Int63n(wiggle) 71 if block < 0 { 72 block = 0 73 } 74 head := &types.Header{Number: big.NewInt(block), BlockScore: big.NewInt(100)} 75 e.VerifySeal(nil, head) 76 } 77 }