Skip to content

Commit

Permalink
fix(core-p2p): limit peers returned by getPeers
Browse files Browse the repository at this point in the history
  • Loading branch information
air1one committed Dec 11, 2020
1 parent 9370175 commit 4c79c45
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/core-p2p/src/constants.ts
Expand Up @@ -3,4 +3,5 @@ export const constants = {
DEFAULT_MAX_PAYLOAD: 20 * 1024 * 1024, // default maxPayload value on the server WS socket
DEFAULT_MAX_PAYLOAD_CLIENT: 100 * 1024, // default maxPayload value on the client WS socket
KILOBYTE: 1024,
MAX_PEERS_GETPEERS: 2000,
};
3 changes: 2 additions & 1 deletion packages/core-p2p/src/schemas.ts
@@ -1,4 +1,5 @@
import { app } from "@arkecosystem/core-container";
import { constants } from "./constants";

export const requestSchemas = {
peer: {
Expand Down Expand Up @@ -100,7 +101,7 @@ export const replySchemas = {
},
"p2p.peer.getPeers": {
type: "array",
maxItems: 2000,
maxItems: constants.MAX_PEERS_GETPEERS,
items: {
type: "object",
properties: {
Expand Down
2 changes: 2 additions & 0 deletions packages/core-p2p/src/socket-server/versions/peer.ts
Expand Up @@ -3,6 +3,7 @@ import { Blockchain, Database, Logger, P2P, TransactionPool } from "@arkecosyste
import { isBlockChained } from "@arkecosystem/core-utils";
import { Blocks, Crypto, Interfaces } from "@arkecosystem/crypto";
import pluralize from "pluralize";
import { constants } from "../../constants";
import { MissingCommonBlockError } from "../../errors";
import { IPeerPingResponse } from "../../interfaces";
import { isWhitelisted } from "../../utils";
Expand All @@ -15,6 +16,7 @@ export const getPeers = ({ service }: { service: P2P.IPeerService }): P2P.IPeerB
.getStorage()
.getPeers()
.sort((a, b) => a.latency - b.latency)
.slice(0, constants.MAX_PEERS_GETPEERS)
.map(peer => peer.toBroadcast());
};

Expand Down

0 comments on commit 4c79c45

Please sign in to comment.