Don't feed invalid headers into DB (#4693)

* Reduce code duplication in verifyAndSaveNewPoSHeader

* Don't feed invalid headers into DB

* remove superfluous comment
This commit is contained in:
Andrew Ashikhmin 2022-07-12 10:33:43 +02:00 committed by GitHub
parent 9637b25a42
commit e0845f229b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -579,39 +579,21 @@ func verifyAndSaveNewPoSHeader(
}, false, nil }, false, nil
} }
if err := headerInserter.FeedHeaderPoS(tx, header, headerHash); err != nil {
return nil, false, err
}
currentHeadHash := rawdb.ReadHeadHeaderHash(tx) currentHeadHash := rawdb.ReadHeadHeaderHash(tx)
if currentHeadHash != header.ParentHash { canExtendCanonical := header.ParentHash == currentHeadHash
// Side chain or something weird canExtendInMemory := cfg.memoryOverlay && (cfg.forkValidator.ExtendingForkHeadHash() == (common.Hash{}) || header.ParentHash == cfg.forkValidator.ExtendingForkHeadHash())
// 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?
status, latestValidHash, validationError, criticalError := cfg.forkValidator.ValidatePayload(tx, header, body, false)
if criticalError != nil {
return &privateapi.PayloadStatus{CriticalError: criticalError}, false, criticalError
}
if validationError != nil {
cfg.hd.ReportBadHeaderPoS(headerHash, latestValidHash)
}
success = status == remote.EngineStatus_VALID || status == remote.EngineStatus_ACCEPTED
return &privateapi.PayloadStatus{
Status: status,
LatestValidHash: latestValidHash,
ValidationError: validationError,
}, success, nil
}
if cfg.memoryOverlay && (cfg.forkValidator.ExtendingForkHeadHash() == (common.Hash{}) || header.ParentHash == cfg.forkValidator.ExtendingForkHeadHash()) { if canExtendInMemory || !canExtendCanonical {
status, latestValidHash, validationError, criticalError := cfg.forkValidator.ValidatePayload(tx, header, body, true) status, latestValidHash, validationError, criticalError := cfg.forkValidator.ValidatePayload(tx, header, body, canExtendCanonical)
if criticalError != nil { if criticalError != nil {
return &privateapi.PayloadStatus{CriticalError: criticalError}, false, criticalError return &privateapi.PayloadStatus{CriticalError: criticalError}, false, criticalError
} }
if validationError != nil { success = validationError == nil
if !success {
cfg.hd.ReportBadHeaderPoS(headerHash, latestValidHash) cfg.hd.ReportBadHeaderPoS(headerHash, latestValidHash)
} else if err := headerInserter.FeedHeaderPoS(tx, header, headerHash); err != nil {
return nil, false, err
} }
success = status == remote.EngineStatus_VALID || status == remote.EngineStatus_ACCEPTED
return &privateapi.PayloadStatus{ return &privateapi.PayloadStatus{
Status: status, Status: status,
LatestValidHash: latestValidHash, LatestValidHash: latestValidHash,
@ -620,6 +602,10 @@ func verifyAndSaveNewPoSHeader(
} }
// OK, we're on the canonical chain // OK, we're on the canonical chain
if err := headerInserter.FeedHeaderPoS(tx, header, headerHash); err != nil {
return nil, false, err
}
if requestStatus == engineapi.New { if requestStatus == engineapi.New {
cfg.hd.SetPendingPayloadHash(headerHash) cfg.hd.SetPendingPayloadHash(headerHash)
} }