Skip to content

Commit

Permalink
Merge 42aeb9f into d8ec024
Browse files Browse the repository at this point in the history
  • Loading branch information
dadepo authored Mar 23, 2022
2 parents d8ec024 + 42aeb9f commit fe831b9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
34 changes: 28 additions & 6 deletions packages/lodestar/src/api/impl/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {routes} from "@chainsafe/lodestar-api";
import {createKeypairFromPeerId} from "@chainsafe/discv5";
import {formatNodePeer} from "./utils";
import {formatNodePeer, getRevelantConnection} from "./utils";
import {ApiError} from "../errors";
import {ApiModules} from "../types";
import {IApiOptions} from "../../options";
Expand Down Expand Up @@ -51,13 +51,35 @@ export function getNodeApi(opts: IApiOptions, {network, sync}: Pick<ApiModules,
},

async getPeerCount() {
// TODO: Implement
// TODO: Implement disconnect count with on-disk persistence
let disconnected = 0;
let connecting = 0;
let connected = 0;
let disconnecting = 0;

for (const connections of network.getConnectionsByPeer().values()) {
const relevantConnection = getRevelantConnection(connections);
switch (relevantConnection?.stat.status) {
case "open":
connected++;
break;
case "closing":
disconnecting++;
break;
case "closed":
disconnected++;
break;
default:
connecting++;
}
}

return {
data: {
disconnected: 0,
connecting: 0,
connected: 0,
disconnecting: 0,
disconnected,
connecting,
connected,
disconnecting,
},
};
},
Expand Down
2 changes: 1 addition & 1 deletion packages/lodestar/src/api/impl/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function formatNodePeer(peerIdStr: string, connections: Connection[]): ro
* - Otherwise, the first closing connection
* - Otherwise, the first closed connection
*/
function getRevelantConnection(connections: Connection[]): Connection | null {
export function getRevelantConnection(connections: Connection[]): Connection | null {
const byStatus = new Map<PeerStatus, Connection>();
for (const conn of connections) {
if (conn.stat.status === "open") return conn;
Expand Down
41 changes: 41 additions & 0 deletions packages/lodestar/test/unit/api/impl/node/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,47 @@ describe("node api implementation", function () {
});
});

describe("getPeerCount", function () {
let peer1: PeerId, peer2: PeerId, peer3: PeerId;

before(async function () {
peer1 = await createPeerId();
peer2 = await createPeerId();
peer3 = await createPeerId();
});

it("it should return peer count", async function () {
const connectionsByPeer = new Map<string, Connection[]>([
[peer1.toB58String(), [libp2pConnection(peer1, "open", "outbound")]],
[
peer2.toB58String(),
[libp2pConnection(peer2, "closing", "inbound"), libp2pConnection(peer2, "closing", "inbound")],
],
[
peer3.toB58String(),
[
libp2pConnection(peer3, "closed", "inbound"),
libp2pConnection(peer3, "closed", "inbound"),
libp2pConnection(peer3, "closed", "inbound"),
],
],
]);

networkStub.getConnectionsByPeer.returns(connectionsByPeer);

const {data: count} = await api.getPeerCount();
expect(count).to.be.deep.equal(
{
connected: 1,
disconnecting: 1, // picks most relevant connection to count
disconnected: 1, // picks most relevant connection to count
connecting: 0,
},
"getPeerCount incorrect"
);
});
});

describe("getPeers", function () {
let peer1: PeerId, peer2: PeerId;

Expand Down

0 comments on commit fe831b9

Please sign in to comment.