2021-08-03 18:35:25 +03:00
|
|
|
// Copyright 2021 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2024-03-22 20:53:53 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/tracing"
|
2023-05-11 10:19:42 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2024-08-26 17:18:47 +03:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/internal/testrand"
|
|
|
|
"github.com/ethereum/go-ethereum/triedb"
|
2024-01-23 16:51:58 +03:00
|
|
|
"github.com/holiman/uint256"
|
2021-08-03 18:35:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func filledStateDB() *StateDB {
|
2024-09-05 13:10:47 +03:00
|
|
|
state, _ := New(types.EmptyRootHash, NewDatabaseForTesting())
|
2021-08-03 18:35:25 +03:00
|
|
|
|
|
|
|
// Create an account and check if the retrieved balance is correct
|
|
|
|
addr := common.HexToAddress("0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe")
|
|
|
|
skey := common.HexToHash("aaa")
|
|
|
|
sval := common.HexToHash("bbb")
|
|
|
|
|
2024-03-22 20:53:53 +03:00
|
|
|
state.SetBalance(addr, uint256.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie
|
|
|
|
state.SetCode(addr, []byte("hello")) // Change an external metadata
|
|
|
|
state.SetState(addr, skey, sval) // Change the storage trie
|
2021-08-03 18:35:25 +03:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
sk := common.BigToHash(big.NewInt(int64(i)))
|
|
|
|
state.SetState(addr, sk, sk) // Change the storage trie
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2024-05-13 15:47:45 +03:00
|
|
|
func TestUseAfterTerminate(t *testing.T) {
|
2021-08-03 18:35:25 +03:00
|
|
|
db := filledStateDB()
|
2024-06-11 11:10:07 +03:00
|
|
|
prefetcher := newTriePrefetcher(db.db, db.originalRoot, "", true)
|
2021-08-03 18:35:25 +03:00
|
|
|
skey := common.HexToHash("aaa")
|
|
|
|
|
2024-10-20 13:25:15 +03:00
|
|
|
if err := prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, nil, []common.Hash{skey}, false); err != nil {
|
2024-05-13 15:47:45 +03:00
|
|
|
t.Errorf("Prefetch failed before terminate: %v", err)
|
2021-08-03 18:35:25 +03:00
|
|
|
}
|
2024-05-13 15:47:45 +03:00
|
|
|
prefetcher.terminate(false)
|
2021-08-03 18:35:25 +03:00
|
|
|
|
2024-10-20 13:25:15 +03:00
|
|
|
if err := prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, nil, []common.Hash{skey}, false); err == nil {
|
2024-05-13 15:47:45 +03:00
|
|
|
t.Errorf("Prefetch succeeded after terminate: %v", err)
|
2021-08-03 18:35:25 +03:00
|
|
|
}
|
2024-05-28 20:54:55 +03:00
|
|
|
if tr := prefetcher.trie(common.Hash{}, db.originalRoot); tr == nil {
|
|
|
|
t.Errorf("Prefetcher returned nil trie after terminate")
|
2021-08-03 18:35:25 +03:00
|
|
|
}
|
|
|
|
}
|
2024-08-26 17:18:47 +03:00
|
|
|
|
|
|
|
func TestVerklePrefetcher(t *testing.T) {
|
2024-09-05 13:10:47 +03:00
|
|
|
disk := rawdb.NewMemoryDatabase()
|
|
|
|
db := triedb.NewDatabase(disk, triedb.VerkleDefaults)
|
|
|
|
sdb := NewDatabase(db, nil)
|
|
|
|
|
|
|
|
state, err := New(types.EmptyRootHash, sdb)
|
2024-08-26 17:18:47 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to initialize state: %v", err)
|
|
|
|
}
|
|
|
|
// Create an account and check if the retrieved balance is correct
|
|
|
|
addr := testrand.Address()
|
|
|
|
skey := testrand.Hash()
|
|
|
|
sval := testrand.Hash()
|
|
|
|
|
|
|
|
state.SetBalance(addr, uint256.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie
|
|
|
|
state.SetCode(addr, []byte("hello")) // Change an external metadata
|
|
|
|
state.SetState(addr, skey, sval) // Change the storage trie
|
|
|
|
root, _ := state.Commit(0, true)
|
|
|
|
|
2024-09-05 13:10:47 +03:00
|
|
|
state, _ = New(root, sdb)
|
2024-08-26 17:18:47 +03:00
|
|
|
sRoot := state.GetStorageRoot(addr)
|
2024-09-05 13:10:47 +03:00
|
|
|
fetcher := newTriePrefetcher(sdb, root, "", false)
|
2024-08-26 17:18:47 +03:00
|
|
|
|
|
|
|
// Read account
|
2024-10-20 13:25:15 +03:00
|
|
|
fetcher.prefetch(common.Hash{}, root, common.Address{}, []common.Address{addr}, nil, false)
|
2024-08-26 17:18:47 +03:00
|
|
|
|
|
|
|
// Read storage slot
|
2024-10-20 13:25:15 +03:00
|
|
|
fetcher.prefetch(crypto.Keccak256Hash(addr.Bytes()), sRoot, addr, nil, []common.Hash{skey}, false)
|
2024-08-26 17:18:47 +03:00
|
|
|
|
|
|
|
fetcher.terminate(false)
|
|
|
|
accountTrie := fetcher.trie(common.Hash{}, root)
|
|
|
|
storageTrie := fetcher.trie(crypto.Keccak256Hash(addr.Bytes()), sRoot)
|
|
|
|
|
|
|
|
rootA := accountTrie.Hash()
|
|
|
|
rootB := storageTrie.Hash()
|
|
|
|
if rootA != rootB {
|
|
|
|
t.Fatal("Two different tries are retrieved")
|
|
|
|
}
|
|
|
|
}
|