Skip to content

Commit 8d84ecd

Browse files
committed
functional parameters
1 parent d042680 commit 8d84ecd

File tree

3 files changed

+97
-46
lines changed

3 files changed

+97
-46
lines changed

Diff for: examples/echo/main.go

+8-27
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ import (
1818
net "github.com/libp2p/go-libp2p-net"
1919
peer "github.com/libp2p/go-libp2p-peer"
2020
pstore "github.com/libp2p/go-libp2p-peerstore"
21-
swarm "github.com/libp2p/go-libp2p-swarm"
22-
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
2321
ma "github.com/multiformats/go-multiaddr"
2422
gologging "github.com/whyrusleeping/go-logging"
25-
msmux "github.com/whyrusleeping/go-smux-multistream"
26-
yamux "github.com/whyrusleeping/go-smux-yamux"
2723
)
2824

2925
// makeBasicHost creates a LibP2P host with a random peer ID listening on the
@@ -42,37 +38,21 @@ func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error
4238

4339
// Generate a key pair for this host. We will use it at least
4440
// to obtain a valid host ID.
45-
priv, pub, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r)
41+
priv, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r)
4642
if err != nil {
4743
return nil, err
4844
}
4945

50-
// Obtain Peer ID from public key
51-
pid, err := peer.IDFromPublicKey(pub)
52-
if err != nil {
53-
return nil, err
54-
}
55-
56-
// Create a multiaddress
57-
addr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", listenPort))
58-
59-
if err != nil {
60-
return nil, err
46+
opts := []libp2p.Option{
47+
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", listenPort)),
48+
libp2p.WithPeerKey(priv),
6149
}
6250

63-
// Create a peerstore
64-
ps := pstore.NewPeerstore()
65-
66-
// If using secio, we add the keys to the peerstore
67-
// for this peer ID.
68-
if secio {
69-
ps.AddPrivKey(pid, priv)
70-
ps.AddPubKey(pid, pub)
51+
if !secio {
52+
opts = append(opts, libp2p.NoSecio)
7153
}
7254

73-
basicHost, err := libp2p.NewWithCfg(ctx, &libp2p.Config{
74-
ListenAddrs: []ma.Multiaddr{addr},
75-
})
55+
basicHost, err := libp2p.New(context.Background(), opts...)
7656
if err != nil {
7757
return nil, err
7858
}
@@ -82,6 +62,7 @@ func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error
8262

8363
// Now we can build a full multiaddress to reach this host
8464
// by encapsulating both addresses:
65+
addr := basicHost.Addrs()[0]
8566
fullAddr := addr.Encapsulate(hostAddr)
8667
log.Printf("I am %s\n", fullAddr)
8768
if secio {

Diff for: examples/libp2p-host/host.go

+9-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
libp2p "github.com/libp2p/go-libp2p"
99
crypto "github.com/libp2p/go-libp2p-crypto"
10-
ma "github.com/multiformats/go-multiaddr"
1110
)
1211

1312
func main() {
@@ -23,27 +22,23 @@ func main() {
2322

2423
fmt.Printf("Hello World, my hosts ID is %s\n", h.ID())
2524

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)
25+
// If you want more control over the configuration, you can specify some
26+
// options to the constructor
2927

3028
// Set your own keypair
3129
priv, _, err := crypto.GenerateEd25519Key(rand.Reader)
3230
if err != nil {
3331
panic(err)
3432
}
35-
cfg.PeerKey = priv
3633

37-
// Set your own listen address
38-
maddr, err := ma.NewMultiaddr("/ip4/0.0.0.0/tcp/9000")
39-
if err != nil {
40-
panic(err)
41-
}
42-
43-
// The config takes an array of addresses, specify as many as you want.
44-
cfg.ListenAddrs = []ma.Multiaddr{maddr}
34+
h2, err := libp2p.New(ctx,
35+
// Use your own created keypair
36+
libp2p.WithPeerKey(priv),
4537

46-
h2, err := libp2p.NewWithCfg(ctx, cfg)
38+
// Set your own listen address
39+
// The config takes an array of addresses, specify as many as you want.
40+
libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/9000"),
41+
)
4742
if err != nil {
4843
panic(err)
4944
}

Diff for: libp2p.go

+80-5
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,88 @@ type Config struct {
3232
DisableSecio bool
3333
}
3434

35-
func New(ctx context.Context) (host.Host, error) {
36-
return NewWithCfg(ctx, DefaultConfig())
35+
type Option func(cfg *Config) error
36+
37+
func Transports(tpts []transport.Transport) Option {
38+
return func(cfg *Config) error {
39+
cfg.Transports = tpts
40+
return nil
41+
}
42+
}
43+
44+
func ListenAddrStrings(s ...string) Option {
45+
return func(cfg *Config) error {
46+
for _, addrstr := range s {
47+
a, err := ma.NewMultiaddr(addrstr)
48+
if err != nil {
49+
return err
50+
}
51+
cfg.ListenAddrs = append(cfg.ListenAddrs, a)
52+
}
53+
return nil
54+
}
55+
}
56+
57+
func ListenAddrs(addrs []ma.Multiaddr) Option {
58+
return func(cfg *Config) error {
59+
cfg.ListenAddrs = append(cfg.ListenAddrs, addrs...)
60+
return nil
61+
}
62+
}
63+
64+
func NoSecio(cfg *Config) error {
65+
cfg.DisableSecio = true
66+
return nil
67+
}
68+
69+
func WithMuxer(m mux.Transport) Option {
70+
return func(cfg *Config) error {
71+
cfg.Muxer = m
72+
return nil
73+
}
74+
}
75+
76+
func WithPeerstore(ps pstore.Peerstore) Option {
77+
return func(cfg *Config) error {
78+
cfg.Peerstore = ps
79+
return nil
80+
}
81+
}
82+
83+
func WithNetProtector(prot pnet.Protector) Option {
84+
return func(cfg *Config) error {
85+
cfg.Protector = prot
86+
return nil
87+
}
88+
}
89+
90+
func WithBandwidthReporter(rep metrics.Reporter) Option {
91+
return func(cfg *Config) error {
92+
cfg.Reporter = rep
93+
return nil
94+
}
95+
}
96+
97+
func New(ctx context.Context, opts ...Option) (host.Host, error) {
98+
cfg := DefaultConfig()
99+
100+
for _, opt := range opts {
101+
if err := opt(cfg); err != nil {
102+
return nil, err
103+
}
104+
}
105+
106+
return newWithCfg(ctx, cfg)
107+
}
108+
109+
func WithPeerKey(sk crypto.PrivKey) Option {
110+
return func(cfg *Config) error {
111+
cfg.PeerKey = sk
112+
return nil
113+
}
37114
}
38115

39-
// Construct instantiates a libp2p host using information from the given
40-
// config. `nil` may be passed to use default options.
41-
func NewWithCfg(ctx context.Context, cfg *Config) (host.Host, error) {
116+
func newWithCfg(ctx context.Context, cfg *Config) (host.Host, error) {
42117
if cfg == nil {
43118
cfg = DefaultConfig()
44119
}

0 commit comments

Comments
 (0)