Skip to content

Add peer discovery - #17

Merged
tzdybal merged 18 commits into
mainfrom
tzdybal/p2p/discovery
Mar 23, 2021
Merged

Add peer discovery#17
tzdybal merged 18 commits into
mainfrom
tzdybal/p2p/discovery

Conversation

@tzdybal

Copy link
Copy Markdown
Contributor

Resolves#2.

@tzdybaltzdybal mentioned this pull request Mar 5, 2021
@tzdybal
tzdybalforce-pushed the tzdybal/p2p/bootstrapping branch from 63118f1 to bb1da59CompareMarch 9, 2021 13:28
@tzdybal
tzdybal changed the base branch from tzdybal/p2p/bootstrapping to mainMarch 9, 2021 13:30
@tzdybal
tzdybalforce-pushed the tzdybal/p2p/discovery branch from b50405d to fc66088CompareMarch 9, 2021 13:35
In default ('auto') mode, peers are not advertising as DHT servers, and
peer discovery is filtering them out during peer traversal.
@tzdybal
tzdybalforce-pushed the tzdybal/p2p/discovery branch from fc66088 to f924497CompareMarch 9, 2021 14:08
@codecov-io

codecov-io commented Mar 13, 2021

Copy link
Copy Markdown

Codecov Report

Merging #17 (e97f4bf) into main (cd64a50) will increase coverage by 5.40%.
The diff coverage is 72.46%.

Impacted file tree graph

@@ Coverage Diff @@## main #17 +/- ##
==========================================
+ Coverage 66.66% 72.07% +5.40% 
==========================================
Files 3 3 Lines 63 111 +48 ==========================================
+ Hits 42 80 +38 - Misses 12 16 +4 - Partials 9 15 +6 
Impacted FilesCoverage Δ
p2p/client.go69.47% <72.46%> (+9.89%)⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cd64a50...e97f4bf. Read the comment docs.

@tzdybal
tzdybal marked this pull request as ready for review March 17, 2021 12:28
@tzdybal
tzdybal requested review from Wondertan and liamsiMarch 17, 2021 12:29
@tzdybaltzdybal mentioned this pull request Mar 18, 2021
Comment threadp2p/client.go
Comment threadp2p/client.go
func (c *Client) Close() error {
dhtErr := c.dht.Close()
if dhtErr != nil {
c.logger.Error("failed to close DHT", "error", dhtErr)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not return it? We can either:

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Currently it's the first option ;) dhtErr is returned in line 99.
I just want to log both errors, in case only one of them is returned.

Comment threadconfig/defaults.go Outdated
Comment threadp2p/client.go
func (c *Client) listen() error {
// TODO(tzdybal): consider requiring listen address in multiaddress format
maddr, err := GetMultiAddr(c.conf.ListenAddress)
var err error

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe declare var within func body(err error)?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

You mean named return value?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes

Comment threadp2p/client.go Outdated
}

var err error
c.dht, err = dht.New(c.ctx, c.host, dht.Mode(dht.ModeServer), dht.BootstrapPeers(seedNodes...))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why ModeServer if this is a Client?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Found the answer in the commit. Can you please point to the place in the code where filtering of non-Server nodes happens?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Any updates? I think that this setting at least have to be configured

@tzdybaltzdybalMar 19, 2021

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Here, is the filtering: https://github.com/libp2p/go-libp2p-kad-dht/blob/master/subscriber_notifee.go#L148
To ensure, that we're support DHT protocols, we need to go to server mode: https://github.com/libp2p/go-libp2p-kad-dht/blob/master/dht.go#L727

Comment threadp2p/client.go
}
}

