-
Notifications
You must be signed in to change notification settings - Fork 127
/
payload_queue.nim
101 lines (81 loc) · 3.04 KB
/
payload_queue.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Nimbus
# Copyright (c) 2022-2024 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.
import
eth/common/[headers, hashes],
web3/engine_api_types,
web3/execution_types
const
# maxTrackedPayloads is the maximum number of prepared payloads the execution
# 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.
MaxTrackedPayloads = 10
# maxTrackedHeaders is the maximum number of executed payloads the execution
# 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.
MaxTrackedHeaders = 96
type
QueueItem[T] = object
used: bool
data: T
SimpleQueue[M: static[int]; T] = object
list: array[M, QueueItem[T]]
ExecutionBundle* = object
payload*: ExecutionPayload
blockValue*: UInt256
blobsBundle*: Opt[BlobsBundleV1]
executionRequests*: Opt[array[3, seq[byte]]]
targetBlobsPerBlock*: Opt[Quantity]
PayloadItem = object
id: Bytes8
payload: ExecutionBundle
HeaderItem = object
hash: Hash32
header: Header
PayloadQueue* = object
payloadQueue: SimpleQueue[MaxTrackedPayloads, PayloadItem]
headerQueue: SimpleQueue[MaxTrackedHeaders, HeaderItem]
{.push gcsafe, raises:[].}
# ------------------------------------------------------------------------------
# Private helpers
# ------------------------------------------------------------------------------
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
# ------------------------------------------------------------------------------
# Public functions, setters
# ------------------------------------------------------------------------------
proc put*(api: var PayloadQueue,
hash: Hash32, header: Header) =
api.headerQueue.put(HeaderItem(hash: hash, header: header))
proc put*(api: var PayloadQueue, id: Bytes8,
payload: ExecutionBundle) =
api.payloadQueue.put(PayloadItem(id: id, payload: payload))
# ------------------------------------------------------------------------------
# Public functions, getters
# ------------------------------------------------------------------------------
proc get*(api: PayloadQueue, hash: Hash32,
header: var Header): bool =
for x in api.headerQueue:
if x.hash == hash:
header = x.header
return true
false
proc get*(api: PayloadQueue, id: Bytes8,
payload: var ExecutionBundle): bool =
for x in api.payloadQueue:
if x.id == id:
payload = x.payload
return true
false