Skip to content

Commit b16ccf1

Browse files
authored
Portal: Cleanup JSON-RPC InvalidRequest usage (#3858)
1 parent 2e07bc5 commit b16ccf1

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

portal/rpc/rpc_portal_beacon_api.nim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ proc installPortalBeaconApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
7777
let
7878
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
7979
contentId = p.toContentId(keyBytes).valueOr:
80-
raise invalidKeyErr()
80+
raise invalidContentKeyError()
8181

8282
contentLookupResult = (await p.contentLookup(keyBytes, contentId)).valueOr:
8383
raise contentNotFoundErr()
@@ -91,7 +91,7 @@ proc installPortalBeaconApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
9191
let
9292
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
9393
contentId = p.toContentId(keyBytes).valueOr:
94-
raise invalidKeyErr()
94+
raise invalidContentKeyError()
9595

9696
# TODO: Might want to restructure the lookup result here. Potentially doing
9797
# the json conversion in this module.
@@ -110,7 +110,7 @@ proc installPortalBeaconApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
110110
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
111111
offerValueBytes = hexToSeqByte(contentValue)
112112
contentId = p.toContentId(keyBytes).valueOr:
113-
raise invalidKeyErr()
113+
raise invalidContentKeyError()
114114

115115
# TODO: Do we need to convert the received offer to a value without proofs before storing?
116116
p.storeContent(keyBytes, contentId, offerValueBytes)
@@ -119,7 +119,7 @@ proc installPortalBeaconApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
119119
let
120120
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
121121
contentId = p.toContentId(keyBytes).valueOr:
122-
raise invalidKeyErr()
122+
raise invalidContentKeyError()
123123

124124
valueBytes = p.dbGet(keyBytes, contentId).valueOr:
125125
raise contentNotFoundErr()
@@ -132,7 +132,7 @@ proc installPortalBeaconApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
132132
let
133133
keyBytes = ContentKeyByteList.init(hexToSeqByte(contentKey))
134134
_ = p.toContentId(keyBytes).valueOr:
135-
raise invalidKeyErr()
135+
raise invalidContentKeyError()
136136
offerValueBytes = hexToSeqByte(contentValue)
137137

138138
# TODO: Do we need to convert the received offer to a value without proofs before storing?

portal/rpc/rpc_portal_history_api.nim

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, n: HistoryNetwork) =
4545
let
4646
contentKeyByteList = ContentKeyByteList.init(hexToSeqByte(contentKeyBytes))
4747
contentKey = decode(contentKeyByteList).valueOr:
48-
raise invalidKeyErr()
48+
raise invalidContentKeyError()
4949
contentId = toContentId(contentKey)
5050

5151
n.portalProtocol.getLocalContent(contentKeyByteList, contentId).isErrOr:
@@ -66,7 +66,7 @@ proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, n: HistoryNetwork) =
6666
let
6767
contentKeyByteList = ContentKeyByteList.init(hexToSeqByte(contentKeyBytes))
6868
contentKey = decode(contentKeyByteList).valueOr:
69-
raise invalidKeyErr()
69+
raise invalidContentKeyError()
7070
contentId = toContentId(contentKey)
7171

7272
n.portalProtocol.getLocalContent(contentKeyByteList, contentId).isErrOr:
@@ -96,7 +96,7 @@ proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, n: HistoryNetwork) =
9696
let
9797
contentKeyByteList = ContentKeyByteList.init(hexToSeqByte(contentKeyBytes))
9898
_ = decode(contentKeyByteList).valueOr:
99-
raise invalidKeyErr()
99+
raise invalidContentKeyError()
100100
offerValueBytes = hexToSeqByte(contentValueBytes)
101101

102102
# Note: Not validating content as this would have a high impact on bridge
@@ -128,15 +128,15 @@ proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, n: HistoryNetwork) =
128128
contentKeyByteList = ContentKeyByteList.init(hexToSeqByte(contentKeyBytes))
129129
offerValueBytes = hexToSeqByte(contentValueBytes)
130130
contentId = n.portalProtocol.toContentId(contentKeyByteList).valueOr:
131-
raise invalidKeyErr()
131+
raise invalidContentKeyError()
132132

