|
| 1 | +# Nimbus |
| 2 | +# Copyright (c) 2025 Status Research & Development GmbH |
| 3 | +# Licensed and distributed under either of |
| 4 | +# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). |
| 5 | +# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). |
| 6 | +# at your option. This file may not be copied, modified, or distributed except according to those terms. |
| 7 | + |
| 8 | +{.push raises: [].} |
| 9 | + |
| 10 | +import |
| 11 | + json_rpc/rpcserver, |
| 12 | + json_serialization/std/tables, |
| 13 | + stew/byteutils, |
| 14 | + ../common/common_types, |
| 15 | + ../network/wire/portal_protocol, |
| 16 | + ../network/finalized_history/finalized_history_content, |
| 17 | + ../network/finalized_history/finalized_history_validation, |
| 18 | + ./rpc_types |
| 19 | + |
| 20 | +export tables |
| 21 | + |
| 22 | +# Portal Finalized History Network JSON-RPC API |
| 23 | +# Note: |
| 24 | +# - This API is not part of the Portal Network specification yet. |
| 25 | +# - Lower level API calls are not implemented as they are typically only used for (Hive) |
| 26 | +# testing and it is not clear yet of this will be needed in the future. |
| 27 | +# - Added the header parameter so that validation can happen on json-rpc server side, |
| 28 | +# but it could also be moved to client side. |
| 29 | +# - Could also make a less generic API |
| 30 | + |
| 31 | +ContentInfo.useDefaultSerializationIn JrpcConv |
| 32 | +TraceContentLookupResult.useDefaultSerializationIn JrpcConv |
| 33 | +TraceObject.useDefaultSerializationIn JrpcConv |
| 34 | +NodeMetadata.useDefaultSerializationIn JrpcConv |
| 35 | +TraceResponse.useDefaultSerializationIn JrpcConv |
| 36 | + |
| 37 | +# TODO: It would be cleaner to use the existing getContent/getBlockBody/getReceipts calls for |
| 38 | +# less code duplication + automatic retries, but the specific error messages + extra content |
| 39 | +# info would need to be added to the existing calls. |
| 40 | +proc installPortalFinalizedHistoryApiHandlers*( |
| 41 | + rpcServer: RpcServer, p: PortalProtocol |
| 42 | +) = |
| 43 | + rpcServer.rpc("portal_finalizedHistoryGetContent") do( |
| 44 | + contentKeyBytes: string, headerBytes: string |
| 45 | + ) -> ContentInfo: |
| 46 | + let |
| 47 | + contentKeyByteList = ContentKeyByteList.init(hexToSeqByte(contentKeyBytes)) |
| 48 | + contentKey = decode(contentKeyByteList).valueOr: |
| 49 | + raise invalidKeyErr() |
| 50 | + contentId = toContentId(contentKey) |
| 51 | + header = decodeRlp(hexToSeqByte(headerBytes), Header).valueOr: |
| 52 | + raise invalidRequest((code: -39005, msg: "Failed to decode header: " & error)) |
| 53 | + |
| 54 | + p.getLocalContent(contentKeyByteList, contentId).isErrOr: |
| 55 | + return ContentInfo(content: value.to0xHex(), utpTransfer: false) |
| 56 | + |
| 57 | + let contentLookupResult = (await p.contentLookup(contentKeyByteList, contentId)).valueOr: |
| 58 | + raise contentNotFoundErr() |
| 59 | + |
| 60 | + validateContent(contentKey, contentLookupResult.content, header).isOkOr: |
| 61 | + p.banNode( |
| 62 | + contentLookupResult.receivedFrom.id, |
| 63 | + NodeBanDurationContentLookupFailedValidation, |
| 64 | + ) |
| 65 | + raise invalidValueErr() |
| 66 | + |
| 67 | + p.storeContent( |
| 68 | + contentKeyByteList, contentId, contentLookupResult.content, cacheContent = true |
| 69 | + ) |
| 70 | + |
| 71 | + ContentInfo( |
| 72 | + content: contentLookupResult.content.to0xHex(), |
| 73 | + utpTransfer: contentLookupResult.utpTransfer, |
| 74 | + ) |
| 75 | + |
| 76 | + rpcServer.rpc("portal_finalizedHistoryTraceGetContent") do( |
| 77 | + contentKeyBytes: string, headerBytes: string |
| 78 | + ) -> TraceContentLookupResult: |
| 79 | + let |
| 80 | + contentKeyByteList = ContentKeyByteList.init(hexToSeqByte(contentKeyBytes)) |
| 81 | + contentKey = decode(contentKeyByteList).valueOr: |
| 82 | + raise invalidKeyErr() |
| 83 | + contentId = toContentId(contentKey) |
| 84 | + header = decodeRlp(hexToSeqByte(headerBytes), Header).valueOr: |
| 85 | + raise invalidRequest((code: -39005, msg: "Failed to decode header: " & error)) |
| 86 | + |
| 87 | + p.getLocalContent(contentKeyByteList, contentId).isErrOr: |
| 88 | + return TraceContentLookupResult( |
| 89 | + content: Opt.some(value), |
| 90 | + utpTransfer: false, |
| 91 | + trace: TraceObject( |
| 92 | + origin: p.localNode.id, |
| 93 | + targetId: contentId, |
| 94 | + receivedFrom: Opt.some(p.localNode.id), |
| 95 | + ), |
| 96 | + ) |
| 97 | + |
| 98 | + # TODO: Might want to restructure the lookup result here. Potentially doing |
| 99 | + # the json conversion in this module. |
| 100 | + let |
| 101 | + res = await p.traceContentLookup(contentKeyByteList, contentId) |
| 102 | + valueBytes = res.content.valueOr: |
| 103 | + let data = Opt.some(JrpcConv.encode(res.trace).JsonString) |
| 104 | + raise contentNotFoundErrWithTrace(data) |
| 105 | + |
| 106 | + validateContent(contentKey, valueBytes, header).isOkOr: |
| 107 | + if res.trace.receivedFrom.isSome(): |
| 108 | + p.banNode( |
| 109 | + res.trace.receivedFrom.get(), NodeBanDurationContentLookupFailedValidation |
| 110 | + ) |
| 111 | + raise invalidValueErr() |
| 112 | + |
| 113 | + p.storeContent(contentKeyByteList, contentId, valueBytes, cacheContent = true) |
| 114 | + |
| 115 | + res |
| 116 | + |
| 117 | + rpcServer.rpc("portal_finalizedHistoryPutContent") do( |
| 118 | + contentKeyBytes: string, contentValueBytes: string |
| 119 | + ) -> PutContentResult: |
| 120 | + let |
| 121 | + contentKeyByteList = ContentKeyByteList.init(hexToSeqByte(contentKeyBytes)) |
| 122 | + _ = decode(contentKeyByteList).valueOr: |
| 123 | + raise invalidKeyErr() |
| 124 | + offerValueBytes = hexToSeqByte(contentValueBytes) |
| 125 | + |
| 126 | + # Note: Not validating content as this would have a high impact on bridge |
| 127 | + # gossip performance. |
| 128 | + # As no validation is done here, the content is not stored locally. |
| 129 | + # TODO: Add default on validation by optional validation parameter. |
| 130 | + gossipMetadata = await p.neighborhoodGossip( |
| 131 | + Opt.none(NodeId), |
| 132 | + ContentKeysList(@[contentKeyByteList]), |
| 133 | + @[offerValueBytes], |
| 134 | + enableNodeLookup = true, |
| 135 | + ) |
| 136 | + |
| 137 | + PutContentResult( |
| 138 | + storedLocally: false, |
| 139 | + peerCount: gossipMetadata.successCount, |
| 140 | + acceptMetadata: AcceptMetadata( |
| 141 | + acceptedCount: gossipMetadata.acceptedCount, |
| 142 | + genericDeclineCount: gossipMetadata.genericDeclineCount, |
| 143 | + alreadyStoredCount: gossipMetadata.alreadyStoredCount, |
| 144 | + notWithinRadiusCount: gossipMetadata.notWithinRadiusCount, |
| 145 | + rateLimitedCount: gossipMetadata.rateLimitedCount, |
| 146 | + transferInProgressCount: gossipMetadata.transferInProgressCount, |
| 147 | + ), |
| 148 | + ) |
0 commit comments