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