Skip to content

Commit bbbc040

Browse files
committed
incorporate lgierths opinions
1 parent 8d84ecd commit bbbc040

File tree

3 files changed

+75
-35
lines changed

3 files changed

+75
-35
lines changed

examples/echo/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error
4545

4646
opts := []libp2p.Option{
4747
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", listenPort)),
48-
libp2p.WithPeerKey(priv),
48+
libp2p.Identity(priv),
4949
}
5050

5151
if !secio {
52-
opts = append(opts, libp2p.NoSecio)
52+
opts = append(opts, libp2p.NoEncryption())
5353
}
5454

5555
basicHost, err := libp2p.New(context.Background(), opts...)

examples/libp2p-host/host.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func main() {
3333

3434
h2, err := libp2p.New(ctx,
3535
// Use your own created keypair
36-
libp2p.WithPeerKey(priv),
36+
libp2p.Identity(priv),
3737

3838
// Set your own listen address
3939
// The config takes an array of addresses, specify as many as you want.

libp2p.go

+72-32
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package libp2p
33
import (
44
"context"
55
"crypto/rand"
6+
"fmt"
67

78
crypto "github.com/libp2p/go-libp2p-crypto"
89
host "github.com/libp2p/go-libp2p-host"
@@ -34,9 +35,9 @@ type Config struct {
3435

3536
type Option func(cfg *Config) error
3637

37-
func Transports(tpts []transport.Transport) Option {
38+
func Transports(tpts ...transport.Transport) Option {
3839
return func(cfg *Config) error {
39-
cfg.Transports = tpts
40+
cfg.Transports = append(cfg.Transports, tpts...)
4041
return nil
4142
}
4243
}
@@ -54,70 +55,110 @@ func ListenAddrStrings(s ...string) Option {
5455
}
5556
}
5657

57-
func ListenAddrs(addrs []ma.Multiaddr) Option {
58+
func ListenAddrs(addrs ...ma.Multiaddr) Option {
5859
return func(cfg *Config) error {
5960
cfg.ListenAddrs = append(cfg.ListenAddrs, addrs...)
6061
return nil
6162
}
6263
}
6364

64-
func NoSecio(cfg *Config) error {
65-
cfg.DisableSecio = true
66-
return nil
65+
type transportEncOpt int
66+
67+
const (
68+
EncPlaintext = transportEncOpt(0)
69+
EncSecio = transportEncOpt(1)
70+
)
71+
72+
func TransportEncryption(tenc ...transportEncOpt) Option {
73+
return func(cfg *Config) error {
74+
if len(tenc) != 1 {
75+
return fmt.Errorf("can only specify a single transport encryption option right now")
76+
}
77+
78+
// TODO: actually make this pluggable, otherwise tls will get tricky
79+
switch tenc[0] {
80+
case EncPlaintext:
81+
cfg.DisableSecio = true
82+
case EncSecio:
83+
// noop
84+
default:
85+
return fmt.Errorf("unrecognized transport encryption option: %d", tenc[0])
86+
}
87+
return nil
88+
}
6789
}
6890

69-
func WithMuxer(m mux.Transport) Option {
91+
func NoEncryption() Option {
92+
return TransportEncryption(EncPlaintext)
93+
}
94+
95+
func Muxer(m mux.Transport) Option {
7096
return func(cfg *Config) error {
97+
if cfg.Muxer != nil {
98+
return fmt.Errorf("cannot specify multiple muxer options")
99+
}
100+
71101
cfg.Muxer = m
72102
return nil
73103
}
74104
}
75105

76-
func WithPeerstore(ps pstore.Peerstore) Option {
106+
func Peerstore(ps pstore.Peerstore) Option {
77107
return func(cfg *Config) error {
108+
if cfg.Peerstore != nil {
109+
return fmt.Errorf("cannot specify multiple peerstore options")
110+
}
111+
78112
cfg.Peerstore = ps
79113
return nil
80114
}
81115
}
82116

83-
func WithNetProtector(prot pnet.Protector) Option {
117+
func PrivateNetwork(prot pnet.Protector) Option {
84118
return func(cfg *Config) error {
119+
if cfg.Protector != nil {
120+
return fmt.Errorf("cannot specify multiple private network options")
121+
}
122+
85123
cfg.Protector = prot
86124
return nil
87125
}
88126
}
89127

90-
func WithBandwidthReporter(rep metrics.Reporter) Option {
128+
func BandwidthReporter(rep metrics.Reporter) Option {
91129
return func(cfg *Config) error {
130+
if cfg.Reporter != nil {
131+
return fmt.Errorf("cannot specify multiple bandwidth reporter options")
132+
}
133+
92134
cfg.Reporter = rep
93135
return nil
94136
}
95137
}
96138

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
139+
func Identity(sk crypto.PrivKey) Option {
140+
return func(cfg *Config) error {
141+
if cfg.PeerKey != nil {
142+
return fmt.Errorf("cannot specify multiple identities")
103143
}
104-
}
105-
106-
return newWithCfg(ctx, cfg)
107-
}
108144

109-
func WithPeerKey(sk crypto.PrivKey) Option {
110-
return func(cfg *Config) error {
111145
cfg.PeerKey = sk
112146
return nil
113147
}
114148
}
115149

116-
func newWithCfg(ctx context.Context, cfg *Config) (host.Host, error) {
117-
if cfg == nil {
118-
cfg = DefaultConfig()
150+
func New(ctx context.Context, opts ...Option) (host.Host, error) {
151+
var cfg Config
152+
for _, opt := range opts {
153+
if err := opt(&cfg); err != nil {
154+
return nil, err
155+
}
119156
}
120157

158+
return newWithCfg(ctx, &cfg)
159+
}
160+
161+
func newWithCfg(ctx context.Context, cfg *Config) (host.Host, error) {
121162
// If no key was given, generate a random 2048 bit RSA key
122163
if cfg.PeerKey == nil {
123164
priv, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, rand.Reader)
@@ -166,16 +207,15 @@ func DefaultMuxer() mux.Transport {
166207
return tpt
167208
}
168209

169-
func DefaultConfig() *Config {
210+
func Defaults(cfg *Config) error {
170211
// Create a multiaddress that listens on a random port on all interfaces
171212
addr, err := ma.NewMultiaddr("/ip4/0.0.0.0/tcp/0")
172213
if err != nil {
173-
panic(err)
214+
return err
174215
}
175216

176-
return &Config{
177-
ListenAddrs: []ma.Multiaddr{addr},
178-
Peerstore: pstore.NewPeerstore(),
179-
Muxer: DefaultMuxer(),
180-
}
217+
cfg.ListenAddrs = []ma.Multiaddr{addr}
218+
cfg.Peerstore = pstore.NewPeerstore()
219+
cfg.Muxer = DefaultMuxer()
220+
return nil
181221
}

0 commit comments

Comments
 (0)