-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
90 lines (79 loc) · 2.83 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package ipfsnucleus
import (
"context"
datastore "github.com/ipfs/go-datastore"
ipns "github.com/ipfs/go-ipns"
"github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-core/crypto"
host "github.com/libp2p/go-libp2p-core/host"
routing "github.com/libp2p/go-libp2p-core/routing"
dht "github.com/libp2p/go-libp2p-kad-dht"
dualdht "github.com/libp2p/go-libp2p-kad-dht/dual"
libp2pnoise "github.com/libp2p/go-libp2p-noise"
libp2pquic "github.com/libp2p/go-libp2p-quic-transport"
record "github.com/libp2p/go-libp2p-record"
libp2ptls "github.com/libp2p/go-libp2p-tls"
"github.com/multiformats/go-multiaddr"
)
// Libp2pOptionsExtra provides some useful libp2p options
// to create a fully featured libp2p host. It can be used with
// SetupLibp2p.
var Libp2pOptionsExtra = []libp2p.Option{
libp2p.NATPortMap(),
libp2p.EnableAutoRelay(),
libp2p.EnableNATService(),
libp2p.Transport(libp2pquic.NewTransport),
libp2p.Security(libp2pnoise.ID, libp2pnoise.New),
libp2p.Security(libp2ptls.ID, libp2ptls.New),
libp2p.DefaultTransports,
}
// SetupLibp2p returns a routed host and DHT instances that can be used to
// easily create a ipfsnucleus Peer. You may consider to use Peer.Bootstrap()
// after creating the IPFS-Nucleus Peer to connect to other peers. When the
// datastore parameter is nil, the DHT will use an in-memory datastore, so all
// provider records are lost on program shutdown.
//
// Additional libp2p options can be passed. Note that the Identity,
// ListenAddrs and PrivateNetwork options will be setup automatically.
// Interesting options to pass: NATPortMap() EnableAutoRelay(),
// libp2p.EnableNATService(), DisableRelay(), ConnectionManager(...)... see
// https://godoc.org/github.com/libp2p/go-libp2p#Option for more info.
func SetupLibp2p(
ctx context.Context,
hostKey crypto.PrivKey,
listenAddrs []multiaddr.Multiaddr,
ds datastore.Batching,
opts ...libp2p.Option,
) (host.Host, *dualdht.DHT, error) {
var ddht *dualdht.DHT
var err error
finalOpts := []libp2p.Option{
libp2p.Identity(hostKey),
libp2p.ListenAddrs(listenAddrs...),
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
ddht, err = newDHT(ctx, h, ds)
return ddht, err
}),
}
finalOpts = append(finalOpts, opts...)
h, err := libp2p.New(
ctx,
finalOpts...,
)
if err != nil {
return nil, nil, err
}
return h, ddht, nil
}
func newDHT(ctx context.Context, h host.Host, ds datastore.Batching) (*dualdht.DHT, error) {
dhtOpts := []dualdht.Option{
dualdht.DHTOption(dht.NamespacedValidator("pk", record.PublicKeyValidator{})),
dualdht.DHTOption(dht.NamespacedValidator("ipns", ipns.Validator{KeyBook: h.Peerstore()})),
dualdht.DHTOption(dht.Concurrency(10)),
dualdht.DHTOption(dht.Mode(dht.ModeAuto)),
}
if ds != nil {
dhtOpts = append(dhtOpts, dualdht.DHTOption(dht.Datastore(ds)))
}
return dualdht.New(ctx, h, dhtOpts...)
}