maticnetwork / bor

Official repository for the Polygon Blockchain
https://polygon.technology/
GNU Lesser General Public License v3.0
986 stars 476 forks source link

eth/internal: add `debug.peerStats` for stats related to all connected peers #1252

Closed manav2401 closed 1 month ago

manav2401 commented 1 month ago

Description

This PR implements a new RPC/IPC call named debug.peerStats which can be used to get additional block related information from all the connected peers.

More specifically, it emits hash, number and td (total difficulty) of the head block of all the peers along with some metadata and identifiers. Note that the number field is derived locally and isn't maintained in the peer's struct. It may be a non-zero block number if we have the hash locally else 0.

Example: IPC

> debug.peerStats()
[{
    enode: "<enode>",
    hash: "0x8147d592629352a275b7dab82f8e0ff9d8a77bb63f3e1f833d7b7bd91366d464",
    id: "<id>",
    name: "bor/v1.2.8/linux-amd...",
    td: 1057759962,
    number: 0
}]

RPC: curl http://localhost:8545 -X POST --data '{"jsonrpc":"2.0","method":"debug_peerStats","params":[],"id":1}' -H "Content-Type: application/json" | jq

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": [
    {
      "enode": "<enode>",
      "id": "<id>",
      "name": "bor/v1.2.8/linux-amd...",
      "hash": "0x64c8ae56ad588565ea98ed807c3dd8298b74e66c31d2e4748548a1f7421f4d6f",
      "td": 1057761180,
      "number": 0
    }
  ]
}

Changes

Checklist

Cross repository changes

Testing

Manual tests

Please complete this section with the steps you performed if you ran manual tests for this functionality, otherwise delete it