-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdht.go
85 lines (76 loc) · 1.95 KB
/
dht.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
package exchange
import (
"context"
"errors"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multihash"
)
type fulaDht struct {
*options
h host.Host
dh *dht.IpfsDHT
ctx context.Context
cancel context.CancelFunc
}
func newDhtProvider(h host.Host, opts *options) (*fulaDht, error) {
d := &fulaDht{
h: h,
options: opts,
}
d.ctx, d.cancel = context.WithCancel(context.Background())
var err error
d.dh, err = dht.New(d.ctx, h, opts.dhtProviderOpts...)
if err != nil {
return nil, err
}
log.Infow("Dht is initialized with this", "peers")
return d, nil
}
func (d *fulaDht) PingDht(p peer.ID) error {
err := d.dh.Ping(d.ctx, p)
return err
}
func (d *fulaDht) AddPeer(p peer.ID) {
d.dh.RoutingTable().PeerAdded(p)
log.Infow("AddPeer", "self", d.dh.PeerID(), "added", p)
d.dh.RefreshRoutingTable()
}
func (d *fulaDht) Provide(l ipld.Link) error {
if l == nil {
err := errors.New("link is nil")
log.Errorw("Failed to Provide Dht, link is nil", "err", err)
return err
}
link, ok := l.(cidlink.Link)
if ok &&
!cid.Undef.Equals(link.Cid) &&
link.Cid.Prefix().MhType != multihash.IDENTITY {
return d.dh.Provide(d.ctx, link.Cid, true)
}
err := errors.New("cid is undefined or invalid")
return err
}
func (d *fulaDht) FindProviders(l ipld.Link) ([]peer.AddrInfo, error) {
if l == nil {
err := errors.New("link is nil")
log.Errorw("Failed to Provide Dht, link is nil", "err", err)
return nil, err
}
link, ok := l.(cidlink.Link)
if ok &&
!cid.Undef.Equals(link.Cid) &&
link.Cid.Prefix().MhType != multihash.IDENTITY {
return d.dh.FindProviders(d.ctx, link.Cid)
}
err := errors.New("cid is undefined or invalid")
return nil, err
}
func (d *fulaDht) Shutdown() error {
d.cancel()
return d.dh.Close()
}