-
Notifications
You must be signed in to change notification settings - Fork 6
/
ipni_publisher.go
169 lines (157 loc) · 4.26 KB
/
ipni_publisher.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package exchange
import (
"context"
"sync/atomic"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipni/go-libipni/ingest/schema"
"github.com/ipni/index-provider/engine"
"github.com/libp2p/go-libp2p/core/host"
"github.com/multiformats/go-multihash"
"github.com/multiformats/go-varint"
)
//lint:ignore U1000 will be used in future
var (
ipniContextID = []byte("fx.land")
ipniMetadata = varint.ToUvarint(0xfe001)
)
//lint:ignore U1000 will be used in future
type ipniPublisher struct {
*options
h host.Host
buffer chan cid.Cid
e *engine.Engine
ctx context.Context
cancel context.CancelFunc
}
type MultihashResult struct {
Multihash string `json:"Multihash"`
ProviderResults []ProviderResult `json:"ProviderResults"`
}
type ProviderResult struct {
ContextID string `json:"ContextID"`
}
//lint:ignore U1000 will be used in future
func newIpniPublisher(h host.Host, opts *options) (*ipniPublisher, error) {
p := &ipniPublisher{
h: h,
options: opts,
}
p.ctx, p.cancel = context.WithCancel(context.Background())
p.buffer = make(chan cid.Cid, p.ipniPublishChanBuffer)
var err error
p.e, err = engine.New(opts.ipniProviderEngineOpts...)
if err != nil {
return nil, err
}
return p, nil
}
//lint:ignore U1000 will be used in future
func (p *ipniPublisher) Start(ctx context.Context) error {
if err := p.e.Start(ctx); err != nil {
return err
}
go func() {
unpublished := make(map[cid.Cid]struct{})
var publishing atomic.Bool
maybePublish := func() {
remaining := len(unpublished)
if remaining == 0 {
//log.Debug("No remaining entries to publish")
return
}
if publishing.Load() {
log.Debugw("IPNI publishing in progress", "remaining", remaining)
return
}
log.Debugw("Attempting to publish links to IPNI", "count", remaining)
mhs := make([]multihash.Multihash, 0, remaining)
for c := range unpublished {
mhs = append(mhs, c.Hash())
delete(unpublished, c)
}
publishing.Store(true)
go func(entries []multihash.Multihash) {
log.Debug("IPNI publish attempt in progress...")
defer func() {
publishing.Store(false)
log.Debug("Finished attempt to publish to IPNI.")
}()
if err := p.publish(entries); err != nil {
log.Errorw("Failed to publish to IPNI", "entriesCount", len(mhs), "err", err)
}
}(mhs)
}
for {
select {
case <-p.ctx.Done():
log.Infow("IPNI publisher stopped", "remainingLinks", len(unpublished))
return
case <-p.ipniPublishTicker.C:
maybePublish()
case c := <-p.buffer:
unpublished[c] = struct{}{}
maybePublish()
}
}
}()
return nil
}
//lint:ignore U1000 will be used in future
func (p *ipniPublisher) notifyReceivedLink(l ipld.Link) {
if l == nil {
return
}
link, ok := l.(cidlink.Link)
if ok &&
!cid.Undef.Equals(link.Cid) &&
link.Cid.Prefix().MhType != multihash.IDENTITY {
p.buffer <- link.Cid
}
}
//lint:ignore U1000 will be used in future
func (p *ipniPublisher) publish(mhs []multihash.Multihash) error {
chunk, err := schema.EntryChunk{
Entries: mhs,
}.ToNode()
if err != nil {
log.Errorw("Failed to instantiate entries chunk node", "err", err)
return err
}
chunkLink, err := p.e.LinkSystem().Store(ipld.LinkContext{Ctx: p.ctx}, schema.Linkproto, chunk)
if err != nil {
log.Errorw("Failed to store chunk in IPNI provider engine", "err", err)
return err
}
addrs := p.h.Addrs()
saddrs := make([]string, 0, len(addrs))
for _, addr := range addrs {
saddrs = append(saddrs, addr.String())
}
ad := schema.Advertisement{
PreviousID: nil,
Provider: p.h.ID().String(),
Addresses: saddrs,
Entries: chunkLink,
ContextID: ipniContextID,
Metadata: ipniMetadata,
}
if err := ad.Sign(p.h.Peerstore().PrivKey(p.h.ID())); err != nil {
log.Errorw("Failed to sign IPNI advertisement", "err", err)
return err
}
adLink, err := p.e.Publish(p.ctx, ad)
if err != nil {
log.Errorw("Failed to publish IPNI advertisement", "err", err)
return err
}
log.Infow("Successfully published ad to IPNI", "ad", adLink.String(), "entriesCount", len(mhs))
return nil
}
//lint:ignore U1000 will be used in future
func (p *ipniPublisher) shutdown() error {
p.cancel()
p.ipniPublishTicker.Stop()
return p.e.Shutdown()
}