Add score to peers RPC call
This commit is contained in:
parent
8a7cdc61f6
commit
2dc1a8508e
|
@ -972,7 +972,7 @@ proc queryRandom*(
|
|||
d.rng[].shuffle(filtered)
|
||||
return filtered.sortedByIt(-it[0]).mapIt(it[1])
|
||||
|
||||
proc trimConnections(node: Eth2Node, count: int) {.async.} =
|
||||
proc getPeersScores*(node: Eth2Node): OrderedTable[PeerId, int] =
|
||||
# Kill `count` peers, scoring them to remove the least useful ones
|
||||
|
||||
var scores = initOrderedTable[PeerID, int]()
|
||||
|
@ -1004,7 +1004,7 @@ proc trimConnections(node: Eth2Node, count: int) {.async.} =
|
|||
|
||||
if peerCount == 0: continue
|
||||
|
||||
for peer in node.pubsub.mesh[topic]:
|
||||
for peer in node.pubsub.mesh.getOrDefault(topic):
|
||||
if peer.peerId notin scores: continue
|
||||
|
||||
# Divide by the number of connections
|
||||
|
@ -1013,13 +1013,21 @@ proc trimConnections(node: Eth2Node, count: int) {.async.} =
|
|||
connCount = node.switch.connmanager.connCount(peer.peerId)
|
||||
thisPeersScore = scorePerPeer div max(1, connCount)
|
||||
|
||||
scores[peer.peerId] = scores[peer.peerId] + thisPeersScore
|
||||
scores[peer.peerId] =
|
||||
scores.getOrDefault(peer.peerId) + thisPeersScore
|
||||
|
||||
proc sortPerScore(a, b: (PeerID, int)): int =
|
||||
system.cmp(a[1], b[1])
|
||||
|
||||
scores.sort(sortPerScore)
|
||||
|
||||
return scores
|
||||
|
||||
|
||||
proc trimConnections(node: Eth2Node, count: int) {.async.} =
|
||||
# Kill `count` peers, scoring them to remove the least useful ones
|
||||
|
||||
var scores = node.getPeersScores()
|
||||
var toKick = count
|
||||
|
||||
for peerId in scores.keys:
|
||||
|
|
|
@ -191,6 +191,7 @@ proc installNodeApiHandlers*(router: var RestRouter, node: BeaconNode) =
|
|||
dres.get()
|
||||
|
||||
var res: seq[RpcNodePeer]
|
||||
let scores = node.network.getPeersScores()
|
||||
for peer in node.network.peers.values():
|
||||
if (peer.connectionState in connectionMask) and
|
||||
(peer.direction in directionMask):
|
||||
|
@ -200,8 +201,9 @@ proc installNodeApiHandlers*(router: var RestRouter, node: BeaconNode) =
|
|||
last_seen_p2p_address: getLastSeenAddress(node, peer.peerId),
|
||||
state: peer.connectionState.toString(),
|
||||
direction: peer.direction.toString(),
|
||||
agent: node.network.switch.peerStore.agentBook.get(peer.peerId), # Fields `agent` and `proto` are not
|
||||
proto: node.network.switch.peerStore.protoVersionBook.get(peer.peerId) # part of specification
|
||||
agent: node.network.switch.peerStore.agentBook.get(peer.peerId), # Fields `agent` and `proto`, `score`
|
||||
proto: node.network.switch.peerStore.protoVersionBook.get(peer.peerId),# are not part of specification
|
||||
score: scores.getOrDefault(peer.peerId)
|
||||
)
|
||||
res.add(peer)
|
||||
return RestApiResponse.jsonResponseWMeta(res, (count: uint64(len(res))))
|
||||
|
|
|
@ -189,6 +189,7 @@ proc installNodeApiHandlers*(rpcServer: RpcServer, node: BeaconNode) {.
|
|||
raise newException(CatchableError, "Incorrect direction parameter")
|
||||
let states = rstates.get()
|
||||
let dirs = rdirs.get()
|
||||
let scores = node.network.getPeersScores()
|
||||
for peer in node.network.peers.values():
|
||||
if (peer.connectionState in states) and (peer.direction in dirs):
|
||||
let resPeer = (
|
||||
|
@ -197,8 +198,9 @@ proc installNodeApiHandlers*(rpcServer: RpcServer, node: BeaconNode) {.
|
|||
last_seen_p2p_address: getLastSeenAddress(node, peer.peerId),
|
||||
state: peer.connectionState.toString(),
|
||||
direction: peer.direction.toString(),
|
||||
agent: node.network.switch.peerStore.agentBook.get(peer.peerId), # Fields `agent` and `proto` are not
|
||||
proto: node.network.switch.peerStore.protoVersionBook.get(peer.peerId) # part of specification
|
||||
agent: node.network.switch.peerStore.agentBook.get(peer.peerId), # Fields `agent`, `proto` and `score`
|
||||
proto: node.network.switch.peerStore.protoVersionBook.get(peer.peerId),# are not part of specification
|
||||
score: scores.getOrDefault(peer.peerId)
|
||||
)
|
||||
res.add(resPeer)
|
||||
return res
|
||||
|
@ -236,8 +238,9 @@ proc installNodeApiHandlers*(rpcServer: RpcServer, node: BeaconNode) {.
|
|||
last_seen_p2p_address: getLastSeenAddress(node, peer.peerId),
|
||||
state: peer.connectionState.toString(),
|
||||
direction: peer.direction.toString(),
|
||||
agent: node.network.switch.peerStore.agentBook.get(peer.peerId), # Fields `agent` and `proto` are not
|
||||
proto: node.network.switch.peerStore.protoVersionBook.get(peer.peerId) # part of specification
|
||||
agent: node.network.switch.peerStore.agentBook.get(peer.peerId), # Fields `agent`, `proto` and `score`
|
||||
proto: node.network.switch.peerStore.protoVersionBook.get(peer.peerId),# are not part of specification
|
||||
score: -1 # Isn't available for this call
|
||||
)
|
||||
|
||||
rpcServer.rpc("get_v1_node_version") do () -> JsonNode:
|
||||
|
|
|
@ -70,6 +70,7 @@ type
|
|||
direction: string
|
||||
agent: string # This is not part of specification
|
||||
proto: string # This is not part of specification
|
||||
score: int # This is not part of specification
|
||||
|
||||
RpcNodePeerCount* = tuple
|
||||
disconnected: int
|
||||
|
|
Loading…
Reference in New Issue