github.com/theQRL/go-zond@v0.1.1/core/state/trie_prefetcher_test.go (about)

     1  // Copyright 2021 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 state
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/theQRL/go-zond/common"
    25  	"github.com/theQRL/go-zond/core/rawdb"
    26  	"github.com/theQRL/go-zond/core/types"
    27  )
    28  
    29  func filledStateDB() *StateDB {
    30  	state, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
    31  
    32  	// Create an account and check if the retrieved balance is correct
    33  	addr := common.HexToAddress("0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe")
    34  	skey := common.HexToHash("aaa")
    35  	sval := common.HexToHash("bbb")
    36  
    37  	state.SetBalance(addr, big.NewInt(42)) // Change the account trie
    38  	state.SetCode(addr, []byte("hello"))   // Change an external metadata
    39  	state.SetState(addr, skey, sval)       // Change the storage trie
    40  	for i := 0; i < 100; i++ {
    41  		sk := common.BigToHash(big.NewInt(int64(i)))
    42  		state.SetState(addr, sk, sk) // Change the storage trie
    43  	}
    44  	return state
    45  }
    46  
    47  func TestCopyAndClose(t *testing.T) {
    48  	db := filledStateDB()
    49  	prefetcher := newTriePrefetcher(db.db, db.originalRoot, "")
    50  	skey := common.HexToHash("aaa")
    51  	prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
    52  	prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
    53  	time.Sleep(1 * time.Second)
    54  	a := prefetcher.trie(common.Hash{}, db.originalRoot)
    55  	prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
    56  	b := prefetcher.trie(common.Hash{}, db.originalRoot)
    57  	cpy := prefetcher.copy()
    58  	cpy.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
    59  	cpy.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
    60  	c := cpy.trie(common.Hash{}, db.originalRoot)
    61  	prefetcher.close()
    62  	cpy2 := cpy.copy()
    63  	cpy2.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
    64  	d := cpy2.trie(common.Hash{}, db.originalRoot)
    65  	cpy.close()
    66  	cpy2.close()
    67  	if a.Hash() != b.Hash() || a.Hash() != c.Hash() || a.Hash() != d.Hash() {
    68  		t.Fatalf("Invalid trie, hashes should be equal: %v %v %v %v", a.Hash(), b.Hash(), c.Hash(), d.Hash())
    69  	}
    70  }
    71  
    72  func TestUseAfterClose(t *testing.T) {
    73  	db := filledStateDB()
    74  	prefetcher := newTriePrefetcher(db.db, db.originalRoot, "")
    75  	skey := common.HexToHash("aaa")
    76  	prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
    77  	a := prefetcher.trie(common.Hash{}, db.originalRoot)
    78  	prefetcher.close()
    79  	b := prefetcher.trie(common.Hash{}, db.originalRoot)
    80  	if a == nil {
    81  		t.Fatal("Prefetching before close should not return nil")
    82  	}
    83  	if b != nil {
    84  		t.Fatal("Trie after close should return nil")
    85  	}
    86  }
    87  
    88  func TestCopyClose(t *testing.T) {
    89  	db := filledStateDB()
    90  	prefetcher := newTriePrefetcher(db.db, db.originalRoot, "")
    91  	skey := common.HexToHash("aaa")
    92  	prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
    93  	cpy := prefetcher.copy()
    94  	a := prefetcher.trie(common.Hash{}, db.originalRoot)
    95  	b := cpy.trie(common.Hash{}, db.originalRoot)
    96  	prefetcher.close()
    97  	c := prefetcher.trie(common.Hash{}, db.originalRoot)
    98  	d := cpy.trie(common.Hash{}, db.originalRoot)
    99  	if a == nil {
   100  		t.Fatal("Prefetching before close should not return nil")
   101  	}
   102  	if b == nil {
   103  		t.Fatal("Copy trie should return nil")
   104  	}
   105  	if c != nil {
   106  		t.Fatal("Trie after close should return nil")
   107  	}
   108  	if d == nil {
   109  		t.Fatal("Copy trie should not return nil")
   110  	}
   111  }