Skip to content

Commit c937b88

Browse files
committed
review feedback
1 parent f34163e commit c937b88

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

libp2p.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
yamux "github.com/whyrusleeping/go-smux-yamux"
2121
)
2222

23+
// Config describes a set of settings for a libp2p node
2324
type Config struct {
2425
Transports []transport.Transport
2526
Muxer mux.Transport
@@ -30,11 +31,18 @@ type Config struct {
3031
Reporter metrics.Reporter
3132
}
3233

33-
func Construct(ctx context.Context, cfg *Config) (host.Host, error) {
34+
func New(ctx context.Context) (host.Host, error) {
35+
return NewWithCfg(ctx, DefaultConfig())
36+
}
37+
38+
// Construct instantiates a libp2p host using information from the given
39+
// config. `nil` may be passed to use default options.
40+
func NewWithCfg(ctx context.Context, cfg *Config) (host.Host, error) {
3441
if cfg == nil {
3542
cfg = DefaultConfig()
3643
}
3744

45+
// If no key was given, generate a random 2048 bit RSA key
3846
if cfg.PeerKey == nil {
3947
priv, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, rand.Reader)
4048
if err != nil {
@@ -49,6 +57,7 @@ func Construct(ctx context.Context, cfg *Config) (host.Host, error) {
4957
return nil, err
5058
}
5159

60+
// Create a new blank peerstore if none was passed in
5261
ps := cfg.Peerstore
5362
if ps == nil {
5463
ps = pstore.NewPeerstore()
@@ -70,14 +79,17 @@ func Construct(ctx context.Context, cfg *Config) (host.Host, error) {
7079
func DefaultMuxer() mux.Transport {
7180
// Set up stream multiplexer
7281
tpt := msmux.NewBlankTransport()
82+
83+
// By default, support yamux and multiplex
7384
tpt.AddTransport("/yamux/1.0.0", yamux.DefaultTransport)
7485
tpt.AddTransport("/mplex/6.3.0", mplex.DefaultTransport)
86+
7587
return tpt
7688
}
7789

7890
func DefaultConfig() *Config {
79-
// Create a multiaddress
80-
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0")
91+
// Create a multiaddress that listens on a random port on all interfaces
92+
addr, err := ma.NewMultiaddr("/ip4/0.0.0.0/tcp/0")
8193
if err != nil {
8294
panic(err)
8395
}

0 commit comments

Comments
 (0)