add haveBlockAndState to BaseChainDB and use it in engine api

This commit is contained in:
jangko 2022-06-30 14:34:55 +07:00
parent fdb048ed21
commit e274b347ae
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
3 changed files with 31 additions and 7 deletions

View File

@ -478,3 +478,10 @@ proc safeHeader*(self: BaseChainDB): BlockHeader =
proc finalizedHeader*(self: BaseChainDB): BlockHeader =
self.getBlockHeader(self.finalizedHeaderHash)
proc haveBlockAndState*(self: BaseChainDB, headerHash: Hash256): bool =
var header: BlockHeader
if not self.getBlockHeader(headerHash, header):
return false
# see if stateRoot exists
self.exists(header.stateRoot)

View File

@ -105,6 +105,12 @@ proc invalidStatus*(validHash: Hash256 = Hash256()): PayloadStatusV1 =
latestValidHash: some(BlockHash validHash.data)
)
proc acceptedStatus*(validHash: Hash256): PayloadStatusV1 =
PayloadStatusV1(
status: PayloadExecutionStatus.accepted,
latestValidHash: some(BlockHash validHash.data)
)
proc toBlockBody*(payload: ExecutionPayloadV1): BlockBody =
result.transactions.setLen(payload.transactions.len)
for i, tx in payload.transactions:

View File

@ -20,6 +20,15 @@ import
import eth/common/eth_types except BlockHeader
proc latestValidHash(db: BaseChainDB, parent: EthBlockHeader, ttd: DifficultyInt): Hash256 =
let ptd = db.getScore(parent.parentHash)
if ptd >= ttd:
db.getHeadBlockHash()
else:
# If the most recent valid ancestor is a PoW block,
# latestValidHash MUST be set to ZERO
Hash256()
proc setupEngineAPI*(
sealingEngine: SealingEngineRef,
server: RpcServer) =
@ -91,18 +100,20 @@ proc setupEngineAPI*(
parent = header.timestamp, header = header.timestamp
return invalidStatus(db.getHeadBlockHash(), "Invalid timestamp")
if not db.haveBlockAndState(header.parentHash):
api.put(blockHash, header)
warn "State not available, ignoring new payload",
hash = blockHash.data.toHex,
number = header.blockNumber
let blockHash = latestValidHash(db, parent, ttd)
return acceptedStatus(blockHash)
trace "Inserting block without sethead",
hash = blockHash.data.toHex, number = header.blockNumber
let body = toBlockBody(payload)
let vres = sealingEngine.chain.insertBlockWithoutSetHead(header, body)
if vres != ValidationResult.OK:
let ptd = db.getScore(parent.parentHash)
let blockHash = if ptd >= ttd:
db.getHeadBlockHash()
else:
# If the most recent valid ancestor is a PoW block,
# latestValidHash MUST be set to ZERO
Hash256()
let blockHash = latestValidHash(db, parent, ttd)
return invalidStatus(blockHash, "Failed to insert block")
# We've accepted a valid payload from the beacon client. Mark the local