*Refactor to only broadcast to peers listening on our topic hash
This commit is contained in:
parent
993eff5398
commit
7c2e6d1df8
26
src/index.ts
26
src/index.ts
|
@ -9,6 +9,8 @@ import { Packet } from "./messages.js";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import c from "compact-encoding";
|
import c from "compact-encoding";
|
||||||
import b4a from "b4a";
|
import b4a from "b4a";
|
||||||
|
// @ts-ignore
|
||||||
|
import sodium from "sodium-universal";
|
||||||
|
|
||||||
const debug = debug0("dht-flood");
|
const debug = debug0("dht-flood");
|
||||||
|
|
||||||
|
@ -23,6 +25,7 @@ export default class DHTFlood extends EventEmitter {
|
||||||
private lru: LRU;
|
private lru: LRU;
|
||||||
private swarm: any;
|
private swarm: any;
|
||||||
private protocol: string;
|
private protocol: string;
|
||||||
|
private topic: Buffer;
|
||||||
private symbol: Symbol;
|
private symbol: Symbol;
|
||||||
private socketMap: Set<Function> = new Set<Function>();
|
private socketMap: Set<Function> = new Set<Function>();
|
||||||
|
|
||||||
|
@ -51,6 +54,14 @@ export default class DHTFlood extends EventEmitter {
|
||||||
mux.pair({ protocol: this.protocol }, () => this.setupPeer(peer));
|
mux.pair({ protocol: this.protocol }, () => this.setupPeer(peer));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const topic = b4a.from(this.protocol);
|
||||||
|
const topicHash = b4a.allocUnsafe(32);
|
||||||
|
sodium.crypto_generichash(topicHash, topic);
|
||||||
|
|
||||||
|
this.topic = topicHash as Buffer;
|
||||||
|
|
||||||
|
this.swarm.join(topicHash);
|
||||||
|
|
||||||
this.symbol = Symbol.for(this.protocol);
|
this.symbol = Symbol.for(this.protocol);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,8 +159,19 @@ export default class DHTFlood extends EventEmitter {
|
||||||
this.messageNumber++;
|
this.messageNumber++;
|
||||||
const { id, messageNumber } = this;
|
const { id, messageNumber } = this;
|
||||||
|
|
||||||
for (const peer of this.swarm.connections.values()) {
|
let topicString = this.topic.toString("hex");
|
||||||
const message = this.setupPeer(peer);
|
|
||||||
|
let peers: Buffer[] = [...this.swarm.peers.values()]
|
||||||
|
.filter((peerInfo: any) => peerInfo.topics.includes(topicString))
|
||||||
|
.map((peerInfo) => peerInfo.publicKey);
|
||||||
|
|
||||||
|
for (const peer of peers) {
|
||||||
|
const conn = this.swarm._allConnections.get(peer);
|
||||||
|
if (conn) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = this.setupPeer(conn);
|
||||||
message.send({
|
message.send({
|
||||||
originId: id,
|
originId: id,
|
||||||
messageNumber,
|
messageNumber,
|
||||||
|
|
Loading…
Reference in New Issue