forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
100 lines (86 loc) · 2.63 KB
/
util.go
File metadata and controls
100 lines (86 loc) · 2.63 KB
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
package util
import (
"context"
"fmt"
"time"
"github.com/ipfs/go-cid"
ma "github.com/multiformats/go-multiaddr"
dns "github.com/multiformats/go-multiaddr-dns"
)
var (
// AvgBlockTime is the expected duration between block in two consecutive epochs.
AvgBlockTime = time.Second * time.Duration(EpochDurationSeconds)
)
const (
// EpochDurationSeconds is the expected duration in seconds of an epoch.
// Defined at the filecoin spec level.
EpochDurationSeconds = 30
// MinDealDuration is the minium deal duration accepted in the Filecoin network.
// Original calculation: 180 * EpochsInADay
MinDealDuration = 180 * (24 * 60 * 60 / EpochDurationSeconds)
// CidUndef is a magic value to represent an undefined cid as a string.
CidUndef = "CID_UNDEF"
// DefaultCidUndef is the string generated by the cid module's String() func for an undefined cid.
DefaultCidUndef = "b"
)
// TCPAddrFromMultiAddr converts a multiaddress to a string representation of a tcp address.
func TCPAddrFromMultiAddr(maddr ma.Multiaddr) (string, error) {
if maddr == nil {
return "", fmt.Errorf("invalid address")
}
var ip string
if _, err := maddr.ValueForProtocol(ma.P_DNS4); err == nil {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
maddrs, err := dns.Resolve(ctx, maddr)
if err != nil {
return "", fmt.Errorf("resolving dns: %s", err)
}
for _, m := range maddrs {
if ip, err = getIPFromMaddr(m); err == nil {
break
}
}
} else {
ip, err = getIPFromMaddr(maddr)
if err != nil {
return "", fmt.Errorf("getting ip from maddr: %s", err)
}
}
tcp, err := maddr.ValueForProtocol(ma.P_TCP)
if err != nil {
return "", fmt.Errorf("getting port from maddr: %s", err)
}
return fmt.Sprintf("%s:%s", ip, tcp), nil
}
func getIPFromMaddr(maddr ma.Multiaddr) (string, error) {
if ip, err := maddr.ValueForProtocol(ma.P_IP4); err == nil {
return ip, nil
}
if ip, err := maddr.ValueForProtocol(ma.P_IP6); err == nil {
return fmt.Sprintf("[%s]", ip), nil
}
return "", fmt.Errorf("no ip in multiaddr")
}
// MustParseAddr returns a parsed Multiaddr, or panics if invalid.
func MustParseAddr(str string) ma.Multiaddr {
addr, err := ma.NewMultiaddr(str)
if err != nil {
panic(err)
}
return addr
}
// CidToString converts a cid to string, representing cid.Undef as an empty string.
func CidToString(c cid.Cid) string {
if c == cid.Undef {
return CidUndef
}
return c.String()
}
// CidFromString converts a string to a cid assuming that an empty string is cid.Undef.
func CidFromString(c string) (cid.Cid, error) {
if c == DefaultCidUndef || c == CidUndef {
return cid.Undef, nil
}
return cid.Decode(c)
}