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

refactor for transport changes #299

Merged
merged 2 commits into from
Jun 6, 2018
Merged

refactor for transport changes #299

merged 2 commits into from
Jun 6, 2018

Conversation

Stebalien
Copy link
Member

@Stebalien Stebalien commented Mar 13, 2018

Also, make the libp2p constructor fully useful. There should now be no need to manually construct a swarm/host. That's actually where most of the new code comes from (hence the +450 lines of (actual) code). This allows me to actually use the libp2p constructor in go-ipfs.

DO NOT MERGE (yet)
Part of: #297

@ghost ghost assigned Stebalien Mar 13, 2018
@ghost ghost added the status/in-progress In progress label Mar 13, 2018
Copy link
Member Author

@Stebalien Stebalien left a comment

Choose a reason for hiding this comment

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

While I usually don't like dynamic typing or duck typing, I figured that the usability benefit of duck typing in the libp2p constructor significantly outweighs the drawbacks. Anyways, errors should be caught as soon as the application starts.

type AddrsFactory = bhost.AddrsFactory

// NATManagerC is a NATManager constructor.
type NATManagerC func(inet.Network) bhost.NATManager
Copy link
Member Author

Choose a reason for hiding this comment

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

Could also call this NATManagerConstructor (hence the C) or NATManagerFactory. However, while technically exported, users should never need to actually use this type (or any of the other *C types).

Copy link
Member

Choose a reason for hiding this comment

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

Still better than in Java world.

@@ -0,0 +1,173 @@
package config
Copy link
Member Author

Choose a reason for hiding this comment

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

I moved this logic into a separate package for organization (it was growing a bit large). I left all the actual options in the main libp2p package.

