2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2021-02-26 00:23:22 +00:00
|
|
|
## Copyright (c) 2021 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 std/tables
|
2023-08-22 06:35:16 +00:00
|
|
|
import std/monotimes
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-12 20:23:05 +00:00
|
|
|
import pkg/upraises
|
|
|
|
|
|
|
|
push: {.upraises: [].}
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
2023-07-20 07:56:28 +00:00
|
|
|
import pkg/metrics
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
import ../protobuf/blockexc
|
2022-05-19 22:28:53 +00:00
|
|
|
import ../../blocktype
|
feat: create logging proxy (#663)
* implement a logging proxy
The logging proxy:
- prevents the need to import chronicles (as well as export except toJson),
- prevents the need to override `writeValue` or use or import nim-json-seralization elsewhere in the codebase, allowing for sole use of utils/json for de/serialization,
- and handles json formatting correctly in chronicles json sinks
* Rename logging -> logutils to avoid ambiguity with common names
* clean up
* add setProperty for JsonRecord, remove nim-json-serialization conflict
* Allow specifying textlines and json format separately
Not specifying a LogFormat will apply the formatting to both textlines and json sinks.
Specifying a LogFormat will apply the formatting to only that sink.
* remove unneeded usages of std/json
We only need to import utils/json instead of std/json
* move serialization from rest/json to utils/json so it can be shared
* fix NoColors ambiguity
Was causing unit tests to fail on Windows.
* Remove nre usage to fix Windows error
Windows was erroring with `could not load: pcre64.dll`. Instead of fixing that error, remove the pcre usage :)
* Add logutils module doc
* Shorten logutils.formatIt for `NBytes`
Both json and textlines formatIt were not needed, and could be combined into one formatIt
* remove debug integration test config
debug output and logformat of json for integration test logs
* Use ## module doc to support docgen
* bump nim-poseidon2 to export fromBytes
Before the changes in this branch, fromBytes was likely being resolved by nim-stew, or other dependency. With the changes in this branch, that dependency was removed and fromBytes could no longer be resolved. By exporting fromBytes from nim-poseidon, the correct resolution is now happening.
* fixes to get compiling after rebasing master
* Add support for Result types being logged using formatIt
2024-01-23 07:35:03 +00:00
|
|
|
import ../../logutils
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
logScope:
|
2022-11-15 15:46:21 +00:00
|
|
|
topics = "codex pendingblocks"
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-11-03 15:21:54 +00:00
|
|
|
declareGauge(codex_block_exchange_pending_block_requests, "codex blockexchange pending block requests")
|
|
|
|
declareGauge(codex_block_exchange_retrieval_time_us, "codex blockexchange block retrieval time us")
|
2023-07-20 07:56:28 +00:00
|
|
|
|
2022-05-12 20:09:40 +00:00
|
|
|
const
|
|
|
|
DefaultBlockTimeout* = 10.minutes
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
type
|
2022-11-15 06:12:05 +00:00
|
|
|
BlockReq* = object
|
|
|
|
handle*: Future[Block]
|
|
|
|
inFlight*: bool
|
2023-08-22 06:35:16 +00:00
|
|
|
startTime*: int64
|
2022-11-15 06:12:05 +00:00
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
PendingBlocksManager* = ref object of RootObj
|
2023-11-14 12:02:17 +00:00
|
|
|
blocks*: Table[BlockAddress, BlockReq] # pending Block requests
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-07-20 07:56:28 +00:00
|
|
|
proc updatePendingBlockGauge(p: PendingBlocksManager) =
|
2023-11-03 15:21:54 +00:00
|
|
|
codex_block_exchange_pending_block_requests.set(p.blocks.len.int64)
|
2023-07-20 07:56:28 +00:00
|
|
|
|
2022-05-12 20:09:40 +00:00
|
|
|
proc getWantHandle*(
|
2024-03-12 12:10:14 +00:00
|
|
|
p: PendingBlocksManager,
|
|
|
|
address: BlockAddress,
|
|
|
|
timeout = DefaultBlockTimeout,
|
|
|
|
inFlight = false): Future[Block] {.async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
## Add an event for a block
|
|
|
|
##
|
|
|
|
|
|
|
|
try:
|
2023-11-14 12:02:17 +00:00
|
|
|
if address notin p.blocks:
|
|
|
|
p.blocks[address] = BlockReq(
|
2022-11-15 06:12:05 +00:00
|
|
|
handle: newFuture[Block]("pendingBlocks.getWantHandle"),
|
2023-08-22 06:35:16 +00:00
|
|
|
inFlight: inFlight,
|
|
|
|
startTime: getMonoTime().ticks)
|
2022-11-15 06:12:05 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
trace "Adding pending future for block", address, inFlight = p.blocks[address].inFlight
|
2022-11-15 06:12:05 +00:00
|
|
|
|
2023-07-20 07:56:28 +00:00
|
|
|
p.updatePendingBlockGauge()
|
2023-11-14 12:02:17 +00:00
|
|
|
return await p.blocks[address].handle.wait(timeout)
|
2021-02-26 00:23:22 +00:00
|
|
|
except CancelledError as exc:
|
2023-11-14 12:02:17 +00:00
|
|
|
trace "Blocks cancelled", exc = exc.msg, address
|
2021-02-26 00:23:22 +00:00
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Pending WANT failed or expired", exc = exc.msg
|
2022-11-15 06:12:05 +00:00
|
|
|
# no need to cancel, it is already cancelled by wait()
|
|
|
|
raise exc
|
2021-02-26 00:23:22 +00:00
|
|
|
finally:
|
2023-11-14 12:02:17 +00:00
|
|
|
p.blocks.del(address)
|
2023-07-20 07:56:28 +00:00
|
|
|
p.updatePendingBlockGauge()
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
proc getWantHandle*(
|
2024-03-12 12:10:14 +00:00
|
|
|
p: PendingBlocksManager,
|
|
|
|
cid: Cid,
|
|
|
|
timeout = DefaultBlockTimeout,
|
|
|
|
inFlight = false): Future[Block] =
|
2023-11-14 12:02:17 +00:00
|
|
|
p.getWantHandle(BlockAddress.init(cid), timeout, inFlight)
|
|
|
|
|
|
|
|
proc resolve*(
|
|
|
|
p: PendingBlocksManager,
|
2024-03-12 12:10:14 +00:00
|
|
|
blocksDelivery: seq[BlockDelivery]) {.gcsafe, raises: [].} =
|
2021-02-26 00:23:22 +00:00
|
|
|
## Resolve pending blocks
|
|
|
|
##
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
for bd in blocksDelivery:
|
|
|
|
p.blocks.withValue(bd.address, blockReq):
|
|
|
|
trace "Resolving block", address = bd.address
|
|
|
|
|
|
|
|
if not blockReq.handle.finished:
|
2023-08-22 06:35:16 +00:00
|
|
|
let
|
2023-11-14 12:02:17 +00:00
|
|
|
startTime = blockReq.startTime
|
2023-08-22 06:35:16 +00:00
|
|
|
stopTime = getMonoTime().ticks
|
|
|
|
retrievalDurationUs = (stopTime - startTime) div 1000
|
2023-11-14 12:02:17 +00:00
|
|
|
|
|
|
|
blockReq.handle.complete(bd.blk)
|
2023-11-14 17:52:27 +00:00
|
|
|
|
2023-11-03 15:21:54 +00:00
|
|
|
codex_block_exchange_retrieval_time_us.set(retrievalDurationUs)
|
2023-11-14 12:02:17 +00:00
|
|
|
trace "Block retrieval time", retrievalDurationUs, address = bd.address
|
|
|
|
else:
|
|
|
|
trace "Block handle already finished", address = bd.address
|
|
|
|
do:
|
|
|
|
warn "Attempting to resolve block that's not currently a pending block", address = bd.address
|
2022-11-15 06:12:05 +00:00
|
|
|
|
2024-03-12 12:10:14 +00:00
|
|
|
proc setInFlight*(
|
|
|
|
p: PendingBlocksManager,
|
|
|
|
address: BlockAddress,
|
|
|
|
inFlight = true) =
|
|
|
|
## Set inflight status for a block
|
|
|
|
##
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
p.blocks.withValue(address, pending):
|
2022-11-15 15:46:21 +00:00
|
|
|
pending[].inFlight = inFlight
|
2023-11-14 12:02:17 +00:00
|
|
|
trace "Setting inflight", address, inFlight = pending[].inFlight
|
2022-11-15 06:12:05 +00:00
|
|
|
|
2024-03-12 12:10:14 +00:00
|
|
|
proc isInFlight*(
|
|
|
|
p: PendingBlocksManager,
|
|
|
|
address: BlockAddress): bool =
|
|
|
|
## Check if a block is in flight
|
|
|
|
##
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
p.blocks.withValue(address, pending):
|
2022-11-15 06:12:05 +00:00
|
|
|
result = pending[].inFlight
|
2023-11-14 12:02:17 +00:00
|
|
|
trace "Getting inflight", address, inFlight = result
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-06-22 15:11:18 +00:00
|
|
|
proc contains*(p: PendingBlocksManager, cid: Cid): bool =
|
2023-11-14 12:02:17 +00:00
|
|
|
BlockAddress.init(cid) in p.blocks
|
|
|
|
|
|
|
|
proc contains*(p: PendingBlocksManager, address: BlockAddress): bool =
|
|
|
|
address in p.blocks
|
|
|
|
|
|
|
|
iterator wantList*(p: PendingBlocksManager): BlockAddress =
|
|
|
|
for a in p.blocks.keys:
|
|
|
|
yield a
|
|
|
|
|
|
|
|
iterator wantListBlockCids*(p: PendingBlocksManager): Cid =
|
|
|
|
for a in p.blocks.keys:
|
|
|
|
if not a.leaf:
|
|
|
|
yield a.cid
|
|
|
|
|
|
|
|
iterator wantListCids*(p: PendingBlocksManager): Cid =
|
|
|
|
var yieldedCids = initHashSet[Cid]()
|
|
|
|
for a in p.blocks.keys:
|
|
|
|
let cid = a.cidOrTreeCid
|
|
|
|
if cid notin yieldedCids:
|
|
|
|
yieldedCids.incl(cid)
|
|
|
|
yield cid
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-12 20:09:40 +00:00
|
|
|
iterator wantHandles*(p: PendingBlocksManager): Future[Block] =
|
|
|
|
for v in p.blocks.values:
|
2022-11-15 06:12:05 +00:00
|
|
|
yield v.handle
|
2022-05-12 20:09:40 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
proc wantListLen*(p: PendingBlocksManager): int =
|
|
|
|
p.blocks.len
|
|
|
|
|
2022-05-12 20:09:40 +00:00
|
|
|
func len*(p: PendingBlocksManager): int =
|
|
|
|
p.blocks.len
|
|
|
|
|
2023-06-22 15:11:18 +00:00
|
|
|
func new*(T: type PendingBlocksManager): PendingBlocksManager =
|
|
|
|
PendingBlocksManager()
|