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

chore: throw if more than one network config is passed #2056

Merged
merged 10 commits into from
Jul 19, 2024

Conversation

weboko
Copy link
Collaborator

@weboko weboko commented Jul 10, 2024

Problem

Users can provide more than one network config.

Solution

Throw

Notes

Copy link

github-actions bot commented Jul 10, 2024

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
Waku node 183.91 KB (+0.09% 🔺) 3.7 s (+0.09% 🔺) 2.3 s (-17.5% 🔽) 6 s
Waku Simple Light Node 183.85 KB (+0.01% 🔺) 3.7 s (+0.01% 🔺) 2.1 s (+16.73% 🔺) 5.8 s
ECIES encryption 23.12 KB (0%) 463 ms (0%) 674 ms (+94.44% 🔺) 1.2 s
Symmetric encryption 22.58 KB (0%) 452 ms (0%) 905 ms (+79.89% 🔺) 1.4 s
DNS discovery 72.49 KB (0%) 1.5 s (0%) 1.4 s (+4.18% 🔺) 2.9 s
Peer Exchange discovery 74.15 KB (0%) 1.5 s (0%) 1.4 s (+18.19% 🔺) 2.9 s
Local Peer Cache Discovery 67.68 KB (0%) 1.4 s (0%) 1.5 s (-23.43% 🔽) 2.8 s
Privacy preserving protocols 38.87 KB (0%) 778 ms (0%) 1.1 s (-0.17% 🔽) 1.8 s
Waku Filter 113.31 KB (-0.06% 🔽) 2.3 s (-0.06% 🔽) 1.6 s (+19.1% 🔺) 3.9 s
Waku LightPush 111.31 KB (+0.02% 🔺) 2.3 s (+0.02% 🔺) 1.4 s (+9.2% 🔺) 3.6 s
History retrieval protocols 111.81 KB (+0.03% 🔺) 2.3 s (+0.03% 🔺) 1.6 s (-15.97% 🔽) 3.8 s
Deterministic Message Hashing 7.29 KB (0%) 146 ms (0%) 365 ms (+252.39% 🔺) 511 ms

@danisharora099
Copy link
Collaborator

danisharora099 commented Jul 17, 2024

Is this ready for review? Let's update the description

@weboko
Copy link
Collaborator Author

weboko commented Jul 17, 2024

@danisharora099 it's ready and I think description is up to date :D


const libp2pOptions = options?.libp2p ?? {};
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];

if (options?.defaultBootstrap) {
peerDiscovery.push(...defaultPeerDiscoveries(options.pubsubTopics));
peerDiscovery.push(...defaultPeerDiscoveries(options.pubsubTopics!));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we workaround not having to enforce its definition by adding a !?

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 is due to update happening inside of a function so TS don't pick it up and complains.
To address this I think we should fully rework the approach to options and my proposal is here with draft PR #2073

packages/sdk/src/utils/libp2p.ts Outdated Show resolved Hide resolved
Comment on lines +137 to +146
if (options.contentTopics) {
options.shardInfo = { contentTopics: options.contentTopics };
}

const shardInfo = options.shardInfo
? ensureShardingConfigured(options.shardInfo)
: undefined;

options.pubsubTopics = shardInfo?.pubsubTopics ??
options.pubsubTopics ?? [DefaultPubsubTopic];
Copy link
Collaborator

@danisharora099 danisharora099 Jul 17, 2024

Choose a reason for hiding this comment

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

nit: Let's follow the same if-else syntax as you used for options.contentTopics for other two as well. Or the other way around with ? / : operators

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think usage of ?? is the most convenient at this point as each of operators would require a condition

Comment on lines +129 to +148
if (flags.length > 1) {
throw Error(
"Too many network configurations provided. Pass only one of: pubsubTopic, contentTopics or shardInfo."
);
}

logWhichShardInfoIsUsed(options);

if (options.contentTopics) {
options.shardInfo = { contentTopics: options.contentTopics };
}

const shardInfo = options.shardInfo
? ensureShardingConfigured(options.shardInfo)
: undefined;

options.pubsubTopics = shardInfo?.pubsubTopics ??
options.pubsubTopics ?? [DefaultPubsubTopic];

return shardInfo?.shardInfo;
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 use the same if-else syntax for shardInfo and pubsubTopics as well, as used for contentTopics

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

as I mentioned here - I don't think it is improvement for readability

if (shardInfo?.pubsubTopics) {
  options.pubsubTopics = shardInfo.pubsubTopics;
} else if (!options.pubsubTopics) {
  options.pubsubTopics = [DefaultPubsubTopic];
}

if (shardInfo?.shardInfo) {
  return shardInfo.shardInfo;
} 

return undefined;

again, I think this area should be re-worked and here is the task #2073

@@ -128,6 +117,37 @@ export async function createLibp2pAndUpdateOptions(
return libp2p;
}

function configureNetworkOptions(
options: CreateWakuNodeOptions
): ShardInfo | undefined {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why does this return undefined optionally?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

because this is how types are defined and shardInfo can be missing in some cases

@weboko weboko merged commit 2b02f82 into master Jul 19, 2024
10 of 11 checks passed
@weboko weboko deleted the weboko/network-config branch July 19, 2024 17:35
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