Skip to content

Commit f36812f

Browse files
chore: cleanup ReliabilityMonitor
1 parent 2b61774 commit f36812f

File tree

1 file changed

+33
-43
lines changed

1 file changed

+33
-43
lines changed

packages/sdk/src/protocols/filter/reliability_monitor.ts

+33-43
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,17 @@ import { Logger } from "@waku/utils";
66

77
type ReceivedMessageHashes = {
88
all: Set<string>;
9-
nodes: {
10-
[peerId: PeerIdStr]: Set<string>;
11-
};
9+
nodes: Record<PeerIdStr, Set<string>>;
1210
};
1311

1412
const DEFAULT_MAX_MISSED_MESSAGES_THRESHOLD = 3;
1513

1614
const log = new Logger("sdk:filter:reliability_monitor");
1715

1816
export class ReliabilityMonitor {
19-
public receivedMessagesHashStr: string[] = [];
20-
public receivedMessagesHashes: ReceivedMessageHashes;
21-
public missedMessagesByPeer: Map<string, number> = new Map();
22-
public maxMissedMessagesThreshold = DEFAULT_MAX_MISSED_MESSAGES_THRESHOLD;
17+
private receivedMessagesHashes: ReceivedMessageHashes;
18+
private missedMessagesByPeer: Map<PeerIdStr, number> = new Map();
19+
private maxMissedMessagesThreshold = DEFAULT_MAX_MISSED_MESSAGES_THRESHOLD;
2320

2421
public constructor(
2522
private getPeers: () => Peer[],
@@ -29,46 +26,37 @@ export class ReliabilityMonitor {
2926

3027
this.receivedMessagesHashes = {
3128
all: new Set(),
32-
nodes: {
33-
...Object.fromEntries(allPeerIdStr.map((peerId) => [peerId, new Set()]))
34-
}
29+
nodes: Object.fromEntries(
30+
allPeerIdStr.map((peerId) => [peerId, new Set()])
31+
)
3532
};
3633
allPeerIdStr.forEach((peerId) => this.missedMessagesByPeer.set(peerId, 0));
3734
}
3835

3936
public setMaxMissedMessagesThreshold(value: number | undefined): void {
40-
if (value === undefined) {
41-
return;
37+
if (value !== undefined) {
38+
this.maxMissedMessagesThreshold = value;
4239
}
43-
this.maxMissedMessagesThreshold = value;
44-
}
45-
46-
public get messageHashes(): string[] {
47-
return [...this.receivedMessagesHashes.all];
4840
}
4941

5042
public addMessage(
5143
message: WakuMessage,
5244
pubsubTopic: PubsubTopic,
53-
peerIdStr?: string
45+
peerIdStr?: PeerIdStr
5446
): boolean {
5547
const hashedMessageStr = messageHashStr(
5648
pubsubTopic,
5749
message as IProtoMessage
5850
);
5951

52+
const isNewMessage = !this.receivedMessagesHashes.all.has(hashedMessageStr);
6053
this.receivedMessagesHashes.all.add(hashedMessageStr);
6154

6255
if (peerIdStr) {
6356
this.receivedMessagesHashes.nodes[peerIdStr].add(hashedMessageStr);
6457
}
6558

66-
if (this.receivedMessagesHashStr.includes(hashedMessageStr)) {
67-
return true;
68-
} else {
69-
this.receivedMessagesHashStr.push(hashedMessageStr);
70-
return false;
71-
}
59+
return !isNewMessage;
7260
}
7361

7462
public async validateMessage(): Promise<void> {
@@ -79,36 +67,38 @@ export class ReliabilityMonitor {
7967
if (!hashes.has(hash)) {
8068
this.incrementMissedMessageCount(peerIdStr);
8169
if (this.shouldRenewPeer(peerIdStr)) {
82-
log.info(
83-
`Peer ${peerIdStr} has missed too many messages, renewing.`
84-
);
85-
const peerId = this.getPeers().find(
86-
(p) => p.id.toString() === peerIdStr
87-
)?.id;
88-
if (!peerId) {
89-
log.error(
90-
`Unexpected Error: Peer ${peerIdStr} not found in connected peers.`
91-
);
92-
continue;
93-
}
94-
try {
95-
await this.renewAndSubscribePeer(peerId);
96-
} catch (error) {
97-
log.error(`Failed to renew peer ${peerIdStr}: ${error}`);
98-
}
70+
await this.renewPeer(peerIdStr);
9971
}
10072
}
10173
}
10274
}
10375
}
10476

105-
private incrementMissedMessageCount(peerIdStr: string): void {
77+
private incrementMissedMessageCount(peerIdStr: PeerIdStr): void {
10678
const currentCount = this.missedMessagesByPeer.get(peerIdStr) || 0;
10779
this.missedMessagesByPeer.set(peerIdStr, currentCount + 1);
10880
}
10981

110-
private shouldRenewPeer(peerIdStr: string): boolean {
82+
private shouldRenewPeer(peerIdStr: PeerIdStr): boolean {
11183
const missedMessages = this.missedMessagesByPeer.get(peerIdStr) || 0;
11284
return missedMessages > this.maxMissedMessagesThreshold;
11385
}
86+
87+
private async renewPeer(peerIdStr: PeerIdStr): Promise<void> {
88+
log.info(`Peer ${peerIdStr} has missed too many messages, renewing.`);
89+
const peerId = this.getPeers().find(
90+
(p) => p.id.toString() === peerIdStr
91+
)?.id;
92+
if (!peerId) {
93+
log.error(
94+
`Unexpected Error: Peer ${peerIdStr} not found in connected peers.`
95+
);
96+
return;
97+
}
98+
try {
99+
await this.renewAndSubscribePeer(peerId);
100+
} catch (error) {
101+
log.error(`Failed to renew peer ${peerIdStr}: ${error}`);
102+
}
103+
}
114104
}

0 commit comments

Comments
 (0)