Skip to content

Commit

Permalink
feat!: use MUID for gossipsub message id
Browse files Browse the repository at this point in the history
  • Loading branch information
fryorcraken committed Mar 13, 2023
1 parent 48db430 commit a3ea420
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"packages/proto",
"packages/interfaces",
"packages/enr",
"packages/core",
"packages/message-hash",
"packages/core",
"packages/peer-exchange",
"packages/dns-discovery",
"packages/message-encryption",
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"@chainsafe/libp2p-gossipsub": "^6.1.0",
"@noble/hashes": "^1.2.0",
"@waku/interfaces": "*",
"@waku/message-hash": "*",
"@waku/proto": "*",
"@waku/utils": "*",
"debug": "^4.3.4",
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/lib/relay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import { TopicOnlyDecoder } from "../message/topic_only_message.js";
import { pushOrInitMapSet } from "../push_or_init_map.js";

import * as constants from "./constants.js";
import { gossipsubMessageId } from "./message_id.js";
import { messageValidator } from "./message_validator.js";

const log = debug("waku:relay");

export { messageValidator };
export type Observer<T extends IDecodedMessage> = {
decoder: IDecoder<T>;
callback: Callback<T>;
Expand Down Expand Up @@ -60,6 +62,7 @@ class Relay extends GossipSub implements IRelay {
// Ensure that no signature is included nor expected in the messages.
globalSignaturePolicy: SignaturePolicy.StrictNoSign,
fallbackToFloodsub: false,
msgIdFn: gossipsubMessageId,
});

super(components, options);
Expand All @@ -68,7 +71,6 @@ class Relay extends GossipSub implements IRelay {
this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic;

this.observers = new Map();

// TODO: User might want to decide what decoder should be used (e.g. for RLN)
this.defaultDecoder = new TopicOnlyDecoder();
}
Expand Down
32 changes: 32 additions & 0 deletions packages/core/src/lib/relay/message_id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Message } from "@libp2p/interface-pubsub";
import { sha256 } from "@noble/hashes/sha256";
import { messageHash } from "@waku/message-hash";
import { proto_message } from "@waku/proto";
import { concat, toProtoMessage } from "@waku/utils";
import debug from "debug";

const log = debug("waku:relay:message-id");
export function gossipsubMessageId(
message: Message
): Promise<Uint8Array> | Uint8Array {
const startTime = performance.now();

let result;
try {
const protoMessage = toProtoMessage(
proto_message.WakuMessage.decode(message.data)
);

result = messageHash(message.topic, protoMessage);

if (protoMessage.meta) {
result = concat([result, protoMessage.meta]);
}
} catch (e) {
log("Failed to deserialize message, falling back to standard message id");
result = sha256(message.data);
}
const endTime = performance.now();
log(`Gossipsub message id generation: ${endTime - startTime}ms`);
return result;
}

0 comments on commit a3ea420

Please sign in to comment.