-
Notifications
You must be signed in to change notification settings - Fork 885
/
routing.go
69 lines (59 loc) · 1.68 KB
/
routing.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
package p2p
import (
"context"
"fmt"
"github.com/ipfs/go-datastore"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/core/routing"
"go.uber.org/fx"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
)
// contentRouting constructs nil content routing,
// as for our use-case existing ContentRouting mechanisms, e.g DHT, are unsuitable
func contentRouting(r routing.PeerRouting) routing.ContentRouting {
return r.(*dht.IpfsDHT)
}
// peerRouting provides constructor for PeerRouting over DHT.
// Basically, this provides a way to discover peer addresses by respecting public keys.
func peerRouting(cfg Config, tp node.Type, params routingParams) (routing.PeerRouting, error) {
opts := []dht.Option{
dht.Mode(dht.ModeAuto),
dht.BootstrapPeers(params.Peers...),
dht.ProtocolPrefix(protocol.ID(fmt.Sprintf("/celestia/%s", params.Net))),
dht.Datastore(params.DataStore),
dht.RoutingTableRefreshPeriod(cfg.RoutingTableRefreshPeriod),
}
if isBootstrapper() {
opts = append(opts,
dht.BootstrapPeers(), // no bootstrappers for a bootstrapper ¯\_(ツ)_/¯
)
}
if tp == node.Bridge || tp == node.Full {
opts = append(opts,
dht.Mode(dht.ModeServer),
)
}
d, err := dht.New(params.Ctx, params.Host, opts...)
if err != nil {
return nil, err
}
params.Lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return d.Bootstrap(ctx)
},
OnStop: func(context.Context) error {
return d.Close()
},
})
return d, nil
}
type routingParams struct {
fx.In
Ctx context.Context
Net Network
Peers Bootstrappers
Lc fx.Lifecycle
Host HostBase
DataStore datastore.Batching
}