Do a radius check before accessing the db in handleFindContent (#1906)

This commit is contained in:
Kim De Mey 2023-11-24 15:07:23 +01:00 committed by GitHub
parent cf9a44f78e
commit 6d1622875a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 18 deletions

View File

@ -369,27 +369,29 @@ proc handleFindContent(
portal_find_content_log_distance.observe( portal_find_content_log_distance.observe(
int64(logDistance), labelValues = [$p.protocolId]) int64(logDistance), labelValues = [$p.protocolId])
let contentResult = p.dbGet(fc.contentKey, contentId) # Check first if content is in range, as this is a cheaper operation
if p.inRange(contentId):
let contentResult = p.dbGet(fc.contentKey, contentId)
if contentResult.isOk():
let content = contentResult.get()
if content.len <= maxPayloadSize:
return encodeMessage(ContentMessage(
contentMessageType: contentType, content: ByteList(content)))
else:
let connectionId = p.stream.addContentRequest(srcId, content)
if contentResult.isOk(): return encodeMessage(ContentMessage(
let content = contentResult.get() contentMessageType: connectionIdType, connectionId: connectionId))
if content.len <= maxPayloadSize:
encodeMessage(ContentMessage(
contentMessageType: contentType, content: ByteList(content)))
else:
let connectionId = p.stream.addContentRequest(srcId, content)
encodeMessage(ContentMessage( # Node does not have the content, or content is not even in radius,
contentMessageType: connectionIdType, connectionId: connectionId)) # send closest neighbours to the requested content id.
else: let
# Don't have the content, send closest neighbours to content id. closestNodes = p.routingTable.neighbours(
let NodeId(contentId), seenOnly = true)
closestNodes = p.routingTable.neighbours( enrs = truncateEnrs(closestNodes, maxPayloadSize, enrOverhead)
NodeId(contentId), seenOnly = true) portal_content_enrs_packed.observe(enrs.len().int64)
enrs = truncateEnrs(closestNodes, maxPayloadSize, enrOverhead)
portal_content_enrs_packed.observe(enrs.len().int64)
encodeMessage(ContentMessage(contentMessageType: enrsType, enrs: enrs)) encodeMessage(ContentMessage(contentMessageType: enrsType, enrs: enrs))
proc handleOffer(p: PortalProtocol, o: OfferMessage, srcId: NodeId): seq[byte] = proc handleOffer(p: PortalProtocol, o: OfferMessage, srcId: NodeId): seq[byte] =
# Early return when our contentQueue is full. This means there is a backlog # Early return when our contentQueue is full. This means there is a backlog