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: use messageHash as msgIdFn #1361

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions packages/core/src/lib/message/version_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,9 @@ export class Decoder implements IDecoder<DecodedMessage> {
}

fromWireToProtoObj(bytes: Uint8Array): Promise<IProtoMessage | undefined> {
const protoMessage = proto.WakuMessage.decode(bytes);
const protoMessage = fromBytesToProtoMessage(bytes);
log("Message decoded", protoMessage);
return Promise.resolve({
payload: protoMessage.payload,
contentTopic: protoMessage.contentTopic,
version: protoMessage.version ?? undefined,
timestamp: protoMessage.timestamp ?? undefined,
meta: protoMessage.meta ?? undefined,
rateLimitProof: protoMessage.rateLimitProof ?? undefined,
ephemeral: protoMessage.ephemeral ?? false,
});
return Promise.resolve(protoMessage);
}

async fromProtoObj(
Expand All @@ -161,6 +153,24 @@ export class Decoder implements IDecoder<DecodedMessage> {
}
}

/**
* Tries to decode bytes into IProtoMessage.
* @param bytes Uint8Array
* @returns IProtoMessage
*/
export function fromBytesToProtoMessage(bytes: Uint8Array): IProtoMessage {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe should move it to @waku/utils/message 🤔

const protoMessage = proto.WakuMessage.decode(bytes);
return {
payload: protoMessage.payload,
contentTopic: protoMessage.contentTopic,
version: protoMessage.version ?? undefined,
timestamp: protoMessage.timestamp ?? undefined,
meta: protoMessage.meta ?? undefined,
rateLimitProof: protoMessage.rateLimitProof ?? undefined,
ephemeral: protoMessage.ephemeral ?? false,
};
}

/**
* Creates a decoder that decode messages without Waku level encryption.
*
Expand Down
15 changes: 13 additions & 2 deletions packages/relay/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SignaturePolicy } from "@chainsafe/libp2p-gossipsub/types";
import type { Libp2p } from "@libp2p/interface-libp2p";
import type { PubSub } from "@libp2p/interface-pubsub";
import { sha256 } from "@noble/hashes/sha256";
import { DefaultPubSubTopic } from "@waku/core";
import { DefaultPubSubTopic, message } from "@waku/core";
import {
ActiveSubscriptions,
Callback,
Expand All @@ -24,6 +24,7 @@ import {
SendError,
SendResult,
} from "@waku/interfaces";
import { messageHash } from "@waku/message-hash";
import { groupByContentTopic, isSizeValid, toAsyncIterator } from "@waku/utils";
import debug from "debug";

Expand Down Expand Up @@ -249,7 +250,17 @@ export function wakuGossipSub(
return (components: GossipSubComponents) => {
init = {
...init,
msgIdFn: ({ data }) => sha256(data),
msgIdFn: ({ topic, data }) => {
try {
const protoMessage = message.fromBytesToProtoMessage(data);
return messageHash(topic, protoMessage);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Problem with this implementation is that in case meta is missing hash is taken based on this concat([pubsubTopicBytes, message.payload, contentTopicBytes])

It leads to inability of a user to send a message to the a pubSub/contentTopic in case someone already published a message with the same text.

Second problem of using messageHash as msgIdFn is that it won't be consistent with hashing in nwaku/go-waku as I mentioned here

} catch (e) {
log(
"Failed to hash as ProtoMessage, falling back to sha256 from data."
);
return sha256(data);
}
},
// Ensure that no signature is included nor expected in the messages.
globalSignaturePolicy: SignaturePolicy.StrictNoSign,
fallbackToFloodsub: false,
Expand Down