2023-11-01 03:32:09 +00:00
|
|
|
# Nimbus
|
2024-03-20 07:35:38 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-11-01 03:32:09 +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.
|
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
import
|
2023-10-18 02:16:11 +00:00
|
|
|
std/[tables],
|
2022-04-11 10:00:39 +00:00
|
|
|
chronicles,
|
2023-10-19 03:28:52 +00:00
|
|
|
stew/[byteutils],
|
2022-04-11 10:00:39 +00:00
|
|
|
eth/common, chronos,
|
|
|
|
json_rpc/rpcclient,
|
2023-12-08 09:35:50 +00:00
|
|
|
web3/execution_types,
|
2023-08-27 01:23:45 +00:00
|
|
|
../../../nimbus/beacon/web3_eth_conv,
|
|
|
|
../../../nimbus/beacon/payload_conv,
|
2022-07-18 04:34:42 +00:00
|
|
|
../../../nimbus/[constants],
|
2023-08-21 02:08:54 +00:00
|
|
|
../../../nimbus/common as nimbus_common,
|
2023-09-06 09:18:26 +00:00
|
|
|
./client_pool,
|
|
|
|
./engine_env,
|
2023-10-19 03:28:52 +00:00
|
|
|
./engine_client,
|
|
|
|
./types
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-03-09 23:40:55 +00:00
|
|
|
import web3/engine_api_types except Hash256 # conflict with the one from eth/common
|
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
# Consensus Layer Client Mock used to sync the Execution Clients once the TTD has been reached
|
|
|
|
type
|
|
|
|
CLMocker* = ref object
|
2023-08-21 02:08:54 +00:00
|
|
|
com: CommonRef
|
|
|
|
|
|
|
|
# Number of required slots before a block which was set as Head moves to `safe` and `finalized` respectively
|
|
|
|
slotsToSafe* : int
|
|
|
|
slotsToFinalized*: int
|
2023-10-19 03:28:52 +00:00
|
|
|
safeSlotsToImportOptimistically*: int
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
# Wait time before attempting to get the payload
|
2023-10-19 03:28:52 +00:00
|
|
|
payloadProductionClientDelay*: int
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
# Block production related
|
2024-06-14 07:31:08 +00:00
|
|
|
blockTimestampIncrement*: Opt[int]
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
# Block Production State
|
2023-09-06 09:18:26 +00:00
|
|
|
clients : ClientPool
|
|
|
|
nextBlockProducer* : EngineEnv
|
2023-08-21 02:08:54 +00:00
|
|
|
nextFeeRecipient* : EthAddress
|
|
|
|
nextPayloadID* : PayloadID
|
|
|
|
currentPayloadNumber* : uint64
|
|
|
|
|
|
|
|
# Chain History
|
|
|
|
headerHistory : Table[uint64, common.BlockHeader]
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-10-23 13:59:57 +00:00
|
|
|
# Payload ID History
|
|
|
|
payloadIDHistory : Table[string, PayloadID]
|
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
# PoS Chain History Information
|
2023-08-21 02:08:54 +00:00
|
|
|
prevRandaoHistory* : Table[uint64, common.Hash256]
|
|
|
|
executedPayloadHistory* : Table[uint64, ExecutionPayload]
|
|
|
|
headHashHistory : seq[BlockHash]
|
2022-04-11 10:00:39 +00:00
|
|
|
|
|
|
|
# Latest broadcasted data using the PoS Engine API
|
2023-08-21 02:08:54 +00:00
|
|
|
latestHeadNumber* : uint64
|
|
|
|
latestHeader* : common.BlockHeader
|
|
|
|
latestPayloadBuilt* : ExecutionPayload
|
2024-06-14 07:31:08 +00:00
|
|
|
latestBlockValue* : Opt[UInt256]
|
|
|
|
latestBlobsBundle* : Opt[BlobsBundleV1]
|
|
|
|
latestShouldOverrideBuilder*: Opt[bool]
|
2023-08-21 02:08:54 +00:00
|
|
|
latestPayloadAttributes*: PayloadAttributes
|
2024-05-15 16:22:03 +00:00
|
|
|
latestExecutedPayload* : ExecutableData
|
2023-08-21 02:08:54 +00:00
|
|
|
latestForkchoice* : ForkchoiceStateV1
|
2022-04-11 10:00:39 +00:00
|
|
|
|
|
|
|
# Merge related
|
2024-06-14 07:31:08 +00:00
|
|
|
firstPoSBlockNumber* : Opt[uint64]
|
2023-08-21 02:08:54 +00:00
|
|
|
ttdReached* : bool
|
2024-06-14 07:31:08 +00:00
|
|
|
transitionPayloadTimestamp: Opt[int]
|
2023-08-21 02:08:54 +00:00
|
|
|
chainTotalDifficulty : UInt256
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
# Shanghai related
|
2024-06-14 07:31:08 +00:00
|
|
|
nextWithdrawals* : Opt[seq[WithdrawalV1]]
|
2022-06-27 04:15:23 +00:00
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
BlockProcessCallbacks* = object
|
2022-06-01 13:32:07 +00:00
|
|
|
onPayloadProducerSelected* : proc(): bool {.gcsafe.}
|
2023-10-19 03:28:52 +00:00
|
|
|
onPayloadAttributesGenerated* : proc(): bool {.gcsafe.}
|
2023-09-06 09:18:26 +00:00
|
|
|
onRequestNextPayload* : proc(): bool {.gcsafe.}
|
2022-06-01 13:32:07 +00:00
|
|
|
onGetPayload* : proc(): bool {.gcsafe.}
|
|
|
|
onNewPayloadBroadcast* : proc(): bool {.gcsafe.}
|
|
|
|
onForkchoiceBroadcast* : proc(): bool {.gcsafe.}
|
|
|
|
onSafeBlockChange * : proc(): bool {.gcsafe.}
|
|
|
|
onFinalizedBlockChange* : proc(): bool {.gcsafe.}
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-10-23 13:59:57 +00:00
|
|
|
|
|
|
|
proc collectBlobHashes(list: openArray[Web3Tx]): seq[common.Hash256] =
|
|
|
|
for w3tx in list:
|
2024-03-20 07:35:38 +00:00
|
|
|
let tx = ethTx(w3tx)
|
2023-10-23 13:59:57 +00:00
|
|
|
for h in tx.versionedHashes:
|
|
|
|
result.add h
|
|
|
|
|
|
|
|
func latestExecutableData*(cl: CLMocker): ExecutableData =
|
|
|
|
ExecutableData(
|
|
|
|
basePayload: cl.latestPayloadBuilt,
|
|
|
|
beaconRoot : ethHash cl.latestPayloadAttributes.parentBeaconBlockRoot,
|
|
|
|
attr : cl.latestPayloadAttributes,
|
2024-06-14 07:31:08 +00:00
|
|
|
versionedHashes: Opt.some(collectBlobHashes(cl.latestPayloadBuilt.transactions)),
|
2023-10-23 13:59:57 +00:00
|
|
|
)
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
func latestPayloadNumber*(h: Table[uint64, ExecutionPayload]): uint64 =
|
2023-08-21 02:08:54 +00:00
|
|
|
result = 0'u64
|
|
|
|
for n, _ in h:
|
|
|
|
if n > result:
|
|
|
|
result = n
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
func latestWithdrawalsIndex*(h: Table[uint64, ExecutionPayload]): uint64 =
|
2023-08-21 02:08:54 +00:00
|
|
|
result = 0'u64
|
|
|
|
for n, p in h:
|
|
|
|
if p.withdrawals.isNone:
|
|
|
|
continue
|
|
|
|
let wds = p.withdrawals.get
|
|
|
|
for w in wds:
|
|
|
|
if w.index.uint64 > result:
|
|
|
|
result = w.index.uint64
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
func client(cl: CLMocker): RpcClient =
|
|
|
|
cl.clients.first.client
|
|
|
|
|
2023-09-30 12:20:29 +00:00
|
|
|
proc init(cl: CLMocker, eng: EngineEnv, com: CommonRef) =
|
|
|
|
cl.clients = ClientPool()
|
|
|
|
cl.clients.add eng
|
2023-08-21 02:08:54 +00:00
|
|
|
cl.com = com
|
2022-06-27 04:15:23 +00:00
|
|
|
cl.slotsToSafe = 1
|
|
|
|
cl.slotsToFinalized = 2
|
2023-10-19 03:28:52 +00:00
|
|
|
cl.payloadProductionClientDelay = 0
|
2023-08-21 02:08:54 +00:00
|
|
|
cl.headerHistory[0] = com.genesisHeader()
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-09-30 12:20:29 +00:00
|
|
|
proc newClMocker*(eng: EngineEnv, com: CommonRef): CLMocker =
|
2022-04-11 10:00:39 +00:00
|
|
|
new result
|
2023-09-30 12:20:29 +00:00
|
|
|
result.init(eng, com)
|
|
|
|
|
|
|
|
proc addEngine*(cl: CLMocker, eng: EngineEnv) =
|
|
|
|
cl.clients.add eng
|
2024-05-15 16:22:03 +00:00
|
|
|
echo "CLMocker: Adding engine client ", eng.ID()
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
proc removeEngine*(cl: CLMocker, eng: EngineEnv) =
|
|
|
|
cl.clients.remove eng
|
2024-05-15 16:22:03 +00:00
|
|
|
echo "CLMocker: Removing engine client ", eng.ID()
|
2023-10-31 03:18:37 +00:00
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
proc waitForTTD*(cl: CLMocker): Future[bool] {.async.} =
|
2023-08-21 02:08:54 +00:00
|
|
|
let ttd = cl.com.ttd()
|
|
|
|
doAssert(ttd.isSome)
|
|
|
|
let (header, waitRes) = await cl.client.waitForTTD(ttd.get)
|
2022-04-11 10:00:39 +00:00
|
|
|
if not waitRes:
|
2023-08-21 02:08:54 +00:00
|
|
|
error "CLMocker: timeout while waiting for TTD"
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
echo "CLMocker: TTD has been reached at block ", header.number
|
2024-05-15 16:22:03 +00:00
|
|
|
|
2022-06-01 13:32:07 +00:00
|
|
|
cl.latestHeader = header
|
2024-06-14 07:31:08 +00:00
|
|
|
cl.headerHistory[header.number] = header
|
2022-04-11 10:00:39 +00:00
|
|
|
cl.ttdReached = true
|
|
|
|
|
2022-06-01 13:32:07 +00:00
|
|
|
let headerHash = BlockHash(common.blockHash(cl.latestHeader).data)
|
2022-06-27 04:15:23 +00:00
|
|
|
if cl.slotsToSafe == 0:
|
|
|
|
cl.latestForkchoice.safeBlockHash = headerHash
|
|
|
|
|
|
|
|
if cl.slotsToFinalized == 0:
|
|
|
|
cl.latestForkchoice.finalizedBlockHash = headerHash
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
# Reset transition values
|
2024-06-14 07:31:08 +00:00
|
|
|
cl.latestHeadNumber = cl.latestHeader.number
|
2023-08-21 02:08:54 +00:00
|
|
|
cl.headHashHistory = @[]
|
2024-06-14 07:31:08 +00:00
|
|
|
cl.firstPoSBlockNumber = Opt.none(uint64)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
# Prepare initial forkchoice, to be sent to the transition payload producer
|
|
|
|
cl.latestForkchoice = ForkchoiceStateV1()
|
|
|
|
cl.latestForkchoice.headBlockHash = headerHash
|
2022-04-11 10:00:39 +00:00
|
|
|
|
|
|
|
let res = cl.client.forkchoiceUpdatedV1(cl.latestForkchoice)
|
|
|
|
if res.isErr:
|
2022-07-04 12:31:41 +00:00
|
|
|
error "waitForTTD: forkchoiceUpdated error", msg=res.error
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
let s = res.get()
|
|
|
|
if s.payloadStatus.status != PayloadExecutionStatus.valid:
|
2022-07-04 12:31:41 +00:00
|
|
|
error "waitForTTD: forkchoiceUpdated response unexpected",
|
|
|
|
expect = PayloadExecutionStatus.valid,
|
|
|
|
get = s.payloadStatus.status
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
# Check whether a block number is a PoS block
|
|
|
|
proc isBlockPoS*(cl: CLMocker, bn: common.BlockNumber): bool =
|
|
|
|
if cl.firstPoSBlockNumber.isNone:
|
|
|
|
return false
|
|
|
|
|
|
|
|
let number = cl.firstPoSBlockNumber.get()
|
|
|
|
if number > bn:
|
|
|
|
return false
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
2023-10-23 13:59:57 +00:00
|
|
|
proc addPayloadID*(cl: CLMocker, eng: EngineEnv, newPayloadID: PayloadID): bool =
|
|
|
|
# Check if payload ID has been used before
|
|
|
|
var zeroPayloadID: PayloadID
|
|
|
|
if cl.payloadIDHistory.getOrDefault(eng.ID(), zeroPayloadID) == newPayloadID:
|
|
|
|
error "reused payload ID", ID = newPayloadID.toHex
|
|
|
|
return false
|
|
|
|
|
|
|
|
# Add payload ID to history
|
|
|
|
cl.payloadIDHistory[eng.ID()] = newPayloadID
|
|
|
|
info "CLMocker: Added payload for client",
|
|
|
|
ID=newPayloadID.toHex, ID=eng.ID()
|
|
|
|
return true
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
# Return the per-block timestamp value increment
|
2023-10-18 02:16:11 +00:00
|
|
|
func getTimestampIncrement(cl: CLMocker): EthTime =
|
|
|
|
EthTime cl.blockTimestampIncrement.get(1)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
# Returns the timestamp value to be included in the next payload attributes
|
2023-10-18 02:16:11 +00:00
|
|
|
func getNextBlockTimestamp(cl: CLMocker): EthTime =
|
2023-08-21 02:08:54 +00:00
|
|
|
if cl.firstPoSBlockNumber.isNone and cl.transitionPayloadTimestamp.isSome:
|
|
|
|
# We are producing the transition payload and there's a value specified
|
|
|
|
# for this specific payload
|
2023-10-18 02:16:11 +00:00
|
|
|
return EthTime cl.transitionPayloadTimestamp.get
|
|
|
|
return cl.latestHeader.timestamp + cl.getTimestampIncrement()
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
func setNextWithdrawals(cl: CLMocker, nextWithdrawals: Opt[seq[WithdrawalV1]]) =
|
2023-08-21 02:08:54 +00:00
|
|
|
cl.nextWithdrawals = nextWithdrawals
|
|
|
|
|
|
|
|
func isShanghai(cl: CLMocker, timestamp: Quantity): bool =
|
2023-10-18 02:16:11 +00:00
|
|
|
let ts = EthTime(timestamp.uint64)
|
2023-08-21 02:08:54 +00:00
|
|
|
cl.com.isShanghaiOrLater(ts)
|
|
|
|
|
|
|
|
func isCancun(cl: CLMocker, timestamp: Quantity): bool =
|
2023-10-18 02:16:11 +00:00
|
|
|
let ts = EthTime(timestamp.uint64)
|
2023-08-21 02:08:54 +00:00
|
|
|
cl.com.isCancunOrLater(ts)
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
# Picks the next payload producer from the set of clients registered
|
|
|
|
proc pickNextPayloadProducer(cl: CLMocker): bool =
|
|
|
|
doAssert cl.clients.len != 0
|
|
|
|
|
|
|
|
for i in 0 ..< cl.clients.len:
|
|
|
|
# Get a client to generate the payload
|
|
|
|
let id = (cl.latestHeadNumber.int + i) mod cl.clients.len
|
|
|
|
cl.nextBlockProducer = cl.clients[id]
|
|
|
|
|
2024-05-15 16:22:03 +00:00
|
|
|
echo "CLMocker: Selected payload producer: ", cl.nextBlockProducer.ID()
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
# Get latest header. Number and hash must coincide with our view of the chain,
|
|
|
|
# and only then we can build on top of this client's chain
|
2023-10-31 03:18:37 +00:00
|
|
|
let res = cl.nextBlockProducer.client.latestHeader()
|
2023-09-06 09:18:26 +00:00
|
|
|
if res.isErr:
|
|
|
|
error "CLMocker: Could not get latest block header while selecting client for payload production",
|
|
|
|
msg=res.error
|
|
|
|
return false
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
let latestHeader = res.get
|
2023-09-06 09:18:26 +00:00
|
|
|
let lastBlockHash = latestHeader.blockHash
|
|
|
|
if cl.latestHeader.blockHash != lastBlockHash or
|
2024-06-14 07:31:08 +00:00
|
|
|
cl.latestHeadNumber != latestHeader.number:
|
2023-09-06 09:18:26 +00:00
|
|
|
# Selected client latest block hash does not match canonical chain, try again
|
|
|
|
cl.nextBlockProducer = nil
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
doAssert cl.nextBlockProducer != nil
|
|
|
|
return true
|
|
|
|
|
2023-10-19 03:28:52 +00:00
|
|
|
proc generatePayloadAttributes(cl: CLMocker) =
|
2022-04-11 10:00:39 +00:00
|
|
|
# Generate a random value for the PrevRandao field
|
2023-10-31 03:18:37 +00:00
|
|
|
let nextPrevRandao = common.Hash256.randomBytes()
|
2023-08-21 02:08:54 +00:00
|
|
|
let timestamp = Quantity cl.getNextBlockTimestamp.uint64
|
|
|
|
cl.latestPayloadAttributes = PayloadAttributes(
|
2022-04-11 10:00:39 +00:00
|
|
|
timestamp: timestamp,
|
|
|
|
prevRandao: FixedBytes[32] nextPrevRandao.data,
|
|
|
|
suggestedFeeRecipient: Address cl.nextFeeRecipient,
|
|
|
|
)
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
if cl.isShanghai(timestamp):
|
|
|
|
cl.latestPayloadAttributes.withdrawals = cl.nextWithdrawals
|
|
|
|
|
|
|
|
if cl.isCancun(timestamp):
|
|
|
|
# Write a deterministic hash based on the block number
|
|
|
|
let beaconRoot = timestampToBeaconRoot(timestamp)
|
2024-06-14 07:31:08 +00:00
|
|
|
cl.latestPayloadAttributes.parentBeaconBlockRoot = Opt.some(beaconRoot)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
# Save random value
|
2024-06-14 07:31:08 +00:00
|
|
|
let number = cl.latestHeader.number + 1
|
2022-04-11 10:00:39 +00:00
|
|
|
cl.prevRandaoHistory[number] = nextPrevRandao
|
|
|
|
|
2023-10-19 03:28:52 +00:00
|
|
|
proc requestNextPayload(cl: CLMocker): bool =
|
2023-08-21 02:08:54 +00:00
|
|
|
let version = cl.latestPayloadAttributes.version
|
2023-09-30 12:20:29 +00:00
|
|
|
let client = cl.nextBlockProducer.client
|
2024-06-14 07:31:08 +00:00
|
|
|
let res = client.forkchoiceUpdated(version, cl.latestForkchoice, Opt.some(cl.latestPayloadAttributes))
|
2022-04-11 10:00:39 +00:00
|
|
|
if res.isErr:
|
2023-08-21 02:08:54 +00:00
|
|
|
error "CLMocker: Could not send forkchoiceUpdated", version=version, msg=res.error
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
let s = res.get()
|
|
|
|
if s.payloadStatus.status != PayloadExecutionStatus.valid:
|
|
|
|
error "CLMocker: Unexpected forkchoiceUpdated Response from Payload builder",
|
|
|
|
status=s.payloadStatus.status
|
2023-09-06 09:18:26 +00:00
|
|
|
return false
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
if s.payloadStatus.latestValidHash.isNone or s.payloadStatus.latestValidHash.get != cl.latestForkchoice.headBlockHash:
|
|
|
|
error "CLMocker: Unexpected forkchoiceUpdated LatestValidHash Response from Payload builder",
|
|
|
|
latest=s.payloadStatus.latestValidHash,
|
|
|
|
head=cl.latestForkchoice.headBlockHash
|
2023-09-06 09:18:26 +00:00
|
|
|
return false
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
doAssert s.payLoadID.isSome
|
|
|
|
cl.nextPayloadID = s.payloadID.get()
|
|
|
|
return true
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
proc getPayload(cl: CLMocker, payloadId: PayloadID): Result[GetPayloadResponse, string] =
|
|
|
|
let ts = cl.latestPayloadAttributes.timestamp
|
2023-09-06 09:18:26 +00:00
|
|
|
let client = cl.nextBlockProducer.client
|
2023-08-21 02:08:54 +00:00
|
|
|
if cl.isCancun(ts):
|
2023-09-30 12:20:29 +00:00
|
|
|
client.getPayload(payloadId, Version.V3)
|
|
|
|
elif cl.isShanghai(ts):
|
|
|
|
client.getPayload(payloadId, Version.V2)
|
|
|
|
else:
|
|
|
|
client.getPayload(payloadId, Version.V1)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc getNextPayload(cl: CLMocker): bool =
|
2023-08-21 02:08:54 +00:00
|
|
|
let res = cl.getPayload(cl.nextPayloadID)
|
2022-04-11 10:00:39 +00:00
|
|
|
if res.isErr:
|
|
|
|
error "CLMocker: Could not getPayload",
|
|
|
|
payloadID=toHex(cl.nextPayloadID)
|
|
|
|
return false
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
let x = res.get()
|
|
|
|
cl.latestPayloadBuilt = x.executionPayload
|
|
|
|
cl.latestBlockValue = x.blockValue
|
|
|
|
cl.latestBlobsBundle = x.blobsBundle
|
2023-10-19 03:28:52 +00:00
|
|
|
cl.latestShouldOverrideBuilder = x.shouldOverrideBuilder
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
let beaconRoot = ethHash cl.latestPayloadAttributes.parentBeaconblockRoot
|
2024-05-15 03:07:59 +00:00
|
|
|
let header = blockHeader(cl.latestPayloadBuilt, beaconRoot = beaconRoot)
|
2023-08-27 01:23:45 +00:00
|
|
|
let blockHash = w3Hash header.blockHash
|
2022-04-11 10:00:39 +00:00
|
|
|
if blockHash != cl.latestPayloadBuilt.blockHash:
|
2023-08-21 02:08:54 +00:00
|
|
|
error "CLMocker: getNextPayload blockHash mismatch",
|
2023-08-27 01:23:45 +00:00
|
|
|
expected=cl.latestPayloadBuilt.blockHash,
|
2022-04-11 10:00:39 +00:00
|
|
|
get=blockHash.toHex
|
|
|
|
return false
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
if cl.latestPayloadBuilt.timestamp != cl.latestPayloadAttributes.timestamp:
|
|
|
|
error "CLMocker: Incorrect Timestamp on payload built",
|
|
|
|
expect=cl.latestPayloadBuilt.timestamp.uint64,
|
|
|
|
get=cl.latestPayloadAttributes.timestamp.uint64
|
|
|
|
return false
|
|
|
|
|
|
|
|
if cl.latestPayloadBuilt.feeRecipient != cl.latestPayloadAttributes.suggestedFeeRecipient:
|
|
|
|
error "CLMocker: Incorrect SuggestedFeeRecipient on payload built",
|
2023-08-27 01:23:45 +00:00
|
|
|
expect=cl.latestPayloadBuilt.feeRecipient,
|
|
|
|
get=cl.latestPayloadAttributes.suggestedFeeRecipient
|
2023-08-21 02:08:54 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
if cl.latestPayloadBuilt.prevRandao != cl.latestPayloadAttributes.prevRandao:
|
|
|
|
error "CLMocker: Incorrect PrevRandao on payload built",
|
2023-08-27 01:23:45 +00:00
|
|
|
expect=cl.latestPayloadBuilt.prevRandao,
|
|
|
|
get=cl.latestPayloadAttributes.prevRandao
|
2023-08-21 02:08:54 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
if cl.latestPayloadBuilt.parentHash != BlockHash cl.latestHeader.blockHash.data:
|
|
|
|
error "CLMocker: Incorrect ParentHash on payload built",
|
2023-08-27 01:23:45 +00:00
|
|
|
expect=cl.latestPayloadBuilt.parentHash,
|
2023-08-21 02:08:54 +00:00
|
|
|
get=cl.latestHeader.blockHash
|
|
|
|
return false
|
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
if cl.latestPayloadBuilt.blockNumber.uint64 != cl.latestHeader.number + 1'u64:
|
2023-08-21 02:08:54 +00:00
|
|
|
error "CLMocker: Incorrect Number on payload built",
|
|
|
|
expect=cl.latestPayloadBuilt.blockNumber.uint64,
|
2024-06-14 07:31:08 +00:00
|
|
|
get=cl.latestHeader.number+1'u64
|
2023-08-21 02:08:54 +00:00
|
|
|
return false
|
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
return true
|
|
|
|
|
2023-10-22 07:05:20 +00:00
|
|
|
func versionedHashes(payload: ExecutionPayload): seq[Web3Hash] =
|
|
|
|
result = newSeqOfCap[BlockHash](payload.transactions.len)
|
|
|
|
for x in payload.transactions:
|
|
|
|
let tx = rlp.decode(distinctBase(x), Transaction)
|
|
|
|
for vs in tx.versionedHashes:
|
|
|
|
result.add w3Hash vs
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
proc broadcastNewPayload(cl: CLMocker,
|
|
|
|
eng: EngineEnv,
|
|
|
|
payload: ExecutionPayload): Result[PayloadStatusV1, string] =
|
2023-08-21 02:08:54 +00:00
|
|
|
case payload.version
|
2023-10-31 03:18:37 +00:00
|
|
|
of Version.V1: return eng.client.newPayloadV1(payload.V1)
|
|
|
|
of Version.V2: return eng.client.newPayloadV2(payload.V2)
|
|
|
|
of Version.V3: return eng.client.newPayloadV3(payload.V3,
|
2023-10-22 07:05:20 +00:00
|
|
|
versionedHashes(payload),
|
2023-08-21 02:08:54 +00:00
|
|
|
cl.latestPayloadAttributes.parentBeaconBlockRoot.get)
|
2024-03-28 07:16:40 +00:00
|
|
|
of Version.V4: return eng.client.newPayloadV4(payload.V4,
|
|
|
|
versionedHashes(payload),
|
|
|
|
cl.latestPayloadAttributes.parentBeaconBlockRoot.get)
|
2022-04-11 10:00:39 +00:00
|
|
|
|
|
|
|
proc broadcastNextNewPayload(cl: CLMocker): bool =
|
2023-10-31 03:18:37 +00:00
|
|
|
for eng in cl.clients:
|
|
|
|
let res = cl.broadcastNewPayload(eng, cl.latestPayloadBuilt)
|
|
|
|
if res.isErr:
|
|
|
|
error "CLMocker: broadcastNewPayload Error", msg=res.error
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
let s = res.get()
|
2024-05-15 16:22:03 +00:00
|
|
|
echo "CLMocker: Executed payload on ", eng.ID(),
|
|
|
|
" ", s.status, " ", s.latestValidHash
|
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
if s.status == PayloadExecutionStatus.valid:
|
|
|
|
# The client is synced and the payload was immediately validated
|
|
|
|
# https:#github.com/ethereum/execution-apis/blob/main/src/engine/specification.md:
|
|
|
|
# - If validation succeeds, the response MUST contain {status: VALID, latestValidHash: payload.blockHash}
|
|
|
|
let blockHash = cl.latestPayloadBuilt.blockHash
|
|
|
|
if s.latestValidHash.isNone:
|
|
|
|
error "CLMocker: NewPayload returned VALID status with nil LatestValidHash",
|
|
|
|
expected=blockHash.toHex
|
|
|
|
return false
|
|
|
|
|
|
|
|
let latestValidHash = s.latestValidHash.get()
|
2024-04-21 14:44:05 +00:00
|
|
|
if latestValidHash != blockHash:
|
2023-10-31 03:18:37 +00:00
|
|
|
error "CLMocker: NewPayload returned VALID status with incorrect LatestValidHash",
|
|
|
|
get=latestValidHash.toHex, expected=blockHash.toHex
|
|
|
|
return false
|
|
|
|
|
|
|
|
elif s.status == PayloadExecutionStatus.accepted:
|
|
|
|
# The client is not synced but the payload was accepted
|
|
|
|
# https:#github.com/ethereum/execution-apis/blob/main/src/engine/specification.md:
|
|
|
|
# - {status: ACCEPTED, latestValidHash: null, validationError: null} if the following conditions are met:
|
|
|
|
# the blockHash of the payload is valid
|
|
|
|
# the payload doesn't extend the canonical chain
|
|
|
|
# the payload hasn't been fully validated.
|
2024-04-21 14:44:05 +00:00
|
|
|
let nullHash = w3Hash common.Hash256()
|
2023-10-31 03:18:37 +00:00
|
|
|
let latestValidHash = s.latestValidHash.get(nullHash)
|
|
|
|
if s.latestValidHash.isSome and latestValidHash != nullHash:
|
|
|
|
error "CLMocker: NewPayload returned ACCEPTED status with incorrect LatestValidHash",
|
|
|
|
hash=latestValidHash.toHex
|
|
|
|
return false
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
else:
|
|
|
|
error "CLMocker: broadcastNewPayload Response",
|
2024-04-21 14:44:05 +00:00
|
|
|
status=s.status,
|
|
|
|
msg=s.validationError.get("NO MSG")
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
2024-05-15 16:22:03 +00:00
|
|
|
# warning: although latestExecutedPayload is taken from
|
|
|
|
# latestPayloadBuilt, but during the next round, it can be different
|
|
|
|
|
|
|
|
cl.latestExecutedPayload = cl.latestExecutableData()
|
2022-04-11 10:00:39 +00:00
|
|
|
let number = uint64 cl.latestPayloadBuilt.blockNumber
|
|
|
|
cl.executedPayloadHistory[number] = cl.latestPayloadBuilt
|
|
|
|
return true
|
|
|
|
|
2023-11-03 14:41:05 +00:00
|
|
|
proc broadcastForkchoiceUpdated(cl: CLMocker,
|
|
|
|
eng: EngineEnv,
|
|
|
|
version: Version,
|
|
|
|
update: ForkchoiceStateV1):
|
|
|
|
Result[ForkchoiceUpdatedResponse, string] =
|
2024-06-14 07:31:08 +00:00
|
|
|
eng.client.forkchoiceUpdated(version, update, Opt.none(PayloadAttributes))
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-11-03 14:41:05 +00:00
|
|
|
proc broadcastForkchoiceUpdated*(cl: CLMocker,
|
|
|
|
version: Version,
|
|
|
|
update: ForkchoiceStateV1): bool =
|
2023-10-31 03:18:37 +00:00
|
|
|
for eng in cl.clients:
|
2023-11-03 14:41:05 +00:00
|
|
|
let res = cl.broadcastForkchoiceUpdated(eng, version, update)
|
2023-10-31 03:18:37 +00:00
|
|
|
if res.isErr:
|
|
|
|
error "CLMocker: broadcastForkchoiceUpdated Error", msg=res.error
|
|
|
|
return false
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
let s = res.get()
|
|
|
|
if s.payloadStatus.status != PayloadExecutionStatus.valid:
|
|
|
|
error "CLMocker: broadcastForkchoiceUpdated Response",
|
2024-04-21 14:44:05 +00:00
|
|
|
status=s.payloadStatus.status,
|
|
|
|
msg=s.payloadStatus.validationError.get("NO MSG")
|
2023-10-31 03:18:37 +00:00
|
|
|
return false
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
if s.payloadStatus.latestValidHash.get != cl.latestForkchoice.headBlockHash:
|
|
|
|
error "CLMocker: Incorrect LatestValidHash from ForkchoiceUpdated",
|
|
|
|
get=s.payloadStatus.latestValidHash.get.toHex,
|
|
|
|
expect=cl.latestForkchoice.headBlockHash.toHex
|
|
|
|
return false
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
if s.payloadStatus.validationError.isSome:
|
|
|
|
error "CLMocker: Expected empty validationError",
|
|
|
|
msg=s.payloadStatus.validationError.get
|
|
|
|
return false
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
if s.payloadID.isSome:
|
|
|
|
error "CLMocker: Expected empty PayloadID",
|
|
|
|
msg=s.payloadID.get.toHex
|
|
|
|
return false
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
return true
|
|
|
|
|
2023-11-03 14:41:05 +00:00
|
|
|
proc broadcastLatestForkchoice(cl: CLMocker): bool =
|
|
|
|
let version = cl.latestExecutedPayload.version
|
|
|
|
cl.broadcastForkchoiceUpdated(version, cl.latestForkchoice)
|
|
|
|
|
2023-09-30 13:16:45 +00:00
|
|
|
func w3Address(x: int): Web3Address =
|
|
|
|
var res: array[20, byte]
|
|
|
|
res[^1] = x.byte
|
|
|
|
Web3Address(res)
|
|
|
|
|
|
|
|
proc makeNextWithdrawals(cl: CLMocker): seq[WithdrawalV1] =
|
|
|
|
var
|
|
|
|
withdrawalCount = 10
|
|
|
|
withdrawalIndex = 0'u64
|
|
|
|
|
|
|
|
if cl.latestPayloadBuilt.withdrawals.isSome:
|
|
|
|
let wds = cl.latestPayloadBuilt.withdrawals.get
|
|
|
|
for w in wds:
|
|
|
|
if w.index.uint64 > withdrawalIndex:
|
|
|
|
withdrawalIndex = w.index.uint64
|
|
|
|
|
|
|
|
var
|
|
|
|
withdrawals = newSeq[WithdrawalV1](withdrawalCount)
|
|
|
|
|
|
|
|
for i in 0..<withdrawalCount:
|
|
|
|
withdrawalIndex += 1
|
|
|
|
withdrawals[i] = WithdrawalV1(
|
|
|
|
index: w3Qty withdrawalIndex,
|
2024-06-14 07:31:08 +00:00
|
|
|
validatorIndex: Quantity i,
|
2023-09-30 13:16:45 +00:00
|
|
|
address: w3Address i,
|
|
|
|
amount: w3Qty 100'u64,
|
|
|
|
)
|
|
|
|
|
|
|
|
return withdrawals
|
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
proc produceSingleBlock*(cl: CLMocker, cb: BlockProcessCallbacks): bool {.gcsafe.} =
|
|
|
|
doAssert(cl.ttdReached)
|
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
cl.currentPayloadNumber = cl.latestHeader.number + 1'u64
|
2022-04-11 10:00:39 +00:00
|
|
|
if not cl.pickNextPayloadProducer():
|
|
|
|
return false
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
# Check if next withdrawals necessary, test can override this value on
|
|
|
|
# `OnPayloadProducerSelected` callback
|
|
|
|
if cl.nextWithdrawals.isNone:
|
2023-09-30 13:16:45 +00:00
|
|
|
let nw = cl.makeNextWithdrawals()
|
2024-06-14 07:31:08 +00:00
|
|
|
cl.setNextWithdrawals(Opt.some(nw))
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
if cb.onPayloadProducerSelected != nil:
|
|
|
|
if not cb.onPayloadProducerSelected():
|
2023-10-23 13:59:57 +00:00
|
|
|
debugEcho "***PAYLOAD PRODUCER SELECTED ERROR***"
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
2023-10-19 03:28:52 +00:00
|
|
|
cl.generatePayloadAttributes()
|
|
|
|
|
|
|
|
if cb.onPayloadAttributesGenerated != nil:
|
|
|
|
if not cb.onPayloadAttributesGenerated():
|
2023-10-23 13:59:57 +00:00
|
|
|
debugEcho "***ON PAYLOAD ATTRIBUTES ERROR***"
|
2023-10-19 03:28:52 +00:00
|
|
|
return false
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
if not cl.requestNextPayload():
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
cl.setNextWithdrawals(Opt.none(seq[WithdrawalV1]))
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
if cb.onRequestNextPayload != nil:
|
|
|
|
if not cb.onRequestNextPayload():
|
2023-10-23 13:59:57 +00:00
|
|
|
debugEcho "***ON REQUEST NEXT PAYLOAD ERROR***"
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
# Give the client a delay between getting the payload ID and actually retrieving the payload
|
2023-10-19 03:28:52 +00:00
|
|
|
if cl.payloadProductionClientDelay != 0:
|
|
|
|
let period = chronos.seconds(cl.payloadProductionClientDelay)
|
|
|
|
waitFor sleepAsync(period)
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
if not cl.getNextPayload():
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
if cb.onGetPayload != nil:
|
|
|
|
if not cb.onGetPayload():
|
2023-10-23 13:59:57 +00:00
|
|
|
debugEcho "***ON GET PAYLOAD ERROR***"
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
if not cl.broadcastNextNewPayload():
|
2023-10-23 13:59:57 +00:00
|
|
|
debugEcho "***ON BROADCAST NEXT NEW PAYLOAD ERROR***"
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
if cb.onNewPayloadBroadcast != nil:
|
|
|
|
if not cb.onNewPayloadBroadcast():
|
2023-10-23 13:59:57 +00:00
|
|
|
debugEcho "***ON NEW PAYLOAD BROADCAST ERROR***"
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
# Broadcast forkchoice updated with new HeadBlock to all clients
|
2022-06-27 04:15:23 +00:00
|
|
|
let previousForkchoice = cl.latestForkchoice
|
|
|
|
cl.headHashHistory.add cl.latestPayloadBuilt.blockHash
|
|
|
|
|
|
|
|
cl.latestForkchoice = ForkchoiceStateV1()
|
|
|
|
cl.latestForkchoice.headBlockHash = cl.latestPayloadBuilt.blockHash
|
|
|
|
|
|
|
|
let hhLen = cl.headHashHistory.len
|
|
|
|
if hhLen > cl.slotsToSafe:
|
|
|
|
cl.latestForkchoice.safeBlockHash = cl.headHashHistory[hhLen - cl.slotsToSafe - 1]
|
|
|
|
|
|
|
|
if hhLen > cl.slotsToFinalized:
|
|
|
|
cl.latestForkchoice.finalizedBlockHash = cl.headHashHistory[hhLen - cl.slotsToFinalized - 1]
|
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
if not cl.broadcastLatestForkchoice():
|
2023-10-23 13:59:57 +00:00
|
|
|
debugEcho "***ON BROADCAST LATEST FORK CHOICE ERROR***"
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
2022-06-01 13:32:07 +00:00
|
|
|
if cb.onForkchoiceBroadcast != nil:
|
|
|
|
if not cb.onForkchoiceBroadcast():
|
2023-10-23 13:59:57 +00:00
|
|
|
debugEcho "***ON FORK CHOICE BROADCAST ERROR***"
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
# Broadcast forkchoice updated with new SafeBlock to all clients
|
2022-06-27 04:15:23 +00:00
|
|
|
if cb.onSafeBlockChange != nil and cl.latestForkchoice.safeBlockHash != previousForkchoice.safeBlockHash:
|
2022-06-01 13:32:07 +00:00
|
|
|
if not cb.onSafeBlockChange():
|
2023-10-23 13:59:57 +00:00
|
|
|
debugEcho "***ON SAFE BLOCK CHANGE ERROR***"
|
2022-04-11 10:00:39 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
# Broadcast forkchoice updated with new FinalizedBlock to all clients
|
2022-06-27 04:15:23 +00:00
|
|
|
if cb.onFinalizedBlockChange != nil and cl.latestForkchoice.finalizedBlockHash != previousForkchoice.finalizedBlockHash:
|
|
|
|
if not cb.onFinalizedBlockChange():
|
2023-10-23 13:59:57 +00:00
|
|
|
debugEcho "***ON FINALIZED BLOCK CHANGE ERROR***"
|
2022-06-27 04:15:23 +00:00
|
|
|
return false
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2022-06-27 04:15:23 +00:00
|
|
|
# Broadcast forkchoice updated with new FinalizedBlock to all clients
|
2022-04-11 10:00:39 +00:00
|
|
|
# Save the number of the first PoS block
|
|
|
|
if cl.firstPoSBlockNumber.isNone:
|
2024-06-14 07:31:08 +00:00
|
|
|
let number = cl.latestHeader.number + 1
|
|
|
|
cl.firstPoSBlockNumber = Opt.some(number)
|
2022-04-11 10:00:39 +00:00
|
|
|
|
|
|
|
# Save the header of the latest block in the PoS chain
|
2022-06-01 13:32:07 +00:00
|
|
|
cl.latestHeadNumber = cl.latestHeadNumber + 1
|
2022-04-11 10:00:39 +00:00
|
|
|
|
|
|
|
# Check if any of the clients accepted the new payload
|
2023-10-31 03:18:37 +00:00
|
|
|
let res = cl.client.headerByNumber(cl.latestHeadNumber)
|
2022-04-11 10:00:39 +00:00
|
|
|
if res.isErr:
|
|
|
|
error "CLMock ProduceSingleBlock", msg=res.error
|
|
|
|
return false
|
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
let newHeader = res.get
|
|
|
|
let newHash = w3Hash newHeader.blockHash
|
2022-04-11 10:00:39 +00:00
|
|
|
if newHash != cl.latestPayloadBuilt.blockHash:
|
|
|
|
error "CLMocker: None of the clients accepted the newly constructed payload",
|
|
|
|
hash=newHash.toHex
|
|
|
|
return false
|
|
|
|
|
2022-06-27 04:15:23 +00:00
|
|
|
# Check that the new finalized header has the correct properties
|
|
|
|
# ommersHash == 0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347
|
|
|
|
if newHeader.ommersHash != EMPTY_UNCLE_HASH:
|
|
|
|
error "CLMocker: Client produced a new header with incorrect ommersHash", ommersHash = newHeader.ommersHash
|
|
|
|
return false
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2022-06-27 04:15:23 +00:00
|
|
|
# difficulty == 0
|
|
|
|
if newHeader.difficulty != 0.u256:
|
|
|
|
error "CLMocker: Client produced a new header with incorrect difficulty", difficulty = newHeader.difficulty
|
|
|
|
return false
|
|
|
|
|
|
|
|
# mixHash == prevRandao
|
2024-06-14 07:31:08 +00:00
|
|
|
if newHeader.mixHash != cl.prevRandaoHistory[cl.latestHeadNumber]:
|
2022-06-27 04:15:23 +00:00
|
|
|
error "CLMocker: Client produced a new header with incorrect mixHash",
|
2024-06-14 07:31:08 +00:00
|
|
|
get = newHeader.mixHash.data.toHex,
|
2022-06-27 04:15:23 +00:00
|
|
|
expect = cl.prevRandaoHistory[cl.latestHeadNumber].data.toHex
|
|
|
|
return false
|
|
|
|
|
|
|
|
# nonce == 0x0000000000000000
|
|
|
|
if newHeader.nonce != default(BlockNonce):
|
|
|
|
error "CLMocker: Client produced a new header with incorrect nonce",
|
|
|
|
nonce = newHeader.nonce.toHex
|
|
|
|
return false
|
|
|
|
|
|
|
|
if newHeader.extraData.len > 32:
|
|
|
|
error "CLMocker: Client produced a new header with incorrect extraData (len > 32)",
|
|
|
|
len = newHeader.extraData.len
|
|
|
|
return false
|
|
|
|
|
|
|
|
cl.latestHeader = newHeader
|
2023-08-21 02:08:54 +00:00
|
|
|
cl.headerHistory[cl.latestHeadNumber] = cl.latestHeader
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
echo "CLMocker: New block produced: number=", newHeader.number,
|
2024-05-15 16:22:03 +00:00
|
|
|
" hash=", newHeader.blockHash
|
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
return true
|
|
|
|
|
|
|
|
# Loop produce PoS blocks by using the Engine API
|
|
|
|
proc produceBlocks*(cl: CLMocker, blockCount: int, cb: BlockProcessCallbacks): bool {.gcsafe.} =
|
|
|
|
# Produce requested amount of blocks
|
|
|
|
for i in 0..<blockCount:
|
|
|
|
if not cl.produceSingleBlock(cb):
|
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
2022-06-17 00:53:33 +00:00
|
|
|
proc posBlockNumber*(cl: CLMocker): uint64 =
|
|
|
|
cl.firstPoSBlockNumber.get(0'u64)
|