2024-02-29 13:24:08 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2024-02-29 13:24:08 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
## Fake execution engine API implementation useful for testing beacon node without a running execution node
|
2022-10-18 22:18:36 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
std/typetraits,
|
2023-05-09 22:51:57 +00:00
|
|
|
stew/byteutils,
|
2022-10-18 22:18:36 +00:00
|
|
|
json_rpc/[rpcserver, errors],
|
2023-12-12 15:15:00 +00:00
|
|
|
web3/[conversions, engine_api_types, eth_api_types],
|
2022-10-18 22:18:36 +00:00
|
|
|
chronicles
|
|
|
|
|
|
|
|
proc setupEngineAPI*(server: RpcServer) =
|
2023-04-17 20:11:28 +00:00
|
|
|
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/paris.md#engine_newpayloadv1
|
2022-10-18 22:18:36 +00:00
|
|
|
# cannot use `params` as param name. see https:#github.com/status-im/nim-json-rpc/issues/128
|
|
|
|
server.rpc("engine_newPayloadV1") do(payload: ExecutionPayloadV1) -> PayloadStatusV1:
|
|
|
|
info "engine_newPayloadV1",
|
|
|
|
number = $(distinctBase payload.blockNumber), hash = payload.blockHash
|
|
|
|
|
|
|
|
return PayloadStatusV1(
|
|
|
|
status: PayloadExecutionStatus.syncing,
|
|
|
|
)
|
|
|
|
|
2023-05-09 22:51:57 +00:00
|
|
|
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/shanghai.md#engine_newpayloadv2
|
|
|
|
server.rpc("engine_newPayloadV2") do(payload: ExecutionPayloadV2) -> PayloadStatusV1:
|
|
|
|
info "engine_newPayloadV2", payload
|
|
|
|
|
|
|
|
return PayloadStatusV1(
|
|
|
|
status: PayloadExecutionStatus.syncing,
|
|
|
|
)
|
|
|
|
|
2023-04-17 20:11:28 +00:00
|
|
|
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/paris.md#engine_getpayloadv1
|
2022-10-18 22:18:36 +00:00
|
|
|
server.rpc("engine_getPayloadV1") do(payloadId: PayloadID) -> ExecutionPayloadV1:
|
|
|
|
info "engine_getPayloadV1",
|
|
|
|
id = payloadId.toHex
|
|
|
|
|
|
|
|
raise (ref InvalidRequest)(
|
|
|
|
code: engineApiUnknownPayload,
|
2024-01-29 09:37:38 +00:00
|
|
|
msg: "Unknown payload"
|
2022-10-18 22:18:36 +00:00
|
|
|
)
|
|
|
|
|
2023-04-17 20:11:28 +00:00
|
|
|
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/paris.md#engine_exchangetransitionconfigurationv1
|
2022-10-18 22:18:36 +00:00
|
|
|
server.rpc("engine_exchangeTransitionConfigurationV1") do(conf: TransitionConfigurationV1) -> TransitionConfigurationV1:
|
|
|
|
info "engine_exchangeTransitionConfigurationV1",
|
|
|
|
ttd = conf.terminalTotalDifficulty,
|
|
|
|
number = uint64(conf.terminalBlockNumber),
|
|
|
|
blockHash = conf.terminalBlockHash
|
|
|
|
|
|
|
|
return conf
|
|
|
|
|
2023-04-17 20:11:28 +00:00
|
|
|
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/paris.md#engine_forkchoiceupdatedv1
|
2022-10-18 22:18:36 +00:00
|
|
|
server.rpc("engine_forkchoiceUpdatedV1") do(
|
|
|
|
update: ForkchoiceStateV1,
|
2024-06-12 01:46:39 +00:00
|
|
|
payloadAttributes: Opt[PayloadAttributesV1]) -> ForkchoiceUpdatedResponse:
|
2022-10-18 22:18:36 +00:00
|
|
|
info "engine_forkchoiceUpdatedV1",
|
|
|
|
update,
|
|
|
|
payloadAttributes
|
|
|
|
|
|
|
|
return ForkchoiceUpdatedResponse(
|
|
|
|
payloadStatus: PayloadStatusV1(
|
|
|
|
status: PayloadExecutionStatus.syncing))
|
|
|
|
|
2023-05-09 22:51:57 +00:00
|
|
|
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/shanghai.md#engine_forkchoiceupdatedv2
|
|
|
|
server.rpc("engine_forkchoiceUpdatedV2") do(
|
2024-06-12 01:46:39 +00:00
|
|
|
forkchoiceState: ForkchoiceStateV1, payloadAttributes: Opt[PayloadAttributesV2]) -> ForkchoiceUpdatedResponse:
|
2023-05-09 22:51:57 +00:00
|
|
|
info "engine_forkchoiceUpdatedV2",
|
|
|
|
forkchoiceState, payloadAttributes
|
|
|
|
|
|
|
|
return ForkchoiceUpdatedResponse(
|
|
|
|
payloadStatus: PayloadStatusV1(
|
|
|
|
status: PayloadExecutionStatus.syncing))
|
|
|
|
|
|
|
|
server.rpc("eth_getBlockByNumber") do(
|
2024-01-13 01:36:17 +00:00
|
|
|
quantityTag: string, fullTransactions: bool) -> JsonString:
|
2023-05-09 22:51:57 +00:00
|
|
|
info "eth_getBlockByNumber", quantityTag, fullTransactions
|
|
|
|
|
|
|
|
return if quantityTag == "latest":
|
2024-10-08 01:54:02 +00:00
|
|
|
JrpcConv.encode(BlockObject(number: 1000.Quantity)).JsonString
|
2023-05-09 22:51:57 +00:00
|
|
|
else:
|
2024-01-13 01:36:17 +00:00
|
|
|
"{}".JsonString
|
2023-05-09 22:51:57 +00:00
|
|
|
|
|
|
|
server.rpc("eth_getBlockByHash") do(
|
|
|
|
data: string, fullTransactions: bool) -> BlockObject:
|
|
|
|
info "eth_getBlockByHash", data = toHex(data), fullTransactions
|
|
|
|
|
2024-10-08 01:54:02 +00:00
|
|
|
return BlockObject(number: 1000.Quantity)
|
2023-05-09 22:51:57 +00:00
|
|
|
|
|
|
|
server.rpc("eth_chainId") do() -> Quantity:
|
|
|
|
info "eth_chainId"
|
|
|
|
|
|
|
|
return 1.Quantity
|
|
|
|
|
2022-10-18 22:18:36 +00:00
|
|
|
when isMainModule:
|
|
|
|
let server = newRpcHttpServer(
|
2022-10-25 20:01:45 +00:00
|
|
|
# authHooks = @[httpJwtAuthHook, httpCorsHook],
|
2022-10-18 22:18:36 +00:00
|
|
|
)
|
|
|
|
|
2022-10-25 20:01:45 +00:00
|
|
|
server.addHttpServer(
|
|
|
|
initTAddress("127.0.0.1", 8551),
|
|
|
|
maxRequestBodySize = 16 * 1024 * 1024)
|
|
|
|
|
2022-10-18 22:18:36 +00:00
|
|
|
server.setupEngineAPI()
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
when compiles(waitFor waitSignal(SIGINT)):
|
|
|
|
waitFor waitSignal(SIGINT)
|
|
|
|
waitFor server.stop()
|
|
|
|
else:
|
|
|
|
runForever()
|