Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
removed code duplication (ledgerwatch#4697)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 committed Jul 12, 2022
1 parent b161c27 commit 6b6b74e
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions turbo/engineapi/fork_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,7 @@ func (fv *ForkValidator) ValidatePayload(tx kv.RwTx, header *types.Header, body
}
// Update fork head hash.
fv.extendingForkHeadHash = header.Hash()
// Let's assemble the side fork chain if we have others building.
validationError = fv.validatePayload(fv.extendingFork, header, body, 0, nil, nil)
if validationError != nil {
status = remote.EngineStatus_INVALID
latestValidHash = header.ParentHash
return
}
status = remote.EngineStatus_VALID
latestValidHash = header.Hash()
fv.sideForksBlock[latestValidHash] = forkSegment{header, body}
return
return fv.validateAndStorePayload(fv.extendingFork, header, body, 0, nil, nil)
}
// If the block is stored within the side fork it means it was already validated.
if _, ok := fv.sideForksBlock[header.Hash()]; ok {
Expand Down Expand Up @@ -167,19 +157,10 @@ func (fv *ForkValidator) ValidatePayload(tx kv.RwTx, header *types.Header, body
}
unwindPoint = sb.header.Number.Uint64() - 1
}
status = remote.EngineStatus_VALID
// if it is not canonical we validate it as a side fork.
// if it is not canonical we validate it in memory and discard it aferwards.
batch := memdb.NewMemoryBatch(tx)
defer batch.Close()
validationError = fv.validatePayload(batch, header, body, unwindPoint, headersChain, bodiesChain)
latestValidHash = header.Hash()
if validationError != nil {
latestValidHash = header.ParentHash
status = remote.EngineStatus_INVALID
return
}
fv.sideForksBlock[header.Hash()] = forkSegment{header, body}
return
return fv.validateAndStorePayload(batch, header, body, unwindPoint, headersChain, bodiesChain)
}

// Clear wipes out current extending fork data, this method is called after fcu is called,
Expand All @@ -201,6 +182,20 @@ func (fv *ForkValidator) Clear(tx kv.RwTx) {
fv.extendingFork = nil
}

// validateAndStorePayload validate and store a payload fork chain if such chain results valid.
func (fv *ForkValidator) validateAndStorePayload(tx kv.RwTx, header *types.Header, body *types.RawBody, unwindPoint uint64, headersChain []*types.Header, bodiesChain []*types.RawBody) (status remote.EngineStatus, latestValidHash common.Hash, validationError error, criticalError error) {
validationError = fv.validatePayload(tx, header, body, unwindPoint, headersChain, bodiesChain)
latestValidHash = header.Hash()
if validationError != nil {
latestValidHash = header.ParentHash
status = remote.EngineStatus_INVALID
return
}
status = remote.EngineStatus_VALID
fv.sideForksBlock[header.Hash()] = forkSegment{header, body}
return
}

// clean wipes out all outdated sideforks whose distance exceed the height of the head.
func (fv *ForkValidator) clean() {
for hash, sb := range fv.sideForksBlock {
Expand Down

0 comments on commit 6b6b74e

Please sign in to comment.