inserting header without set head from engine api should also validate the header

This commit is contained in:
jangko 2022-03-11 15:13:59 +07:00
parent 7dbc92f54c
commit 400722f1fa
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
3 changed files with 27 additions and 7 deletions

View File

@ -93,4 +93,6 @@ proc invalidStatus*(validHash: Hash256, msg: string): PayloadStatusV1 =
proc toBlockBody*(payload: ExecutionPayloadV1): BlockBody =
# TODO the transactions from the payload have to be converted here
discard payload.transactions
result.transactions.setLen(payload.transactions.len)
for i, tx in payload.transactions:
result.transactions[i] = rlp.decode(distinctBase tx, Transaction)

View File

@ -34,7 +34,7 @@ when not defined(release):
# ------------------------------------------------------------------------------
proc persistBlocksImpl(c: Chain; headers: openarray[BlockHeader];
bodies: openarray[BlockBody]): ValidationResult
bodies: openarray[BlockBody], setHead: bool = true): ValidationResult
# wildcard exception, wrapped below in public section
{.inline, raises: [Exception].} =
c.db.highestBlock = headers[^1].blockNumber
@ -72,8 +72,8 @@ proc persistBlocksImpl(c: Chain; headers: openarray[BlockHeader];
if validationResult != ValidationResult.OK:
return validationResult
if c.extraValidation and c.verifyFrom <= header.blockNumber:
if c.extraValidation and c.verifyFrom <= header.blockNumber:
let isBlockAfterTtd = c.isBlockAfterTtd(header)
if c.db.config.poaEngine and not isBlockAfterTtd:
var parent = if 0 < i: @[headers[i-1]] else: @[]
@ -98,7 +98,11 @@ proc persistBlocksImpl(c: Chain; headers: openarray[BlockHeader];
msg = res.error
return ValidationResult.Error
discard c.db.persistHeaderToDb(header)
if setHead:
discard c.db.persistHeaderToDb(header)
else:
c.db.persistHeaderToDbWithoutSetHead(header)
discard c.db.persistTransactions(header.blockNumber, body.transactions)
discard c.db.persistReceipts(vmState.receipts)
@ -106,9 +110,20 @@ proc persistBlocksImpl(c: Chain; headers: openarray[BlockHeader];
# so the rpc return consistent result
# between eth_blockNumber and eth_syncing
c.db.currentBlock = header.blockNumber
transaction.commit()
# ------------------------------------------------------------------------------
# Public `ChainDB` methods
# ------------------------------------------------------------------------------
proc insertBlockWithoutSetHead*(c: Chain, header: BlockHeader,
body: BlockBody): ValidationResult
{.gcsafe, raises: [Defect,CatchableError].} =
safeP2PChain("persistBlocks"):
result = c.persistBlocksImpl([header], [body], setHead = false)
# ------------------------------------------------------------------------------
# Public `AbstractChainDB` overload method
# ------------------------------------------------------------------------------

View File

@ -93,7 +93,10 @@ proc setupEngineAPI*(
trace "Inserting block without sethead",
hash = blockHash.data.toHex, number = header.blockNumber
db.persistHeaderToDbWithoutSetHead(header)
let body = toBlockBody(payload)
let vres = sealingEngine.chain.insertBlockWithoutSetHead(header, body)
if vres != ValidationResult.OK:
return invalidStatus(db.getCurrentBlockHash(), "Failed to insert block")
# We've accepted a valid payload from the beacon client. Mark the local
# chain transitions to notify other subsystems (e.g. downloader) of the