This repository has been archived by the owner on Nov 2, 2018. It is now read-only.
/
hostdb.go
134 lines (115 loc) · 3.75 KB
/
hostdb.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
package api
import (
"fmt"
"net/http"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/types"
"github.com/julienschmidt/httprouter"
)
type (
// ExtendedHostDBEntry is an extension to modules.HostDBEntry that includes
// the string representation of the public key, otherwise presented as two
// fields, a string and a base64 encoded byte slice.
ExtendedHostDBEntry struct {
modules.HostDBEntry
PublicKeyString string `json:"publickeystring"`
}
// HostdbActiveGET lists active hosts on the network.
HostdbActiveGET struct {
Hosts []ExtendedHostDBEntry `json:"hosts"`
}
// HostdbAllGET lists all hosts that the renter is aware of.
HostdbAllGET struct {
Hosts []ExtendedHostDBEntry `json:"hosts"`
}
// HostdbHostsGET lists detailed statistics for a particular host, selected
// by pubkey.
HostdbHostsGET struct {
Entry ExtendedHostDBEntry `json:"entry"`
ScoreBreakdown modules.HostScoreBreakdown `json:"scorebreakdown"`
}
// HostdbGet holds information about the hostdb.
HostdbGet struct {
InitialScanComplete bool `json:"initialscancomplete"`
}
)
// hostdbHandler handles the API call asking for the list of active
// hosts.
func (api *API) hostdbHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
isc, err := api.renter.InitialScanComplete()
if err != nil {
WriteError(w, Error{"Failed to get initial scan status" + err.Error()}, http.StatusInternalServerError)
return
}
WriteJSON(w, HostdbGet{
InitialScanComplete: isc,
})
}
// hostdbActiveHandler handles the API call asking for the list of active
// hosts.
func (api *API) hostdbActiveHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
var numHosts uint64
hosts := api.renter.ActiveHosts()
if req.FormValue("numhosts") == "" {
// Default value for 'numhosts' is all of them.
numHosts = uint64(len(hosts))
} else {
// Parse the value for 'numhosts'.
_, err := fmt.Sscan(req.FormValue("numhosts"), &numHosts)
if err != nil {
WriteError(w, Error{err.Error()}, http.StatusBadRequest)
return
}
// Catch any boundary errors.
if numHosts > uint64(len(hosts)) {
numHosts = uint64(len(hosts))
}
}
// Convert the entries into extended entries.
var extendedHosts []ExtendedHostDBEntry
for _, host := range hosts {
extendedHosts = append(extendedHosts, ExtendedHostDBEntry{
HostDBEntry: host,
PublicKeyString: host.PublicKey.String(),
})
}
WriteJSON(w, HostdbActiveGET{
Hosts: extendedHosts[:numHosts],
})
}
// hostdbAllHandler handles the API call asking for the list of all hosts.
func (api *API) hostdbAllHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
// Get the set of all hosts and convert them into extended hosts.
hosts := api.renter.AllHosts()
var extendedHosts []ExtendedHostDBEntry
for _, host := range hosts {
extendedHosts = append(extendedHosts, ExtendedHostDBEntry{
HostDBEntry: host,
PublicKeyString: host.PublicKey.String(),
})
}
WriteJSON(w, HostdbAllGET{
Hosts: extendedHosts,
})
}
// hostdbHostsHandler handles the API call asking for a specific host,
// returning detailed informatino about that host.
func (api *API) hostdbHostsHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
var pk types.SiaPublicKey
pk.LoadString(ps.ByName("pubkey"))
entry, exists := api.renter.Host(pk)
if !exists {
WriteError(w, Error{"requested host does not exist"}, http.StatusBadRequest)
return
}
breakdown := api.renter.ScoreBreakdown(entry)
// Extend the hostdb entry to have the public key string.
extendedEntry := ExtendedHostDBEntry{
HostDBEntry: entry,
PublicKeyString: entry.PublicKey.String(),
}
WriteJSON(w, HostdbHostsGET{
Entry: extendedEntry,
ScoreBreakdown: breakdown,
})
}