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

fix peer_id in case of DialError::InvalidPeerId (#2425) #2428

Closed
wants to merge 4 commits into from

Conversation

rkuhn
Copy link
Contributor

@rkuhn rkuhn commented Jan 11, 2022

Previously, the negotiated PeerId was included in the swarm event and
inject_dial_failure’s arguments while the expected one was absent. This
patch adds the negotiated PeerId to the DialError and includes the expected
one in the notifications.

Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

Thank you for taking care of this @rkuhn!

/// match the one that was expected or is otherwise invalid.
InvalidPeerId,
/// match the one that was expected or is the local one.
InvalidPeerId(PeerId),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
InvalidPeerId(PeerId),
InvalidPeerId {
obtained: PeerId,
}

What do you think? That would make it clear that the provided one in InvalidPeerId is the one obtained on the connection.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, and I’ll want to add the failed address if possible (looking into it now).

core/tests/network_dial_error.rs Outdated Show resolved Hide resolved
core/tests/network_dial_error.rs Show resolved Hide resolved
core/tests/network_dial_error.rs Outdated Show resolved Hide resolved
core/tests/network_dial_error.rs Outdated Show resolved Hide resolved
core/tests/network_dial_error.rs Outdated Show resolved Hide resolved

swarm1.listen_on("/memory/0".parse().unwrap()).unwrap();

let address = async_std::task::block_on(future::poll_fn(|cx| match swarm1.poll(cx) {
Copy link
Member

Choose a reason for hiding this comment

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

Side comment: It is unfortunate that Network does not implement Future. Otherwise this could be simplified a lot.

core/tests/network_dial_error.rs Outdated Show resolved Hide resolved
Comment on lines 144 to 134
assert_eq!(peer_id, other_id);
match error {
PendingConnectionError::InvalidPeerId(id) => assert_eq!(id, *swarm1.local_peer_id()),
x => panic!("wrong error {:?}", x),
}
Copy link
Member

Choose a reason for hiding this comment

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

Why not do both of these within the match above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer to assert on the test function level when possible, for ease of stack trace reading (and reliability of reporting).

swarm/src/lib.rs Outdated Show resolved Hide resolved
@rkuhn
Copy link
Contributor Author

rkuhn commented Jan 12, 2022

@mxinden Thanks for the review! While trying this out and working a bit more on Actyx & ipfs-embed I found that InvalidPeerId should contain the address at which the mismatch occurred — since the user will very likely want to scratch that Multiaddr from any lists they may be maintaining (in conjunction with the expected PeerId).

The WASM failures seem unrelated to my changes (at least I hope so).

Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

One last comment. Otherwise looks good to me.

}
}
Ok(())
})
// Check peer is not local peer.
.and_then(|()| {
if self.local_id == peer_id {
Err(Error::InvalidPeerId)
Err(PendingConnectionError::InvalidPeerId {
obtained: peer_id,
Copy link
Member

Choose a reason for hiding this comment

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

How about renaming peer_id to obtained_peer_id? That would be consistent with expected_peer_id and would be less confusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, will do that once I get a minute — crunch time right now

rkuhn and others added 2 commits January 15, 2022 11:24
Previously, the negotiated PeerId was included in the swarm event and
inject_dial_failure’s arguments while the expected one was absent. This
patch adds the negotiated PeerId to the DialError and includes the expected
one in the notifications.
Co-authored-by: Max Inden <mail@max-inden.de>
@rkuhn
Copy link
Contributor Author

rkuhn commented Jan 15, 2022

Seeing that you added an actual case of “invalid peer id” in #2404, I think this PR needs to cover a bit more ground: we want to signal two distinct cases now. How about the following:

  • InvalidPeerId(multihash): the Protocol::P2p(multihash) didn’t correspond to a PeerId (why does P2p not hold a PeerId?)
  • WrongPeerId { obtained, address }: the ID obtained from the peer didn’t match our expectations

@mxinden
Copy link
Member

mxinden commented Jan 15, 2022

Seeing that you added an actual case of “invalid peer id” in #2404, I think this PR needs to cover a bit more ground.

👍 thanks for being rigorous here.

  • InvalidPeerId(multihash): the Protocol::P2p(multihash) didn’t correspond to a PeerId

👍

(why does P2p not hold a PeerId?)

I haven't been involved back then. I guess due to PeerId being defined inside the libp2p-core crate and multiaddr crate not wanting to depend on libp2p-core.

That said, I agree that this is not ideal. I would very much like this to be enforced at the type system level.

  • WrongPeerId { obtained, address }: the ID obtained from the peer didn’t match our expectations

👍

Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

Thanks for splitting the two error cases @rkuhn.

/// the connection did not match the one that was expected or is otherwise
/// invalid.
InvalidPeerId,
/// The provided peer identity is invalid
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// The provided peer identity is invalid
/// The provided peer identity is invalid.

/// match the one that was expected or is the local one.
WrongPeerId {
obtained: PeerId,
address: Multiaddr,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
address: Multiaddr,
address: ConnectedPoint,

How about a ConnectedPoint instead of a plain Multiaddr here? Mostly for the sake of additional context.

@mxinden
Copy link
Member

mxinden commented Jan 18, 2022

Seems like I am not allowed to push to this branch. I will merge this via #2441.

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

2 participants