From a3ea42070ab38803f533e1ffbf4476309b2fc8c6 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Tue, 14 Mar 2023 10:39:28 +1100 Subject: [PATCH] feat!: use MUID for gossipsub message id --- package-lock.json | 4 ++- package.json | 2 +- packages/core/package.json | 1 + packages/core/src/lib/relay/index.ts | 4 ++- packages/core/src/lib/relay/message_id.ts | 32 +++++++++++++++++++++++ 5 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 packages/core/src/lib/relay/message_id.ts 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..2435d15c0b 100644 --- a/packages/core/src/lib/relay/index.ts +++ b/packages/core/src/lib/relay/index.ts @@ -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 = { decoder: IDecoder; callback: Callback; @@ -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); @@ -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(); } 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..c2fe352be9 --- /dev/null +++ b/packages/core/src/lib/relay/message_id.ts @@ -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 { + 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; +}