@lodestar/beacon-node
Version:
A Typescript implementation of the beacon chain
47 lines • 1.53 kB
JavaScript
/**
* Format a list of connections from libp2p connections manager into the API's format NodePeer
*/
export function formatNodePeer(peerIdStr, connections) {
const conn = getRelevantConnection(connections);
return {
peerId: conn ? conn.remotePeer.toString() : peerIdStr,
// TODO: figure out how to get enr of peer
enr: "",
lastSeenP2pAddress: conn ? conn.remoteAddr.toString() : "",
direction: conn ? conn.direction : null,
state: conn ? getPeerState(conn.status) : "disconnected",
};
}
/**
* From a list of connections, get the most relevant of a peer
* - The first open connection if any
* - Otherwise, the first closing connection
* - Otherwise, the first closed connection
*/
export function getRelevantConnection(connections) {
const byStatus = new Map();
for (const conn of connections) {
if (conn.status === "open")
return conn;
if (!byStatus.has(conn.status))
byStatus.set(conn.status, conn);
}
return byStatus.get("open") || byStatus.get("closing") || byStatus.get("closed") || null;
}
/**
* Map libp2p connection status to the API's peer state notation
* @param status
*/
function getPeerState(status) {
switch (status) {
case "open":
return "connected";
case "closing":
return "disconnecting";
case "closed":
return "disconnected";
default:
return "disconnected";
}
}
//# sourceMappingURL=utils.js.map