Skip to content

Commit

Permalink
Added proper cleanup when we get notified of new height (#4753)
Browse files Browse the repository at this point in the history
* added proper cleanup when we get notified of new height

* added extra cleanup

* removed bad if condition

* fixed hive tests

Co-authored-by: giuliorebuffo <giuliorebuffo@system76-pc.localdomain>
  • Loading branch information
Giulio2002 and giuliorebuffo committed Jul 19, 2022
1 parent d039901 commit d4f865d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
15 changes: 9 additions & 6 deletions eth/stagedsync/stage_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ func startHandlingForkChoice(
cfg HeadersCfg,
headerInserter *headerdownload.HeaderInserter,
) (*privateapi.PayloadStatus, error) {
headerHash := forkChoice.HeadBlockHash
log.Debug(fmt.Sprintf("[%s] Handling fork choice", s.LogPrefix()), "headerHash", headerHash)
if cfg.memoryOverlay {
defer cfg.forkValidator.Clear(tx)
defer cfg.forkValidator.ClearWithUnwind(tx)
}
headerHash := forkChoice.HeadBlockHash
log.Debug(fmt.Sprintf("[%s] Handling fork choice", s.LogPrefix()), "headerHash", headerHash)

currentHeadHash := rawdb.ReadHeadHeaderHash(tx)
if currentHeadHash == headerHash { // no-op
Expand Down Expand Up @@ -577,10 +577,11 @@ func verifyAndSaveNewPoSHeader(
forkingHash, err := cfg.blockReader.CanonicalHash(ctx, tx, forkingPoint)

canExtendCanonical := forkingHash == currentHeadHash
canExtendFork := cfg.forkValidator.ExtendingForkHeadHash() == (common.Hash{}) || header.ParentHash == cfg.forkValidator.ExtendingForkHeadHash()

if cfg.memoryOverlay && (canExtendFork || header.ParentHash != currentHeadHash) {
status, latestValidHash, validationError, criticalError := cfg.forkValidator.ValidatePayload(tx, header, body, header.ParentHash == currentHeadHash /* extendCanonical */)
if cfg.memoryOverlay {
extendingHash := cfg.forkValidator.ExtendingForkHeadHash()
extendCanonical := (extendingHash == common.Hash{} && header.ParentHash == currentHeadHash) || extendingHash == header.ParentHash
status, latestValidHash, validationError, criticalError := cfg.forkValidator.ValidatePayload(tx, header, body, extendCanonical)
if criticalError != nil {
return nil, false, criticalError
}
Expand Down Expand Up @@ -664,6 +665,8 @@ func schedulePoSDownload(
}

func verifyAndSaveDownloadedPoSHeaders(tx kv.RwTx, cfg HeadersCfg, headerInserter *headerdownload.HeaderInserter) {
defer cfg.forkValidator.Clear()

var lastValidHash common.Hash
var badChainError error
var foundPow bool
Expand Down
25 changes: 22 additions & 3 deletions turbo/engineapi/fork_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ func (fv *ForkValidator) ExtendingForkHeadHash() common.Hash {
// NotifyCurrentHeight is to be called at the end of the stage cycle and repressent the last processed block.
func (fv *ForkValidator) NotifyCurrentHeight(currentHeight uint64) {
fv.currentHeight = currentHeight
// If the head changed,e previous assumptions on head are incorrect now.
if fv.extendingFork != nil {
fv.extendingFork.Rollback()
}
fv.extendingFork = nil
fv.extendingForkHeadHash = common.Hash{}
}

// FlushExtendingFork flush the current extending fork if fcu chooses its head hash as the its forkchoice.
Expand Down Expand Up @@ -176,7 +182,16 @@ func (fv *ForkValidator) ValidatePayload(tx kv.RwTx, header *types.Header, body
// Clear wipes out current extending fork data, this method is called after fcu is called,
// because fcu decides what the head is and after the call is done all the non-chosed forks are
// to be considered obsolete.
func (fv *ForkValidator) Clear(tx kv.RwTx) {
func (fv *ForkValidator) Clear() {
if fv.extendingFork != nil {
fv.extendingFork.Rollback()
}
fv.extendingForkHeadHash = common.Hash{}
fv.extendingFork = nil
}

// Clear wipes out current extending fork data and notify txpool.
func (fv *ForkValidator) ClearWithUnwind(tx kv.RwTx) {
sb, ok := fv.sideForksBlock[fv.extendingForkHeadHash]
// If we did not flush the fork state, then we need to notify the txpool through unwind.
if fv.extendingFork != nil && fv.extendingForkHeadHash != (common.Hash{}) && ok {
Expand All @@ -187,8 +202,7 @@ func (fv *ForkValidator) Clear(tx kv.RwTx) {
}
fv.extendingFork.Rollback()
}
fv.extendingForkHeadHash = common.Hash{}
fv.extendingFork = nil
fv.Clear()
}

// validateAndStorePayload validate and store a payload fork chain if such chain results valid.
Expand All @@ -198,6 +212,11 @@ func (fv *ForkValidator) validateAndStorePayload(tx kv.RwTx, header *types.Heade
if validationError != nil {
latestValidHash = header.ParentHash
status = remote.EngineStatus_INVALID
if fv.extendingFork != nil {
fv.extendingFork.Rollback()
fv.extendingFork = nil
}
fv.extendingForkHeadHash = common.Hash{}
return
}
// If we do not have the body we can recover it from the batch.
Expand Down

0 comments on commit d4f865d

Please sign in to comment.