add getGossipSubPeers RPC (#2107)

This commit is contained in:
Jacek Sieka 2020-11-29 20:07:20 +01:00 committed by GitHub
parent e2d09fca23
commit a2364ce1bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 15 deletions

View File

@ -58,7 +58,7 @@ type
Eth2Node* = ref object of RootObj Eth2Node* = ref object of RootObj
switch*: Switch switch*: Switch
pubsub*: PubSub pubsub*: GossipSub
discovery*: Eth2DiscoveryProtocol discovery*: Eth2DiscoveryProtocol
discoveryEnabled*: bool discoveryEnabled*: bool
wantedPeers*: int wantedPeers*: int
@ -1065,7 +1065,7 @@ proc onConnEvent(node: Eth2Node, peerId: PeerID, event: ConnEvent) {.async.} =
peer.connectionState = Disconnected peer.connectionState = Disconnected
proc init*(T: type Eth2Node, conf: BeaconNodeConf, enrForkId: ENRForkID, proc init*(T: type Eth2Node, conf: BeaconNodeConf, enrForkId: ENRForkID,
switch: Switch, pubsub: PubSub, ip: Option[ValidIpAddress], switch: Switch, pubsub: GossipSub, ip: Option[ValidIpAddress],
tcpPort, udpPort: Port, privKey: keys.PrivateKey, discovery: bool, tcpPort, udpPort: Port, privKey: keys.PrivateKey, discovery: bool,
rng: ref BrHmacDrbgContext): T = rng: ref BrHmacDrbgContext): T =
new result new result
@ -1565,7 +1565,7 @@ proc createEth2Node*(rng: ref BrHmacDrbgContext,
sign = false, sign = false,
verifySignature = false, verifySignature = false,
anonymize = true, anonymize = true,
parameters = params).PubSub parameters = params)
switch.mount(pubsub) switch.mount(pubsub)

View File

@ -5,7 +5,7 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms. # at your option. This file may not be copied, modified, or distributed except according to those terms.
import import
std/[deques, sequtils, strutils], std/[deques, sequtils],
chronos, chronos,
stew/shims/macros, stew/shims/macros,
stew/byteutils, stew/byteutils,
@ -14,7 +14,10 @@ import
rpc_utils, rpc_utils,
../beacon_node_common, ../nimbus_binary_common, ../eth2_network, ../beacon_node_common, ../nimbus_binary_common, ../eth2_network,
../eth1_monitor, ../validator_duties, ../eth1_monitor, ../validator_duties,
../spec/[digest, datatypes, presets] ../spec/[digest, datatypes, presets],
libp2p/protocols/pubsub/[gossipsub, pubsubpeer]
logScope: topics = "nimbusapi" logScope: topics = "nimbusapi"
@ -22,14 +25,12 @@ type
RpcServer = RpcHttpServer RpcServer = RpcHttpServer
Eth1Block = eth1_monitor.Eth1Block Eth1Block = eth1_monitor.Eth1Block
when defined(chronosFutureTracking): FutureInfo* = object
type id*: int
FutureInfo* = object procname*: string
id*: int filename*: string
procname*: string line*: int
filename*: string state*: string
line*: int
state: string
proc installNimbusApiHandlers*(rpcServer: RpcServer, node: BeaconNode) = proc installNimbusApiHandlers*(rpcServer: RpcServer, node: BeaconNode) =
## Install non-standard api handlers - some of these are used by 3rd-parties ## Install non-standard api handlers - some of these are used by 3rd-parties
@ -98,8 +99,8 @@ proc installNimbusApiHandlers*(rpcServer: RpcServer, node: BeaconNode) =
node.chainDag.withState(node.chainDag.tmpState, head.atSlot(wallSlot)): node.chainDag.withState(node.chainDag.tmpState, head.atSlot(wallSlot)):
return node.getBlockProposalEth1Data(state) return node.getBlockProposalEth1Data(state)
when defined(chronosFutureTracking): rpcServer.rpc("getChronosFutures") do () -> seq[FutureInfo]:
rpcServer.rpc("getChronosFutures") do () -> seq[FutureInfo]: when defined(chronosFutureTracking):
var res: seq[FutureInfo] var res: seq[FutureInfo]
for item in pendingFutures(): for item in pendingFutures():
@ -113,3 +114,42 @@ proc installNimbusApiHandlers*(rpcServer: RpcServer, node: BeaconNode) =
) )
return res return res
else:
raise (ref CatchableError)(
msg: "Compile with '-d:chronosFutureTracking' to enable this request")
rpcServer.rpc("getGossipSubPeers") do () -> JsonNode:
var res = newJObject()
var gossipsub = newJObject()
proc toNode(v: PubSubPeer): JsonNode =
%(
peerId: $v.peerId,
score: v.score,
iWantBudget: v.iWantBudget,
iHaveBudget: v.iHaveBudget,
outbound: v.outbound,
appScore: v.appScore,
behaviourPenalty: v.behaviourPenalty
)
for topic, v in node.network.pubsub.gossipsub:
var peers = newJArray()
for peer in v:
peers.add(peer.toNode())
gossipsub.add(topic, peers)
res.add("gossipsub", gossipsub)
var mesh = newJObject()
for topic, v in node.network.pubsub.mesh:
var peers = newJArray()
for peer in v:
peers.add(peer.toNode())
mesh.add(topic, peers)
res.add("mesh", mesh)
return res

View File

@ -278,3 +278,11 @@ Get the current list of live async futures in the process - compile with `-d:chr
``` ```
curl -d '{"jsonrpc":"2.0","id":"id","method":"getChronosFutures","params":[] }' -H 'Content-Type: application/json' localhost:9190 -s | jq '.result | (.[0] | keys_unsorted) as $keys | $keys, map([.[ $keys[] ]])[] | @csv' curl -d '{"jsonrpc":"2.0","id":"id","method":"getChronosFutures","params":[] }' -H 'Content-Type: application/json' localhost:9190 -s | jq '.result | (.[0] | keys_unsorted) as $keys | $keys, map([.[ $keys[] ]])[] | @csv'
``` ```
### getGossipSubPeers
Get the current list of live async futures in the process - compile with `-d:chronosFutureTracking` to enable.
```
curl -d '{"jsonrpc":"2.0","id":"id","method":"getGossipSubPeers","params":[] }' -H 'Content-Type: application/json' localhost:9190 -s | jq '.result'
```