diff --git a/package-lock.json b/package-lock.json index cc4abbcdde..d1c209572a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,8 +10,8 @@ "packages/proto", "packages/interfaces", "packages/enr", - "packages/core", "packages/message-hash", + "packages/core", "packages/peer-exchange", "packages/dns-discovery", "packages/message-encryption", @@ -28015,6 +28015,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", @@ -32801,6 +32802,7 @@ "@typescript-eslint/parser": "^5.51.0", "@waku/build-utils": "*", "@waku/interfaces": "*", + "@waku/message-hash": "*", "@waku/proto": "*", "@waku/utils": "*", "chai": "^4.3.7", diff --git a/package.json b/package.json index b6d00f34e3..cced634401 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/core/package.json b/packages/core/package.json index b59360e736..61c4cb96c6 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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", diff --git a/packages/core/src/lib/relay/index.ts b/packages/core/src/lib/relay/index.ts index b588a79bb5..a4299807bc 100644 --- a/packages/core/src/lib/relay/index.ts +++ b/packages/core/src/lib/relay/index.ts @@ -23,6 +23,7 @@ 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"); @@ -60,6 +61,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); diff --git a/packages/core/src/lib/relay/message_id.ts b/packages/core/src/lib/relay/message_id.ts new file mode 100644 index 0000000000..14a6510efd --- /dev/null +++ b/packages/core/src/lib/relay/message_id.ts @@ -0,0 +1,34 @@ +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 } from "@waku/utils"; +import debug from "debug"; + +import { toProtoMessage } from "../to_proto_message.js"; + +const log = debug("waku:relay:message-id"); +export function gossipsubMessageId( + message: Message +): Promise | 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; +}