From 07fd4e9b50659e7b3b585998d84ef217089daf45 Mon Sep 17 00:00:00 2001 From: jangko Date: Thu, 17 Aug 2023 07:27:34 +0700 Subject: [PATCH] Implement engine_getPayloadBodiesByRangeV1 --- nimbus/rpc/engine_api.nim | 53 +++++++++++++++++++++++++++++++-- nimbus/rpc/merge/mergeutils.nim | 6 ++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/nimbus/rpc/engine_api.nim b/nimbus/rpc/engine_api.nim index ba5b1aa39..2eb9d47f1 100644 --- a/nimbus/rpc/engine_api.nim +++ b/nimbus/rpc/engine_api.nim @@ -33,6 +33,7 @@ import {.push raises: [].} type + BlockHeader = eth_types.BlockHeader Hash256 = eth_types.Hash256 Web3Blob = web3types.Blob Web3KZGProof = web3types.KZGProof @@ -495,7 +496,15 @@ proc handle_forkchoiceUpdated(sealingEngine: SealingEngineRef, func toHash(value: array[32, byte]): Hash256 = result.data = value -proc handle_getPayloadBodiesByHash(sealingEngine: SealingEngineRef, hashes: seq[BlockHash]): seq[Option[ExecutionPayloadBodyV1]] {.raises: [CatchableError].} = +const + maxBodyRequest = 32 + +proc handle_getPayloadBodiesByHash(sealingEngine: SealingEngineRef, + hashes: seq[BlockHash]): + seq[Option[ExecutionPayloadBodyV1]] {.raises: [CatchableError].} = + if hashes.len > maxBodyRequest: + raise tooLargeRequest("request exceeds max allowed " & $maxBodyRequest) + let db = sealingEngine.chain.db var body: BlockBody for h in hashes: @@ -515,6 +524,42 @@ proc handle_getPayloadBodiesByHash(sealingEngine: SealingEngineRef, hashes: seq[ else: result.add(none[ExecutionPayloadBodyV1]()) +proc handle_getPayloadBodiesByRange(sealingEngine: SealingEngineRef, + start: uint64, count: uint64): + seq[Option[ExecutionPayloadBodyV1]] {.raises: [CatchableError].} = + if start == 0: + raise invalidParams("start block should greater than zero") + + if count == 0: + raise invalidParams("blocks count should greater than zero") + + if count > maxBodyRequest: + raise tooLargeRequest("request exceeds max allowed " & $maxBodyRequest) + + let db = sealingEngine.chain.db + var body: BlockBody + var header: BlockHeader + for bn in start.. seq[Option[ExecutionPayloadBodyV1]]: return handle_getPayloadBodiesByHash(sealingEngine, hashes) + server.rpc("engine_getPayloadBodiesByRangeV1") do( + start: Quantity, count: Quantity) -> seq[Option[ExecutionPayloadBodyV1]]: + return handle_getPayloadBodiesByRange(sealingEngine, start.uint64, count.uint64) diff --git a/nimbus/rpc/merge/mergeutils.nim b/nimbus/rpc/merge/mergeutils.nim index 71f548c27..a33c46f1f 100644 --- a/nimbus/rpc/merge/mergeutils.nim +++ b/nimbus/rpc/merge/mergeutils.nim @@ -245,3 +245,9 @@ proc unsupportedFork*(msg: string): ref InvalidRequest = code: engineApiUnsupportedFork, msg: msg ) + +proc tooLargeRequest*(msg: string): ref InvalidRequest = + (ref InvalidRequest)( + code: engineApiTooLargeRequest, + msg: msg + )