2022-11-23 16:11:38 +00:00
|
|
|
# nimbus_verified_proxy
|
2024-02-15 13:50:25 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-09-28 08:01:32 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2022-09-28 08:01:32 +00:00
|
|
|
|
2024-10-16 12:18:02 +00:00
|
|
|
import eth/common/hashes, web3/primitives, stew/keyed_queue, results, ./rpc/rpc_utils
|
2022-09-28 08:01:32 +00:00
|
|
|
|
2022-11-23 16:11:38 +00:00
|
|
|
## Cache for payloads received through block gossip and validated by the
|
|
|
|
## consensus light client.
|
|
|
|
## The payloads are stored in order of arrival. When the cache is full, the
|
|
|
|
## oldest payload is deleted first.
|
2022-09-28 08:01:32 +00:00
|
|
|
type BlockCache* = ref object
|
|
|
|
max: int
|
2024-10-16 12:18:02 +00:00
|
|
|
blocks: KeyedQueue[Hash32, ExecutionData]
|
2022-09-28 08:01:32 +00:00
|
|
|
|
|
|
|
proc `==`(x, y: Quantity): bool {.borrow, noSideEffect.}
|
|
|
|
|
|
|
|
proc new*(T: type BlockCache, max: uint32): T =
|
|
|
|
let maxAsInt = int(max)
|
2024-10-16 12:18:02 +00:00
|
|
|
return
|
|
|
|
BlockCache(max: maxAsInt, blocks: KeyedQueue[Hash32, ExecutionData].init(maxAsInt))
|
2022-09-28 08:01:32 +00:00
|
|
|
|
|
|
|
func len*(self: BlockCache): int =
|
|
|
|
return len(self.blocks)
|
|
|
|
|
|
|
|
func isEmpty*(self: BlockCache): bool =
|
|
|
|
return len(self.blocks) == 0
|
|
|
|
|
2022-12-02 15:09:31 +00:00
|
|
|
proc add*(self: BlockCache, payload: ExecutionData) =
|
2022-09-28 08:01:32 +00:00
|
|
|
if self.blocks.hasKey(payload.blockHash):
|
|
|
|
return
|
|
|
|
|
|
|
|
if len(self.blocks) >= self.max:
|
2024-02-15 13:50:25 +00:00
|
|
|
discard self.blocks.shift()
|
2022-09-28 08:01:32 +00:00
|
|
|
|
|
|
|
discard self.blocks.append(payload.blockHash, payload)
|
|
|
|
|
2022-12-02 15:09:31 +00:00
|
|
|
proc latest*(self: BlockCache): results.Opt[ExecutionData] =
|
2024-02-15 13:50:25 +00:00
|
|
|
let latestPair = ?self.blocks.last()
|
2022-09-28 08:01:32 +00:00
|
|
|
return Opt.some(latestPair.data)
|
|
|
|
|
2024-02-15 13:50:25 +00:00
|
|
|
proc getByNumber*(self: BlockCache, number: Quantity): Opt[ExecutionData] =
|
2022-12-02 15:09:31 +00:00
|
|
|
var payloadResult: Opt[ExecutionData]
|
2022-09-28 08:01:32 +00:00
|
|
|
|
|
|
|
for payload in self.blocks.prevValues:
|
|
|
|
if payload.blockNumber == number:
|
|
|
|
payloadResult = Opt.some(payload)
|
|
|
|
break
|
|
|
|
|
|
|
|
return payloadResult
|
2022-11-23 11:46:09 +00:00
|
|
|
|
2024-10-16 12:18:02 +00:00
|
|
|
proc getPayloadByHash*(self: BlockCache, hash: Hash32): Opt[ExecutionData] =
|
2022-11-23 11:46:09 +00:00
|
|
|
return self.blocks.eq(hash)
|