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
48 changes: 34 additions & 14 deletions packages/sdk/src/utils/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,19 @@
...pubsubService,
...options?.services
}
}) as any as Libp2p; // TODO: make libp2p include it;

Check warning on line 89 in packages/sdk/src/utils/libp2p.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

Check warning on line 89 in packages/sdk/src/utils/libp2p.ts

View workflow job for this annotation

GitHub Actions / proto

Unexpected any. Specify a different type
}

export async function createLibp2pAndUpdateOptions(
options: CreateWakuNodeOptions
): Promise<Libp2p> {
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];
const shardInfo = configureNetworkOptions(options);

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

}

if (options?.bootstrapPeers) {
Expand All @@ -119,7 +108,7 @@
libp2pOptions.peerDiscovery = peerDiscovery;

const libp2p = await defaultLibp2p(
shardInfo?.shardInfo,
shardInfo,
wakuGossipSub(options),
libp2pOptions,
options?.userAgent
Expand All @@ -128,6 +117,37 @@
return libp2p;
}

function configureNetworkOptions(
options: CreateWakuNodeOptions
): ShardInfo | undefined {
const flags = [
options.contentTopics,
options.pubsubTopics,
options.shardInfo
].filter((v) => !!v);

if (flags.length > 1) {
throw Error(
"Too many network configurations, pass only: pubsubTopic, contentTopics or shardInfo."
weboko marked this conversation as resolved.
Show resolved Hide resolved
);
}

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];
Comment on lines +137 to +146
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


return shardInfo?.shardInfo;
}

function logWhichShardInfoIsUsed(options: CreateWakuNodeOptions): void {
if (options.pubsubTopics) {
logger.info("Using pubsubTopics array to bootstrap the node.");
Expand Down
10 changes: 7 additions & 3 deletions packages/tests/tests/filter/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,15 @@ export async function runMultipleNodes(
staticNoiseKey: NOISE_KEY_1,
libp2p: {
addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] }
},
pubsubTopics,
shardInfo
}
};

if (shardInfo) {
waku_options.shardInfo = shardInfo;
weboko marked this conversation as resolved.
Show resolved Hide resolved
} else {
waku_options.pubsubTopics = pubsubTopics;
}

log.info("Starting js waku node with :", JSON.stringify(waku_options));
let waku: LightNode | undefined;
try {
Expand Down
Loading