2023-01-31 12:38:08 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2022-2023 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.
|
|
|
|
|
2022-02-22 08:55:04 +00:00
|
|
|
import
|
2023-08-27 01:23:45 +00:00
|
|
|
eth/common,
|
2022-02-22 08:55:04 +00:00
|
|
|
web3/engine_api_types,
|
2023-12-08 09:35:50 +00:00
|
|
|
web3/execution_types
|
2022-02-22 08:55:04 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
# maxTrackedPayloads is the maximum number of prepared payloads the execution
|
2023-08-27 01:23:45 +00:00
|
|
|
# engine tracks before evicting old ones. Ideally we should only ever track
|
|
|
|
# the latest one; but have a slight wiggle room for non-ideal conditions.
|
2022-02-22 08:55:04 +00:00
|
|
|
MaxTrackedPayloads = 10
|
|
|
|
|
|
|
|
# maxTrackedHeaders is the maximum number of executed payloads the execution
|
2023-08-27 01:23:45 +00:00
|
|
|
# engine tracks before evicting old ones. Ideally we should only ever track
|
|
|
|
# the latest one; but have a slight wiggle room for non-ideal conditions.
|
2023-09-30 12:20:29 +00:00
|
|
|
MaxTrackedHeaders = 96
|
2022-02-22 08:55:04 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
QueueItem[T] = object
|
|
|
|
used: bool
|
|
|
|
data: T
|
|
|
|
|
|
|
|
SimpleQueue[M: static[int]; T] = object
|
|
|
|
list: array[M, QueueItem[T]]
|
|
|
|
|
|
|
|
PayloadItem = object
|
2022-04-08 04:54:11 +00:00
|
|
|
id: PayloadID
|
2023-08-08 07:29:23 +00:00
|
|
|
payload: ExecutionPayload
|
2023-08-18 07:25:11 +00:00
|
|
|
blockValue: UInt256
|
2024-05-06 20:52:56 +00:00
|
|
|
blobsBundle: Option[BlobsBundleV1]
|
2022-02-22 08:55:04 +00:00
|
|
|
|
|
|
|
HeaderItem = object
|
2023-08-27 01:23:45 +00:00
|
|
|
hash: common.Hash256
|
|
|
|
header: common.BlockHeader
|
2022-02-22 08:55:04 +00:00
|
|
|
|
2023-09-28 06:20:12 +00:00
|
|
|
PayloadQueue* = object
|
2022-02-22 08:55:04 +00:00
|
|
|
payloadQueue: SimpleQueue[MaxTrackedPayloads, PayloadItem]
|
|
|
|
headerQueue: SimpleQueue[MaxTrackedHeaders, HeaderItem]
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
{.push gcsafe, raises:[].}
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-02-22 08:55:04 +00:00
|
|
|
template shiftRight[M, T](x: var SimpleQueue[M, T]) =
|
|
|
|
x.list[1..^1] = x.list[0..^2]
|
|
|
|
|
|
|
|
proc put[M, T](x: var SimpleQueue[M, T], val: T) =
|
|
|
|
x.shiftRight()
|
|
|
|
x.list[0] = QueueItem[T](used: true, data: val)
|
|
|
|
|
|
|
|
iterator items[M, T](x: SimpleQueue[M, T]): T =
|
|
|
|
for z in x.list:
|
|
|
|
if z.used:
|
|
|
|
yield z.data
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, setters
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-11-14 07:32:33 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
proc put*(api: var PayloadQueue,
|
|
|
|
hash: common.Hash256, header: common.BlockHeader) =
|
2022-02-22 08:55:04 +00:00
|
|
|
api.headerQueue.put(HeaderItem(hash: hash, header: header))
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
proc put*(api: var PayloadQueue, id: PayloadID,
|
2024-05-06 20:52:56 +00:00
|
|
|
blockValue: UInt256, payload: ExecutionPayload,
|
|
|
|
blobsBundle: Option[BlobsBundleV1]) =
|
2023-08-18 07:25:11 +00:00
|
|
|
api.payloadQueue.put(PayloadItem(id: id,
|
2024-05-06 20:52:56 +00:00
|
|
|
payload: payload, blockValue: blockValue, blobsBundle: blobsBundle))
|
2022-02-22 08:55:04 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
proc put*(api: var PayloadQueue, id: PayloadID,
|
2024-05-06 20:52:56 +00:00
|
|
|
blockValue: UInt256, payload: SomeExecutionPayload,
|
|
|
|
blobsBundle: Option[BlobsBundleV1]) =
|
|
|
|
doAssert blobsBundle.isNone == (payload is
|
|
|
|
ExecutionPayloadV1 | ExecutionPayloadV2)
|
|
|
|
api.put(id, blockValue, payload.executionPayload, blobsBundle: blobsBundle)
|
|
|
|
|
|
|
|
proc put*(api: var PayloadQueue, id: PayloadID,
|
|
|
|
blockValue: UInt256,
|
|
|
|
payload: ExecutionPayloadV1 | ExecutionPayloadV2) =
|
|
|
|
api.put(id, blockValue, payload, blobsBundle = options.none(BlobsBundleV1))
|
2023-03-09 23:40:55 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, getters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc get*(api: PayloadQueue, hash: common.Hash256,
|
|
|
|
header: var common.BlockHeader): bool =
|
|
|
|
for x in api.headerQueue:
|
|
|
|
if x.hash == hash:
|
|
|
|
header = x.header
|
|
|
|
return true
|
|
|
|
false
|
|
|
|
|
|
|
|
proc get*(api: PayloadQueue, id: PayloadID,
|
2023-08-18 07:25:11 +00:00
|
|
|
blockValue: var UInt256,
|
2024-05-06 20:52:56 +00:00
|
|
|
payload: var ExecutionPayload,
|
|
|
|
blobsBundle: var Option[BlobsBundleV1]): bool =
|
2022-02-22 08:55:04 +00:00
|
|
|
for x in api.payloadQueue:
|
|
|
|
if x.id == id:
|
|
|
|
payload = x.payload
|
2023-08-18 07:25:11 +00:00
|
|
|
blockValue = x.blockValue
|
2024-05-06 20:52:56 +00:00
|
|
|
blobsBundle = x.blobsBundle
|
2022-02-22 08:55:04 +00:00
|
|
|
return true
|
|
|
|
false
|
2022-11-14 07:32:33 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
proc get*(api: PayloadQueue, id: PayloadID,
|
2023-08-18 07:25:11 +00:00
|
|
|
blockValue: var UInt256,
|
|
|
|
payload: var ExecutionPayloadV1): bool =
|
2024-05-06 20:52:56 +00:00
|
|
|
var
|
|
|
|
p: ExecutionPayload
|
|
|
|
blobsBundleOpt: Option[BlobsBundleV1]
|
|
|
|
let found = api.get(id, blockValue, p, blobsBundleOpt)
|
|
|
|
if found:
|
|
|
|
doAssert(p.version == Version.V1)
|
|
|
|
payload = p.V1
|
|
|
|
doAssert(blobsBundleOpt.isNone)
|
2023-08-08 07:29:23 +00:00
|
|
|
return found
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
proc get*(api: PayloadQueue, id: PayloadID,
|
2023-08-18 07:25:11 +00:00
|
|
|
blockValue: var UInt256,
|
|
|
|
payload: var ExecutionPayloadV2): bool =
|
2024-05-06 20:52:56 +00:00
|
|
|
var
|
|
|
|
p: ExecutionPayload
|
|
|
|
blobsBundleOpt: Option[BlobsBundleV1]
|
|
|
|
let found = api.get(id, blockValue, p, blobsBundleOpt)
|
|
|
|
if found:
|
|
|
|
doAssert(p.version == Version.V2)
|
|
|
|
payload = p.V2
|
|
|
|
doAssert(blobsBundleOpt.isNone)
|
2023-08-08 07:29:23 +00:00
|
|
|
return found
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
proc get*(api: PayloadQueue, id: PayloadID,
|
2023-08-18 07:25:11 +00:00
|
|
|
blockValue: var UInt256,
|
2024-05-06 20:52:56 +00:00
|
|
|
payload: var ExecutionPayloadV3,
|
|
|
|
blobsBundle: var BlobsBundleV1): bool =
|
|
|
|
var
|
|
|
|
p: ExecutionPayload
|
|
|
|
blobsBundleOpt: Option[BlobsBundleV1]
|
|
|
|
let found = api.get(id, blockValue, p, blobsBundleOpt)
|
|
|
|
if found:
|
|
|
|
doAssert(p.version == Version.V3)
|
|
|
|
payload = p.V3
|
|
|
|
doAssert(blobsBundleOpt.isSome)
|
|
|
|
blobsBundle = blobsBundleOpt.unsafeGet
|
2023-08-08 07:29:23 +00:00
|
|
|
return found
|
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
proc get*(api: PayloadQueue, id: PayloadID,
|
2023-08-18 07:25:11 +00:00
|
|
|
blockValue: var UInt256,
|
|
|
|
payload: var ExecutionPayloadV1OrV2): bool =
|
2024-05-06 20:52:56 +00:00
|
|
|
var
|
|
|
|
p: ExecutionPayload
|
|
|
|
blobsBundleOpt: Option[BlobsBundleV1]
|
|
|
|
let found = api.get(id, blockValue, p, blobsBundleOpt)
|
|
|
|
if found:
|
|
|
|
doAssert(p.version in {Version.V1, Version.V2})
|
|
|
|
payload = p.V1V2
|
|
|
|
doAssert(blobsBundleOpt.isNone)
|
2023-03-09 23:40:55 +00:00
|
|
|
return found
|