133133
n.portalProtocol.storeContent(contentKeyByteList, contentId, offerValueBytes)
134134

135135
rpcServer.rpc("portal_historyLocalContent") do(contentKeyBytes: string) -> string:
136136
let
137137
contentKeyByteList = ContentKeyByteList.init(hexToSeqByte(contentKeyBytes))
138138
contentId = n.portalProtocol.toContentId(contentKeyByteList).valueOr:
139-
raise invalidKeyErr()
139+
raise invalidContentKeyError()
140140

141141
valueBytes = n.portalProtocol.getLocalContent(contentKeyByteList, contentId).valueOr:
142142
raise contentNotFoundErr()
@@ -145,7 +145,7 @@ proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, n: HistoryNetwork) =
145145

146146
rpcServer.rpc("portal_historyGetBlockBody") do(headerBytes: string) -> string:
147147
let header = decodeRlp(hexToSeqByte(headerBytes), Header).valueOr:
148-
raise invalidRequest((code: -39005, msg: "Failed to decode header: " & error))
148+
raise applicationError((code: -39010, msg: "Failed to decode header: " & error))
149149

150150
let blockBody = (await n.getBlockBody(header)).valueOr:
151151
raise contentNotFoundErr()
@@ -154,7 +154,7 @@ proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, n: HistoryNetwork) =
154154

155155
rpcServer.rpc("portal_historyGetReceipts") do(headerBytes: string) -> string:
156156
let header = decodeRlp(hexToSeqByte(headerBytes), Header).valueOr:
157-
raise invalidRequest((code: -39005, msg: "Failed to decode header: " & error))
157+
raise applicationError((code: -39010, msg: "Failed to decode header: " & error))
158158

159159
let receipts = (await n.getReceipts(header)).valueOr:
160160
raise contentNotFoundErr()

portal/rpc/rpc_types.nim

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export jsonmarshal, routing_table, enr, node, results
2020
# Portal Network JSON-RPC errors
2121

2222
const
23-
# These errors are defined in the portal jsonrpc spec: https://github.com/ethereum/portal-network-specs/tree/master/jsonrpc
23+
# These errors (code + msg) are defined in the Portal JSON-RPC spec:
24+
# https://github.com/ethereum/portal-network-specs/tree/master/jsonrpc
2425
ContentNotFoundError* = (code: -39001, msg: "Content not found")
2526
ContentNotFoundErrorWithTrace* = (code: -39002, msg: "Content not found")
2627
PayloadTypeNotSupportedError* = (code: -39004, msg: "Payload type not supported")
@@ -32,11 +33,10 @@ const
3233
msg: "The client has blocked users from specifying the payload for this extension",
3334
)
3435

35-
# These errors are used by Nimbus Portal but are not yet in the spec
36-
InvalidContentKeyError* = (code: -32602, msg: "Invalid content key")
37-
InvalidContentValueError* = (code: -32603, msg: "Invalid content value")
36+
# Errors used by Nimbus Portal but that are not part of current Portal JSON-RPC spec
37+
InvalidContentKeyError* = (code: -39008, msg: "Invalid content key")
3838

39-
template applicationError(error: (int, string)): auto =
39+
template applicationError*(error: (int, string)): auto =
4040
(ref ApplicationError)(code: error.code, msg: error.msg)
4141

4242
template contentNotFoundErr*(): auto =
@@ -65,14 +65,8 @@ template payloadTypeRequiredError*(): auto =
6565
template userSpecifiedPayloadBlockedByClientError*(): auto =
6666
UserSpecifiedPayloadBlockedByClientError.applicationError()
6767

68-
template invalidRequest*(error: (int, string)): auto =
69-
(ref errors.InvalidRequest)(code: error.code, msg: error.msg)
70-
71-
template invalidKeyErr*(): auto =
72-
InvalidContentKeyError.invalidRequest()
73-
74-
template invalidValueErr*(): auto =
75-
InvalidContentValueError.invalidRequest()
68+
template invalidContentKeyError*(): auto =
69+
InvalidContentKeyError.applicationError()
7670

7771
type
7872
NodeInfo* = object

0 commit comments

Comments
 (0)