/
host.go
196 lines (172 loc) · 5 KB
/
host.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package api
import (
"errors"
"fmt"
"net/url"
"strings"
"go.sia.tech/core/types"
"go.sia.tech/renterd/hostdb"
)
const (
HostFilterModeAll = "all"
HostFilterModeAllowed = "allowed"
HostFilterModeBlocked = "blocked"
UsabilityFilterModeAll = "all"
UsabilityFilterModeUsable = "usable"
UsabilityFilterModeUnusable = "unusable"
)
var (
// ErrHostNotFound is returned when a host can't be retrieved from the
// database.
ErrHostNotFound = errors.New("host doesn't exist in hostdb")
)
type (
// HostsScanRequest is the request type for the /hosts/scans endpoint.
HostsScanRequest struct {
Scans []hostdb.HostScan `json:"scans"`
}
// HostsPriceTablesRequest is the request type for the /hosts/pricetables endpoint.
HostsPriceTablesRequest struct {
PriceTableUpdates []hostdb.PriceTableUpdate `json:"priceTableUpdates"`
}
// HostsRemoveRequest is the request type for the /hosts/remove endpoint.
HostsRemoveRequest struct {
MaxDowntimeHours DurationH `json:"maxDowntimeHours"`
MinRecentScanFailures uint64 `json:"minRecentScanFailures"`
}
// SearchHostsRequest is the request type for the /api/bus/search/hosts
// endpoint.
SearchHostsRequest struct {
Offset int `json:"offset"`
Limit int `json:"limit"`
FilterMode string `json:"filterMode"`
UsabilityMode string `json:"usabilityMode"`
AddressContains string `json:"addressContains"`
KeyIn []types.PublicKey `json:"keyIn"`
}
)
type (
// UpdateAllowlistRequest is the request type for /hosts/allowlist endpoint.
UpdateAllowlistRequest struct {
Add []types.PublicKey `json:"add"`
Remove []types.PublicKey `json:"remove"`
Clear bool `json:"clear"`
}
// UpdateBlocklistRequest is the request type for /hosts/blocklist endpoint.
UpdateBlocklistRequest struct {
Add []string `json:"add"`
Remove []string `json:"remove"`
Clear bool `json:"clear"`
}
)
// Option types.
type (
GetHostsOptions struct {
Offset int
Limit int
}
HostsForScanningOptions struct {
MaxLastScan TimeRFC3339
Limit int
Offset int
}
SearchHostOptions struct {
AddressContains string
FilterMode string
KeyIn []types.PublicKey
Limit int
Offset int
}
)
func (opts GetHostsOptions) Apply(values url.Values) {
if opts.Offset != 0 {
values.Set("offset", fmt.Sprint(opts.Offset))
}
if opts.Limit != 0 {
values.Set("limit", fmt.Sprint(opts.Limit))
}
}
func (opts HostsForScanningOptions) Apply(values url.Values) {
if opts.Offset != 0 {
values.Set("offset", fmt.Sprint(opts.Offset))
}
if opts.Limit != 0 {
values.Set("limit", fmt.Sprint(opts.Limit))
}
if !opts.MaxLastScan.IsZero() {
values.Set("lastScan", TimeRFC3339(opts.MaxLastScan).String())
}
}
type (
Host struct {
hostdb.Host
Blocked bool `json:"blocked"`
Checks map[string]HostCheck `json:"checks"`
}
HostCheck struct {
Gouging HostGougingBreakdown `json:"gouging"`
Score HostScoreBreakdown `json:"score"`
Usability HostUsabilityBreakdown `json:"usability"`
}
HostGougingBreakdown struct {
ContractErr string `json:"contractErr"`
DownloadErr string `json:"downloadErr"`
GougingErr string `json:"gougingErr"`
PruneErr string `json:"pruneErr"`
UploadErr string `json:"uploadErr"`
}
HostScoreBreakdown struct {
Age float64 `json:"age"`
Collateral float64 `json:"collateral"`
Interactions float64 `json:"interactions"`
StorageRemaining float64 `json:"storageRemaining"`
Uptime float64 `json:"uptime"`
Version float64 `json:"version"`
Prices float64 `json:"prices"`
}
HostUsabilityBreakdown struct {
Blocked bool `json:"blocked"`
Offline bool `json:"offline"`
LowScore bool `json:"lowScore"`
RedundantIP bool `json:"redundantIP"`
Gouging bool `json:"gouging"`
NotAcceptingContracts bool `json:"notAcceptingContracts"`
NotAnnounced bool `json:"notAnnounced"`
NotCompletingScan bool `json:"notCompletingScan"`
}
)
func (sb HostScoreBreakdown) String() string {
return fmt.Sprintf("Age: %v, Col: %v, Int: %v, SR: %v, UT: %v, V: %v, Pr: %v", sb.Age, sb.Collateral, sb.Interactions, sb.StorageRemaining, sb.Uptime, sb.Version, sb.Prices)
}
func (hgb HostGougingBreakdown) Gouging() bool {
for _, err := range []string{
hgb.ContractErr,
hgb.DownloadErr,
hgb.GougingErr,
hgb.PruneErr,
hgb.UploadErr,
} {
if err != "" {
return true
}
}
return false
}
func (hgb HostGougingBreakdown) String() string {
var reasons []string
for _, errStr := range []string{
hgb.ContractErr,
hgb.DownloadErr,
hgb.GougingErr,
hgb.PruneErr,
hgb.UploadErr,
} {
if errStr != "" {
reasons = append(reasons, errStr)
}
}
return strings.Join(reasons, ";")
}
func (sb HostScoreBreakdown) Score() float64 {
return sb.Age * sb.Collateral * sb.Interactions * sb.StorageRemaining * sb.Uptime * sb.Version * sb.Prices
}