Skip to content

Commit d042680

Browse files
committed
use new constructor for examples
1 parent c937b88 commit d042680

File tree

3 files changed

+38
-47
lines changed

3 files changed

+38
-47
lines changed

examples/echo/main.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
mrand "math/rand"
1313

1414
golog "github.com/ipfs/go-log"
15+
libp2p "github.com/libp2p/go-libp2p"
1516
crypto "github.com/libp2p/go-libp2p-crypto"
1617
host "github.com/libp2p/go-libp2p-host"
1718
net "github.com/libp2p/go-libp2p-net"
@@ -69,28 +70,13 @@ func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error
6970
ps.AddPubKey(pid, pub)
7071
}
7172

72-
// Set up stream multiplexer
73-
tpt := msmux.NewBlankTransport()
74-
tpt.AddTransport("/yamux/1.0.0", yamux.DefaultTransport)
75-
76-
// Create swarm (implements libP2P Network)
77-
swrm, err := swarm.NewSwarmWithProtector(
78-
context.Background(),
79-
[]ma.Multiaddr{addr},
80-
pid,
81-
ps,
82-
nil,
83-
tpt,
84-
nil,
85-
)
73+
basicHost, err := libp2p.NewWithCfg(ctx, &libp2p.Config{
74+
ListenAddrs: []ma.Multiaddr{addr},
75+
})
8676
if err != nil {
8777
return nil, err
8878
}
8979

90-
netw := (*swarm.Network)(swrm)
91-
92-
basicHost := bhost.New(netw)
93-
9480
// Build host multiaddress
9581
hostAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s", basicHost.ID().Pretty()))
9682

examples/libp2p-host/host.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,48 @@ import (
55
"crypto/rand"
66
"fmt"
77

8+
libp2p "github.com/libp2p/go-libp2p"
89
crypto "github.com/libp2p/go-libp2p-crypto"
9-
peer "github.com/libp2p/go-libp2p-peer"
10-
pstore "github.com/libp2p/go-libp2p-peerstore"
11-
swarm "github.com/libp2p/go-libp2p-swarm"
12-
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
1310
ma "github.com/multiformats/go-multiaddr"
1411
)
1512

1613
func main() {
17-
// Generate an identity keypair using go's cryptographic randomness source
18-
priv, pub, err := crypto.GenerateEd25519Key(rand.Reader)
14+
// The context governs the lifetime of the libp2p node
15+
ctx, cancel := context.WithCancel(context.Background())
16+
defer cancel()
17+
18+
// To construct a simple host with all the default settings, just use `New`
19+
h, err := libp2p.New(ctx)
1920
if err != nil {
2021
panic(err)
2122
}
2223

23-
// A peers ID is the hash of its public key
24-
pid, err := peer.IDFromPublicKey(pub)
24+
fmt.Printf("Hello World, my hosts ID is %s\n", h.ID())
25+
26+
// If you want more control over the configuration, you can fill out fields
27+
// in the libp2p config, and use `NewWithCfg`
28+
cfg := new(libp2p.Config)
29+
30+
// Set your own keypair
31+
priv, _, err := crypto.GenerateEd25519Key(rand.Reader)
2532
if err != nil {
2633
panic(err)
2734
}
35+
cfg.PeerKey = priv
2836

29-
// We've created the identity, now we need to store it.
30-
// A peerstore holds information about peers, including your own
31-
ps := pstore.NewPeerstore()
32-
ps.AddPrivKey(pid, priv)
33-
ps.AddPubKey(pid, pub)
34-
37+
// Set your own listen address
3538
maddr, err := ma.NewMultiaddr("/ip4/0.0.0.0/tcp/9000")
3639
if err != nil {
3740
panic(err)
3841
}
3942

40-
// Make a context to govern the lifespan of the swarm
41-
ctx := context.Background()
43+
// The config takes an array of addresses, specify as many as you want.
44+
cfg.ListenAddrs = []ma.Multiaddr{maddr}
4245

43-
// Put all this together
44-
netw, err := swarm.NewNetwork(ctx, []ma.Multiaddr{maddr}, pid, ps, nil)
46+
h2, err := libp2p.NewWithCfg(ctx, cfg)
4547
if err != nil {
4648
panic(err)
4749
}
4850

49-
myhost := bhost.New(netw)
50-
fmt.Printf("Hello World, my hosts ID is %s\n", myhost.ID())
51+
fmt.Printf("Hello World, my second hosts ID is %s\n", h2.ID())
5152
}

libp2p.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ import (
2222

2323
// Config describes a set of settings for a libp2p node
2424
type Config struct {
25-
Transports []transport.Transport
26-
Muxer mux.Transport
27-
ListenAddrs []ma.Multiaddr
28-
PeerKey crypto.PrivKey
29-
Peerstore pstore.Peerstore
30-
Protector pnet.Protector
31-
Reporter metrics.Reporter
25+
Transports []transport.Transport
26+
Muxer mux.Transport
27+
ListenAddrs []ma.Multiaddr
28+
PeerKey crypto.PrivKey
29+
Peerstore pstore.Peerstore
30+
Protector pnet.Protector
31+
Reporter metrics.Reporter
32+
DisableSecio bool
3233
}
3334

3435
func New(ctx context.Context) (host.Host, error) {
@@ -63,8 +64,11 @@ func NewWithCfg(ctx context.Context, cfg *Config) (host.Host, error) {
6364
ps = pstore.NewPeerstore()
6465
}
6566

66-
ps.AddPrivKey(pid, cfg.PeerKey)
67-
ps.AddPubKey(pid, cfg.PeerKey.GetPublic())
67+
// If secio is disabled, don't add our private key to the peerstore
68+
if !cfg.DisableSecio {
69+
ps.AddPrivKey(pid, cfg.PeerKey)
70+
ps.AddPubKey(pid, cfg.PeerKey.GetPublic())
71+
}
6872

6973
swrm, err := swarm.NewSwarmWithProtector(ctx, cfg.ListenAddrs, pid, ps, cfg.Protector, cfg.Muxer, cfg.Reporter)
7074
if err != nil {

0 commit comments

Comments
 (0)