2023-08-27 01:23:45 +00:00
|
|
|
# Nimbus
|
2024-03-26 02:31:21 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2023-08-27 01:23:45 +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
|
|
|
|
std/[typetraits, strutils],
|
|
|
|
eth/[rlp],
|
|
|
|
json_rpc/errors,
|
|
|
|
nimcrypto/[hash, sha2],
|
2024-05-30 12:54:03 +00:00
|
|
|
stew/endians2,
|
|
|
|
results,
|
2023-08-27 01:23:45 +00:00
|
|
|
../../constants,
|
|
|
|
../../db/core_db,
|
|
|
|
../../utils/utils,
|
|
|
|
../../common/common,
|
2023-12-08 09:35:50 +00:00
|
|
|
web3/execution_types,
|
2023-08-27 01:23:45 +00:00
|
|
|
../web3_eth_conv
|
|
|
|
|
|
|
|
{.push gcsafe, raises:[].}
|
|
|
|
|
|
|
|
proc update(ctx: var sha256, wd: WithdrawalV1) =
|
|
|
|
ctx.update(toBytesBE distinctBase wd.index)
|
|
|
|
ctx.update(toBytesBE distinctBase wd.validatorIndex)
|
|
|
|
ctx.update(distinctBase wd.address)
|
|
|
|
ctx.update(toBytesBE distinctBase wd.amount)
|
|
|
|
|
|
|
|
proc computePayloadId*(blockHash: common.Hash256,
|
|
|
|
params: PayloadAttributes): PayloadID =
|
|
|
|
var dest: common.Hash256
|
|
|
|
var ctx: sha256
|
|
|
|
ctx.init()
|
|
|
|
ctx.update(blockHash.data)
|
|
|
|
ctx.update(toBytesBE distinctBase params.timestamp)
|
|
|
|
ctx.update(distinctBase params.prevRandao)
|
|
|
|
ctx.update(distinctBase params.suggestedFeeRecipient)
|
|
|
|
if params.withdrawals.isSome:
|
|
|
|
for wd in params.withdrawals.get:
|
|
|
|
ctx.update(wd)
|
|
|
|
if params.parentBeaconBlockRoot.isSome:
|
|
|
|
ctx.update(distinctBase params.parentBeaconBlockRoot.get)
|
|
|
|
ctx.finish dest.data
|
|
|
|
ctx.clear()
|
|
|
|
(distinctBase result)[0..7] = dest.data[0..7]
|
|
|
|
|
|
|
|
proc validateBlockHash*(header: common.BlockHeader,
|
|
|
|
gotHash: common.Hash256,
|
|
|
|
version: Version): Result[void, PayloadStatusV1]
|
|
|
|
{.gcsafe, raises: [ValueError].} =
|
|
|
|
let wantHash = header.blockHash
|
|
|
|
if wantHash != gotHash:
|
|
|
|
let status = if version == Version.V1:
|
|
|
|
PayloadExecutionStatus.invalid_block_hash
|
|
|
|
else:
|
|
|
|
PayloadExecutionStatus.invalid
|
|
|
|
|
|
|
|
let res = PayloadStatusV1(
|
|
|
|
status: status,
|
2024-06-14 07:31:08 +00:00
|
|
|
validationError: Opt.some("blockhash mismatch, want $1, got $2" % [
|
2023-08-27 01:23:45 +00:00
|
|
|
$wantHash, $gotHash])
|
|
|
|
)
|
|
|
|
return err(res)
|
|
|
|
|
|
|
|
return ok()
|
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
template toValidHash*(x: common.Hash256): Opt[Web3Hash] =
|
|
|
|
Opt.some(w3Hash x)
|
2023-08-27 01:23:45 +00:00
|
|
|
|
2024-05-17 01:38:46 +00:00
|
|
|
proc simpleFCU*(status: PayloadStatusV1): ForkchoiceUpdatedResponse =
|
|
|
|
ForkchoiceUpdatedResponse(payloadStatus: status)
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
proc simpleFCU*(status: PayloadExecutionStatus): ForkchoiceUpdatedResponse =
|
|
|
|
ForkchoiceUpdatedResponse(payloadStatus: PayloadStatusV1(status: status))
|
|
|
|
|
|
|
|
proc simpleFCU*(status: PayloadExecutionStatus,
|
|
|
|
msg: string): ForkchoiceUpdatedResponse =
|
|
|
|
ForkchoiceUpdatedResponse(
|
|
|
|
payloadStatus: PayloadStatusV1(
|
|
|
|
status: status,
|
2024-06-14 07:31:08 +00:00
|
|
|
validationError: Opt.some(msg)
|
2023-08-27 01:23:45 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-05-31 07:13:56 +00:00
|
|
|
proc invalidFCU*(
|
|
|
|
validationError: string,
|
2024-09-26 11:24:36 +00:00
|
|
|
hash = default(common.Hash256)): ForkchoiceUpdatedResponse =
|
2023-08-27 01:23:45 +00:00
|
|
|
ForkchoiceUpdatedResponse(payloadStatus:
|
|
|
|
PayloadStatusV1(
|
|
|
|
status: PayloadExecutionStatus.invalid,
|
2024-05-31 07:13:56 +00:00
|
|
|
latestValidHash: toValidHash(hash),
|
2024-06-14 07:31:08 +00:00
|
|
|
validationError: Opt.some validationError
|
2023-08-27 01:23:45 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
proc validFCU*(id: Opt[PayloadID],
|
2023-08-27 01:23:45 +00:00
|
|
|
validHash: common.Hash256): ForkchoiceUpdatedResponse =
|
|
|
|
ForkchoiceUpdatedResponse(
|
|
|
|
payloadStatus: PayloadStatusV1(
|
|
|
|
status: PayloadExecutionStatus.valid,
|
|
|
|
latestValidHash: toValidHash(validHash)
|
|
|
|
),
|
|
|
|
payloadId: id
|
|
|
|
)
|
|
|
|
|
|
|
|
proc invalidStatus*(validHash: common.Hash256, msg: string): PayloadStatusV1 =
|
|
|
|
PayloadStatusV1(
|
|
|
|
status: PayloadExecutionStatus.invalid,
|
|
|
|
latestValidHash: toValidHash(validHash),
|
2024-06-14 07:31:08 +00:00
|
|
|
validationError: Opt.some(msg)
|
2023-08-27 01:23:45 +00:00
|
|
|
)
|
|
|
|
|
2024-09-26 11:24:36 +00:00
|
|
|
proc invalidStatus*(validHash = default(common.Hash256)): PayloadStatusV1 =
|
2023-08-27 01:23:45 +00:00
|
|
|
PayloadStatusV1(
|
|
|
|
status: PayloadExecutionStatus.invalid,
|
|
|
|
latestValidHash: toValidHash(validHash)
|
|
|
|
)
|
|
|
|
|
|
|
|
proc acceptedStatus*(validHash: common.Hash256): PayloadStatusV1 =
|
|
|
|
PayloadStatusV1(
|
|
|
|
status: PayloadExecutionStatus.accepted,
|
|
|
|
latestValidHash: toValidHash(validHash)
|
|
|
|
)
|
|
|
|
|
|
|
|
proc acceptedStatus*(): PayloadStatusV1 =
|
|
|
|
PayloadStatusV1(
|
|
|
|
status: PayloadExecutionStatus.accepted
|
|
|
|
)
|
|
|
|
|
|
|
|
proc validStatus*(validHash: common.Hash256): PayloadStatusV1 =
|
|
|
|
PayloadStatusV1(
|
|
|
|
status: PayloadExecutionStatus.valid,
|
|
|
|
latestValidHash: toValidHash(validHash)
|
|
|
|
)
|
|
|
|
|
|
|
|
proc invalidParams*(msg: string): ref InvalidRequest =
|
|
|
|
(ref InvalidRequest)(
|
|
|
|
code: engineApiInvalidParams,
|
|
|
|
msg: msg
|
|
|
|
)
|
|
|
|
|
2024-03-26 02:31:21 +00:00
|
|
|
proc invalidForkChoiceState*(msg: string): ref InvalidRequest =
|
|
|
|
(ref InvalidRequest)(
|
|
|
|
code: engineApiInvalidForkchoiceState,
|
|
|
|
msg: msg
|
|
|
|
)
|
2024-05-17 01:38:46 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
proc unknownPayload*(msg: string): ref InvalidRequest =
|
|
|
|
(ref InvalidRequest)(
|
|
|
|
code: engineApiUnknownPayload,
|
|
|
|
msg: msg
|
|
|
|
)
|
|
|
|
|
|
|
|
proc invalidAttr*(msg: string): ref InvalidRequest =
|
|
|
|
(ref InvalidRequest)(
|
|
|
|
code: engineApiInvalidPayloadAttributes,
|
|
|
|
msg: msg
|
|
|
|
)
|
|
|
|
|
|
|
|
proc unsupportedFork*(msg: string): ref InvalidRequest =
|
|
|
|
(ref InvalidRequest)(
|
|
|
|
code: engineApiUnsupportedFork,
|
|
|
|
msg: msg
|
|
|
|
)
|
|
|
|
|
|
|
|
proc tooLargeRequest*(msg: string): ref InvalidRequest =
|
|
|
|
(ref InvalidRequest)(
|
|
|
|
code: engineApiTooLargeRequest,
|
|
|
|
msg: msg
|
|
|
|
)
|
|
|
|
|
|
|
|
proc latestValidHash*(db: CoreDbRef,
|
|
|
|
parent: common.BlockHeader,
|
2024-06-14 05:10:00 +00:00
|
|
|
ttd: DifficultyInt): common.Hash256 =
|
2023-10-05 03:04:12 +00:00
|
|
|
if parent.isGenesis:
|
2024-09-26 11:24:36 +00:00
|
|
|
return default(common.Hash256)
|
2024-06-14 05:10:00 +00:00
|
|
|
let ptd = db.getScore(parent.parentHash).valueOr(0.u256)
|
2023-08-27 01:23:45 +00:00
|
|
|
if ptd >= ttd:
|
|
|
|
parent.blockHash
|
|
|
|
else:
|
|
|
|
# If the most recent valid ancestor is a PoW block,
|
|
|
|
# latestValidHash MUST be set to ZERO
|
2024-09-26 11:24:36 +00:00
|
|
|
default(common.Hash256)
|
2023-08-27 01:23:45 +00:00
|
|
|
|
2024-05-31 07:13:56 +00:00
|
|
|
proc invalidFCU*(validationError: string,
|
|
|
|
com: CommonRef,
|
|
|
|
header: common.BlockHeader): ForkchoiceUpdatedResponse =
|
2023-08-27 01:23:45 +00:00
|
|
|
var parent: common.BlockHeader
|
|
|
|
if not com.db.getBlockHeader(header.parentHash, parent):
|
2024-05-31 07:13:56 +00:00
|
|
|
return invalidFCU(validationError)
|
2023-08-27 01:23:45 +00:00
|
|
|
|
2024-05-31 07:13:56 +00:00
|
|
|
let blockHash = try:
|
2024-06-14 07:31:08 +00:00
|
|
|
latestValidHash(com.db, parent, com.ttd.get(high(UInt256)))
|
2024-05-31 07:13:56 +00:00
|
|
|
except RlpError:
|
|
|
|
default(common.Hash256)
|
|
|
|
|
|
|
|
invalidFCU(validationError, blockHash)
|