Skip to content

Commit

Permalink
Address #5539.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheatfate committed Oct 31, 2023
1 parent 173582a commit 935625b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
16 changes: 6 additions & 10 deletions beacon_chain/rpc/rest_node_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -176,24 +176,19 @@ proc installNodeApiHandlers*(router: var RestRouter, node: BeaconNode) =
if state.isErr():
return RestApiResponse.jsonError(Http400, InvalidPeerStateValueError,
$state.error())
let sres = validateState(state.get())
if sres.isErr():
validateState(state.get()).valueOr:
return RestApiResponse.jsonError(Http400, InvalidPeerStateValueError,
$sres.error())
sres.get()
$error)
let directionMask =
block:
if direction.isErr():
return RestApiResponse.jsonError(Http400,
InvalidPeerDirectionValueError,
$direction.error())
let dres = validateDirection(direction.get())
if dres.isErr():
validateDirection(direction.get()).valueOr:
return RestApiResponse.jsonError(Http400,
InvalidPeerDirectionValueError,
$dres.error())
dres.get()

$error)
var res: seq[RestNodePeer]
for peer in node.network.peers.values():
if (peer.connectionState in connectionMask) and
Expand All @@ -209,7 +204,8 @@ proc installNodeApiHandlers*(router: var RestRouter, node: BeaconNode) =
proto: node.network.switch.peerStore[ProtoVersionBook][peer.peerId]
)
res.add(peer)
return RestApiResponse.jsonResponseWMeta(res, (count: uint64(len(res))))
return RestApiResponse.jsonResponseWMeta(res,
(count: RestNumeric(len(res))))

# https://ethereum.github.io/beacon-APIs/#/Node/getPeerCount
router.api(MethodGet, "/eth/v1/node/peer_count") do () -> RestApiResponse:
Expand Down
10 changes: 10 additions & 0 deletions beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,16 @@ proc readValue*(reader: var JsonReader[RestJson], value: var uint8) {.
else:
reader.raiseUnexpectedValue($res.error() & ": " & svalue)

## RestNumeric
proc writeValue*(w: var JsonWriter[RestJson],
value: RestNumeric) {.raises: [IOError].} =
writeValue(w, int(value))

proc readValue*(reader: var JsonReader[RestJson],
value: var RestNumeric) {.
raises: [IOError, SerializationError].} =
value = RestNumeric(reader.readValue(int))

## JustificationBits
proc writeValue*(
w: var JsonWriter[RestJson], value: JustificationBits
Expand Down
2 changes: 2 additions & 0 deletions beacon_chain/spec/eth2_apis/rest_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ type
PeerDirectKind* {.pure.} = enum
Inbound, Outbound

RestNumeric* = distinct int

RestAttesterDuty* = object
pubkey*: ValidatorPubKey
validator_index*: ValidatorIndex
Expand Down
18 changes: 9 additions & 9 deletions ncli/resttest-rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -2899,7 +2899,7 @@
"response": {
"status": {"operator": "equals", "value": "200"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": ""}}}]
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": 0}}}]
}
},
{
Expand All @@ -2911,7 +2911,7 @@
"response": {
"status": {"operator": "equals", "value": "200"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": ""}}}]
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": 0}}}]
}
},
{
Expand All @@ -2923,7 +2923,7 @@
"response": {
"status": {"operator": "equals", "value": "200"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": ""}}}]
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": 0}}}]
}
},
{
Expand All @@ -2935,7 +2935,7 @@
"response": {
"status": {"operator": "equals", "value": "200"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": ""}}}]
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": 0}}}]
}
},
{
Expand All @@ -2947,7 +2947,7 @@
"response": {
"status": {"operator": "equals", "value": "200"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": ""}}}]
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": 0}}}]
}
},
{
Expand All @@ -2959,7 +2959,7 @@
"response": {
"status": {"operator": "equals", "value": "200"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": ""}}}]
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": 0}}}]
}
},
{
Expand All @@ -2971,7 +2971,7 @@
"response": {
"status": {"operator": "equals", "value": "200"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": ""}}}]
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": 0}}}]
}
},
{
Expand All @@ -2983,7 +2983,7 @@
"response": {
"status": {"operator": "equals", "value": "200"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": ""}}}]
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": 0}}}]
}
},
{
Expand All @@ -2995,7 +2995,7 @@
"response": {
"status": {"operator": "equals", "value": "200"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": ""}}}]
"body": [{"operator": "jstructcmpns", "value": {"data": [{"peer_id": "", "enr": "", "last_seen_p2p_address": "", "state": "", "direction": ""}], "meta": {"count": 0}}}]
}
},
{
Expand Down

0 comments on commit 935625b

Please sign in to comment.