2021-10-05 23:31:35 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# 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
|
|
|
|
std/[typetraits, times],
|
|
|
|
stew/[objects, results],
|
|
|
|
json_rpc/[rpcserver, errors],
|
|
|
|
web3/[conversions, engine_api_types],
|
|
|
|
eth/[trie, rlp, common, trie/db],
|
|
|
|
".."/db/db_chain,
|
|
|
|
".."/p2p/chain/[chain_desc, persist_blocks],
|
|
|
|
".."/[sealer, utils, constants]
|
|
|
|
|
|
|
|
import eth/common/eth_types except BlockHeader
|
|
|
|
type EthBlockHeader = eth_types.BlockHeader
|
|
|
|
|
|
|
|
# TODO move this to stew/objects
|
|
|
|
template newClone*[T: not ref](x: T): ref T =
|
|
|
|
# TODO not nil in return type: https://github.com/nim-lang/Nim/issues/14146
|
|
|
|
# TODO use only when x is a function call that returns a new instance!
|
|
|
|
let res = new typeof(x) # TODO safe to do noinit here?
|
|
|
|
res[] = x
|
|
|
|
res
|
|
|
|
|
|
|
|
template asEthHash*(hash: engine_api_types.BlockHash): Hash256 =
|
|
|
|
Hash256(data: distinctBase(hash))
|
|
|
|
|
|
|
|
template unsafeQuantityToInt64(q: Quantity): int64 =
|
|
|
|
int64 q
|
|
|
|
|
|
|
|
proc calcRootHashRlp*(items: openArray[seq[byte]]): Hash256 =
|
|
|
|
var tr = initHexaryTrie(newMemoryDB())
|
|
|
|
for i, t in items:
|
|
|
|
tr.put(rlp.encode(i), t)
|
|
|
|
return tr.rootHash()
|
|
|
|
|
2022-01-24 13:08:33 +00:00
|
|
|
proc toBlockHeader(payload: ExecutionPayloadV1): eth_types.BlockHeader =
|
2022-02-27 05:21:46 +00:00
|
|
|
discard payload.prevRandao # TODO: What should this be used for?
|
2021-10-05 23:31:35 +00:00
|
|
|
|
|
|
|
let transactions = seq[seq[byte]](payload.transactions)
|
|
|
|
let txRoot = calcRootHashRlp(transactions)
|
|
|
|
|
|
|
|
EthBlockHeader(
|
|
|
|
parentHash : payload.parentHash.asEthHash,
|
|
|
|
ommersHash : EMPTY_UNCLE_HASH,
|
2022-01-24 13:08:33 +00:00
|
|
|
coinbase : EthAddress payload.feeRecipient,
|
2021-10-05 23:31:35 +00:00
|
|
|
stateRoot : payload.stateRoot.asEthHash,
|
|
|
|
txRoot : txRoot,
|
2022-01-24 13:08:33 +00:00
|
|
|
receiptRoot : payload.receiptsRoot.asEthHash,
|
2021-10-05 23:31:35 +00:00
|
|
|
bloom : distinctBase(payload.logsBloom),
|
|
|
|
difficulty : default(DifficultyInt),
|
|
|
|
blockNumber : payload.blockNumber.distinctBase.u256,
|
|
|
|
gasLimit : payload.gasLimit.unsafeQuantityToInt64,
|
|
|
|
gasUsed : payload.gasUsed.unsafeQuantityToInt64,
|
|
|
|
timestamp : fromUnix payload.timestamp.unsafeQuantityToInt64,
|
|
|
|
extraData : distinctBase payload.extraData,
|
|
|
|
mixDigest : default(Hash256),
|
|
|
|
nonce : default(BlockNonce),
|
|
|
|
fee : some payload.baseFeePerGas
|
|
|
|
)
|
|
|
|
|
2022-01-24 13:08:33 +00:00
|
|
|
proc toBlockBody(payload: ExecutionPayloadV1): BlockBody =
|
2021-10-05 23:31:35 +00:00
|
|
|
# TODO the transactions from the payload have to be converted here
|
|
|
|
discard payload.transactions
|
|
|
|
|
2022-01-24 13:08:33 +00:00
|
|
|
proc setupEngineAPI*(
|
|
|
|
sealingEngine: SealingEngineRef,
|
|
|
|
server: RpcServer) =
|
2021-10-05 23:31:35 +00:00
|
|
|
|
2022-01-24 13:08:33 +00:00
|
|
|
var payloadsInstance = newClone(newSeq[ExecutionPayloadV1]())
|
2021-10-05 23:31:35 +00:00
|
|
|
template payloads: auto = payloadsInstance[]
|
|
|
|
|
2022-01-24 13:08:33 +00:00
|
|
|
# https://github.com/ethereum/execution-apis/blob/v1.0.0-alpha.5/src/engine/specification.md#engine_getpayloadv1
|
|
|
|
server.rpc("engine_getPayloadV1") do(payloadIdBytes: FixedBytes[8]) -> ExecutionPayloadV1:
|
|
|
|
let payloadId = uint64.fromBytesBE(distinctBase payloadIdBytes)
|
|
|
|
if payloadId > payloads.len.uint64:
|
|
|
|
raise (ref InvalidRequest)(code: engineApiUnknownPayload, msg: "Unknown payload")
|
2021-10-05 23:31:35 +00:00
|
|
|
return payloads[int payloadId]
|
|
|
|
|
2022-01-24 13:08:33 +00:00
|
|
|
# https://github.com/ethereum/execution-apis/blob/v1.0.0-alpha.5/src/engine/specification.md#engine_executepayloadv1
|
2022-02-27 05:21:46 +00:00
|
|
|
#[server.rpc("engine_executePayloadV1") do(payload: ExecutionPayloadV1) -> ExecutePayloadResponse:
|
2021-10-05 23:31:35 +00:00
|
|
|
# TODO
|
|
|
|
if payload.transactions.len > 0:
|
|
|
|
# Give us a break, a block with transcations? instructions to execute?
|
|
|
|
# Nah, we are syncing!
|
2022-01-24 13:08:33 +00:00
|
|
|
return ExecutePayloadResponse(status: PayloadExecutionStatus.syncing)
|
|
|
|
|
|
|
|
# TODO check whether we are syncing
|
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
let
|
|
|
|
headers = [payload.toBlockHeader]
|
|
|
|
bodies = [payload.toBlockBody]
|
|
|
|
|
|
|
|
if rlpHash(headers[0]) != payload.blockHash.asEthHash:
|
2022-01-24 13:08:33 +00:00
|
|
|
return ExecutePayloadResponse(status: PayloadExecutionStatus.invalid,
|
|
|
|
validationError: some "payload root doesn't match its contents")
|
2021-10-05 23:31:35 +00:00
|
|
|
|
|
|
|
if sealingEngine.chain.persistBlocks(headers, bodies) != ValidationResult.OK:
|
2022-01-24 13:08:33 +00:00
|
|
|
# TODO Provide validationError and latestValidHash
|
|
|
|
return ExecutePayloadResponse(status: PayloadExecutionStatus.invalid)
|
2021-10-05 23:31:35 +00:00
|
|
|
|
2022-01-24 13:08:33 +00:00
|
|
|
return ExecutePayloadResponse(status: PayloadExecutionStatus.valid,
|
|
|
|
latestValidHash: some payload.blockHash)
|
2021-10-05 23:31:35 +00:00
|
|
|
|
2022-01-24 13:08:33 +00:00
|
|
|
# https://github.com/ethereum/execution-apis/blob/v1.0.0-alpha.5/src/engine/specification.md#engine_forkchoiceupdatedv1
|
|
|
|
server.rpc("engine_forkchoiceUpdatedV1") do(
|
|
|
|
update: ForkchoiceStateV1,
|
|
|
|
payloadAttributes: Option[PayloadAttributesV1]) -> ForkchoiceUpdatedResponse:
|
2021-10-05 23:31:35 +00:00
|
|
|
let
|
|
|
|
db = sealingEngine.chain.db
|
|
|
|
newHead = update.headBlockHash.asEthHash
|
|
|
|
|
|
|
|
# TODO Use the finalized block information to prune any alterantive
|
|
|
|
# histories that are no longer relevant
|
|
|
|
discard update.finalizedBlockHash
|
|
|
|
|
2022-01-24 13:08:33 +00:00
|
|
|
# TODO Check whether we are syncing
|
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
if not db.setHead(newHead):
|
2022-01-24 13:08:33 +00:00
|
|
|
return ForkchoiceUpdatedResponse(status: ForkchoiceUpdatedStatus.syncing)
|
|
|
|
|
|
|
|
if payloadAttributes.isSome:
|
|
|
|
let payloadId = uint64 payloads.len
|
|
|
|
|
|
|
|
var payload: ExecutionPayloadV1
|
|
|
|
let generatePayloadRes = sealingEngine.generateExecutionPayload(
|
|
|
|
payloadAttributes.get,
|
|
|
|
payload)
|
|
|
|
if generatePayloadRes.isErr:
|
|
|
|
raise newException(CatchableError, generatePayloadRes.error)
|
|
|
|
|
|
|
|
payloads.add payload
|
|
|
|
|
|
|
|
return ForkchoiceUpdatedResponse(status: ForkchoiceUpdatedStatus.success,
|
|
|
|
payloadId: some payloadId.toBytesBE.PayloadID)
|
|
|
|
else:
|
2022-02-27 05:21:46 +00:00
|
|
|
return ForkchoiceUpdatedResponse(status: ForkchoiceUpdatedStatus.success)]#
|