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