2021-10-05 23:31:35 +00:00
|
|
|
# Nimbus
|
2023-08-27 01:23:45 +00:00
|
|
|
# Copyright (c) 202-2023 Status Research & Development GmbH
|
2021-10-05 23:31:35 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
|
|
|
import
|
2023-08-27 01:23:45 +00:00
|
|
|
std/[typetraits, sequtils, sets],
|
|
|
|
stew/[byteutils],
|
2023-01-31 01:32:17 +00:00
|
|
|
json_rpc/rpcserver,
|
2023-12-08 09:35:50 +00:00
|
|
|
web3/[conversions, execution_types],
|
2023-08-27 01:23:45 +00:00
|
|
|
../beacon/api_handler,
|
|
|
|
../beacon/beacon_engine,
|
|
|
|
../beacon/web3_eth_conv,
|
2023-10-23 13:59:57 +00:00
|
|
|
../beacon/api_handler/api_utils
|
2021-10-05 23:31:35 +00:00
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2023-03-09 23:40:55 +00:00
|
|
|
const supportedMethods: HashSet[string] =
|
|
|
|
toHashSet([
|
|
|
|
"engine_newPayloadV1",
|
|
|
|
"engine_newPayloadV2",
|
2023-08-08 03:44:29 +00:00
|
|
|
"engine_newPayloadV3",
|
2023-03-09 23:40:55 +00:00
|
|
|
"engine_getPayloadV1",
|
|
|
|
"engine_getPayloadV2",
|
2023-08-08 03:44:29 +00:00
|
|
|
"engine_getPayloadV3",
|
2023-03-09 23:40:55 +00:00
|
|
|
"engine_exchangeTransitionConfigurationV1",
|
|
|
|
"engine_forkchoiceUpdatedV1",
|
|
|
|
"engine_forkchoiceUpdatedV2",
|
2023-08-08 07:29:23 +00:00
|
|
|
"engine_forkchoiceUpdatedV3",
|
2023-08-17 00:27:34 +00:00
|
|
|
"engine_getPayloadBodiesByHashV1",
|
|
|
|
"engine_getPayloadBodiesByRangeV1",
|
2023-03-09 23:40:55 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
# I'm trying to keep the handlers below very thin, and move the
|
|
|
|
# bodies up to the various procs above. Once we have multiple
|
|
|
|
# versions, they'll need to be able to share code.
|
2023-08-27 01:23:45 +00:00
|
|
|
proc setupEngineAPI*(engine: BeaconEngineRef, server: RpcServer) =
|
2021-10-05 23:31:35 +00:00
|
|
|
|
2023-03-09 23:40:55 +00:00
|
|
|
server.rpc("engine_exchangeCapabilities") do(methods: seq[string]) -> seq[string]:
|
|
|
|
return methods.filterIt(supportedMethods.contains(it))
|
2022-12-10 01:32:55 +00:00
|
|
|
|
2023-03-09 23:40:55 +00:00
|
|
|
server.rpc("engine_newPayloadV1") do(payload: ExecutionPayloadV1) -> PayloadStatusV1:
|
2023-10-23 02:25:03 +00:00
|
|
|
return engine.newPayload(Version.V1, payload.executionPayload)
|
2023-05-16 13:52:44 +00:00
|
|
|
|
2023-08-08 07:29:23 +00:00
|
|
|
server.rpc("engine_newPayloadV2") do(payload: ExecutionPayload) -> PayloadStatusV1:
|
2023-10-23 02:25:03 +00:00
|
|
|
return engine.newPayload(Version.V2, payload)
|
2023-08-08 07:29:23 +00:00
|
|
|
|
|
|
|
server.rpc("engine_newPayloadV3") do(payload: ExecutionPayload,
|
2023-10-23 13:59:57 +00:00
|
|
|
expectedBlobVersionedHashes: Option[seq[Web3Hash]],
|
|
|
|
parentBeaconBlockRoot: Option[Web3Hash]) -> PayloadStatusV1:
|
|
|
|
if expectedBlobVersionedHashes.isNone:
|
|
|
|
raise invalidParams("newPayloadV3 expect blobVersionedHashes but got none")
|
|
|
|
if not validateVersionedHashed(payload, expectedBlobVersionedHashes.get):
|
2023-08-27 01:23:45 +00:00
|
|
|
return invalidStatus()
|
2023-10-23 13:59:57 +00:00
|
|
|
return engine.newPayload(Version.V3, payload, parentBeaconBlockRoot)
|
2022-12-10 01:32:55 +00:00
|
|
|
|
2023-03-09 23:40:55 +00:00
|
|
|
server.rpc("engine_getPayloadV1") do(payloadId: PayloadID) -> ExecutionPayloadV1:
|
2023-10-23 13:59:57 +00:00
|
|
|
return engine.getPayload(Version.V1, payloadId).executionPayload.V1
|
2021-10-05 23:31:35 +00:00
|
|
|
|
2023-03-09 23:40:55 +00:00
|
|
|
server.rpc("engine_getPayloadV2") do(payloadId: PayloadID) -> GetPayloadV2Response:
|
2023-10-23 13:59:57 +00:00
|
|
|
return engine.getPayload(Version.V2, payloadId)
|
2021-10-05 23:31:35 +00:00
|
|
|
|
2023-08-08 07:29:23 +00:00
|
|
|
server.rpc("engine_getPayloadV3") do(payloadId: PayloadID) -> GetPayloadV3Response:
|
2023-08-27 01:23:45 +00:00
|
|
|
return engine.getPayloadV3(payloadId)
|
2023-08-08 07:29:23 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
server.rpc("engine_exchangeTransitionConfigurationV1") do(
|
|
|
|
conf: TransitionConfigurationV1) -> TransitionConfigurationV1:
|
|
|
|
return engine.exchangeConf(conf)
|
2021-10-05 23:31:35 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
server.rpc("engine_forkchoiceUpdatedV1") do(update: ForkchoiceStateV1,
|
|
|
|
attrs: Option[PayloadAttributesV1]) -> ForkchoiceUpdatedResponse:
|
2023-10-23 02:25:03 +00:00
|
|
|
return engine.forkchoiceUpdated(Version.V1, update, attrs.payloadAttributes)
|
2022-02-22 08:55:04 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
server.rpc("engine_forkchoiceUpdatedV2") do(update: ForkchoiceStateV1,
|
|
|
|
attrs: Option[PayloadAttributes]) -> ForkchoiceUpdatedResponse:
|
2023-10-23 02:25:03 +00:00
|
|
|
return engine.forkchoiceUpdated(Version.V2, update, attrs)
|
2023-08-08 07:29:23 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
server.rpc("engine_forkchoiceUpdatedV3") do(update: ForkchoiceStateV1,
|
|
|
|
attrs: Option[PayloadAttributes]) -> ForkchoiceUpdatedResponse:
|
2023-10-23 02:25:03 +00:00
|
|
|
return engine.forkchoiceUpdated(Version.V3, update, attrs)
|
2023-03-09 23:40:55 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
server.rpc("engine_getPayloadBodiesByHashV1") do(hashes: seq[Web3Hash]) ->
|
|
|
|
seq[Option[ExecutionPayloadBodyV1]]:
|
|
|
|
return engine.getPayloadBodiesByHash(hashes)
|
2022-02-22 08:55:04 +00:00
|
|
|
|
2023-08-17 00:27:34 +00:00
|
|
|
server.rpc("engine_getPayloadBodiesByRangeV1") do(
|
|
|
|
start: Quantity, count: Quantity) -> seq[Option[ExecutionPayloadBodyV1]]:
|
2023-08-27 01:23:45 +00:00
|
|
|
return engine.getPayloadBodiesByRange(start.uint64, count.uint64)
|