-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
host.go
123 lines (99 loc) · 2.94 KB
/
host.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package lp2p
import (
"context"
"fmt"
nilrouting "github.com/ipfs/boxo/routing/none"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
record "github.com/libp2p/go-libp2p-record"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
routedhost "github.com/libp2p/go-libp2p/p2p/host/routed"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"go.uber.org/fx"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/node/modules/helpers"
)
type P2PHostIn struct {
fx.In
ID peer.ID
Peerstore peerstore.Peerstore
Opts [][]libp2p.Option `group:"libp2p"`
}
// ////////////////////////
type RawHost host.Host
func Peerstore() (peerstore.Peerstore, error) {
return pstoremem.NewPeerstore()
}
func Host(mctx helpers.MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (RawHost, error) {
pkey := params.Peerstore.PrivKey(params.ID)
if pkey == nil {
return nil, fmt.Errorf("missing private key for node ID: %s", params.ID.Pretty())
}
opts := []libp2p.Option{
libp2p.Identity(pkey),
libp2p.Peerstore(params.Peerstore),
libp2p.NoListenAddrs,
libp2p.Ping(true),
libp2p.UserAgent("lotus-" + build.UserVersion()),
}
for _, o := range params.Opts {
opts = append(opts, o...)
}
h, err := libp2p.New(opts...)
if err != nil {
return nil, err
}
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return h.Close()
},
})
return h, nil
}
func UserAgentOption(agent string) func() (opts Libp2pOpts, err error) {
return func() (opts Libp2pOpts, err error) {
opts.Opts = append(opts.Opts, libp2p.UserAgent(agent))
return
}
}
func MockHost(mn mocknet.Mocknet, id peer.ID, ps peerstore.Peerstore) (RawHost, error) {
return mn.AddPeerWithPeerstore(id, ps)
}
func DHTRouting(mode dht.ModeOpt) interface{} {
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, host RawHost, dstore dtypes.MetadataDS, validator record.Validator, nn dtypes.NetworkName, bs dtypes.Bootstrapper) (BaseIpfsRouting, error) {
ctx := helpers.LifecycleCtx(mctx, lc)
if bs {
mode = dht.ModeServer
}
opts := []dht.Option{dht.Mode(mode),
dht.Datastore(dstore),
dht.Validator(validator),
dht.ProtocolPrefix(build.DhtProtocolName(nn)),
dht.QueryFilter(dht.PublicQueryFilter),
dht.RoutingTableFilter(dht.PublicRoutingTableFilter),
dht.DisableProviders(),
dht.DisableValues()}
d, err := dht.New(
ctx, host, opts...,
)
if err != nil {
return nil, err
}
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return d.Close()
},
})
return d, nil
}
}
func NilRouting(mctx helpers.MetricsCtx) (BaseIpfsRouting, error) {
return nilrouting.ConstructNilRouting(mctx, nil, nil, nil)
}
func RoutedHost(rh RawHost, r BaseIpfsRouting) host.Host {
return routedhost.Wrap(rh, r)
}