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: refactor peer-exchange according to nwaku 0.15.0 #1193

Merged
merged 3 commits into from
Mar 14, 2023

Conversation

danisharora099
Copy link
Collaborator

@danisharora099 danisharora099 commented Feb 21, 2023

Problem

Peer Exchange is supposed to be a request-response protocol (https://rfc.vac.dev/spec/34/) but due to a bug in the earlier versions of nwaku, it would asynchronously send a callback on a different stream. To adapt to this, js-waku would have an event listener open.

With nwaku 0.15.0, this is addressed and peer-exchange works as the originally designed request-response protocol.

Solution

js-waku's peer exchange implementation now removes the event listener on the registrar, and use the response received in the request to dispatch the ENRs.

Notes

@danisharora099 danisharora099 marked this pull request as ready for review February 21, 2023 08:54
@github-actions
Copy link

github-actions bot commented Feb 21, 2023

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
Waku core 116.71 KB (+0.07% 🔺) 2.4 s (+0.07% 🔺) 3.3 s (+6.63% 🔺) 5.7 s
Waku default setup 383.99 KB (+2.93% 🔺) 7.7 s (+2.93% 🔺) 7.6 s (+4.13% 🔺) 15.3 s
ECIES encryption 27.99 KB (0%) 560 ms (0%) 1.7 s (+0.3% 🔺) 2.3 s
Symmetric encryption 27.99 KB (0%) 560 ms (0%) 1.7 s (-8.47% 🔽) 2.2 s
DNS discovery 108.07 KB (+0.02% 🔺) 2.2 s (+0.02% 🔺) 4.3 s (+50.63% 🔺) 6.5 s
Privacy preserving protocols 116.19 KB (+0.06% 🔺) 2.4 s (+0.06% 🔺) 2.5 s (-4.01% 🔽) 4.8 s
Light protocols 117.96 KB (+0.04% 🔺) 2.4 s (+0.04% 🔺) 3.1 s (+10.09% 🔺) 5.5 s
History retrieval protocols 118.03 KB (+0.07% 🔺) 2.4 s (+0.07% 🔺) 2.3 s (+10.47% 🔺) 4.6 s

@@ -18,19 +18,17 @@ import { Nwaku } from "../src/nwaku.js";
describe("Peer Exchange", () => {
let waku: LightNode;

before(async function () {
afterEach(async function () {
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
Copy link
Collaborator

Choose a reason for hiding this comment

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

async/await can be used to prettify code

Copy link
Collaborator

Choose a reason for hiding this comment

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

How would it look like? You'd need to add a try/catch block which would make this more verbose than necessary, no?

Copy link
Collaborator

Choose a reason for hiding this comment

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

something like

afterEach(async () => {
  try {
    await waku?.stop();
  } catch (err) {
    console.log("Waku failed to stop", e);
  }
});

It depends on perception what is verbose for this case.

Comment on lines 80 to 91
const enrs = await Promise.all(
decoded.peerInfos.map(
(peerInfo) => peerInfo.enr && ENR.decode(peerInfo.enr)
)
);

const peerInfos = enrs.map((enr) => {
return {
ENR: enr,
};
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

could probably merge both maps to avoid traversing the array twice

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we need to wait for the promise resolution first before converting it into an array again

Copy link
Collaborator

Choose a reason for hiding this comment

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

      const peerInfos = await Promise.all(
        decoded.peerInfos.map(
          (peerInfo) => {
              if (!peerInfo.enr) return;
              
	          const enr = await ENR.decode(peerInfo.enr);

             return { ENR: enr }
          }
        )
      );

Probably need to use a typeguard:

export function isDefined<T>(msg: T | undefined): msg is T {
  return !!msg;
}

Or use this lib: https://twitter.com/mattpocockuk/status/1627686864579600385?s=20

for (const _peerInfo of peerInfos) {
const { ENR } = _peerInfo;
if (!ENR) {
log("no ENR");
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can see that this should not be possible from the code in waku_peer_exchange.ts

I don't think this log line is useful.

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 of the protobuf definitions and typescript interpretations, it interprets that the fields might be optional which makes it necessary for us to handle that scenario without a forbidden assertion

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should be sorted with #1196

@@ -18,19 +18,17 @@ import { Nwaku } from "../src/nwaku.js";
describe("Peer Exchange", () => {
let waku: LightNode;

before(async function () {
afterEach(async function () {
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
Copy link
Collaborator

Choose a reason for hiding this comment

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

How would it look like? You'd need to add a try/catch block which would make this more verbose than necessary, no?

@danisharora099
Copy link
Collaborator Author

danisharora099 commented Feb 26, 2023

Will aim to merge this for now with the comments addressed in the interest of time as this PR also unblocks #1186. Please feel free to tag me again in case we want to address some of the remaining open conversations.

@danisharora099
Copy link
Collaborator Author

danisharora099 commented Feb 26, 2023

@fryorcraken are you aware why the px test with go-waku is failing with a timeout error, while it succeeds for node & nwaku? how can i run go-waku testing locally to reproduce the same?
(cc @richard-ramos)

@fryorcraken
Copy link
Collaborator

fryorcraken commented Feb 26, 2023

@fryorcraken are you aware why the px test with go-waku is failing with a timeout error, while it succeeds for node & nwaku?

I don't know if @richard-ramos has aligned go-waku's behaviour just yet.

how can i run go-waku testing locally to reproduce the same?

Just set some env vars (modify as needed).

WAKU_SERVICE_NODE_BIN=/home/fryorcraken/src/waku-org/go-waku/build/waku;WAKU_SERVICE_NODE_DIR=/home/fryorcraken/src/waku-org/go-waku/build;WAKU_SERVICE_NODE_PARAMS=--min-relay-peers-to-publish\=0

@richard-ramos
Copy link
Member

go-waku's peer exchange implementation is not compatible with nwaku. I'll look at it this week

@danisharora099 danisharora099 force-pushed the chore/refactor-px-nwaku0.15.0 branch from 69ba3b8 to eb885c5 Compare March 13, 2023 06:14
@danisharora099 danisharora099 force-pushed the chore/refactor-px-nwaku0.15.0 branch from eb885c5 to 55cc8f9 Compare March 13, 2023 06:15
@danisharora099
Copy link
Collaborator Author

go-waku's peer exchange implementation is not compatible with nwaku. I'll look at it this week

Hey @richard-ramos, is there an update yet? 😅

@fryorcraken
Copy link
Collaborator

go-waku's peer exchange implementation is not compatible with nwaku. I'll look at it this week

Hey @richard-ramos, is there an update yet? sweat_smile

Update CI to use go-waku 0.5.2 and see how it goes.

@danisharora099
Copy link
Collaborator Author

Merging this PR for now.

Tracking the breaking go-waku CI test here: waku-org/go-waku#491

@danisharora099 danisharora099 merged commit a20b797 into master Mar 14, 2023
@danisharora099 danisharora099 deleted the chore/refactor-px-nwaku0.15.0 branch March 14, 2023 08:26
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.

None yet

4 participants