gitlab.com/jokerrs1/Sia@v1.3.2/node/api/hostdb.go (about) 1 package api 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/NebulousLabs/Sia/modules" 8 "github.com/NebulousLabs/Sia/types" 9 10 "github.com/julienschmidt/httprouter" 11 ) 12 13 type ( 14 // ExtendedHostDBEntry is an extension to modules.HostDBEntry that includes 15 // the string representation of the public key, otherwise presented as two 16 // fields, a string and a base64 encoded byte slice. 17 ExtendedHostDBEntry struct { 18 modules.HostDBEntry 19 PublicKeyString string `json:"publickeystring"` 20 } 21 22 // HostdbActiveGET lists active hosts on the network. 23 HostdbActiveGET struct { 24 Hosts []ExtendedHostDBEntry `json:"hosts"` 25 } 26 27 // HostdbAllGET lists all hosts that the renter is aware of. 28 HostdbAllGET struct { 29 Hosts []ExtendedHostDBEntry `json:"hosts"` 30 } 31 32 // HostdbHostsGET lists detailed statistics for a particular host, selected 33 // by pubkey. 34 HostdbHostsGET struct { 35 Entry ExtendedHostDBEntry `json:"entry"` 36 ScoreBreakdown modules.HostScoreBreakdown `json:"scorebreakdown"` 37 } 38 ) 39 40 // hostdbActiveHandler handles the API call asking for the list of active 41 // hosts. 42 func (api *API) hostdbActiveHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) { 43 var numHosts uint64 44 hosts := api.renter.ActiveHosts() 45 46 if req.FormValue("numhosts") == "" { 47 // Default value for 'numhosts' is all of them. 48 numHosts = uint64(len(hosts)) 49 } else { 50 // Parse the value for 'numhosts'. 51 _, err := fmt.Sscan(req.FormValue("numhosts"), &numHosts) 52 if err != nil { 53 WriteError(w, Error{err.Error()}, http.StatusBadRequest) 54 return 55 } 56 57 // Catch any boundary errors. 58 if numHosts > uint64(len(hosts)) { 59 numHosts = uint64(len(hosts)) 60 } 61 } 62 63 // Convert the entries into extended entries. 64 var extendedHosts []ExtendedHostDBEntry 65 for _, host := range hosts { 66 extendedHosts = append(extendedHosts, ExtendedHostDBEntry{ 67 HostDBEntry: host, 68 PublicKeyString: host.PublicKey.String(), 69 }) 70 } 71 72 WriteJSON(w, HostdbActiveGET{ 73 Hosts: extendedHosts[:numHosts], 74 }) 75 } 76 77 // hostdbAllHandler handles the API call asking for the list of all hosts. 78 func (api *API) hostdbAllHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) { 79 // Get the set of all hosts and convert them into extended hosts. 80 hosts := api.renter.AllHosts() 81 var extendedHosts []ExtendedHostDBEntry 82 for _, host := range hosts { 83 extendedHosts = append(extendedHosts, ExtendedHostDBEntry{ 84 HostDBEntry: host, 85 PublicKeyString: host.PublicKey.String(), 86 }) 87 } 88 89 WriteJSON(w, HostdbAllGET{ 90 Hosts: extendedHosts, 91 }) 92 } 93 94 // hostdbHostsHandler handles the API call asking for a specific host, 95 // returning detailed informatino about that host. 96 func (api *API) hostdbHostsHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { 97 var pk types.SiaPublicKey 98 pk.LoadString(ps.ByName("pubkey")) 99 100 entry, exists := api.renter.Host(pk) 101 if !exists { 102 WriteError(w, Error{"requested host does not exist"}, http.StatusBadRequest) 103 return 104 } 105 breakdown := api.renter.ScoreBreakdown(entry) 106 107 // Extend the hostdb entry to have the public key string. 108 extendedEntry := ExtendedHostDBEntry{ 109 HostDBEntry: entry, 110 PublicKeyString: entry.PublicKey.String(), 111 } 112 WriteJSON(w, HostdbHostsGET{ 113 Entry: extendedEntry, 114 ScoreBreakdown: breakdown, 115 }) 116 }