mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-03-21 14:23:10 +00:00
handle local discovery for events
This commit is contained in:
parent
199f6ab2ff
commit
f8213e425f
@ -395,11 +395,26 @@ export class ConnectionManager
|
||||
this.libp2p.peerStore
|
||||
);
|
||||
|
||||
const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
|
||||
Tags.BOOTSTRAP
|
||||
);
|
||||
// const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
|
||||
// Tags.BOOTSTRAP
|
||||
// );
|
||||
const tags = await this.getTagNamesForPeer(peerId);
|
||||
|
||||
if (isBootstrap) {
|
||||
let tag: Tags;
|
||||
if (tags.includes(Tags.BOOTSTRAP)) {
|
||||
tag = Tags.BOOTSTRAP;
|
||||
} else if (tags.includes(Tags.PEER_EXCHANGE)) {
|
||||
tag = Tags.PEER_EXCHANGE;
|
||||
} else if (tags.includes(Tags.LOCAL)) {
|
||||
tag = Tags.LOCAL;
|
||||
} else {
|
||||
log.warn(
|
||||
`Peer ${peerId.toString()} has no discovery tag. Not dispatching discovery event`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (tag === Tags.BOOTSTRAP) {
|
||||
const bootstrapConnections = this.libp2p
|
||||
.getConnections()
|
||||
.filter((conn) => conn.tags.includes(Tags.BOOTSTRAP));
|
||||
@ -420,13 +435,23 @@ export class ConnectionManager
|
||||
);
|
||||
}
|
||||
} else {
|
||||
let discoveryTagName: EPeersByDiscoveryEvents;
|
||||
if (tag === Tags.PEER_EXCHANGE) {
|
||||
discoveryTagName =
|
||||
EPeersByDiscoveryEvents.PEER_CONNECT_PEER_EXCHANGE;
|
||||
} else if (tag === Tags.LOCAL) {
|
||||
discoveryTagName = EPeersByDiscoveryEvents.PEER_CONNECT_LOCAL;
|
||||
} else {
|
||||
log.warn(
|
||||
`Peer ${peerId.toString()} has no discovery tag. Not dispatching discovery event`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
this.dispatchEvent(
|
||||
new CustomEvent<PeerId>(
|
||||
EPeersByDiscoveryEvents.PEER_CONNECT_PEER_EXCHANGE,
|
||||
{
|
||||
detail: peerId
|
||||
}
|
||||
)
|
||||
new CustomEvent<PeerId>(discoveryTagName, {
|
||||
detail: peerId
|
||||
})
|
||||
);
|
||||
}
|
||||
this.toggleOnline();
|
||||
@ -518,19 +543,26 @@ export class ConnectionManager
|
||||
}
|
||||
|
||||
private async dispatchDiscoveryEvent(peerId: PeerId): Promise<void> {
|
||||
const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
|
||||
Tags.BOOTSTRAP
|
||||
);
|
||||
const tags = await this.getTagNamesForPeer(peerId);
|
||||
|
||||
let tag: Tags;
|
||||
if (tags.includes(Tags.BOOTSTRAP)) {
|
||||
tag = Tags.BOOTSTRAP;
|
||||
} else if (tags.includes(Tags.PEER_EXCHANGE)) {
|
||||
tag = Tags.PEER_EXCHANGE;
|
||||
} else if (tags.includes(Tags.LOCAL)) {
|
||||
tag = Tags.LOCAL;
|
||||
} else {
|
||||
log.warn(
|
||||
`Peer ${peerId.toString()} has no discovery tag. Not dispatching discovery event`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
this.dispatchEvent(
|
||||
new CustomEvent<PeerId>(
|
||||
isBootstrap
|
||||
? EPeersByDiscoveryEvents.PEER_DISCOVERY_BOOTSTRAP
|
||||
: EPeersByDiscoveryEvents.PEER_DISCOVERY_PEER_EXCHANGE,
|
||||
{
|
||||
detail: peerId
|
||||
}
|
||||
)
|
||||
new CustomEvent<PeerId>(tag, {
|
||||
detail: peerId
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -26,15 +26,19 @@ export interface ConnectionManagerOptions {
|
||||
export enum EPeersByDiscoveryEvents {
|
||||
PEER_DISCOVERY_BOOTSTRAP = "peer:discovery:bootstrap",
|
||||
PEER_DISCOVERY_PEER_EXCHANGE = "peer:discovery:peer-exchange",
|
||||
PEER_DISCOVERY_LOCAL = "peer:discovery:local",
|
||||
PEER_CONNECT_BOOTSTRAP = "peer:connected:bootstrap",
|
||||
PEER_CONNECT_PEER_EXCHANGE = "peer:connected:peer-exchange"
|
||||
PEER_CONNECT_PEER_EXCHANGE = "peer:connected:peer-exchange",
|
||||
PEER_CONNECT_LOCAL = "peer:connected:local"
|
||||
}
|
||||
|
||||
export interface IPeersByDiscoveryEvents {
|
||||
[EPeersByDiscoveryEvents.PEER_DISCOVERY_BOOTSTRAP]: CustomEvent<PeerId>;
|
||||
[EPeersByDiscoveryEvents.PEER_DISCOVERY_PEER_EXCHANGE]: CustomEvent<PeerId>;
|
||||
[EPeersByDiscoveryEvents.PEER_DISCOVERY_LOCAL]: CustomEvent<PeerId>;
|
||||
[EPeersByDiscoveryEvents.PEER_CONNECT_BOOTSTRAP]: CustomEvent<PeerId>;
|
||||
[EPeersByDiscoveryEvents.PEER_CONNECT_PEER_EXCHANGE]: CustomEvent<PeerId>;
|
||||
[EPeersByDiscoveryEvents.PEER_CONNECT_LOCAL]: CustomEvent<PeerId>;
|
||||
}
|
||||
|
||||
export interface PeersByDiscoveryResult {
|
||||
|
||||
@ -26,6 +26,7 @@ describe("Events", function () {
|
||||
await tearDownNodes([], waku);
|
||||
});
|
||||
|
||||
//TODO: add tests for local discovery
|
||||
describe("peer:discovery", () => {
|
||||
it("should emit `peer:discovery:bootstrap` event when a peer is discovered", async function () {
|
||||
const peerIdBootstrap = await createSecp256k1PeerId();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user