github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/node/api/hostdb.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"SiaPrime/modules"
     8  	"SiaPrime/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  		ScoreBreakdown  modules.HostScoreBreakdown `json:"scorebreakdown"`
    21  	}
    22  
    23  	// HostdbActiveGET lists active hosts on the network.
    24  	HostdbActiveGET struct {
    25  		Hosts []ExtendedHostDBEntry `json:"hosts"`
    26  	}
    27  
    28  	// HostdbAllGET lists all hosts that the renter is aware of.
    29  	HostdbAllGET struct {
    30  		Hosts []ExtendedHostDBEntry `json:"hosts"`
    31  	}
    32  
    33  	// HostdbHostsGET lists detailed statistics for a particular host, selected
    34  	// by pubkey.
    35  	HostdbHostsGET struct {
    36  		Entry          ExtendedHostDBEntry        `json:"entry"`
    37  		ScoreBreakdown modules.HostScoreBreakdown `json:"scorebreakdown"`
    38  	}
    39  
    40  	// HostdbGet holds information about the hostdb.
    41  	HostdbGet struct {
    42  		InitialScanComplete bool `json:"initialscancomplete"`
    43  	}
    44  )
    45  
    46  // hostdbHandler handles the API call asking for the list of active
    47  // hosts.
    48  func (api *API) hostdbHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    49  	isc, err := api.renter.InitialScanComplete()
    50  	if err != nil {
    51  		WriteError(w, Error{"Failed to get initial scan status" + err.Error()}, http.StatusInternalServerError)
    52  		return
    53  	}
    54  	WriteJSON(w, HostdbGet{
    55  		InitialScanComplete: isc,
    56  	})
    57  }
    58  
    59  // hostdbActiveHandler handles the API call asking for the list of active
    60  // hosts.
    61  func (api *API) hostdbActiveHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    62  	var numHosts uint64
    63  	hosts := api.renter.ActiveHosts()
    64  
    65  	if req.FormValue("numhosts") == "" {
    66  		// Default value for 'numhosts' is all of them.
    67  		numHosts = uint64(len(hosts))
    68  	} else {
    69  		// Parse the value for 'numhosts'.
    70  		_, err := fmt.Sscan(req.FormValue("numhosts"), &numHosts)
    71  		if err != nil {
    72  			WriteError(w, Error{err.Error()}, http.StatusBadRequest)
    73  			return
    74  		}
    75  
    76  		// Catch any boundary errors.
    77  		if numHosts > uint64(len(hosts)) {
    78  			numHosts = uint64(len(hosts))
    79  		}
    80  	}
    81  
    82  	// Convert the entries into extended entries.
    83  	var extendedHosts []ExtendedHostDBEntry
    84  	for _, host := range hosts {
    85  		extendedHosts = append(extendedHosts, ExtendedHostDBEntry{
    86  			HostDBEntry:     host,
    87  			PublicKeyString: host.PublicKey.String(),
    88  			ScoreBreakdown:  api.renter.ScoreBreakdown(host),
    89  		})
    90  	}
    91  
    92  	WriteJSON(w, HostdbActiveGET{
    93  		Hosts: extendedHosts[:numHosts],
    94  	})
    95  }
    96  
    97  // hostdbAllHandler handles the API call asking for the list of all hosts.
    98  func (api *API) hostdbAllHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    99  	// Get the set of all hosts and convert them into extended hosts.
   100  	hosts := api.renter.AllHosts()
   101  	var extendedHosts []ExtendedHostDBEntry
   102  	for _, host := range hosts {
   103  		extendedHosts = append(extendedHosts, ExtendedHostDBEntry{
   104  			HostDBEntry:     host,
   105  			PublicKeyString: host.PublicKey.String(),
   106  			ScoreBreakdown:  api.renter.ScoreBreakdown(host),
   107  		})
   108  	}
   109  
   110  	WriteJSON(w, HostdbAllGET{
   111  		Hosts: extendedHosts,
   112  	})
   113  }
   114  
   115  // hostdbHostsHandler handles the API call asking for a specific host,
   116  // returning detailed informatino about that host.
   117  func (api *API) hostdbHostsHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
   118  	var pk types.SiaPublicKey
   119  	pk.LoadString(ps.ByName("pubkey"))
   120  
   121  	entry, exists := api.renter.Host(pk)
   122  	if !exists {
   123  		WriteError(w, Error{"requested host does not exist"}, http.StatusBadRequest)
   124  		return
   125  	}
   126  	breakdown := api.renter.ScoreBreakdown(entry)
   127  
   128  	// Extend the hostdb entry  to have the public key string.
   129  	extendedEntry := ExtendedHostDBEntry{
   130  		HostDBEntry:     entry,
   131  		PublicKeyString: entry.PublicKey.String(),
   132  	}
   133  	WriteJSON(w, HostdbHostsGET{
   134  		Entry:          extendedEntry,
   135  		ScoreBreakdown: breakdown,
   136  	})
   137  }