Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Rewrite the libp2p networking #742

Merged
merged 16 commits into from
Sep 26, 2018
Merged

Conversation

tomaka
Copy link
Contributor

@tomaka tomaka commented Sep 14, 2018

Rewrites the libp2p code to use the new Swarm API of libp2p.
While this rewrite probably contains more bugs that the previous version, the code is globally much more clean and predictable, and is much less magic compared to the previous one.

Small walk through the code:

  • secret.rs just contains loading/saving the private key on the disk, extracted from the former network_state.rs.
  • node_handler.rs contains the behavior for one single node we connect to.
  • swarm.rs contains the listeners and all the peers we're connected to, and has an API that allows opening Kademlia and Substrate substreams towards peers. It handles things like NAT traversal internally.
  • service_task.rs wraps around swarm.rs and handles the topology, connecting to new nodes, reserved nodes, banned nodes, max/min number of nodes, etc. and Kademlia queries.
  • service.rs wraps around service_task.rs and exposes an API compatible with network.rs, which I intend to rework in the future.

While this is ready for review, I keep finding small bugs from time to time (mostly on the side of libp2p), so it's not totally ready to be merged. libp2p/rust-libp2p#482 seems to have fixed everything.

Once merged and backported, I think we should deploy on a single bootstrap node first, and then deploy on more and more boot nodes if it works correctly.

@tomaka tomaka added the A0-please_review Pull request needs code review. label Sep 14, 2018
tomaka added a commit to tomaka/polkadot that referenced this pull request Sep 14, 2018
tomaka added a commit to tomaka/polkadot that referenced this pull request Sep 14, 2018
gavofyork pushed a commit that referenced this pull request Sep 15, 2018
@gavofyork gavofyork added A4-gotissues and removed A0-please_review Pull request needs code review. labels Sep 18, 2018
@gavofyork
Copy link
Member

image

Running the v0.2 backport on the office node after a day or two.

@tomaka
Copy link
Contributor Author

tomaka commented Sep 18, 2018

I don't think the sync issue is new.
It was probably caused by a problem when the nodes are full or near full (substrate streams being dropped even when a connection is established), and deploying this PR should fix that problem.
I'm unable to reproduce the sync issue on my side, and I assume that it's because it's already deployed on bootnodes.

@gavofyork
Copy link
Member

i'll try with the latest v0.2 if you think it's fixed.

@tomaka
Copy link
Contributor Author

tomaka commented Sep 18, 2018

There is still a panic happening in libp2p every 3/4 hours that I need to understand, and I should write proofs for the panics in substrate's code. I'm working on both these things right now.

@tomaka
Copy link
Contributor Author

tomaka commented Sep 19, 2018

Should be good now.

@tomaka tomaka added A0-please_review Pull request needs code review. and removed A4-gotissues labels Sep 19, 2018
@gavofyork
Copy link
Member

this is with a later p2p; still happening:

2018-09-19 21:47:09 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:47:14 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:47:19 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:47:24 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:47:29 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:47:34 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:47:39 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:47:44 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:47:49 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:47:54 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:47:59 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:48:04 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:48:09 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:48:14 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:48:19 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:48:24 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:48:29 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:48:34 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:48:39 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:48:44 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:48:49 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:48:54 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:48:59 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:49:04 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:49:09 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:49:14 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:49:19 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:49:24 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:49:29 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:49:34 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:49:39 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:49:44 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:49:49 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:49:54 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:49:59 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:50:04 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:50:09 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:50:14 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:50:19 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:50:24 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:50:29 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:50:34 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:50:39 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:50:44 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:50:49 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:50:54 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:50:59 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:51:04 Idle (43 peers), best: #1457401 (8905�63bf)
2018-09-19 21:51:09 Idle (43 peers), best: #1457401 (8905�63bf)

@gavofyork
Copy link
Member

gavofyork commented Sep 19, 2018

i'll try again with the latest PR once the latest PR has been backported, but honestly, i feel quite uncomfortable. when this bug is fixed, i'd like to be certain that it really is.

@gavofyork
Copy link
Member

@tomaka how's this looking?

@gavofyork gavofyork added A0-please_review Pull request needs code review. and removed A4-gotissues labels Sep 25, 2018
@tomaka
Copy link
Contributor Author

tomaka commented Sep 26, 2018

I can continue to improve the code by splitting it into more modules and moving some parts to the libp2p codebase, or we can review the code as it is, depending on whether it's pressing or not.

@arkpar arkpar added A6-seemsok and removed A0-please_review Pull request needs code review. labels Sep 26, 2018
@arkpar
Copy link
Member

arkpar commented Sep 26, 2018

Looks ok in general. My own tests show that it is more stable. Let's merge this

@arkpar arkpar merged commit 7c28ab4 into paritytech:master Sep 26, 2018
@tomaka tomaka deleted the libp2p-rewrite branch September 26, 2018 10:06
@webmaster128
Copy link
Contributor

Why did you choose secp256k1 for p2p level keypairs when the rest of substrate uses Ed25519? Is this a libp2p requirement?

@tomaka
Copy link
Contributor Author

tomaka commented Dec 24, 2018

@webmaster128 Libp2p supports ed25519, secp256k1 and RSA. We can use any of the three, even on the same network.
Secp256k1 is favoured because from a very pragmatic perspective it's the easiest to generate keys for, and because that's what the ethereum networking uses if I'm not mistaken.
However switching to another format wouldn't be a breaking change for the network.

@webmaster128
Copy link
Contributor

@tomaka Interesting, thanks! Why is it easier to generate keys for Secp256k1 than Ed25519? In Secp256k1, not every 32 byte value is a valid key, which makes using --node-key from /dev/random without special Secp256k1 tooling a matter of luck (your chances are good to hit a valid one, but still). Using Ed25519 allows every 32 byte value as the private key (called "seed" in many places).

that's what the ethereum networking uses if I'm not mistaken

Correct, they use it for transaction level signing. One advantage of Secp256k1 is that you can so public HD derivation using Slip0010. But this is not a property which is needed here.

@tomaka
Copy link
Contributor Author

tomaka commented Dec 24, 2018

Why is it easier to generate keys for Secp256k1 than Ed25519?

Sorry, it was not a problem of generating the key, but it's a similar very stupid problem: the ed25519 library we were using in libp2p (ring) doesn't expose any API to determine the public key from a private key, so we would have to store both the public and private keys on the disk, and verify that they match when we load them. That's extremely clumsy, and also longer to implement.

We switched to a different ed25519 library because of all the issues we were having with ring, so we could give it a try again.

@webmaster128
Copy link
Contributor

Thanks! Would be great to switch that.

Just in case you're still unhappy with Ed25519 libs: in one rust project I use ed25519_dalek. Looks like it is incredible flexible (you can switch out the internal Sha512 hashing if you need to) but does the job:
https://github.com/webmaster128/lisk-vanity/blob/9ca9ab7ec4cb896/src/derivation.rs#L14-L18

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants