Skip to content

Commit 4b63cde

Browse files
authored
Remove direct local content lookup from PortalRpcClient. (#2857)
1 parent 508ce79 commit 4b63cde

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

fluffy/rpc/portal_rpc_client.nim

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,6 @@ func toPortalRpcError(e: ref CatchableError): PortalRpcError =
4848
else:
4949
raiseAssert(e.msg)
5050

51-
proc portal_historyLocalContent(
52-
client: PortalRpcClient, contentKey: string
53-
): Future[Result[string, PortalRpcError]] {.async: (raises: []).} =
54-
try:
55-
let content = await RpcClient(client).portal_historyLocalContent(contentKey)
56-
ok(content)
57-
except CatchableError as e:
58-
err(e.toPortalRpcError())
59-
6051
proc portal_historyGetContent(
6152
client: PortalRpcClient, contentKey: string
6253
): Future[Result[string, PortalRpcError]] {.async: (raises: []).} =
@@ -78,17 +69,6 @@ template valueOrErr[T](res: Result[T, string], error: PortalRpcError): auto =
7869
else:
7970
err(error)
8071

81-
proc historyGetContent(
82-
client: PortalRpcClient, contentKey: string
83-
): Future[Result[string, PortalRpcError]] {.async: (raises: []).} =
84-
# Look up the content from the local db before trying to get it from the network
85-
let content = (await client.portal_historyLocalContent(contentKey)).valueOr:
86-
if error == ContentNotFound:
87-
?await client.portal_historyGetContent(contentKey)
88-
else:
89-
return err(error)
90-
ok(content)
91-
9272
proc historyGetBlockHeader*(
9373
client: PortalRpcClient, blockHash: Hash32, validateContent = true
9474
): Future[Result[Header, PortalRpcError]] {.async: (raises: []).} =
@@ -104,7 +84,7 @@ proc historyGetBlockHeader*(
10484

10585
let
10686
contentKey = blockHeaderContentKey(blockHash).encode().asSeq().to0xHex()
107-
content = ?await client.historyGetContent(contentKey)
87+
content = ?await client.portal_historyGetContent(contentKey)
10888
headerWithProof = decodeSsz(content.toBytes(), BlockHeaderWithProof).valueOr:
10989
return err(InvalidContentValue)
11090
headerBytes = headerWithProof.header.asSeq()
@@ -124,7 +104,7 @@ proc historyGetBlockBody*(
124104

125105
let
126106
contentKey = blockBodyContentKey(blockHash).encode().asSeq().to0xHex()
127-
content = ?await client.historyGetContent(contentKey)
107+
content = ?await client.portal_historyGetContent(contentKey)
128108

129109
if validateContent:
130110
let blockHeader = ?await client.historyGetBlockHeader(blockHash)
@@ -144,7 +124,7 @@ proc historyGetReceipts*(
144124

145125
let
146126
contentKey = receiptsContentKey(blockHash).encode().asSeq().to0xHex()
147-
content = ?await client.historyGetContent(contentKey)
127+
content = ?await client.portal_historyGetContent(contentKey)
148128

149129
if validateContent:
150130
let blockHeader = ?await client.historyGetBlockHeader(blockHash)

fluffy/rpc/rpc_portal_history_api.nim

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,13 @@ proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
8181
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
8282
contentId = p.toContentId(key).valueOr:
8383
raise invalidKeyErr()
84+
maybeContent = p.getLocalContent(key, contentId)
8485

85-
contentResult = (await p.contentLookup(key, contentId)).valueOr:
86-
raise contentNotFoundErr()
86+
if maybeContent.isSome():
87+
return ContentInfo(content: maybeContent.get().to0xHex(), utpTransfer: false)
88+
89+
let contentResult = (await p.contentLookup(key, contentId)).valueOr:
90+
raise contentNotFoundErr()
8791

8892
return ContentInfo(
8993
content: contentResult.content.to0xHex(), utpTransfer: contentResult.utpTransfer
@@ -96,8 +100,12 @@ proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
96100
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
97101
contentId = p.toContentId(key).valueOr:
98102
raise invalidKeyErr()
103+
maybeContent = p.getLocalContent(key, contentId)
104+
105+
if maybeContent.isSome():
106+
return TraceContentLookupResult(content: maybeContent, utpTransfer: false)
99107

100-
res = await p.traceContentLookup(key, contentId)
108+
let res = await p.traceContentLookup(key, contentId)
101109

102110
# TODO: Might want to restructure the lookup result here. Potentially doing
103111
# the json conversion in this module.

0 commit comments

Comments
 (0)