Skip to content

Commit

Permalink
logs added
Browse files Browse the repository at this point in the history
  • Loading branch information
krish-nr committed Jun 25, 2024
1 parent 7c537b7 commit 2422000
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 9 deletions.
34 changes: 28 additions & 6 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ func (bc *BlockChain) loadLastState() error {
return bc.Reset()
}
// Everything seems to be fine, set as the head block
bc.currentBlock.Store(headBlock.Header())
bc.currentBlock.Store(headBlock.Header()) //这里是获取后改的地方
headBlockGauge.Update(int64(headBlock.NumberU64()))

// Restore the last known head header
Expand All @@ -589,7 +589,9 @@ func (bc *BlockChain) loadLastState() error {
// Note: the safe block is not stored on disk and it is set to the last
// known finalized block on startup
if head := rawdb.ReadFinalizedBlockHash(bc.db); head != (common.Hash{}) {
log.Info("ZXL: finalized from db exist")
if block := bc.GetBlockByHash(head); block != nil {
log.Info("ZXL: finalized from db", "finalized", block.Number().Uint64())
bc.currentFinalBlock.Store(block.Header())
headFinalizedBlockGauge.Update(int64(block.NumberU64()))
bc.currentSafeBlock.Store(block.Header())
Expand All @@ -604,7 +606,9 @@ func (bc *BlockChain) loadLastState() error {
headerTd = bc.GetTd(headHeader.Hash(), headHeader.Number.Uint64())
blockTd = bc.GetTd(headBlock.Hash(), headBlock.NumberU64())
)
//headHeader是DB里读出来的
if headHeader.Hash() != headBlock.Hash() {
//这里确实不一样,header是没有回退过的,和打点结果一样,header没有回退保持为重启前
log.Info("Loaded most recent local header", "number", headHeader.Number, "hash", headHeader.Hash(), "td", headerTd, "age", common.PrettyAge(time.Unix(int64(headHeader.Time), 0)))
}
log.Info("Loaded most recent local block", "number", headBlock.Number(), "hash", headBlock.Hash(), "td", blockTd, "age", common.PrettyAge(time.Unix(int64(headBlock.Time()), 0)))
Expand Down Expand Up @@ -728,13 +732,19 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
beyondRoot := (root == common.Hash{}) // Flag whether we're beyond the requested root (no root, always true)

for {
log.Info("ZXL: step into for loop", "newHeadBlock", newHeadBlock.Number().Uint64())

// If a root threshold was requested but not yet crossed, check
if root != (common.Hash{}) && !beyondRoot && newHeadBlock.Root() == root {
log.Info("ZXL: step into newHeadBlock.Root() == root")
beyondRoot, rootNumber = true, newHeadBlock.NumberU64()
}
if !bc.HasState(newHeadBlock.Root()) && !bc.stateRecoverable(newHeadBlock.Root()) {
log.Trace("Block state missing, rewinding further", "number", newHeadBlock.NumberU64(), "hash", newHeadBlock.Hash())
if pivot == nil || newHeadBlock.NumberU64() > *pivot {
if pivot == nil {
log.Info("ZXL step into null pivot", "newHeadBlock", newHeadBlock.Number().Uint64())
}
parent := bc.GetBlock(newHeadBlock.ParentHash(), newHeadBlock.NumberU64()-1)
if parent != nil {
newHeadBlock = parent
Expand All @@ -756,6 +766,8 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
}
log.Debug("Rewound to block with state", "number", newHeadBlock.NumberU64(), "hash", newHeadBlock.Hash())
}
log.Info("ZXL step into found newhead logic", "newHeadBlock", newHeadBlock.Number().Uint64())

break
}
log.Debug("Skipping block with threshold state", "number", newHeadBlock.NumberU64(), "hash", newHeadBlock.Hash(), "root", newHeadBlock.Root())
Expand All @@ -764,11 +776,17 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
}
rawdb.WriteHeadBlockHash(db, newHeadBlock.Hash()) //这里已经写入的是带MPT的高度

/*
rawdb.WriteFinalizedBlockHash()
rawdb.WriteCanonicalHash()
*/

// Degrade the chain markers if they are explicitly reverted.
// In theory we should update all in-memory markers in the
// last step, however the direction of SetHead is from high
// to low, so it's safe to update in-memory markers directly.
bc.currentBlock.Store(newHeadBlock.Header())
bc.currentBlock.Store(newHeadBlock.Header()) //这里是改的地方
//这里是内存还是数据库?像是内存
log.Info("ZXL: rewind header now is", "header", newHeadBlock.Header().Number.Uint64())
headBlockGauge.Update(int64(newHeadBlock.NumberU64()))

Expand All @@ -784,6 +802,8 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
// Rewind the snap block in a simpleton way to the target head
if currentSnapBlock := bc.CurrentSnapBlock(); currentSnapBlock != nil && header.Number.Uint64() < currentSnapBlock.Number.Uint64() {
newHeadSnapBlock := bc.GetBlock(header.Hash(), header.Number.Uint64())
log.Info("ZXL: step into snap logic", "newHeadSnapBlock", newHeadSnapBlock.Number().Uint64())

// If either blocks reached nil, reset to the genesis state
if newHeadSnapBlock == nil {
newHeadSnapBlock = bc.genesisBlock
Expand All @@ -808,6 +828,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
if headNumber+1 < frozen {
wipe = pivot == nil || headNumber >= *pivot
}
//TODO 这里加上清理逻辑
return headHeader, wipe // Only force wipe if full synced
}
// Rewind the header chain, deleting all block bodies until then
Expand Down Expand Up @@ -868,31 +889,31 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
headHeaderUpdated := bc.CurrentBlock()
var safe *types.Header
if safe = bc.CurrentSafeBlock(); safe != nil && headHeaderUpdated.Number.Uint64() < safe.Number.Uint64() {
log.Warn("SetHead invalidated safe block", "before", safe.Number.Uint64(), "after", headHeaderUpdated.Number.Uint64())
log.Warn("1.SetHead invalidated safe block", "before", safe.Number.Uint64(), "after", headHeaderUpdated.Number.Uint64())
bc.SetSafe(headHeaderUpdated)
}
log.Info("ZXL", "safe before set", safe.Number.Uint64(), "after", bc.CurrentSafeBlock().Number.Uint64())

var finalized *types.Header

if finalized = bc.CurrentFinalBlock(); finalized != nil && headHeaderUpdated.Number.Uint64() < finalized.Number.Uint64() {
log.Error("SetHead invalidated finalized block", "before", finalized.Number.Uint64(), "after", headHeaderUpdated.Number.Uint64())
log.Error("1.SetHead invalidated finalized block", "before", finalized.Number.Uint64(), "after", headHeaderUpdated.Number.Uint64())
bc.SetFinalized(headHeaderUpdated)
}
log.Info("ZXL", "finalized before set", finalized.Number.Uint64(), "after", bc.CurrentFinalBlock().Number.Uint64())
} else {
// Clear safe block, finalized block if needed
var safe *types.Header
if safe = bc.CurrentSafeBlock(); safe != nil && head < safe.Number.Uint64() {
log.Warn("SetHead invalidated safe block")
log.Warn("2.SetHead invalidated safe block")
bc.SetSafe(nil)
}
log.Info("ZXL", "safe after set", safe.Number.Uint64(), "head", head)

var finalized *types.Header

if finalized = bc.CurrentFinalBlock(); finalized != nil && head < finalized.Number.Uint64() {
log.Error("SetHead invalidated finalized block")
log.Error("2.SetHead invalidated finalized block")
bc.SetFinalized(nil)
}
log.Info("ZXL", "finalized after set", finalized.Number.Uint64(), "head", head)
Expand Down Expand Up @@ -2475,6 +2496,7 @@ func (bc *BlockChain) InsertBlockWithoutSetHead(block *types.Block) error {
}
defer bc.chainmu.Unlock()

log.Info("ZXL: insert chain", "block", block.NumberU64())
_, err := bc.insertChain(types.Blocks{block}, false)
return err
}
Expand Down
6 changes: 6 additions & 0 deletions core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/log"
)

// CurrentHeader retrieves the current head header of the canonical chain. The
Expand Down Expand Up @@ -160,10 +161,15 @@ func (bc *BlockChain) HasFastBlock(hash common.Hash, number uint64) bool {
func (bc *BlockChain) GetBlock(hash common.Hash, number uint64) *types.Block {
// Short circuit if the block's already in the cache, retrieve otherwise
if block, ok := bc.blockCache.Get(hash); ok {
log.Debug("zxl get from cache", "block", number)
return block
}
block := rawdb.ReadBlock(bc.db, hash, number)
log.Debug("zxl get from db not cache", "block", number)
if block == nil {
if number == 3942690 {
log.Info("zxl: block 3942690 nil")
}
return nil
}
// Cache the found block for next time and return
Expand Down
21 changes: 21 additions & 0 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,16 @@ func ReadHeaderRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValu
// the canonical data.
data, _ = reader.Ancient(ChainFreezerHeaderTable, number)
if len(data) > 0 && crypto.Keccak256Hash(data) == hash {
if number == 3942690 {
log.Info("zxl: read header 3942690 Ancient")
}
return nil
}
// If not, try reading from leveldb
data, _ = db.Get(headerKey(number, hash))
if number == 3942690 {
log.Info("zxl: read header 3942690 db")
}
return nil
})
return data
Expand All @@ -383,6 +389,9 @@ func HasHeader(db ethdb.Reader, hash common.Hash, number uint64) bool {
func ReadHeader(db ethdb.Reader, hash common.Hash, number uint64) *types.Header {
data := ReadHeaderRLP(db, hash, number)
if len(data) == 0 {
if number == 3942690 {
log.Info("zxl: 3942690 header nil")
}
return nil
}
header := new(types.Header)
Expand Down Expand Up @@ -450,10 +459,16 @@ func ReadBodyRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue
// Check if the data is in ancients
if isCanon(reader, number, hash) {
data, _ = reader.Ancient(ChainFreezerBodiesTable, number)
if number == 3942690 {
log.Info("zxl: read body 3942690 from Ancient")
}
return nil
}
// If not, try reading from leveldb
data, _ = db.Get(blockBodyKey(number, hash))
if number == 3942690 {
log.Info("zxl: read body 3942690 from leveldb")
}
return nil
})
return data
Expand Down Expand Up @@ -772,10 +787,16 @@ func ReadLogs(db ethdb.Reader, hash common.Hash, number uint64) [][]*types.Log {
func ReadBlock(db ethdb.Reader, hash common.Hash, number uint64) *types.Block {
header := ReadHeader(db, hash, number)
if header == nil {
if number == 3942690 {
log.Debug("3242690 header nil")
}
return nil
}
body := ReadBody(db, hash, number)
if body == nil {
if number == 3942690 {
log.Debug("3242690 body nil")
}
return nil
}
return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles).WithWithdrawals(body.Withdrawals)
Expand Down
13 changes: 11 additions & 2 deletions eth/catalyst/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
api.forkchoiceLock.Lock()
defer api.forkchoiceLock.Unlock()

log.Trace("Engine API request received", "method", "ForkchoiceUpdated", "head", update.HeadBlockHash, "finalized", update.FinalizedBlockHash, "safe", update.SafeBlockHash)
log.Debug("Engine API request received", "method", "ForkchoiceUpdated", "head", update.HeadBlockHash, "finalized", update.FinalizedBlockHash, "safe", update.SafeBlockHash)
if update.HeadBlockHash == (common.Hash{}) {
log.Warn("Forkchoice requested update to zero hash")
return engine.STATUS_INVALID, nil // TODO(karalabe): Why does someone send us this?
Expand Down Expand Up @@ -324,6 +324,7 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
}
if rawdb.ReadCanonicalHash(api.eth.ChainDb(), block.NumberU64()) != update.HeadBlockHash {
// Block is not canonical, set head.
log.Debug("ZXL not Canonical")
if latestValid, err := api.eth.BlockChain().SetCanonical(block); err != nil {
return engine.ForkChoiceResponse{PayloadStatus: engine.PayloadStatusV1{Status: engine.INVALID, LatestValidHash: &latestValid}}, err
}
Expand Down Expand Up @@ -355,6 +356,7 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
return engine.STATUS_INVALID, engine.InvalidForkChoiceState.With(errors.New("final block not in canonical chain"))
}
// Set the finalized block
log.Info("ZXL: finalized set", "finalized", finalBlock.Number().Uint64())
api.eth.BlockChain().SetFinalized(finalBlock.Header())
}
// Check if the safe block hash is in our canonical tree, if not somethings wrong
Expand All @@ -369,12 +371,14 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
return engine.STATUS_INVALID, engine.InvalidForkChoiceState.With(errors.New("safe block not in canonical chain"))
}
// Set the safe block
log.Info("ZXL: safe set", "safe", safeBlock.Number().Uint64())
api.eth.BlockChain().SetSafe(safeBlock.Header())
}
// If payload generation was requested, create a new block to be potentially
// sealed by the beacon client. The payload will be requested later, and we
// will replace it arbitrarily many times in between.
if payloadAttributes != nil {
log.Warn("ZXL: fcu payloadAttributes has values")
if api.eth.BlockChain().Config().Optimism != nil && payloadAttributes.GasLimit == nil {
return engine.STATUS_INVALID, engine.InvalidPayloadAttributes.With(errors.New("gasLimit parameter is required"))
}
Expand Down Expand Up @@ -549,7 +553,7 @@ func (api *ConsensusAPI) newPayload(params engine.ExecutableData, versionedHashe
api.newPayloadLock.Lock()
defer api.newPayloadLock.Unlock()

log.Trace("Engine API request received", "method", "NewPayload", "number", params.Number, "hash", params.BlockHash)
log.Debug("Engine API request received", "method", "NewPayload", "number", params.Number, "hash", params.BlockHash)
block, err := engine.ExecutableDataToBlock(params, versionedHashes, beaconRoot)
if err != nil {
log.Warn("Invalid NewPayload params", "params", params, "error", err)
Expand All @@ -562,6 +566,11 @@ func (api *ConsensusAPI) newPayload(params engine.ExecutableData, versionedHashe

// If we already have the block locally, ignore the entire execution and just
// return a fake success.

log.Info("zxl current block", "block", api.eth.BlockChain().CurrentBlock().Number)
log.Info("zxl current safe", "safe", api.eth.BlockChain().CurrentSafeBlock().Number)
log.Info("zxl current header", "header", api.eth.BlockChain().CurrentHeader().Number)

if block := api.eth.BlockChain().GetBlockByHash(params.BlockHash); block != nil {
log.Warn("Ignoring already known beacon payload", "number", params.Number, "hash", params.BlockHash, "age", common.PrettyAge(time.Unix(int64(block.Time()), 0)))
hash := block.Hash()
Expand Down
2 changes: 1 addition & 1 deletion miner/payload_building.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (w *worker) buildPayload(args *BuildPayloadArgs) (*Payload, error) {
log.Error("Built empty payload error", "id", args.Id(), "error", empty.err)
return nil, empty.err
}
log.Info("Built empty payload succeed", "id", args.Id(), "number", empty.block.NumberU64(), "hash", empty.block.Hash(), "elapsed", common.PrettyDuration(time.Since(start)))
log.Info("Built empty payload succeed", "id", args.Id(), "number", empty.block.NumberU64(), "hash", empty.block.Hash(), "root", empty.block.Header().Root, "elapsed", common.PrettyDuration(time.Since(start)))

payload := newPayload(empty.block, args.Id())
// make sure to make it appear as full, otherwise it will wait indefinitely for payload building to complete.
Expand Down
1 change: 1 addition & 0 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ func (w *worker) makeEnv(parent *types.Header, header *types.Header, coinbase co
}
}
if err != nil {
log.Error("zxl error occured here", "missing block", parent.Number.Uint64(), "root", parent.Root)
return nil, err
}
state.StartPrefetcher("miner")
Expand Down
1 change: 1 addition & 0 deletions trie/triedb/pathdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func New(diskdb ethdb.Database, config *Config) *Database {

// Reader retrieves a layer belonging to the given state root.
func (db *Database) Reader(root common.Hash) (layer, error) {
log.Info("zxl get mpt", "root", root)
l := db.tree.get(root)
if l == nil {
r, err := db.tree.bottom().buffer.proposedBlockReader(root)
Expand Down

0 comments on commit 2422000

Please sign in to comment.