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: deterministic message hashing #1233

Merged
merged 5 commits into from
May 6, 2023
Merged

Conversation

fryorcraken
Copy link
Collaborator

@fryorcraken fryorcraken commented Mar 13, 2023

Ref #1208
After checking with @jm-clius, we should not change the gossipsub message id just yet. This change will have to be coordinated across the implementations.

However, we can provide the MUID calculation logic for now. This will be first use by nwaku to remove store duplicates.

@fryorcraken fryorcraken marked this pull request as draft March 13, 2023 04:54
@fryorcraken
Copy link
Collaborator Author

Can only be merged once #1230 introduces the meta field.

package.json Outdated Show resolved Hide resolved
@fryorcraken fryorcraken changed the title chore: new empty message-hash package feat: implement deterministic message hashing and use it for gossipsub message id Mar 13, 2023
@fryorcraken fryorcraken changed the title feat: implement deterministic message hashing and use it for gossipsub message id feat!: implement deterministic message hashing and use it for gossipsub message id Mar 13, 2023
@fryorcraken
Copy link
Collaborator Author

Remove reviewers as PR is actually still draft

@fryorcraken fryorcraken force-pushed the feat/hash-message-hashing branch 2 times, most recently from 998e350 to 5f61914 Compare March 14, 2023 00:31
@github-actions
Copy link

github-actions bot commented Mar 14, 2023

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
Waku core 121.45 KB (0%) 2.5 s (0%) 2.9 s (+23.83% 🔺) 5.4 s
Waku default setup 391 KB (0%) 7.9 s (0%) 10.7 s (+110.18% 🔺) 18.5 s
ECIES encryption 28.02 KB (0%) 561 ms (0%) 1.9 s (+64% 🔺) 2.5 s
Symmetric encryption 28.03 KB (0%) 561 ms (0%) 2.3 s (+83.28% 🔺) 2.9 s
DNS discovery 108.24 KB (0%) 2.2 s (0%) 3.3 s (-0.95% 🔽) 5.5 s
Privacy preserving protocols 118.32 KB (0%) 2.4 s (0%) 3.1 s (+12.56% 🔺) 5.4 s
Light protocols 120.11 KB (0%) 2.5 s (0%) 3.1 s (+12.89% 🔺) 5.5 s
History retrieval protocols 120.12 KB (0%) 2.5 s (0%) 3.1 s (+25% 🔺) 5.5 s
Deterministic Message Hashing 5.78 KB (+100% 🔺) 116 ms (+100% 🔺) 718 ms (+100% 🔺) 834 ms

@fryorcraken fryorcraken marked this pull request as ready for review March 14, 2023 00:39
@fryorcraken
Copy link
Collaborator Author

Now ready for review

* Deterministic Message Hashing as defined in
* [14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/#deterministic-message-hashing)
*/
export function messageHash(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Interesting part: the deterministic message hash logic

import { toProtoMessage } from "../to_proto_message.js";

const log = debug("waku:relay:message-id");
export function gossipsubMessageId(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

interesting part: the gossipsub message id calculation

export function gossipsubMessageId(
message: Message
): Promise<Uint8Array> | Uint8Array {
const startTime = performance.now();
Copy link
Collaborator

Choose a reason for hiding this comment

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

where does performance come from? can't find an import for this 👀

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah, TIL it's a native method as an alternative to Date.now() 💯

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's a JS API to handle performance. Which matters here as the methods need to execute within 100ms.

Comment on lines 18 to 27
if (message.meta) {
bytesToHash = concat([
pubsubTopicBytes,
message.payload,
contentTopicBytes,
message.meta,
]);
} else {
bytesToHash = concat([
pubsubTopicBytes,
message.payload,
contentTopicBytes,
]);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

is bytesToHash an apt naming?
as this variable does not hold the actual hash of the bytes but only the concatenated version - the hash is returned in the last line

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It holds the bytes that needs to be hashed. The bytes to hash. What's apt?
Could probably just call it bytes

build
bundle
dist
node_modules
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's add CHANGELOG to ignore

@fryorcraken
Copy link
Collaborator Author

TODO: Check the API around the seen cache key and ensure MUID is used for it.

@fryorcraken
Copy link
Collaborator Author

Discussing with @jm-clius, we do not want to do the change on Relay at this stage. We will want to coordinate across implementations when we do it.

@fryorcraken fryorcraken changed the title feat!: implement deterministic message hashing and use it for gossipsub message id feat: deterministic message hashing May 6, 2023
@fryorcraken fryorcraken merged commit 9eb3384 into master May 6, 2023
@fryorcraken fryorcraken deleted the feat/hash-message-hashing branch May 6, 2023 03:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants