From 087105d1f31d83ff88ceeca30dfe36dc01de28d7 Mon Sep 17 00:00:00 2001 From: Giulio rebuffo Date: Thu, 30 Jun 2022 18:20:21 +0200 Subject: [PATCH] lvh on invalid transition block (#4583) --- eth/stagedsync/stage_headers.go | 4 ++-- turbo/stages/headerdownload/header_algos.go | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/eth/stagedsync/stage_headers.go b/eth/stagedsync/stage_headers.go index cebd06e98a9..4aefc5079e6 100644 --- a/eth/stagedsync/stage_headers.go +++ b/eth/stagedsync/stage_headers.go @@ -585,7 +585,7 @@ func verifyAndSaveNewPoSHeader( // TODO(yperbasis): considered non-canonical because some missing headers were downloaded but not canonized // Or it's not a problem because forkChoice is updated frequently? if cfg.memoryOverlay { - status, latestValidHash, validationError, criticalError := cfg.hd.ValidatePayload(tx, header, body, false, cfg.execPayload) + status, latestValidHash, validationError, criticalError := cfg.hd.ValidatePayload(tx, header, body, cfg.chainConfig.TerminalTotalDifficulty, false, cfg.execPayload) if criticalError != nil { return &privateapi.PayloadStatus{CriticalError: criticalError}, false, criticalError } @@ -601,7 +601,7 @@ func verifyAndSaveNewPoSHeader( } if cfg.memoryOverlay && (cfg.hd.GetNextForkHash() == (common.Hash{}) || header.ParentHash == cfg.hd.GetNextForkHash()) { - status, latestValidHash, validationError, criticalError := cfg.hd.ValidatePayload(tx, header, body, true, cfg.execPayload) + status, latestValidHash, validationError, criticalError := cfg.hd.ValidatePayload(tx, header, body, cfg.chainConfig.TerminalTotalDifficulty, true, cfg.execPayload) if criticalError != nil { return &privateapi.PayloadStatus{CriticalError: criticalError}, false, criticalError } diff --git a/turbo/stages/headerdownload/header_algos.go b/turbo/stages/headerdownload/header_algos.go index 759ba81be44..1aabb1e3f00 100644 --- a/turbo/stages/headerdownload/header_algos.go +++ b/turbo/stages/headerdownload/header_algos.go @@ -1093,7 +1093,7 @@ func abs64(n int64) uint64 { return uint64(n) } -func (hd *HeaderDownload) ValidatePayload(tx kv.RwTx, header *types.Header, body *types.RawBody, store bool, execPayload func(kv.RwTx, *types.Header, *types.RawBody, uint64, []*types.Header, []*types.RawBody) error) (status remote.EngineStatus, latestValidHash common.Hash, validationError error, criticalError error) { +func (hd *HeaderDownload) ValidatePayload(tx kv.RwTx, header *types.Header, body *types.RawBody, terminalTotalDifficulty *big.Int, store bool, execPayload func(kv.RwTx, *types.Header, *types.RawBody, uint64, []*types.Header, []*types.RawBody) error) (status remote.EngineStatus, latestValidHash common.Hash, validationError error, criticalError error) { hd.lock.Lock() defer hd.lock.Unlock() maxDepth := uint64(16) @@ -1103,6 +1103,11 @@ func (hd *HeaderDownload) ValidatePayload(tx kv.RwTx, header *types.Header, body criticalError = fmt.Errorf("could not read block number.") return } + + isAncestorPosBlock, criticalError := rawdb.Transitioned(tx, header.Number.Uint64()-1, terminalTotalDifficulty) + if criticalError != nil { + return + } if store { // If it is a continuation of the canonical chain we can stack it up. if hd.nextForkState == nil { @@ -1115,7 +1120,9 @@ func (hd *HeaderDownload) ValidatePayload(tx kv.RwTx, header *types.Header, body validationError = execPayload(hd.nextForkState, header, body, 0, nil, nil) if validationError != nil { status = remote.EngineStatus_INVALID - latestValidHash = header.ParentHash + if isAncestorPosBlock { + latestValidHash = header.ParentHash + } return } status = remote.EngineStatus_VALID @@ -1165,7 +1172,9 @@ func (hd *HeaderDownload) ValidatePayload(tx kv.RwTx, header *types.Header, body validationError = execPayload(batch, header, body, unwindPoint, headersChain, bodiesChain) latestValidHash = header.Hash() if validationError != nil { - latestValidHash = header.ParentHash + if isAncestorPosBlock { + latestValidHash = header.ParentHash + } status = remote.EngineStatus_INVALID } // After the we finished executing, we clean up old forks