2022-11-23 16:11:38 +00:00
|
|
|
# nimbus_verified_proxy
|
2023-01-31 12:38:08 +00:00
|
|
|
# Copyright (c) 2022-2023 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
|
|
|
|
|
|
|
import
|
|
|
|
std/tables,
|
|
|
|
web3/ethtypes,
|
2022-12-02 15:09:31 +00:00
|
|
|
stew/[results, keyed_queue],
|
|
|
|
./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
|
2022-12-02 15:09:31 +00:00
|
|
|
blocks: KeyedQueue[BlockHash, 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)
|
|
|
|
return BlockCache(
|
|
|
|
max: maxAsInt,
|
2022-12-02 15:09:31 +00:00
|
|
|
blocks: KeyedQueue[BlockHash, 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:
|
|
|
|
discard self.blocks.shift()
|
|
|
|
|
|
|
|
discard self.blocks.append(payload.blockHash, payload)
|
|
|
|
|
2022-12-02 15:09:31 +00:00
|
|
|
proc latest*(self: BlockCache): results.Opt[ExecutionData] =
|
2022-09-28 08:01:32 +00:00
|
|
|
let latestPair = ? self.blocks.last()
|
|
|
|
return Opt.some(latestPair.data)
|
|
|
|
|
|
|
|
proc getByNumber*(
|
|
|
|
self: BlockCache,
|
2022-12-02 15:09:31 +00:00
|
|
|
number: Quantity): Opt[ExecutionData] =
|
2022-09-28 08:01:32 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
proc getPayloadByHash*(
|
|
|
|
self: BlockCache,
|
2022-12-02 15:09:31 +00:00
|
|
|
hash: BlockHash): Opt[ExecutionData] =
|
2022-11-23 11:46:09 +00:00
|
|
|
return self.blocks.eq(hash)
|