Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: validate messages for individual filter nodes & perform renewals #2057

Merged
merged 13 commits into from
Jul 17, 2024
9 changes: 7 additions & 2 deletions packages/core/src/lib/filter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
constructor(
private handleIncomingMessage: (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we make it `private handleIncomingMessage: (options: IncomingMessageOptions) => Promise

pubsubTopic: PubsubTopic,
wakuMessage: WakuMessage
wakuMessage: WakuMessage,
peerIdStr: string
) => Promise<void>,
libp2p: Libp2p,
options?: ProtocolCreateOptions
Expand Down Expand Up @@ -78,7 +79,11 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
return;
}

await this.handleIncomingMessage(pubsubTopic, wakuMessage);
await this.handleIncomingMessage(
pubsubTopic,
wakuMessage,
connection.remotePeer.toString()
);
}
}).then(
() => {
Expand Down
106 changes: 101 additions & 5 deletions packages/sdk/src/protocols/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ type SubscriptionCallback<T extends IDecodedMessage> = {
callback: Callback<T>;
};

type ReceivedMessageHashes = {
all: Set<string>;
nodes: {
[peerId: string]: Set<string>;
};
};

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

const MINUTE = 60 * 1000;
Expand All @@ -51,6 +58,8 @@ export class SubscriptionManager implements ISubscriptionSDK {
private readonly pubsubTopic: PubsubTopic;
readonly receivedMessagesHashStr: string[] = [];
private keepAliveTimer: number | null = null;
private readonly receivedMessagesHashes: ReceivedMessageHashes;
private messageValidationTimer: number | null = null;
private peerFailures: Map<string, number> = new Map();
private maxPingFailures: number = DEFAULT_MAX_PINGS;

Expand All @@ -67,6 +76,33 @@ export class SubscriptionManager implements ISubscriptionSDK {
) {
this.pubsubTopic = pubsubTopic;
this.subscriptionCallbacks = new Map();
const allPeerIdStrs = this.getPeers().map((p) => p.id.toString());
this.receivedMessagesHashes = {
all: new Set(),
nodes: {
...Object.fromEntries(
allPeerIdStrs.map((peerId) => [peerId, new Set()])
)
}
};

this.startMessageValidationCycle();
}

get messageHashes(): string[] {
return [...this.receivedMessagesHashes.all];
}

addHash(hash: string, peerIdStr?: string): void {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's not forget access modifiers

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Added to this PR part of 128dd58

I believe we should introduce it into eslint config as well so it's not skipped

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened a PR adding the rule to eslint: #2068

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool!

this.receivedMessagesHashes.all.add(hash);

if (peerIdStr) {
if (peerIdStr in this.receivedMessagesHashes) {
this.receivedMessagesHashes.nodes[peerIdStr].add(hash);
} else {
this.receivedMessagesHashes.nodes[peerIdStr] = new Set([hash]);
}
}
}

public async subscribe<T extends IDecodedMessage>(
Expand Down Expand Up @@ -146,8 +182,13 @@ export class SubscriptionManager implements ISubscriptionSDK {
const results = await Promise.allSettled(promises);
const finalResult = this.handleResult(results, "unsubscribe");

if (this.subscriptionCallbacks.size === 0 && this.keepAliveTimer) {
this.stopKeepAlivePings();
if (this.subscriptionCallbacks.size === 0) {
if (this.keepAliveTimer) {
this.stopKeepAlivePings();
}
if (this.messageValidationTimer) {
this.stopMessageValidationCycle();
}
}

return finalResult;
Expand Down Expand Up @@ -180,11 +221,17 @@ export class SubscriptionManager implements ISubscriptionSDK {
return finalResult;
}

async processIncomingMessage(message: WakuMessage): Promise<void> {
async processIncomingMessage(
message: WakuMessage,
peerIdStr: string
): Promise<void> {
const hashedMessageStr = messageHashStr(
this.pubsubTopic,
message as IProtoMessage
);

this.addHash(hashedMessageStr, peerIdStr);

if (this.receivedMessagesHashStr.includes(hashedMessageStr)) {
log.info("Message already received, skipping");
return;
Expand Down Expand Up @@ -312,6 +359,55 @@ export class SubscriptionManager implements ISubscriptionSDK {
clearInterval(this.keepAliveTimer);
this.keepAliveTimer = null;
}

private startMessageValidationCycle(): void {
const validationCycle = async (): Promise<void> => {
log.info("Starting message validation cycle");

for (const hash of this.receivedMessagesHashes.all) {
for (const [peerIdStr, hashes] of Object.entries(
this.receivedMessagesHashes.nodes
)) {
if (!hashes.has(hash)) {
log.error(
`Message with hash ${hash} not received from peer ${peerIdStr}. Attempting renewal.`
);
const peerId = this.getPeers().find(
(p) => p.id.toString() === peerIdStr
)?.id;
if (!peerId) {
log.error(
`Unexpected Error: Peer ${peerIdStr} not found in connected peers.`
);
return;
}
try {
await this.renewPeer(peerId);
} catch (error) {
log.error(`Failed to renew peer ${peerIdStr}: ${error}`);
}
}
}
}
};

this.messageValidationTimer = setInterval(() => {
void validationCycle().catch((error) => {
log.error("Error in message validation cycle:", error);
});
}, MINUTE) as unknown as number;
}

private stopMessageValidationCycle(): void {
if (!this.messageValidationTimer) {
log.info("Already stopped message validation cycle.");
return;
}

log.info("Stopping message validation cycle.");
clearInterval(this.messageValidationTimer);
this.messageValidationTimer = null;
}
}

class FilterSDK extends BaseProtocolSDK implements IFilterSDK {
Expand All @@ -326,7 +422,7 @@ class FilterSDK extends BaseProtocolSDK implements IFilterSDK {
) {
super(
new FilterCore(
async (pubsubTopic: PubsubTopic, wakuMessage: WakuMessage) => {
async (pubsubTopic, wakuMessage, peerIdStr) => {
const subscription = this.getActiveSubscription(pubsubTopic);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, I just noticed - we keep only one subscription per pubsubTopic.
That mean that if another one will be created in the application - first one would be dropped.

Also we probably still have this one #1678

Let's follow up if I am not wrong

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No no. We have the ability to have subscriptions with multiple pubsub topics -- each pubsub topic is represented by a new SubscriptionManager object.

The line of code you are reading is actually a callback function:

handleIncomingMessage: (pubsubTopic: PubsubTopic, wakuMessage: WakuMessage, peerIdStr: string) => Promise<void>

TL;DR: We can indeed subscribe to multiple pubsub topics

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right, there is a fork here -

const subscription =

if a subscription exists - then we return it which will be used for subscribe request

if (!subscription) {
log.error(
Expand All @@ -335,7 +431,7 @@ class FilterSDK extends BaseProtocolSDK implements IFilterSDK {
return;
}

await subscription.processIncomingMessage(wakuMessage);
await subscription.processIncomingMessage(wakuMessage, peerIdStr);
},
libp2p,
options
Expand Down
Loading