Skip to content

Commit

Permalink
Rename Fluffy debug rpc methods (#2710)
Browse files Browse the repository at this point in the history
* Rename Fluffy debug rpc methods.

* Organize portal json-rpc errors.
  • Loading branch information
bhartnett authored Oct 8, 2024
1 parent 6f9fc3e commit 72ee610
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 114 deletions.
4 changes: 2 additions & 2 deletions fluffy/docs/the_fluffy_book/docs/history-content-bridging.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ This will store blocks 1 to 10 into a json file located at
`./user_data_dir/eth-history-data.json`.

3. Run Fluffy and trigger the propagation of data with the
`portal_history_propagate` JSON-RPC API call:
`portal_debug_history_propagate` JSON-RPC API call:

```bash
./build/fluffy --rpc --rpc-api:portal,portal_debug

# From another shell
curl -s -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":"1","method":"portal_history_propagate","params":["./user_data_dir/eth-history-data.json"]}' http://localhost:8545 | jq
curl -s -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":"1","method":"portal_debug_history_propagate","params":["./user_data_dir/eth-history-data.json"]}' http://localhost:8545 | jq
```
17 changes: 10 additions & 7 deletions fluffy/rpc/rpc_calls/rpc_portal_debug_calls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ Opt[string].useDefaultSerializationIn JrpcConv

createRpcSigsFromNim(RpcClient):
## Portal History Network json-rpc debug & custom calls
proc portal_historyGossipHeaders(era1File: string, epochRecordFile: Opt[string]): bool
proc portal_historyGossipHeaders(era1File: string): bool
proc portal_historyGossipBlockContent(era1File: string): bool
proc portal_history_storeContent(dataFile: string): bool
proc portal_history_propagate(dataFile: string): bool
proc portal_history_propagateHeaders(dataFile: string): bool
proc portal_history_propagateBlock(dataFile: string, blockHash: string): bool
proc portal_debug_historyGossipHeaders(
era1File: string, epochRecordFile: Opt[string]
): bool

proc portal_debug_historyGossipHeaders(era1File: string): bool
proc portal_debug_historyGossipBlockContent(era1File: string): bool
proc portal_debug_history_storeContent(dataFile: string): bool
proc portal_debug_history_propagate(dataFile: string): bool
proc portal_debug_history_propagateHeaders(dataFile: string): bool
proc portal_debug_history_propagateBlock(dataFile: string, blockHash: string): bool
36 changes: 7 additions & 29 deletions fluffy/rpc/rpc_portal_beacon_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,13 @@ export tables
# Portal Network JSON-RPC implementation as per specification:
# https://github.com/ethereum/portal-network-specs/tree/master/jsonrpc

const
ContentNotFoundError = (code: -39001, msg: "Content not found")
ContentNotFoundErrorWithTrace = (code: -39002, msg: "Content not found")

type ContentInfo = object
content: string
utpTransfer: bool

ContentInfo.useDefaultSerializationIn JrpcConv
TraceContentLookupResult.useDefaultSerializationIn JrpcConv
TraceObject.useDefaultSerializationIn JrpcConv
NodeMetadata.useDefaultSerializationIn JrpcConv
TraceResponse.useDefaultSerializationIn JrpcConv

proc installPortalBeaconApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
let
invalidKeyErr =
(ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
invalidValueErr =
(ref errors.InvalidRequest)(code: -32602, msg: "Invalid content value")

rpcServer.rpc("portal_beaconFindContent") do(
enr: Record, contentKey: string
) -> JsonString:
Expand Down Expand Up @@ -96,12 +82,10 @@ proc installPortalBeaconApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
let
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
contentId = p.toContentId(key).valueOr:
raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
raise invalidKeyErr()

contentResult = (await p.contentLookup(key, contentId)).valueOr:
raise (ref ApplicationError)(
code: ContentNotFoundError.code, msg: ContentNotFoundError.msg
)
raise contentNotFoundErr()

return ContentInfo(
content: contentResult.content.to0xHex(), utpTransfer: contentResult.utpTransfer
Expand All @@ -113,7 +97,7 @@ proc installPortalBeaconApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
let
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
contentId = p.toContentId(key).valueOr:
raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
raise invalidKeyErr()

res = await p.traceContentLookup(key, contentId)

Expand All @@ -123,11 +107,7 @@ proc installPortalBeaconApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
return res
else:
let data = Opt.some(JrpcConv.encode(res.trace).JsonString)
raise (ref ApplicationError)(
code: ContentNotFoundErrorWithTrace.code,
msg: ContentNotFoundErrorWithTrace.msg,
data: data,
)
raise contentNotFoundErrWithTrace(data)

rpcServer.rpc("portal_beaconStore") do(
contentKey: string, contentValue: string
Expand All @@ -141,18 +121,16 @@ proc installPortalBeaconApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
p.storeContent(key, contentId.get(), contentValueBytes)
return true
else:
raise invalidKeyErr
raise invalidKeyErr()

rpcServer.rpc("portal_beaconLocalContent") do(contentKey: string) -> string:
let
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
contentId = p.toContentId(key).valueOr:
raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
raise invalidKeyErr()

contentResult = p.dbGet(key, contentId).valueOr:
raise (ref ApplicationError)(
code: ContentNotFoundError.code, msg: ContentNotFoundError.msg
)
raise contentNotFoundErr()

return contentResult.to0xHex()

Expand Down
16 changes: 7 additions & 9 deletions fluffy/rpc/rpc_portal_debug_history_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ import

export rpcserver

# TODO: perhaps these endpoints should be named differently staring with "portal_debug_"?

# Non-spec-RPCs that are used for testing, debugging and seeding data without a
# bridge.
proc installPortalDebugHistoryApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
## Portal debug API calls related to storage and seeding from Era1 files.
rpcServer.rpc("portal_historyGossipHeaders") do(
rpcServer.rpc("portal_debug_historyGossipHeaders") do(
era1File: string, epochRecordFile: Opt[string]
) -> bool:
let res = await p.historyGossipHeadersWithProof(era1File, epochRecordFile)
Expand All @@ -30,7 +28,7 @@ proc installPortalDebugHistoryApiHandlers*(rpcServer: RpcServer, p: PortalProtoc
else:
raise newException(ValueError, $res.error)

rpcServer.rpc("portal_historyGossipBlockContent") do(era1File: string) -> bool:
rpcServer.rpc("portal_debug_historyGossipBlockContent") do(era1File: string) -> bool:
let res = await p.historyGossipBlockContent(era1File)
if res.isOk():
return true
Expand All @@ -39,28 +37,28 @@ proc installPortalDebugHistoryApiHandlers*(rpcServer: RpcServer, p: PortalProtoc

## Portal debug API calls related to storage and seeding
## TODO: To be removed/replaced with the Era1 versions where applicable.
rpcServer.rpc("portal_history_storeContent") do(dataFile: string) -> bool:
rpcServer.rpc("portal_debug_history_storeContent") do(dataFile: string) -> bool:
let res = p.historyStore(dataFile)
if res.isOk():
return true
else:
raise newException(ValueError, $res.error)

rpcServer.rpc("portal_history_propagate") do(dataFile: string) -> bool:
rpcServer.rpc("portal_debug_history_propagate") do(dataFile: string) -> bool:
let res = await p.historyPropagate(dataFile)
if res.isOk():
return true
else:
raise newException(ValueError, $res.error)

rpcServer.rpc("portal_history_propagateHeaders") do(dataDir: string) -> bool:
rpcServer.rpc("portal_debug_history_propagateHeaders") do(dataDir: string) -> bool:
let res = await p.historyPropagateHeadersWithProof(dataDir)
if res.isOk():
return true
else:
raise newException(ValueError, $res.error)

rpcServer.rpc("portal_history_propagateHeaders") do(
rpcServer.rpc("portal_debug_history_propagateHeaders") do(
epochHeadersFile: string, epochRecordFile: string
) -> bool:
let res =
Expand All @@ -70,7 +68,7 @@ proc installPortalDebugHistoryApiHandlers*(rpcServer: RpcServer, p: PortalProtoc
else:
raise newException(ValueError, $res.error)

rpcServer.rpc("portal_history_propagateBlock") do(
rpcServer.rpc("portal_debug_history_propagateBlock") do(
dataFile: string, blockHash: string
) -> bool:
let res = await p.historyPropagateBlock(dataFile, blockHash)
Expand Down
36 changes: 7 additions & 29 deletions fluffy/rpc/rpc_portal_history_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,13 @@ export tables
# Portal Network JSON-RPC implementation as per specification:
# https://github.com/ethereum/portal-network-specs/tree/master/jsonrpc

const
ContentNotFoundError = (code: -39001, msg: "Content not found")
ContentNotFoundErrorWithTrace = (code: -39002, msg: "Content not found")

type ContentInfo = object
content: string
utpTransfer: bool

ContentInfo.useDefaultSerializationIn JrpcConv
TraceContentLookupResult.useDefaultSerializationIn JrpcConv
TraceObject.useDefaultSerializationIn JrpcConv
NodeMetadata.useDefaultSerializationIn JrpcConv
TraceResponse.useDefaultSerializationIn JrpcConv

proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
let
invalidKeyErr =
(ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
invalidValueErr =
(ref errors.InvalidRequest)(code: -32602, msg: "Invalid content value")

rpcServer.rpc("portal_historyFindContent") do(
enr: Record, contentKey: string
) -> JsonString:
Expand Down Expand Up @@ -96,12 +82,10 @@ proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
let
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
contentId = p.toContentId(key).valueOr:
raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
raise invalidKeyErr()

contentResult = (await p.contentLookup(key, contentId)).valueOr:
raise (ref ApplicationError)(
code: ContentNotFoundError.code, msg: ContentNotFoundError.msg
)
raise contentNotFoundErr()

return ContentInfo(
content: contentResult.content.to0xHex(), utpTransfer: contentResult.utpTransfer
Expand All @@ -113,7 +97,7 @@ proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
let
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
contentId = p.toContentId(key).valueOr:
raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
raise invalidKeyErr()

res = await p.traceContentLookup(key, contentId)

Expand All @@ -123,11 +107,7 @@ proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
return res
else:
let data = Opt.some(JrpcConv.encode(res.trace).JsonString)
raise (ref ApplicationError)(
code: ContentNotFoundErrorWithTrace.code,
msg: ContentNotFoundErrorWithTrace.msg,
data: data,
)
raise contentNotFoundErrWithTrace(data)

rpcServer.rpc("portal_historyStore") do(
contentKey: string, contentValue: string
Expand All @@ -141,18 +121,16 @@ proc installPortalHistoryApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
p.storeContent(key, contentId.get(), contentValueBytes)
return true
else:
raise invalidKeyErr
raise invalidKeyErr()

rpcServer.rpc("portal_historyLocalContent") do(contentKey: string) -> string:
let
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
contentId = p.toContentId(key).valueOr:
raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
raise invalidKeyErr()

contentResult = p.dbGet(key, contentId).valueOr:
raise (ref ApplicationError)(
code: ContentNotFoundError.code, msg: ContentNotFoundError.msg
)
raise contentNotFoundErr()

return contentResult.to0xHex()

Expand Down
40 changes: 9 additions & 31 deletions fluffy/rpc/rpc_portal_state_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,13 @@ export tables
# Portal Network JSON-RPC implementation as per specification:
# https://github.com/ethereum/portal-network-specs/tree/master/jsonrpc

const
ContentNotFoundError = (code: -39001, msg: "Content not found")
ContentNotFoundErrorWithTrace = (code: -39002, msg: "Content not found")

type ContentInfo = object
content: string
utpTransfer: bool

ContentInfo.useDefaultSerializationIn JrpcConv
TraceContentLookupResult.useDefaultSerializationIn JrpcConv
TraceObject.useDefaultSerializationIn JrpcConv
NodeMetadata.useDefaultSerializationIn JrpcConv
TraceResponse.useDefaultSerializationIn JrpcConv

proc installPortalStateApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
let
invalidKeyErr =
(ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
invalidValueErr =
(ref errors.InvalidRequest)(code: -32602, msg: "Invalid content value")

rpcServer.rpc("portal_stateFindContent") do(
enr: Record, contentKey: string
) -> JsonString:
Expand Down Expand Up @@ -97,12 +83,10 @@ proc installPortalStateApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
let
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
contentId = p.toContentId(key).valueOr:
raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
raise invalidKeyErr()

contentResult = (await p.contentLookup(key, contentId)).valueOr:
raise (ref ApplicationError)(
code: ContentNotFoundError.code, msg: ContentNotFoundError.msg
)
raise contentNotFoundErr()

return ContentInfo(
content: contentResult.content.to0xHex(), utpTransfer: contentResult.utpTransfer
Expand All @@ -114,7 +98,7 @@ proc installPortalStateApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
let
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
contentId = p.toContentId(key).valueOr:
raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
raise invalidKeyErr()

res = await p.traceContentLookup(key, contentId)

Expand All @@ -124,11 +108,7 @@ proc installPortalStateApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
return res
else:
let data = Opt.some(JrpcConv.encode(res.trace).JsonString)
raise (ref ApplicationError)(
code: ContentNotFoundErrorWithTrace.code,
msg: ContentNotFoundErrorWithTrace.msg,
data: data,
)
raise contentNotFoundErrWithTrace(data)

rpcServer.rpc("portal_stateStore") do(
contentKey: string, contentValue: string
Expand All @@ -137,11 +117,11 @@ proc installPortalStateApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
contentValueBytes = hexToSeqByte(contentValue)
decodedKey = ContentKey.decode(key).valueOr:
raise invalidKeyErr
raise invalidKeyErr()
valueToStore =
case decodedKey.contentType
of unused:
raise invalidKeyErr
raise invalidKeyErr()
of accountTrieNode:
let offerValue = AccountTrieNodeOffer.decode(contentValueBytes).valueOr:
raise invalidValueErr
Expand All @@ -160,18 +140,16 @@ proc installPortalStateApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) =
p.storeContent(key, contentId.get(), valueToStore)
return true
else:
raise invalidKeyErr
raise invalidKeyErr()

rpcServer.rpc("portal_stateLocalContent") do(contentKey: string) -> string:
let
key = ContentKeyByteList.init(hexToSeqByte(contentKey))
contentId = p.toContentId(key).valueOr:
raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
raise invalidKeyErr()

contentResult = p.dbGet(key, contentId).valueOr:
raise (ref ApplicationError)(
code: ContentNotFoundError.code, msg: ContentNotFoundError.msg
)
raise contentNotFoundErr()

return contentResult.to0xHex()

Expand Down
Loading

0 comments on commit 72ee610

Please sign in to comment.