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