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 24, 2024
1 parent 7c537b7 commit 23edeb0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
33 changes: 27 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
6 changes: 5 additions & 1 deletion 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
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())
return nil, err
}
state.StartPrefetcher("miner")
Expand Down

0 comments on commit 23edeb0

Please sign in to comment.