Skip to content

Commit

Permalink
Review comments by Dmitriy
Browse files Browse the repository at this point in the history
  • Loading branch information
benbierens committed Dec 5, 2024
1 parent 64e691b commit ed3e91c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
64 changes: 35 additions & 29 deletions codex/blockexchange/engine/engine.nim
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ proc requestBlock*(
asyncSpawn b.monitorBlockHandle(blockFuture, address, selected.id)

Check warning on line 194 in codex/blockexchange/engine/engine.nim

View check run for this annotation

Codecov / codecov/patch

codex/blockexchange/engine/engine.nim#L194

Added line #L194 was not covered by tests
b.pendingBlocks.setInFlight(address)
await b.sendWantBlock(@[address], selected)

Check warning on line 196 in codex/blockexchange/engine/engine.nim

View check run for this annotation

Codecov / codecov/patch

codex/blockexchange/engine/engine.nim#L196

Added line #L196 was not covered by tests

await b.sendWantHave(@[address], peers.without)

# Don't let timeouts bubble up. We can't be too broad here or we break
Expand Down Expand Up @@ -399,33 +399,15 @@ proc wantListHandler*(
address = e.address
wantType = $e.wantType

# Update metrics
if e.wantType == WantType.WantHave:
codex_block_exchange_want_have_lists_received.inc()
elif e.wantType == WantType.WantBlock:
codex_block_exchange_want_block_lists_received.inc()

# Update peerCtx
if idx < 0: # new entry
peerCtx.peerWants.add(e)

let
have = await e.address in b.localStore
price = @(
b.pricing.get(Pricing(price: 0.u256))
.price.toBytesBE)

if e.wantType == WantType.WantHave:
codex_block_exchange_want_have_lists_received.inc()
elif e.wantType == WantType.WantBlock:
codex_block_exchange_want_block_lists_received.inc()

if not have and e.sendDontHave:
presence.add(
BlockPresence(
address: e.address,
`type`: BlockPresenceType.DontHave,
price: price))
elif have and e.wantType == WantType.WantHave:
presence.add(
BlockPresence(
address: e.address,
`type`: BlockPresenceType.Have,
price: price))

else: # update existing entry
# peer doesn't want this block anymore
if e.cancel:
Expand All @@ -435,13 +417,37 @@ proc wantListHandler*(
# different want params
peerCtx.peerWants[idx] = e # update entry

if not e.cancel:
# Respond to wantHaves immediately
if e.wantType == WantType.WantHave:
let
have = await e.address in b.localStore
price = @(
b.pricing.get(Pricing(price: 0.u256))
.price.toBytesBE)

if not have and e.sendDontHave:
presence.add(
BlockPresence(
address: e.address,
`type`: BlockPresenceType.DontHave,
price: price))
elif have:
presence.add(
BlockPresence(
address: e.address,
`type`: BlockPresenceType.Have,
price: price))

# Schedule handling of wantBlocks
if e.wantType == WantType.WantBlock:
if not b.scheduleTask(peerCtx):
warn "Unable to schedule task for peer", peer

if presence.len > 0:
trace "Sending presence to remote", items = presence.mapIt($it).join(",")
await b.network.request.sendPresence(peer, presence)

if not b.scheduleTask(peerCtx):
warn "Unable to schedule task for peer", peer

proc accountHandler*(
engine: BlockExcEngine,
peer: PeerId,
Expand Down
2 changes: 1 addition & 1 deletion codex/blockexchange/peers/peerctxstore.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ logScope:
type
PeerCtxStore* = ref object of RootObj
peers*: OrderedTable[PeerId, BlockExcPeerCtx]
PeersForBlock* = ref object of RootObj
PeersForBlock* = object of RootObj
with*: seq[BlockExcPeerCtx]
without*: seq[BlockExcPeerCtx]

Expand Down

0 comments on commit ed3e91c

Please sign in to comment.