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: [].}
|
|
|
|
|
2023-12-20 02:24:40 +00:00
|
|
|
import pkg/chronicles
|
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
|
2023-11-14 12:02:17 +00:00
|
|
|
import pkg/questionable/results
|
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
|
2023-11-14 12:02:17 +00:00
|
|
|
import ../../merkletree
|
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*(
|
2023-06-22 15:11:18 +00:00
|
|
|
p: PendingBlocksManager,
|
2023-11-14 12:02:17 +00:00
|
|
|
address: BlockAddress,
|
2023-06-22 15:11:18 +00:00
|
|
|
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*(
|
|
|
|
p: PendingBlocksManager,
|
|
|
|
cid: Cid,
|
|
|
|
timeout = DefaultBlockTimeout,
|
|
|
|
inFlight = false
|
|
|
|
): Future[Block] =
|
|
|
|
p.getWantHandle(BlockAddress.init(cid), timeout, inFlight)
|
|
|
|
|
|
|
|
proc resolve*(
|
|
|
|
p: PendingBlocksManager,
|
|
|
|
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
|
|
|
|
2023-06-22 15:11:18 +00:00
|
|
|
proc setInFlight*(p: PendingBlocksManager,
|
2023-11-14 12:02:17 +00:00
|
|
|
address: BlockAddress,
|
2023-06-22 15:11:18 +00:00
|
|
|
inFlight = true) =
|
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
|
|
|
|
2023-06-22 15:11:18 +00:00
|
|
|
proc isInFlight*(p: PendingBlocksManager,
|
2023-11-14 12:02:17 +00:00
|
|
|
address: BlockAddress,
|
2023-06-22 15:11:18 +00:00
|
|
|
): bool =
|
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()
|