From 5153e6978bfc62a1a324881ee2ed745fe0e19608 Mon Sep 17 00:00:00 2001 From: joeycli Date: Tue, 13 Aug 2024 09:47:02 +0800 Subject: [PATCH] chore: add debug print statedb --- core/blockchain.go | 2 ++ core/state/statedb.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index 51387a13f0..65c929d2b2 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2270,6 +2270,8 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) } ptime := time.Since(pstart) + statedb.DebugPrint(block.NumberU64(), true) + // Validate the state using the default validator vstart := time.Now() if err := bc.validator.ValidateState(block, statedb, receipts, usedGas); err != nil { diff --git a/core/state/statedb.go b/core/state/statedb.go index 525da23b0f..f205f9fbe2 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1475,6 +1475,49 @@ func (s *StateDB) handleDestruction(nodes *trienode.MergedNodeSet) (map[common.A return incomplete, nil } +func (s *StateDB) DebugPrint(block uint64, deleteEmptyObjects bool) { + log.Info("================== block start ===============", "number", block) + hash := s.IntermediateRoot(deleteEmptyObjects) + log.Info("mpt root", "hash", hash) + + addrs := make([]common.Address, 0) + for addr := range s.stateObjectsDirty { + if obj := s.stateObjects[addr]; !obj.deleted { + addrs = append(addrs, addr) + } + } + sort.SliceStable(addrs, func(i, j int) bool { + return addrs[i].Cmp(addrs[j]) < 0 + }) + + for _, addr := range addrs { + if obj := s.stateObjects[addr]; !obj.deleted { + log.Info("state object", "address", obj.address) + log.Info("state object", "addrHash", obj.addrHash) + log.Info("state object", "dirtyCode", obj.dirtyCode) + log.Info("state object", "selfDestructed", obj.selfDestructed) + log.Info("state object", "deleted", obj.deleted) + log.Info("state object", "created", obj.created) + if obj.origin != nil { + log.Info("state object origin", "Nonce", obj.origin.Nonce) + log.Info("state object origin", "Balance", obj.origin.Balance) + log.Info("state object origin", "Root", obj.origin.Root) + log.Info("state object origin", "CodeHash", common.Bytes2Hex(obj.origin.CodeHash)) + } else { + log.Info("state object origin is nil") + } + log.Info("state object new", "Nonce", obj.data.Nonce) + log.Info("state object new", "Balance", obj.data.Balance) + log.Info("state object new", "Root", obj.data.Root) + log.Info("state object new", "CodeHash", common.Bytes2Hex(obj.data.CodeHash)) + } + } + log.Info("================== block end ================", "number", block) + if block == 1 { + log.Crit("exit....") + } +} + // Once the state is committed, tries cached in stateDB (including account // trie, storage tries) will no longer be functional. A new state instance // must be created with new root and updated database for accessing post-