-
Notifications
You must be signed in to change notification settings - Fork 0
/
announce.go
68 lines (63 loc) · 1.5 KB
/
announce.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
package main
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"github.com/fxamacker/cbor/v2"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
)
type announcement struct {
Cid cid.Cid
Addrs []multiaddr.Multiaddr
}
func (a announcement) MarshalCBOR() ([]byte, error) {
msg := make([]any, 0, 3)
msg = append(msg, cbor.Tag{
Number: 42,
Content: append([]byte{0}, a.Cid.Bytes()...),
})
baddrs := make([][]byte, 0, len(a.Addrs))
for _, addr := range a.Addrs {
baddrs = append(baddrs, addr.Bytes())
}
msg = append(msg, baddrs)
msg = append(msg, []byte{0})
return cbor.Marshal(msg)
}
func (hf *heyFil) announce(ctx context.Context, ai *peer.AddrInfo, head cid.Cid) error {
maddrs, err := peer.AddrInfoToP2pAddrs(ai)
if err != nil {
return err
}
anncb, err := cbor.Marshal(announcement{
Cid: head,
Addrs: maddrs,
})
if err != nil {
return err
}
url := hf.httpIndexerEndpoint + `/ingest/announce`
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, bytes.NewBuffer(anncb))
if err != nil {
return err
}
resp, err := hf.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respb, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode > 299 {
logger.Debugw("unsuccessful HTTP reponse while announcing", "status", resp.Close, "body", string(respb))
return fmt.Errorf("unsuccessful announce: %d", resp.StatusCode)
}
logger.Info("successfully announced")
return nil
}