2021-05-18 11:18:15 +00:00
|
|
|
# Nimbus
|
2024-02-20 03:07:38 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-05-18 11:18:15 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-05-18 11:18:15 +00:00
|
|
|
import
|
2023-10-18 02:16:11 +00:00
|
|
|
std/[sequtils, sets, strutils],
|
2023-12-12 19:12:56 +00:00
|
|
|
../db/ledger,
|
2023-06-24 13:56:44 +00:00
|
|
|
".."/[transaction, common/common],
|
|
|
|
".."/[errors],
|
2023-10-05 03:04:12 +00:00
|
|
|
../utils/utils,
|
2022-12-13 20:06:26 +00:00
|
|
|
"."/[dao, eip4844, gaslimit, withdrawals],
|
2022-12-02 04:35:41 +00:00
|
|
|
./pow/[difficulty, header],
|
|
|
|
./pow,
|
2023-10-05 03:04:12 +00:00
|
|
|
nimcrypto/utils as cryptoutils,
|
2023-01-30 22:10:23 +00:00
|
|
|
stew/[objects, results]
|
2021-06-24 15:29:21 +00:00
|
|
|
|
|
|
|
from stew/byteutils
|
|
|
|
import nil
|
2021-05-18 11:18:15 +00:00
|
|
|
|
2021-05-19 10:40:03 +00:00
|
|
|
export
|
|
|
|
results
|
|
|
|
|
2021-06-24 15:29:21 +00:00
|
|
|
const
|
2022-01-24 13:08:33 +00:00
|
|
|
daoForkBlockExtraData* =
|
2021-06-24 15:29:21 +00:00
|
|
|
byteutils.hexToByteArray[13](DAOForkBlockExtra).toSeq
|
|
|
|
|
2021-05-19 10:40:03 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Pivate validator functions
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-05-18 11:18:15 +00:00
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
proc validateSeal(pow: PowRef; header: BlockHeader): Result[void,string] =
|
2023-06-12 05:01:48 +00:00
|
|
|
try:
|
|
|
|
let (expMixDigest, miningValue) = pow.getPowDigest(header)
|
|
|
|
|
|
|
|
if expMixDigest != header.mixDigest:
|
|
|
|
let
|
|
|
|
miningHash = header.getPowSpecs.miningHash
|
|
|
|
(size, cachedHash) = try: pow.getPowCacheLookup(header.blockNumber)
|
|
|
|
except KeyError: return err("Unknown block")
|
|
|
|
except CatchableError as e: return err(e.msg)
|
|
|
|
return err("mixHash mismatch. actual=$1, expected=$2," &
|
|
|
|
" blockNumber=$3, miningHash=$4, nonce=$5, difficulty=$6," &
|
|
|
|
" size=$7, cachedHash=$8" % [
|
|
|
|
$header.mixDigest, $expMixDigest, $header.blockNumber,
|
|
|
|
$miningHash, header.nonce.toHex, $header.difficulty,
|
|
|
|
$size, $cachedHash])
|
|
|
|
|
|
|
|
let value = UInt256.fromBytesBE(miningValue.data)
|
|
|
|
if value > UInt256.high div header.difficulty:
|
|
|
|
return err("mining difficulty error")
|
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
except CatchableError as err:
|
2023-06-12 05:01:48 +00:00
|
|
|
return err(err.msg)
|
2021-05-18 11:18:15 +00:00
|
|
|
|
2021-12-10 08:49:57 +00:00
|
|
|
ok()
|
2021-05-18 11:18:15 +00:00
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
proc validateHeader(
|
|
|
|
com: CommonRef;
|
|
|
|
header: BlockHeader;
|
|
|
|
parentHeader: BlockHeader;
|
|
|
|
body: BlockBody;
|
|
|
|
checkSealOK: bool;
|
|
|
|
): Result[void,string]
|
2023-10-05 03:04:12 +00:00
|
|
|
{.gcsafe, raises: [].} =
|
2021-10-12 04:06:39 +00:00
|
|
|
|
|
|
|
template inDAOExtraRange(blockNumber: BlockNumber): bool =
|
|
|
|
# EIP-799
|
|
|
|
# Blocks with block numbers in the range [1_920_000, 1_920_009]
|
|
|
|
# MUST have DAOForkBlockExtra
|
2022-12-02 04:35:41 +00:00
|
|
|
let daoForkBlock = com.daoForkBlock.get
|
|
|
|
let DAOHigh = daoForkBlock + DAOForkExtraRange.u256
|
|
|
|
daoForkBlock <= blockNumber and
|
2021-10-12 04:06:39 +00:00
|
|
|
blockNumber < DAOHigh
|
|
|
|
|
2021-05-18 11:18:15 +00:00
|
|
|
if header.extraData.len > 32:
|
2021-05-19 10:40:03 +00:00
|
|
|
return err("BlockHeader.extraData larger than 32 bytes")
|
2021-05-18 11:18:15 +00:00
|
|
|
|
2023-06-25 13:30:34 +00:00
|
|
|
if header.gasUsed == 0 and 0 < body.transactions.len:
|
2023-06-24 13:56:44 +00:00
|
|
|
return err("zero gasUsed but transactions present");
|
2021-06-24 15:29:21 +00:00
|
|
|
|
2021-06-30 09:42:55 +00:00
|
|
|
if header.gasUsed < 0 or header.gasUsed > header.gasLimit:
|
|
|
|
return err("gasUsed should be non negative and smaller or equal gasLimit")
|
2021-09-28 15:16:15 +00:00
|
|
|
|
2021-05-18 11:18:15 +00:00
|
|
|
if header.blockNumber != parentHeader.blockNumber + 1:
|
2021-06-24 15:29:21 +00:00
|
|
|
return err("Blocks must be numbered consecutively")
|
2021-05-18 11:18:15 +00:00
|
|
|
|
2023-10-18 02:16:11 +00:00
|
|
|
if header.timestamp <= parentHeader.timestamp:
|
2021-05-19 10:40:03 +00:00
|
|
|
return err("timestamp must be strictly later than parent")
|
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
if com.daoForkSupport and inDAOExtraRange(header.blockNumber):
|
2021-10-12 04:06:39 +00:00
|
|
|
if header.extraData != daoForkBlockExtraData:
|
|
|
|
return err("header extra data should be marked DAO")
|
2021-06-24 15:29:21 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
if com.consensus == ConsensusType.POS:
|
2022-02-08 09:50:22 +00:00
|
|
|
# EIP-4399 and EIP-3675
|
|
|
|
# no need to check mixDigest because EIP-4399 override this field
|
|
|
|
# checking rule
|
2021-06-24 15:29:21 +00:00
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
if not header.difficulty.isZero:
|
|
|
|
return err("Non-zero difficulty in a post-merge block")
|
2021-05-18 11:18:15 +00:00
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
if not header.nonce.isZeroMemory:
|
|
|
|
return err("Non-zero nonce in a post-merge block")
|
|
|
|
|
|
|
|
if header.ommersHash != EMPTY_UNCLE_HASH:
|
|
|
|
return err("Invalid ommers hash in a post-merge block")
|
|
|
|
else:
|
2022-12-02 04:35:41 +00:00
|
|
|
let calcDiffc = com.calcDifficulty(header.timestamp, parentHeader)
|
2021-10-05 23:31:35 +00:00
|
|
|
if header.difficulty < calcDiffc:
|
|
|
|
return err("provided header difficulty is too low")
|
|
|
|
|
|
|
|
if checkSealOK:
|
2023-06-25 13:30:34 +00:00
|
|
|
return com.pow.validateSeal(header)
|
2021-05-18 11:18:15 +00:00
|
|
|
|
2023-06-25 13:30:34 +00:00
|
|
|
? com.validateWithdrawals(header, body)
|
|
|
|
? com.validateEip4844Header(header, parentHeader, body.transactions)
|
2023-10-05 03:04:12 +00:00
|
|
|
? com.validateGasLimitOrBaseFee(header, parentHeader)
|
2022-12-13 20:06:26 +00:00
|
|
|
|
|
|
|
ok()
|
2021-05-18 11:18:15 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc validateUncles(com: CommonRef; header: BlockHeader;
|
2023-06-25 13:30:34 +00:00
|
|
|
uncles: openArray[BlockHeader];
|
2023-10-05 03:04:12 +00:00
|
|
|
checkSealOK: bool): Result[void,string]
|
|
|
|
{.gcsafe, raises: [].} =
|
2021-05-19 10:40:03 +00:00
|
|
|
let hasUncles = uncles.len > 0
|
|
|
|
let shouldHaveUncles = header.ommersHash != EMPTY_UNCLE_HASH
|
2021-05-18 11:18:15 +00:00
|
|
|
|
|
|
|
if not hasUncles and not shouldHaveUncles:
|
|
|
|
# optimization to avoid loading ancestors from DB, since the block has
|
|
|
|
# no uncles
|
2021-05-19 10:40:03 +00:00
|
|
|
return ok()
|
|
|
|
if hasUncles and not shouldHaveUncles:
|
|
|
|
return err("Block has uncles but header suggests uncles should be empty")
|
|
|
|
if shouldHaveUncles and not hasUncles:
|
|
|
|
return err("Header suggests block should have uncles but block has none")
|
2021-05-18 11:18:15 +00:00
|
|
|
|
|
|
|
# Check for duplicates
|
|
|
|
var uncleSet = initHashSet[Hash256]()
|
2021-05-19 10:40:03 +00:00
|
|
|
for uncle in uncles:
|
2021-07-30 14:06:51 +00:00
|
|
|
let uncleHash = uncle.blockHash
|
2021-05-18 11:18:15 +00:00
|
|
|
if uncleHash in uncleSet:
|
2021-05-19 10:40:03 +00:00
|
|
|
return err("Block contains duplicate uncles")
|
2021-05-18 11:18:15 +00:00
|
|
|
else:
|
|
|
|
uncleSet.incl uncleHash
|
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
let chainDB = com.db
|
2021-10-05 23:31:35 +00:00
|
|
|
let recentAncestorHashes = try:
|
|
|
|
chainDB.getAncestorsHashes(MAX_UNCLE_DEPTH + 1, header)
|
|
|
|
except CatchableError as err:
|
|
|
|
return err("Block not present in database")
|
|
|
|
|
|
|
|
let recentUncleHashes = try:
|
|
|
|
chainDB.getUncleHashes(recentAncestorHashes)
|
|
|
|
except CatchableError as err:
|
|
|
|
return err("Ancenstors not present in database")
|
|
|
|
|
2021-07-30 14:06:51 +00:00
|
|
|
let blockHash = header.blockHash
|
2021-05-18 11:18:15 +00:00
|
|
|
|
2021-05-19 10:40:03 +00:00
|
|
|
for uncle in uncles:
|
2021-07-30 14:06:51 +00:00
|
|
|
let uncleHash = uncle.blockHash
|
2021-05-18 11:18:15 +00:00
|
|
|
|
|
|
|
if uncleHash == blockHash:
|
2021-05-19 10:40:03 +00:00
|
|
|
return err("Uncle has same hash as block")
|
2021-05-18 11:18:15 +00:00
|
|
|
|
|
|
|
# ensure the uncle has not already been included.
|
|
|
|
if uncleHash in recentUncleHashes:
|
2021-05-19 10:40:03 +00:00
|
|
|
return err("Duplicate uncle")
|
2021-05-18 11:18:15 +00:00
|
|
|
|
|
|
|
# ensure that the uncle is not one of the canonical chain blocks.
|
|
|
|
if uncleHash in recentAncestorHashes:
|
2021-05-19 10:40:03 +00:00
|
|
|
return err("Uncle cannot be an ancestor")
|
2021-05-18 11:18:15 +00:00
|
|
|
|
|
|
|
# ensure that the uncle was built off of one of the canonical chain
|
|
|
|
# blocks.
|
|
|
|
if (uncle.parentHash notin recentAncestorHashes) or
|
2021-05-19 10:40:03 +00:00
|
|
|
(uncle.parentHash == header.parentHash):
|
|
|
|
return err("Uncle's parent is not an ancestor")
|
2021-05-18 11:18:15 +00:00
|
|
|
|
2023-10-05 03:04:12 +00:00
|
|
|
if uncle.blockNumber >= header.blockNumber:
|
|
|
|
return err("uncle block number larger than current block number")
|
|
|
|
|
2021-06-18 07:37:59 +00:00
|
|
|
# check uncle against own parent
|
|
|
|
var parent: BlockHeader
|
|
|
|
if not chainDB.getBlockHeader(uncle.parentHash,parent):
|
|
|
|
return err("Uncle's parent has gone missing")
|
|
|
|
if uncle.timestamp <= parent.timestamp:
|
|
|
|
return err("Uncle's parent must me older")
|
|
|
|
|
2021-05-18 11:18:15 +00:00
|
|
|
# Now perform VM level validation of the uncle
|
2021-05-19 10:40:03 +00:00
|
|
|
if checkSealOK:
|
2023-06-25 13:30:34 +00:00
|
|
|
result = com.pow.validateSeal(uncle)
|
2021-05-19 10:40:03 +00:00
|
|
|
if result.isErr:
|
|
|
|
return
|
2021-05-18 11:18:15 +00:00
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
let uncleParent = try:
|
|
|
|
chainDB.getBlockHeader(uncle.parentHash)
|
|
|
|
except BlockNotFound:
|
|
|
|
return err("Uncle parent not found")
|
|
|
|
|
2023-10-05 03:04:12 +00:00
|
|
|
result = com.validateHeader(uncle, uncleParent,
|
|
|
|
BlockBody(), checkSealOK)
|
2021-06-30 13:30:39 +00:00
|
|
|
if result.isErr:
|
|
|
|
return
|
|
|
|
|
2021-05-19 10:40:03 +00:00
|
|
|
result = ok()
|
2021-05-18 11:18:15 +00:00
|
|
|
|
2021-05-19 10:40:03 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public function, extracted from executor
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-05-18 11:18:15 +00:00
|
|
|
|
2023-08-21 02:10:18 +00:00
|
|
|
func gasCost*(tx: Transaction): UInt256 =
|
2023-06-24 13:56:44 +00:00
|
|
|
if tx.txType >= TxEip4844:
|
2023-08-04 12:43:30 +00:00
|
|
|
tx.gasLimit.u256 * tx.maxFee.u256 + tx.getTotalBlobGas.u256 * tx.maxFeePerBlobGas.u256
|
2023-06-24 13:56:44 +00:00
|
|
|
elif tx.txType >= TxEip1559:
|
|
|
|
tx.gasLimit.u256 * tx.maxFee.u256
|
|
|
|
else:
|
|
|
|
tx.gasLimit.u256 * tx.gasPrice.u256
|
|
|
|
|
2023-08-21 02:10:18 +00:00
|
|
|
proc validateTxBasic*(
|
2022-01-10 09:04:06 +00:00
|
|
|
tx: Transaction; ## tx to validate
|
2023-10-23 13:59:57 +00:00
|
|
|
fork: EVMFork,
|
|
|
|
validateFork: bool = true): Result[void, string] =
|
|
|
|
|
|
|
|
if validateFork:
|
|
|
|
if tx.txType == TxEip2930 and fork < FkBerlin:
|
|
|
|
return err("invalid tx: Eip2930 Tx type detected before Berlin")
|
2024-02-20 03:07:38 +00:00
|
|
|
|
2023-10-23 13:59:57 +00:00
|
|
|
if tx.txType == TxEip1559 and fork < FkLondon:
|
|
|
|
return err("invalid tx: Eip1559 Tx type detected before London")
|
2024-02-20 03:07:38 +00:00
|
|
|
|
2023-10-23 13:59:57 +00:00
|
|
|
if tx.txType == TxEip4844 and fork < FkCancun:
|
|
|
|
return err("invalid tx: Eip4844 Tx type detected before Cancun")
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2023-01-10 04:25:23 +00:00
|
|
|
if fork >= FkShanghai and tx.contractCreation and tx.payload.len > EIP3860_MAX_INITCODE_SIZE:
|
2023-06-12 05:01:48 +00:00
|
|
|
return err("invalid tx: initcode size exceeds maximum")
|
2023-01-04 13:11:33 +00:00
|
|
|
|
2023-08-21 02:10:18 +00:00
|
|
|
try:
|
|
|
|
# The total must be the larger of the two
|
|
|
|
if tx.maxFee < tx.maxPriorityFee:
|
|
|
|
return err("invalid tx: maxFee is smaller than maPriorityFee. maxFee=$1, maxPriorityFee=$2" % [
|
|
|
|
$tx.maxFee, $tx.maxPriorityFee])
|
|
|
|
|
|
|
|
if tx.gasLimit < tx.intrinsicGas(fork):
|
|
|
|
return err("invalid tx: not enough gas to perform calculation. avail=$1, require=$2" % [
|
|
|
|
$tx.gasLimit, $tx.intrinsicGas(fork)])
|
|
|
|
|
|
|
|
if fork >= FkCancun:
|
|
|
|
if tx.payload.len > MAX_CALLDATA_SIZE:
|
|
|
|
return err("invalid tx: payload len exceeds MAX_CALLDATA_SIZE. len=" &
|
|
|
|
$tx.payload.len)
|
|
|
|
|
|
|
|
if tx.accessList.len > MAX_ACCESS_LIST_SIZE:
|
|
|
|
return err("invalid tx: access list len exceeds MAX_ACCESS_LIST_SIZE. len=" &
|
|
|
|
$tx.accessList.len)
|
|
|
|
|
|
|
|
for i, acl in tx.accessList:
|
|
|
|
if acl.storageKeys.len > MAX_ACCESS_LIST_STORAGE_KEYS:
|
|
|
|
return err("invalid tx: access list storage keys len exceeds MAX_ACCESS_LIST_STORAGE_KEYS. " &
|
|
|
|
"index=$1, len=$2" % [$i, $acl.storageKeys.len])
|
|
|
|
|
|
|
|
if tx.txType >= TxEip4844:
|
|
|
|
if tx.to.isNone:
|
|
|
|
return err("invalid tx: destination must be not empty")
|
|
|
|
|
|
|
|
if tx.versionedHashes.len == 0:
|
|
|
|
return err("invalid tx: there must be at least one blob")
|
|
|
|
|
2023-10-20 14:02:22 +00:00
|
|
|
if tx.versionedHashes.len > MAX_BLOBS_PER_BLOCK:
|
|
|
|
return err("invalid tx: versioned hashes len exceeds MAX_BLOBS_PER_BLOCK=" & $MAX_BLOBS_PER_BLOCK &
|
2023-08-21 02:10:18 +00:00
|
|
|
". get=" & $tx.versionedHashes.len)
|
|
|
|
|
|
|
|
for i, bv in tx.versionedHashes:
|
2023-08-24 05:11:19 +00:00
|
|
|
if bv.data[0] != VERSIONED_HASH_VERSION_KZG:
|
2023-08-21 02:10:18 +00:00
|
|
|
return err("invalid tx: one of blobVersionedHash has invalid version. " &
|
2023-08-24 05:11:19 +00:00
|
|
|
"get=$1, expect=$2" % [$bv.data[0].int, $VERSIONED_HASH_VERSION_KZG.int])
|
2023-08-21 02:10:18 +00:00
|
|
|
|
|
|
|
except CatchableError as ex:
|
|
|
|
return err(ex.msg)
|
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc validateTransaction*(
|
|
|
|
roDB: ReadOnlyStateDB; ## Parent accounts environment for transaction
|
|
|
|
tx: Transaction; ## tx to validate
|
|
|
|
sender: EthAddress; ## tx.getSender or tx.ecRecover
|
|
|
|
maxLimit: GasInt; ## gasLimit from block header
|
|
|
|
baseFee: UInt256; ## baseFee from block header
|
|
|
|
excessBlobGas: uint64; ## excessBlobGas from parent block header
|
|
|
|
fork: EVMFork): Result[void, string] =
|
|
|
|
|
|
|
|
let res = validateTxBasic(tx, fork)
|
|
|
|
if res.isErr:
|
|
|
|
return res
|
|
|
|
|
|
|
|
let
|
|
|
|
balance = roDB.getBalance(sender)
|
|
|
|
nonce = roDB.getNonce(sender)
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
# Note that the following check bears some plausibility but is _not_
|
|
|
|
# covered by the eip-1559 reference (sort of) pseudo code, for details
|
|
|
|
# see `https://eips.ethereum.org/EIPS/eip-1559#specification`_
|
|
|
|
#
|
|
|
|
# Rather this check is needed for surviving the post-London unit test
|
|
|
|
# eth_tests/GeneralStateTests/stEIP1559/lowGasLimit.json which seems to
|
|
|
|
# be sourced and generated from
|
|
|
|
# eth_tests/src/GeneralStateTestsFiller/stEIP1559/lowGasLimitFiller.yml
|
|
|
|
#
|
|
|
|
# Interestingly, the hive tests do not use this particular test but rather
|
|
|
|
# eth_tests/BlockchainTests/GeneralStateTests/stEIP1559/lowGasLimit.json
|
|
|
|
# from a parallel tests series which look like somehow expanded versions.
|
|
|
|
#
|
|
|
|
# The parallel lowGasLimit.json test never triggers the case checked below
|
|
|
|
# as the paricular transaction is omitted (the txs list is just set empty.)
|
2023-06-12 05:01:48 +00:00
|
|
|
try:
|
|
|
|
if maxLimit < tx.gasLimit:
|
|
|
|
return err("invalid tx: block header gasLimit exceeded. maxLimit=$1, gasLimit=$2" % [
|
|
|
|
$maxLimit, $tx.gasLimit])
|
|
|
|
|
|
|
|
# ensure that the user was willing to at least pay the base fee
|
|
|
|
if tx.maxFee < baseFee.truncate(int64):
|
|
|
|
return err("invalid tx: maxFee is smaller than baseFee. maxFee=$1, baseFee=$2" % [
|
|
|
|
$tx.maxFee, $baseFee])
|
|
|
|
|
|
|
|
# the signer must be able to fully afford the transaction
|
2023-06-25 01:27:15 +00:00
|
|
|
let gasCost = tx.gasCost()
|
2023-06-12 05:01:48 +00:00
|
|
|
|
|
|
|
if balance < gasCost:
|
|
|
|
return err("invalid tx: not enough cash for gas. avail=$1, require=$2" % [
|
|
|
|
$balance, $gasCost])
|
|
|
|
|
|
|
|
if balance - gasCost < tx.value:
|
|
|
|
return err("invalid tx: not enough cash to send. avail=$1, availMinusGas=$2, require=$3" % [
|
|
|
|
$balance, $(balance-gasCost), $tx.value])
|
|
|
|
|
|
|
|
if tx.nonce != nonce:
|
|
|
|
return err("invalid tx: account nonce mismatch. txNonce=$1, accNonce=$2" % [
|
|
|
|
$tx.nonce, $nonce])
|
|
|
|
|
|
|
|
if tx.nonce == high(uint64):
|
|
|
|
return err("invalid tx: nonce at maximum")
|
|
|
|
|
|
|
|
# EIP-3607 Reject transactions from senders with deployed code
|
|
|
|
# The EIP spec claims this attack never happened before
|
|
|
|
# Clients might choose to disable this rule for RPC calls like
|
|
|
|
# `eth_call` and `eth_estimateGas`
|
|
|
|
# EOA = Externally Owned Account
|
|
|
|
let codeHash = roDB.getCodeHash(sender)
|
|
|
|
if codeHash != EMPTY_SHA3:
|
|
|
|
return err("invalid tx: sender is not an EOA. sender=$1, codeHash=$2" % [
|
|
|
|
sender.toHex, codeHash.data.toHex])
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2023-08-21 02:10:18 +00:00
|
|
|
if tx.txType >= TxEip4844:
|
2023-06-24 13:56:44 +00:00
|
|
|
# ensure that the user was willing to at least pay the current data gasprice
|
2024-02-21 09:14:34 +00:00
|
|
|
let blobGasPrice = getBlobBaseFee(excessBlobGas)
|
2023-09-24 15:25:41 +00:00
|
|
|
if tx.maxFeePerBlobGas < blobGasPrice:
|
2023-08-04 12:43:30 +00:00
|
|
|
return err("invalid tx: maxFeePerBlobGas smaller than blobGasPrice. " &
|
|
|
|
"maxFeePerBlobGas=$1, blobGasPrice=$2" % [$tx.maxFeePerBlobGas, $blobGasPrice])
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2023-06-12 05:01:48 +00:00
|
|
|
except CatchableError as ex:
|
|
|
|
return err(ex.msg)
|
|
|
|
|
|
|
|
ok()
|
2022-01-10 09:04:06 +00:00
|
|
|
|
2021-05-19 10:40:03 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, extracted from test_blockchain_json
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
proc validateHeaderAndKinship*(
|
2022-12-02 04:35:41 +00:00
|
|
|
com: CommonRef;
|
2021-10-05 23:31:35 +00:00
|
|
|
header: BlockHeader;
|
2023-06-25 13:30:34 +00:00
|
|
|
body: BlockBody;
|
2023-09-18 20:20:28 +00:00
|
|
|
checkSealOK: bool;
|
|
|
|
): Result[void, string]
|
2023-10-05 03:04:12 +00:00
|
|
|
{.gcsafe, raises: [].} =
|
2021-05-19 10:40:03 +00:00
|
|
|
if header.isGenesis:
|
|
|
|
if header.extraData.len > 32:
|
|
|
|
return err("BlockHeader.extraData larger than 32 bytes")
|
|
|
|
return ok()
|
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
let chainDB = com.db
|
2021-10-05 23:31:35 +00:00
|
|
|
let parent = try:
|
|
|
|
chainDB.getBlockHeader(header.parentHash)
|
|
|
|
except CatchableError as err:
|
|
|
|
return err("Failed to load block header from DB")
|
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
result = com.validateHeader(
|
2023-06-25 13:30:34 +00:00
|
|
|
header, parent, body, checkSealOK)
|
2021-05-19 10:40:03 +00:00
|
|
|
if result.isErr:
|
|
|
|
return
|
|
|
|
|
2023-06-25 13:30:34 +00:00
|
|
|
if body.uncles.len > MAX_UNCLES:
|
2021-05-19 10:40:03 +00:00
|
|
|
return err("Number of uncles exceed limit.")
|
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
if com.consensus != ConsensusType.POS:
|
2023-06-25 13:30:34 +00:00
|
|
|
result = com.validateUncles(header, body.uncles, checkSealOK)
|
2021-10-05 23:31:35 +00:00
|
|
|
|
2021-05-18 11:18:15 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|