-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhub.go
176 lines (158 loc) · 4.42 KB
/
hub.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
170
171
172
173
174
175
176
package fulahub
import (
"context"
"encoding/json"
"errors"
"net/http"
"sync"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
logging "github.com/ipfs/go-log/v2"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipni/go-libipni/announce"
"github.com/ipni/go-libipni/dagsync/ipnisync"
"github.com/ipni/go-libipni/ingest/schema"
gostream "github.com/libp2p/go-libp2p-gostream"
"github.com/multiformats/go-multihash"
"github.com/multiformats/go-varint"
)
const FulaHubProtocolV0_0_1 = "/fx.land/hub/0.0.1"
var (
log = logging.Logger("fula/hub")
ipniMetadata = varint.ToUvarint(0xfe001)
latestAdCidKey = datastore.NewKey("fula/hub/latestAdCid")
)
type (
PutContentRequest struct {
Mutlihashes []multihash.Multihash `json:"Mutlihashes"`
}
Hub struct {
*options
httpServer *http.Server
publisher *ipnisync.Publisher
mu sync.Mutex
}
)
func NewHub(o ...Option) (*Hub, error) {
opts, err := newOptions(o...)
if err != nil {
return nil, err
}
p := &Hub{
options: opts,
httpServer: &http.Server{},
}
key := p.h.Peerstore().PrivKey(p.h.ID())
p.publisher, err = ipnisync.NewPublisher(*p.ls, key, ipnisync.WithStreamHost(p.h),
ipnisync.WithHeadTopic("/indexer/ingest/mainnet"))
if err != nil {
return nil, err
}
return p, nil
}
func (p *Hub) Start(_ context.Context) error {
listener, err := gostream.Listen(p.h, FulaHubProtocolV0_0_1)
if err != nil {
return err
}
go func() {
http.HandleFunc("/content", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
http.Error(w, "unknown method", http.StatusBadRequest)
return
}
defer r.Body.Close()
var request PutContentRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
http.Error(w, "failed to decode body", http.StatusBadRequest)
return
}
if err := p.publish(r.Context(), []byte(r.RemoteAddr), request.Mutlihashes); err != nil {
http.Error(w, "failed to publish: "+err.Error(), http.StatusInternalServerError)
return
}
})
if err := p.httpServer.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Errorw("Failed to start HTTP server", "err", err)
}
}()
return nil
}
func (p *Hub) publish(ctx context.Context, contextID []byte, mhs []multihash.Multihash) error {
p.mu.Lock()
defer p.mu.Unlock()
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.ls.Store(ipld.LinkContext{Ctx: ctx}, schema.Linkproto, chunk)
if err != nil {
log.Errorw("Failed to store chunk in IPNI provider engine", "err", err)
return err
}
ad := schema.Advertisement{
Provider: p.h.ID().String(),
Addresses: p.ipniProviderAddrs(p.h),
Entries: chunkLink,
ContextID: contextID,
Metadata: ipniMetadata,
}
latest, err := p.getLatestAdCid(ctx)
if err != nil {
return err
}
if latest != cid.Undef {
ad.PreviousID = cidlink.Link{Cid: latest}
}
if err := ad.Sign(p.h.Peerstore().PrivKey(p.h.ID())); err != nil {
log.Errorw("Failed to sign IPNI advertisement", "err", err)
return err
}
if err := ad.Validate(); err != nil {
return err
}
adNode, err := ad.ToNode()
if err != nil {
return err
}
adLink, err := p.ls.Store(ipld.LinkContext{Ctx: ctx}, schema.Linkproto, adNode)
if err != nil {
return err
}
adCid := adLink.(cidlink.Link).Cid
p.publisher.SetRoot(adCid)
if err := p.setLatestAdCid(ctx, adCid); err != nil {
return err
}
if p.ipniAnnounceSender != nil && p.ipniAnnounceAddrs != nil {
err := announce.Send(ctx, adCid, p.ipniAnnounceAddrs(p.h), p.ipniAnnounceSender)
if err != nil {
log.Errorw("Failed to announce advertisement", "ad", adLink.String())
}
}
log.Infow("Successfully published ad to IPNI", "ad", adLink.String(), "entriesCount", len(mhs))
return nil
}
func (p *Hub) getLatestAdCid(ctx context.Context) (cid.Cid, error) {
switch v, err := p.ds.Get(ctx, latestAdCidKey); {
case err == nil:
_, c, err := cid.CidFromBytes(v)
return c, err
case errors.Is(err, datastore.ErrNotFound):
return cid.Undef, nil
default:
return cid.Undef, err
}
}
func (p *Hub) setLatestAdCid(ctx context.Context, c cid.Cid) error {
return p.ds.Put(ctx, latestAdCidKey, c.Bytes())
}
func (p *Hub) Shutdown(ctx context.Context) error {
hErr := p.httpServer.Shutdown(ctx)
eErr := p.publisher.Close()
return errors.Join(hErr, eErr)
}