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

View File

@ -369,20 +369,22 @@ proc handleFindContent(
portal_find_content_log_distance.observe(
int64(logDistance), labelValues = [$p.protocolId])
# 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:
encodeMessage(ContentMessage(
return encodeMessage(ContentMessage(
contentMessageType: contentType, content: ByteList(content)))
else:
let connectionId = p.stream.addContentRequest(srcId, content)
encodeMessage(ContentMessage(
return encodeMessage(ContentMessage(
contentMessageType: connectionIdType, connectionId: connectionId))
else:
# Don't have the content, send closest neighbours to content id.
# Node does not have the content, or content is not even in radius,
# send closest neighbours to the requested content id.
let
closestNodes = p.routingTable.neighbours(
NodeId(contentId), seenOnly = true)