2023-01-31 12:38:08 +00:00
|
|
|
# Nimbus
|
2024-05-15 03:07:59 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2023-01-31 12:38:08 +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.
|
|
|
|
|
2022-02-22 08:55:04 +00:00
|
|
|
import
|
2024-10-16 01:34:12 +00:00
|
|
|
eth/common/[headers, hashes],
|
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]]
|
|
|
|
|
2024-11-02 01:45:27 +00:00
|
|
|
ExecutionBundle* = object
|
|
|
|
payload*: ExecutionPayload
|
|
|
|
blockValue*: UInt256
|
|
|
|
blobsBundle*: Opt[BlobsBundleV1]
|
|
|
|
executionRequests*: Opt[array[3, seq[byte]]]
|
|
|
|
targetBlobsPerBlock*: Opt[Quantity]
|
|
|
|
|
2022-02-22 08:55:04 +00:00
|
|
|
PayloadItem = object
|
2024-10-16 01:34:12 +00:00
|
|
|
id: Bytes8
|
2024-11-02 01:45:27 +00:00
|
|
|
payload: ExecutionBundle
|
2022-02-22 08:55:04 +00:00
|
|
|
|
|
|
|
HeaderItem = object
|
2024-10-16 01:34:12 +00:00
|
|
|
hash: Hash32
|
|
|
|
header: Header
|
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,
|
2024-10-16 01:34:12 +00:00
|
|
|
hash: Hash32, header: Header) =
|
2022-02-22 08:55:04 +00:00
|
|
|
api.headerQueue.put(HeaderItem(hash: hash, header: header))
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc put*(api: var PayloadQueue, id: Bytes8,
|
2024-11-02 01:45:27 +00:00
|
|
|
payload: ExecutionBundle) =
|
|
|
|
api.payloadQueue.put(PayloadItem(id: id, payload: payload))
|
2023-03-09 23:40:55 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, getters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc get*(api: PayloadQueue, hash: Hash32,
|
|
|
|
header: var Header): bool =
|
2023-08-27 01:23:45 +00:00
|
|
|
for x in api.headerQueue:
|
|
|
|
if x.hash == hash:
|
|
|
|
header = x.header
|
|
|
|
return true
|
|
|
|
false
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc get*(api: PayloadQueue, id: Bytes8,
|
2024-11-02 01:45:27 +00:00
|
|
|
payload: var ExecutionBundle): bool =
|
2024-10-18 09:38:18 +00:00
|
|
|
for x in api.payloadQueue:
|
|
|
|
if x.id == id:
|
|
|
|
payload = x.payload
|
|
|
|
return true
|
|
|
|
false
|