-
Notifications
You must be signed in to change notification settings - Fork 66
/
retrieval.go
85 lines (72 loc) · 2.22 KB
/
retrieval.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 util
import (
"fmt"
"strings"
"time"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
"gorm.io/gorm"
)
type RetrievalFailureRecord struct {
gorm.Model
Miner string `json:"miner"`
Phase string `json:"phase"`
Message string `json:"message"`
Content uint `json:"content"`
Cid DbCID `json:"cid"`
}
type RetrievalProgress struct {
Wait chan struct{}
EndErr error
}
type HeartbeatAutoretrieveResponse struct {
Handle string `json:"handle"`
LastConnection time.Time `json:"lastConnection"`
AddrInfo *peer.AddrInfo `json:"addrInfo"`
}
type AutoretrieveListResponse struct {
Handle string `json:"handle"`
LastConnection time.Time `json:"lastConnection"`
AddrInfo *peer.AddrInfo `json:"addrInfo"`
}
type AutoretrieveInitResponse struct {
Handle string `json:"handle"`
Token string `json:"token"`
LastConnection time.Time `json:"lastConnection"`
AddrInfo *peer.AddrInfo `json:"addrInfo"`
}
// ValidateAddresses checks to see if all multiaddresses are valid
// returns empty []string if all multiaddresses are valid strings
// returns a list of all invalid multiaddresses if any is invalid
func validateAddresses(addresses []string) []string {
var invalidAddresses []string
for _, addr := range addresses {
_, err := ma.NewMultiaddr(addr)
if err != nil {
invalidAddresses = append(invalidAddresses, addr)
}
}
return invalidAddresses
}
func ValidatePeerInfo(peerID string, addresses []string) (*peer.AddrInfo, error) {
// check if peerid format is correct
_, err := peer.IDFromString(peerID)
if err != nil {
return nil, fmt.Errorf("invalid peerID")
}
if len(addresses) == 0 || addresses[0] == "" {
return nil, fmt.Errorf("no addresses provided")
}
// check if multiaddresses formats are correct
invalidAddrs := validateAddresses(addresses)
if len(invalidAddrs) != 0 {
return nil, fmt.Errorf("invalid address(es): %s", strings.Join(invalidAddrs, ", "))
}
// any of the multiaddresses of the peer should work to get addrInfo
// we get the first one
addrInfo, err := peer.AddrInfoFromString(addresses[0])
if err != nil {
return nil, err
}
return addrInfo, nil
}