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-08-27 01:23:45 +00:00
|
|
|
./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.
|
2022-02-22 08:55:04 +00:00
|
|
|
MaxTrackedHeaders = 10
|
|
|
|
|
|
|
|
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
|
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,
|
2023-08-18 07:25:11 +00:00
|
|
|
blockValue: UInt256, payload: ExecutionPayload) =
|
|
|
|
api.payloadQueue.put(PayloadItem(id: id,
|
|
|
|
payload: payload, blockValue: blockValue))
|
2022-02-22 08:55:04 +00:00
|
|
|
|
2023-08-27 01:23:45 +00:00
|
|
|
proc put*(api: var PayloadQueue, id: PayloadID,
|
2023-08-18 07:25:11 +00:00
|
|
|
blockValue: UInt256, payload: SomeExecutionPayload) =
|
|
|
|
api.put(id, blockValue, payload.executionPayload)
|
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,
|
|
|
|
payload: var ExecutionPayload): 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
|
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 =
|
2023-08-08 07:29:23 +00:00
|
|
|
var p: ExecutionPayload
|
2023-08-18 07:25:11 +00:00
|
|
|
let found = api.get(id, blockValue, p)
|
2023-08-08 07:29:23 +00:00
|
|
|
doAssert(p.version == Version.V1)
|
|
|
|
payload = p.V1
|
|
|
|
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 =
|
2023-08-08 07:29:23 +00:00
|
|
|
var p: ExecutionPayload
|
2023-08-18 07:25:11 +00:00
|
|
|
let found = api.get(id, blockValue, p)
|
2023-08-08 07:29:23 +00:00
|
|
|
doAssert(p.version == Version.V2)
|
|
|
|
payload = p.V2
|
|
|
|
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 ExecutionPayloadV3): bool =
|
2023-08-08 07:29:23 +00:00
|
|
|
var p: ExecutionPayload
|
2023-08-18 07:25:11 +00:00
|
|
|
let found = api.get(id, blockValue, p)
|
2023-08-08 07:29:23 +00:00
|
|
|
doAssert(p.version == Version.V3)
|
|
|
|
payload = p.V3
|
|
|
|
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 =
|
2023-08-08 07:29:23 +00:00
|
|
|
var p: ExecutionPayload
|
2023-08-18 07:25:11 +00:00
|
|
|
let found = api.get(id, blockValue, p)
|
2023-08-08 07:29:23 +00:00
|
|
|
doAssert(p.version in {Version.V1, Version.V2})
|
|
|
|
payload = p.V1V2
|
2023-03-09 23:40:55 +00:00
|
|
|
return found
|