func (c *Client) getSeedAddrInfo(seedStr string) []peer.AddrInfo {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why about having seeds as an array and not a single string?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

cosmos-sdk is providing a single, comma-separated string anyways, so converting from string to slice is required.
But it may fit better in the conv package, as this logic is not client-specific.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

After re-thinking I'll leave it as is. I tried to:

  1. only split string into slice in conv; but it makes code more complicated and actual parsing is located in two packages
  2. move entire method to conv; but error handling is client-specific - I can't think of other use of "parse seed nodes and only log errors if necessary, without returning errors" function ;)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you pls provide an example of such a string with multiple seeds? How is that related to Cosmos? I was thinking that this is a multiaddr string

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

cosmos-sdk app, will use optimint as a drop-in replacement of tendermint (see master-optimint branch on our fork).
For better user experience, we reuse configurations, command line options, etc from cosmos-sdk. And cosmos-sdk apps have a config/option Seeds which is a comma separated string. The actual change is that we expect multiaddr format, not cosmos-sdk format.
Example is: /ip4/192.168.1.1/tcp/1234/p2p/seed1_id,/ip4/192.168.1.2/tcp/4321/p2p/seed2_id
Ref: #16

Comment threadp2p/client.go
}

// Close gently stops Client.
func (c *Client) Close() error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You need to cancel the ctx within the Client on Close, otherwise the routine created by discovery.Advertise leaks.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Fixed in e97f4bf.

Comment threadp2p/client.go Outdated
Comment threadp2p/client_test.go Outdated
Comment threadp2p/client_test.go Outdated

@tzdybaltzdybal left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Thanks for review. I'll address those issues

Comment threadp2p/client.go Outdated
chainID string
privKey crypto.PrivKey

ctx context.Context

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

As you said, I kind-of mimicked libp2p. But maybe it's worth fixing this; the sooner the better.

Comment threadp2p/client.go
Comment threadp2p/client.go
func (c *Client) Close() error {
dhtErr := c.dht.Close()
if dhtErr != nil {
c.logger.Error("failed to close DHT", "error", dhtErr)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Currently it's the first option ;) dhtErr is returned in line 99.
I just want to log both errors, in case only one of them is returned.

Comment threadp2p/client.go
}
}

func (c *Client) getSeedAddrInfo(seedStr string) []peer.AddrInfo {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

cosmos-sdk is providing a single, comma-separated string anyways, so converting from string to slice is required.
But it may fit better in the conv package, as this logic is not client-specific.

Comment threadp2p/client.go
func (c *Client) listen() error {
// TODO(tzdybal): consider requiring listen address in multiaddress format
maddr, err := GetMultiAddr(c.conf.ListenAddress)
var err error

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

You mean named return value?

Comment threadconfig/defaults.go Outdated
@tzdybal
tzdybal requested a review from WondertanMarch 19, 2021 15:35
@tzdybaltzdybal mentioned this pull request Mar 22, 2021
Comment threadp2p/client.go

func (c *Client) findPeers(ctx context.Context) error {
// TODO(tzdybal): add configuration parameter for max peers
peerCh, err := c.disc.FindPeers(ctx, c.getNamespace(), cdiscovery.Limit(60))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Extract to a constant like reAdveritse?

@Wondertan

Copy link
Copy Markdown
Contributor

@liamsi, can I have write access?

@tzdybal

Copy link
Copy Markdown
ContributorAuthor

@liamsi, can I have write access?

Granted!

@tzdybal
tzdybalforce-pushed the tzdybal/p2p/discovery branch from e97f4bf to 7777439CompareMarch 23, 2021 09:27
@tzdybal
tzdybal merged commit 567292e into mainMar 23, 2021
@tzdybal
tzdybal deleted the tzdybal/p2p/discovery branch March 23, 2021 09:34
@tzdybal
tzdybal restored the tzdybal/p2p/discovery branch October 9, 2021 13:35
@tzdybal
tzdybal deleted the tzdybal/p2p/discovery branch October 9, 2021 13:37
@claudeclaudeBot mentioned this pull request Nov 5, 2025
11 tasks
@claudeclaudeBot mentioned this pull request Nov 18, 2025
@claudeclaudeBot mentioned this pull request Jan 26, 2026
Sign up for freeto 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.

Peer discovery

4 participants

@tzdybal@codecov-io@Wondertan@liamsi