github.com/aerth/aquachain@v1.4.1/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  	"github.com/aquanetwork/aquachain/core/types"
    28  )
    29  
    30  // Tests that aquahash works correctly in test mode.
    31  func TestTestMode(t *testing.T) {
    32  	head := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
    33  
    34  	aquahash := NewTester()
    35  	block, err := aquahash.Seal(nil, types.NewBlockWithHeader(head), nil)
    36  	if err != nil {
    37  		t.Fatalf("failed to seal block: %v", err)
    38  	}
    39  	head.Nonce = types.EncodeNonce(block.Nonce())
    40  	head.MixDigest = block.MixDigest()
    41  	if err := aquahash.VerifySeal(nil, head); err != nil {
    42  		t.Fatalf("unexpected verification error: %v", err)
    43  	}
    44  }
    45  
    46  // This test checks that cache lru logic doesn't crash under load.
    47  // It reproduces https://github.com/aquanetwork/aquachain/issues/14943
    48  func TestCacheFileEvict(t *testing.T) {
    49  	tmpdir, err := ioutil.TempDir("", "aquahash-test")
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	defer os.RemoveAll(tmpdir)
    54  	e := New(Config{CachesInMem: 3, CachesOnDisk: 10, CacheDir: tmpdir, PowMode: ModeTest})
    55  
    56  	workers := 8
    57  	epochs := 100
    58  	var wg sync.WaitGroup
    59  	wg.Add(workers)
    60  	for i := 0; i < workers; i++ {
    61  		go verifyTest(&wg, e, i, epochs)
    62  	}
    63  	wg.Wait()
    64  }
    65  
    66  func verifyTest(wg *sync.WaitGroup, e *Aquahash, workerIndex, epochs int) {
    67  	defer wg.Done()
    68  
    69  	const wiggle = 4 * epochLength
    70  	r := rand.New(rand.NewSource(int64(workerIndex)))
    71  	for epoch := 0; epoch < epochs; epoch++ {
    72  		block := int64(epoch)*epochLength - wiggle/2 + r.Int63n(wiggle)
    73  		if block < 0 {
    74  			block = 0
    75  		}
    76  		head := &types.Header{Number: big.NewInt(block), Difficulty: big.NewInt(100)}
    77  		e.VerifySeal(nil, head)
    78  	}
    79  }