github.com/aquanetwork/aquachain@v1.7.8/consensus/aquahash/aquahash_test.go (about)

     1  // Copyright 2017 The aquachain Authors
     2  // This file is part of the aquachain library.
     3  //
     4  // The aquachain 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 aquachain 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 aquachain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package aquahash
    18  
    19  import (
    20  	"io/ioutil"
    21  	"math/big"
    22  	"math/rand"
    23  	"os"
    24  	"sync"
    25  	"testing"
    26  
    27  	"gitlab.com/aquachain/aquachain/core/types"
    28  	"gitlab.com/aquachain/aquachain/params"
    29  )
    30  
    31  // Tests that aquahash works correctly in test mode.
    32  func TestTestMode(t *testing.T) {
    33  	head := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
    34  	head.Version = types.H_KECCAK256
    35  	aquahash := NewTester()
    36  	block, err := aquahash.Seal(nil, types.NewBlockWithHeader(head), nil)
    37  	if err != nil {
    38  		t.Fatalf("failed to seal block: %v", err)
    39  	}
    40  	head.Nonce = types.EncodeNonce(block.Nonce())
    41  	head.MixDigest = block.MixDigest()
    42  	if err := aquahash.VerifySeal(nil, head); err != nil {
    43  		t.Fatalf("unexpected verification error: %v", err)
    44  	}
    45  }
    46  
    47  // This test checks that cache lru logic doesn't crash under load.
    48  // It reproduces https://gitlab.com/aquachain/aquachain/issues/14943
    49  func TestCacheFileEvict(t *testing.T) {
    50  	tmpdir, err := ioutil.TempDir("", "aquahash-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})
    56  
    57  	workers := 8
    58  	epochs := 100
    59  	var wg sync.WaitGroup
    60  	wg.Add(workers)
    61  	for i := 0; i < workers; i++ {
    62  		go verifyTest(&wg, e, i, epochs)
    63  	}
    64  	wg.Wait()
    65  }
    66  
    67  func verifyTest(wg *sync.WaitGroup, e *Aquahash, workerIndex, epochs int) {
    68  	defer wg.Done()
    69  
    70  	const wiggle = 4 * epochLength
    71  	r := rand.New(rand.NewSource(int64(workerIndex)))
    72  	for epoch := 0; epoch < epochs; epoch++ {
    73  		block := int64(epoch)*epochLength - wiggle/2 + r.Int63n(wiggle)
    74  		if block < 0 {
    75  			block = 0
    76  		}
    77  		head := &types.Header{Number: big.NewInt(block), Difficulty: big.NewInt(100)}
    78  		head.Version = params.AllAquahashProtocolChanges.GetBlockVersion(big.NewInt(block))
    79  		e.VerifySeal(nil, head)
    80  	}
    81  }