064f37d6f6
Here we add a Go API for running tracing plugins within the main block import process. As an advanced user of geth, you can now create a Go file in eth/tracers/live/, and within that file register your custom tracer implementation. Then recompile geth and select your tracer on the command line. Hooks defined in the tracer will run whenever a block is processed. The hook system is defined in package core/tracing. It uses a struct with callbacks, instead of requiring an interface, for several reasons: - We plan to keep this API stable long-term. The core/tracing hook API does not depend on on deep geth internals. - There are a lot of hooks, and tracers will only need some of them. Using a struct allows you to implement only the hooks you want to actually use. All existing tracers in eth/tracers/native have been rewritten to use the new hook system. This change breaks compatibility with the vm.EVMLogger interface that we used to have. If you are a user of vm.EVMLogger, please migrate to core/tracing, and sorry for breaking your stuff. But we just couldn't have both the old and new tracing APIs coexist in the EVM. --------- Co-authored-by: Matthieu Vachon <matthieu.o.vachon@gmail.com> Co-authored-by: Delweng <delweng@gmail.com> Co-authored-by: Martin HS <martin@swende.se>
114 lines
4.3 KiB
Go
114 lines
4.3 KiB
Go
// 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"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
"github.com/ethereum/go-ethereum/core/tracing"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
"github.com/holiman/uint256"
|
|
)
|
|
|
|
func filledStateDB() *StateDB {
|
|
state, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
|
|
|
// Create an account and check if the retrieved balance is correct
|
|
addr := common.HexToAddress("0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe")
|
|
skey := common.HexToHash("aaa")
|
|
sval := common.HexToHash("bbb")
|
|
|
|
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
|
|
for i := 0; i < 100; i++ {
|
|
sk := common.BigToHash(big.NewInt(int64(i)))
|
|
state.SetState(addr, sk, sk) // Change the storage trie
|
|
}
|
|
return state
|
|
}
|
|
|
|
func TestCopyAndClose(t *testing.T) {
|
|
db := filledStateDB()
|
|
prefetcher := newTriePrefetcher(db.db, db.originalRoot, "")
|
|
skey := common.HexToHash("aaa")
|
|
prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
|
prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
|
time.Sleep(1 * time.Second)
|
|
a := prefetcher.trie(common.Hash{}, db.originalRoot)
|
|
prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
|
b := prefetcher.trie(common.Hash{}, db.originalRoot)
|
|
cpy := prefetcher.copy()
|
|
cpy.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
|
cpy.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
|
c := cpy.trie(common.Hash{}, db.originalRoot)
|
|
prefetcher.close()
|
|
cpy2 := cpy.copy()
|
|
cpy2.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
|
d := cpy2.trie(common.Hash{}, db.originalRoot)
|
|
cpy.close()
|
|
cpy2.close()
|
|
if a.Hash() != b.Hash() || a.Hash() != c.Hash() || a.Hash() != d.Hash() {
|
|
t.Fatalf("Invalid trie, hashes should be equal: %v %v %v %v", a.Hash(), b.Hash(), c.Hash(), d.Hash())
|
|
}
|
|
}
|
|
|
|
func TestUseAfterClose(t *testing.T) {
|
|
db := filledStateDB()
|
|
prefetcher := newTriePrefetcher(db.db, db.originalRoot, "")
|
|
skey := common.HexToHash("aaa")
|
|
prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
|
a := prefetcher.trie(common.Hash{}, db.originalRoot)
|
|
prefetcher.close()
|
|
b := prefetcher.trie(common.Hash{}, db.originalRoot)
|
|
if a == nil {
|
|
t.Fatal("Prefetching before close should not return nil")
|
|
}
|
|
if b != nil {
|
|
t.Fatal("Trie after close should return nil")
|
|
}
|
|
}
|
|
|
|
func TestCopyClose(t *testing.T) {
|
|
db := filledStateDB()
|
|
prefetcher := newTriePrefetcher(db.db, db.originalRoot, "")
|
|
skey := common.HexToHash("aaa")
|
|
prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()})
|
|
cpy := prefetcher.copy()
|
|
a := prefetcher.trie(common.Hash{}, db.originalRoot)
|
|
b := cpy.trie(common.Hash{}, db.originalRoot)
|
|
prefetcher.close()
|
|
c := prefetcher.trie(common.Hash{}, db.originalRoot)
|
|
d := cpy.trie(common.Hash{}, db.originalRoot)
|
|
if a == nil {
|
|
t.Fatal("Prefetching before close should not return nil")
|
|
}
|
|
if b == nil {
|
|
t.Fatal("Copy trie should return nil")
|
|
}
|
|
if c != nil {
|
|
t.Fatal("Trie after close should return nil")
|
|
}
|
|
if d == nil {
|
|
t.Fatal("Copy trie should not return nil")
|
|
}
|
|
}
|