Skip to content

Commit

Permalink
chore:add tree get debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
joeylichang committed Aug 15, 2024
1 parent c252f62 commit e8e90b3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
28 changes: 14 additions & 14 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2233,28 +2233,28 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
}
log.Info("+++++++++++++start block", "number", block.NumberU64())
defer log.Info("+++++++++++++end block", "number", block.NumberU64())
statedb, err := state.NewWithSharedPool(parent.Root, bc.stateCache, bc.snaps)
statedb, err := state.NewWithSharedPool(parent.Root, bc.stateCache, nil)
if err != nil {
return it.index, err
}
bc.updateHighestVerifiedHeader(block.Header())

// Enable prefetching to pull in trie node paths while processing transactions
statedb.StartPrefetcher("chain")
//statedb.StartPrefetcher("chain")
interruptCh := make(chan struct{})
// For diff sync, it may fallback to full sync, so we still do prefetch
if len(block.Transactions()) >= prefetchTxNumber {
// do Prefetch in a separate goroutine to avoid blocking the critical path

// 1.do state prefetch for snapshot cache
throwaway := statedb.CopyDoPrefetch()
go bc.prefetcher.Prefetch(block, throwaway, &bc.vmConfig, interruptCh)

// 2.do trie prefetch for MPT trie node cache
// it is for the big state trie tree, prefetch based on transaction's From/To address.
// trie prefetcher is thread safe now, ok to prefetch in a separate routine
go throwaway.TriePrefetchInAdvance(block, signer)
}
//if len(block.Transactions()) >= prefetchTxNumber {
// // do Prefetch in a separate goroutine to avoid blocking the critical path
//
// // 1.do state prefetch for snapshot cache
// throwaway := statedb.CopyDoPrefetch()
// go bc.prefetcher.Prefetch(block, throwaway, &bc.vmConfig, interruptCh)
//
// // 2.do trie prefetch for MPT trie node cache
// // it is for the big state trie tree, prefetch based on transaction's From/To address.
// // trie prefetcher is thread safe now, ok to prefetch in a separate routine
// go throwaway.TriePrefetchInAdvance(block, signer)
//}

// Process block using the parent state as reference point
if bc.pipeCommit {
Expand Down
5 changes: 5 additions & 0 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func NewWithSharedPool(root common.Hash, db Database, snaps *snapshot.Tree) (*St

// New creates a new state from a given trie.
func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
snaps = nil
sdb := &StateDB{
db: db,
originalRoot: root,
Expand Down Expand Up @@ -222,6 +223,9 @@ func (s *StateDB) TransferPrefetcher(prev *StateDB) {
prev.prefetcherLock.Lock()
fetcher = prev.prefetcher
prev.prefetcher = nil
if fetcher != nil {
panic("TransferPrefetcher")
}
prev.prefetcherLock.Unlock()

s.prefetcherLock.Lock()
Expand All @@ -243,6 +247,7 @@ func (s *StateDB) StartPrefetcher(namespace string) {
s.prefetcher = nil
}
if s.snap != nil {
panic("StartPrefetcher")
parent := s.snap.Parent()
if parent != nil {
s.prefetcher = newTriePrefetcher(s.db, s.originalRoot, parent.Root(), namespace)
Expand Down
2 changes: 1 addition & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
TrieDirtyDisabled: config.NoPruning,
TrieTimeLimit: config.TrieTimeout,
NoTries: config.TriesVerifyMode != core.LocalVerify,
SnapshotLimit: config.SnapshotCache,
SnapshotLimit: 0, // forbid snapshot
TriesInMemory: config.TriesInMemory,
Preimages: config.Preimages,
StateHistory: config.StateHistory,
Expand Down
5 changes: 4 additions & 1 deletion trie/secure_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package trie
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie/trienode"
"github.com/ethereum/go-ethereum/triedb/database"
Expand Down Expand Up @@ -86,12 +87,13 @@ func (t *StateTrie) MustGet(key []byte) []byte {
// and slot key. The value bytes must not be modified by the caller.
// If the specified storage slot is not in the trie, nil will be returned.
// If a trie node is not found in the database, a MissingNodeError is returned.
func (t *StateTrie) GetStorage(_ common.Address, key []byte) ([]byte, error) {
func (t *StateTrie) GetStorage(addr common.Address, key []byte) ([]byte, error) {
enc, err := t.trie.Get(t.hashKey(key))
if err != nil || len(enc) == 0 {
return nil, err
}
_, content, _, err := rlp.Split(enc)
log.Info("get storage", "owner", addr.String(), "key", common.Bytes2Hex(key), "val", common.Bytes2Hex(content))
return content, err
}

Expand All @@ -105,6 +107,7 @@ func (t *StateTrie) GetAccount(address common.Address) (*types.StateAccount, err
}
ret := new(types.StateAccount)
err = rlp.DecodeBytes(res, ret)
log.Info("get account", "addr", address.String(), "nonce", ret.Nonce, "balance", ret.Balance, "root", ret.Root.String(), "code", common.Bytes2Hex(ret.CodeHash))
return ret, err
}

Expand Down

0 comments on commit e8e90b3

Please sign in to comment.