config/config.go Outdated
type Config struct {
Transports []TptC
Muxers []MsMuxC
SecurityTransports []MsSecC
Copy link
Member Author

Choose a reason for hiding this comment

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

One can now easily plug in new security transports.

config/config.go Outdated
// Config describes a set of settings for a libp2p node
type Config struct {
Transports []TptC
Muxers []MsMuxC
Copy link
Member Author

Choose a reason for hiding this comment

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

Instead of taking a single transport, we now take an array of named multiplexer transports. Why not a map? We need the order (priority).

libp2p.go Outdated
if err != nil {
return nil, err
// EnableRelay configures libp2p to enable the relay transport.
func EnableRelay(options ...circuit.RelayOpt) Option {
Copy link
Member Author

Choose a reason for hiding this comment

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

This isn't the most user-friendly interface. It also kind of sucks that one has to import the relay package to call this.

@@ -270,10 +243,6 @@ func (h *BasicHost) newStreamHandler(s inet.Stream) {
}

s.SetProtocol(protocol.ID(protoID))

if h.bwc != nil {
s = mstream.WrapStream(s, h.bwc)
Copy link
Member Author

Choose a reason for hiding this comment

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

The swarm now takes care of this before it even hits the identify service.

)

func init() {
// change the garbage collect timeout for testing.
ps.GarbageCollectTimeout = 10 * time.Millisecond
Copy link
Member Author

Choose a reason for hiding this comment

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

poof


// ConnManager is a libp2p connection manager
ConnManager ifconnmgr.ConnManager

// Relay indicates whether the host should use circuit relay transport
EnableRelay bool
Copy link
Member Author

Choose a reason for hiding this comment

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

Handled by the libp2p constructor.


// BandwidthReporter is used for collecting aggregate metrics of the
// bandwidth used by various protocols.
BandwidthReporter metrics.Reporter
Copy link
Member Author

Choose a reason for hiding this comment

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

Was only needed by the identity service because the old swarm didn't enable the bandwidth reporter early enough. That's no longer the case so the host doesn't need this now.

@@ -536,11 +497,6 @@ func (h *BasicHost) Close() error {
return h.proc.Close()
}

// GetBandwidthReporter exposes the Host's bandiwth metrics reporter
func (h *BasicHost) GetBandwidthReporter() metrics.Reporter {
Copy link
Member Author

Choose a reason for hiding this comment

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

This isn't a part of the Host interface and I can't find anything that actually uses it. Really, users should just hang onto the bandwidth reporter that they passed in.

@Stebalien Stebalien requested review from vyzo and whyrusleeping March 13, 2018 05:08
@Stebalien Stebalien mentioned this pull request Mar 13, 2018
72 tasks
Copy link
Contributor

@vyzo vyzo left a comment

Choose a reason for hiding this comment

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

this overall looks good, but i skimmed through the reflection details.

libp2p.go Outdated

return tpt
// NATPortMap configures libp2p to use the default NATManager. The default
// NATManager will attempt to punch holes in your NAT.
Copy link
Contributor

@vyzo vyzo Mar 13, 2018

Choose a reason for hiding this comment

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

this comment is technically incorrect -- doesn't the nat manager attempt to open ports using UPnP?
It doesn't really use hole punching.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, good point.

config/config.go Outdated
if ps == nil {
ps = pstore.NewPeerstore()
}
if !cfg.Insecure {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we keep lugging around the insecure option?

Copy link
Member Author

@Stebalien Stebalien Mar 13, 2018

Choose a reason for hiding this comment

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

We use it for debugging. It's actually really useful. However, it's designed to be an all or nothing "disable all security" option.

Copy link
Contributor

Choose a reason for hiding this comment

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

fair enough! I can see it being useful in analysing wire protocols without encryption.

Copy link
Contributor

@whyrusleeping whyrusleeping left a comment

Choose a reason for hiding this comment

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

LGTM (reviewed in a video call together)

@Stebalien
Copy link
Member Author

@whyrusleeping massively reduced the code duplication for the reflection stuff. It turned out to be much easier than I had thought. I left the optional error in the transport constructor return type but broke the code int a bunch of smaller, readable functions.

@Stebalien Stebalien force-pushed the feat/refactor branch 5 times, most recently from 12d3618 to fd3f3d9 Compare March 15, 2018 00:37
@hsanjuan
Copy link
Contributor

I was looking to bubble go-reuseport with a fixed go-log that does not panic randomly. Or do you prefer to keep libp2p frozen until all these changes come in?

@Stebalien
Copy link
Member Author

Feel free (that's why I'm merging these all at the same time).

@Stebalien Stebalien force-pushed the feat/refactor branch 4 times, most recently from 99b1900 to 29228f2 Compare March 26, 2018 22:52
@Stebalien Stebalien requested a review from Kubuxu March 27, 2018 18:16
config/config.go Outdated
ConnManager ifconnmgr.ConnManager
AddrsFactory bhost.AddrsFactory
Filters *filter.Filters
NATManager NATManagerC
Copy link
Member

Choose a reason for hiding this comment

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

Some spacing ques would be good here. Like Relay and RelayOpts can have their own section.

config/config.go Outdated
func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
// Check this early. Prevents us from even *starting* without verifying this.
if pnet.ForcePrivateNetwork && cfg.Protector == nil {
log.Error("tried to dial with no Private Network Protector but usage" +
Copy link
Member

Choose a reason for hiding this comment

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

dial is not accurate here

config/config.go Outdated
return nil, err
}

// Create a new blank peerstore if none was passed in
Copy link
Member

Choose a reason for hiding this comment

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

Comment not accurate, either change behaviour or the comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. I moved the defaults to a special FallbackDefaults option.

config/muxer.go Outdated
}, nil
}

fn, err := makeConstructor(m, muxType, muxArgTypes)
Copy link
Member

Choose a reason for hiding this comment

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

IMO s/fn/ctor/

package.json Outdated
{
"author": "whyrusleeping",
"hash": "QmYkxWtBWqkj6SWCiFN2a3xnnk5womAQYd9Kh68LJJR7jz",
"name": "go-ws-transport",
Copy link
Member

Choose a reason for hiding this comment

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

do we have to depend on a transport?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a default transport so yes unless we want to make it not a default transport. Note: we used to depend on it in go-libp2p-swarm but I moved all default transport construction here.

Copy link
Member

@Kubuxu Kubuxu left a comment

Choose a reason for hiding this comment

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

One invalid comment, and a nitpick.

I am not a fan of voodoo magic but I don't think there is a better way.

@Stebalien
Copy link
Member Author

I am not a fan of voodoo magic but I don't think there is a better way.

I completely agree. My first attempt involved defining some nice interfaces but it turned into a mess.

Copy link
Member

@Kubuxu Kubuxu left a comment

Choose a reason for hiding this comment

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

SGWM

@Stebalien Stebalien force-pushed the feat/refactor branch 3 times, most recently from e3579af to cfe6ab5 Compare March 29, 2018 09:41
Stebalien added 2 commits June 4, 2018 21:22
Also, make the libp2p constructor fully useful. There should now be no need to
manually construct a swarm/host.
@Stebalien Stebalien merged commit 8bdf9d0 into master Jun 6, 2018
@ghost ghost removed the status/in-progress In progress label Jun 6, 2018
@Stebalien Stebalien deleted the feat/refactor branch June 6, 2018 07:28
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.

5 participants