github.com/ethereum/go-ethereum@v1.14.4-0.20240516095835-473ee8fc07a3/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  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/core/rawdb"
    25  	"github.com/ethereum/go-ethereum/core/tracing"
    26  	"github.com/ethereum/go-ethereum/core/types"
    27  	"github.com/holiman/uint256"
    28  )
    29  
    30  func filledStateDB() *StateDB {
    31  	state, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
    32  
    33  	// Create an account and check if the retrieved balance is correct
    34  	addr := common.HexToAddress("0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe")
    35  	skey := common.HexToHash("aaa")
    36  	sval := common.HexToHash("bbb")
    37  
    38  	state.SetBalance(addr, uint256.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie
    39  	state.SetCode(addr, []byte("hello"))                                         // Change an external metadata
    40  	state.SetState(addr, skey, sval)                                             // Change the storage trie
    41  	for i := 0; i < 100; i++ {
    42  		sk := common.BigToHash(big.NewInt(int64(i)))
    43  		state.SetState(addr, sk, sk) // Change the storage trie
    44  	}
    45  	return state
    46  }
    47  
    48  func TestUseAfterTerminate(t *testing.T) {
    49  	db := filledStateDB()
    50  	prefetcher := newTriePrefetcher(db.db, db.originalRoot, "")
    51  	skey := common.HexToHash("aaa")
    52  
    53  	if err := prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()}); err != nil {
    54  		t.Errorf("Prefetch failed before terminate: %v", err)
    55  	}
    56  	prefetcher.terminate(false)
    57  
    58  	if err := prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()}); err == nil {
    59  		t.Errorf("Prefetch succeeded after terminate: %v", err)
    60  	}
    61  	if _, err := prefetcher.trie(common.Hash{}, db.originalRoot); err != nil {
    62  		t.Errorf("Trie retrieval failed after terminate: %v", err)
    63  	}
    64  }