Remove direct local content lookup from PortalRpcClient. (#2857)

This commit is contained in:
bhartnett 2024-11-21 20:20:09 +08:00 committed by GitHub
parent 508ce79cdd
commit 4b63cdeaac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 26 deletions

View File

@ -48,15 +48,6 @@ func toPortalRpcError(e: ref CatchableError): PortalRpcError =
else:
raiseAssert(e.msg)
proc portal_historyLocalContent(
client: PortalRpcClient, contentKey: string
): Future[Result[string, PortalRpcError]] {.async: (raises: []).} =
try:
let content = await RpcClient(client).portal_historyLocalContent(contentKey)
ok(content)
except CatchableError as e:
err(e.toPortalRpcError())
proc portal_historyGetContent(
client: PortalRpcClient, contentKey: string
): Future[Result[string, PortalRpcError]] {.async: (raises: []).} =
@ -78,17 +69,6 @@ template valueOrErr[T](res: Result[T, string], error: PortalRpcError): auto =
else:
err(error)
proc historyGetContent(
client: PortalRpcClient, contentKey: string
): Future[Result[string, PortalRpcError]] {.async: (raises: []).} =
# Look up the content from the local db before trying to get it from the network
let content = (await client.portal_historyLocalContent(contentKey)).valueOr:
if error == ContentNotFound:
?await client.portal_historyGetContent(contentKey)
else:
return err(error)
ok(content)
proc historyGetBlockHeader*(
client: PortalRpcClient, blockHash: Hash32, validateContent = true
): Future[Result[Header, PortalRpcError]] {.async: (raises: []).} =
@ -104,7 +84,7 @@ proc historyGetBlockHeader*(
let
contentKey = blockHeaderContentKey(blockHash).encode().asSeq().to0xHex()
content = ?await client.historyGetContent(contentKey)
content = ?await client.portal_historyGetContent(contentKey)
headerWithProof = decodeSsz(content.toBytes(), BlockHeaderWithProof).valueOr:
return err(InvalidContentValue)
headerBytes = headerWithProof.header.asSeq()
@ -124,7 +104,7 @@ proc historyGetBlockBody*(
let
contentKey = blockBodyContentKey(blockHash).encode().asSeq().to0xHex()
content = ?await client.historyGetContent(contentKey)
content = ?await client.portal_historyGetContent(contentKey)
if validateContent:
let blockHeader = ?await client.historyGetBlockHeader(blockHash)
@ -144,7 +124,7 @@ proc historyGetReceipts*(
let
contentKey = receiptsContentKey(blockHash).encode().asSeq().to0xHex()
content = ?await client.historyGetContent(contentKey)
content = ?await client.portal_historyGetContent(contentKey)
if validateContent:
let blockHeader = ?await client.historyGetBlockHeader(blockHash)

View File

@ -81,9 +81,13 @@ proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
contentId = p.toContentId(key).valueOr:
raise invalidKeyErr()
maybeContent = p.getLocalContent(key, contentId)
contentResult = (await p.contentLookup(key, contentId)).valueOr:
raise contentNotFoundErr()
if maybeContent.isSome():
return ContentInfo(content: maybeContent.get().to0xHex(), utpTransfer: false)
let contentResult = (await p.contentLookup(key, contentId)).valueOr:
raise contentNotFoundErr()
return ContentInfo(
content: contentResult.content.to0xHex(), utpTransfer: contentResult.utpTransfer
@ -96,8 +100,12 @@ proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
contentId = p.toContentId(key).valueOr:
raise invalidKeyErr()
maybeContent = p.getLocalContent(key, contentId)
res = await p.traceContentLookup(key, contentId)
if maybeContent.isSome():
return TraceContentLookupResult(content: maybeContent, utpTransfer: false)
let res = await p.traceContentLookup(key, contentId)
# TODO: Might want to restructure the lookup result here. Potentially doing
# the json conversion in this module.