Skip to content

Commit

Permalink
chore: choose peers from the connected pool, instead of dialing them
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Aug 6, 2024
1 parent 32b9c9f commit f32d93c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
12 changes: 7 additions & 5 deletions packages/core/src/lib/base_protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ type GetPeersOptions = {
maxBootstrapPeers: number;
};

const DEFAULT_GET_PEERS_OPTIONS: GetPeersOptions = {
prioritizeLatency: true,
maxBootstrapPeers: 1,
numPeers: 0
};

/**
* A class with predefined helpers, to be used as a base to implement Waku
* Protocols.
Expand Down Expand Up @@ -90,11 +96,7 @@ export class BaseProtocol implements IBaseProtocolCore {
* @returns A list of peers that support the protocol sorted by latency.
*/
public async getPeers(
options: GetPeersOptions = {
prioritizeLatency: true,
maxBootstrapPeers: 1,
numPeers: 0
}
options: GetPeersOptions = DEFAULT_GET_PEERS_OPTIONS
): Promise<Peer[]> {
const { maxBootstrapPeers, numPeers, prioritizeLatency } = options;

Expand Down
16 changes: 9 additions & 7 deletions packages/sdk/src/protocols/base_protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,6 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
this.log.info(`Finding and adding ${numPeers} new peers`);
try {
const additionalPeers = await this.findAdditionalPeers(numPeers);
const dials = additionalPeers.map((peer) =>
this.connectionManager.attemptDial(peer.id)
);

await Promise.all(dials);

const updatedPeers = [...this.peers, ...additionalPeers];
this.updatePeers(updatedPeers);
Expand All @@ -227,10 +222,17 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
private async findAdditionalPeers(numPeers: number): Promise<Peer[]> {
this.log.info(`Finding ${numPeers} additional peers`);
try {
let newPeers = await this.core.allPeers();
let newPeers = await this.core.getPeers({
maxBootstrapPeers: 0,
numPeers: 0
});

if (newPeers.length === 0) {
this.log.warn("No new peers found.");
this.log.warn("No new peers found, trying with bootstrap peers");
newPeers = await this.core.getPeers({
maxBootstrapPeers: numPeers,
numPeers: 0
});
}

newPeers = newPeers
Expand Down

0 comments on commit f32d93c

Please sign in to comment.