gitlab.com/jokerrs1/Sia@v1.3.2/node/api/miner.go (about)

     1  package api
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/NebulousLabs/Sia/encoding"
     7  	"github.com/NebulousLabs/Sia/types"
     8  
     9  	"github.com/julienschmidt/httprouter"
    10  )
    11  
    12  type (
    13  	// MinerGET contains the information that is returned after a GET request
    14  	// to /miner.
    15  	MinerGET struct {
    16  		BlocksMined      int  `json:"blocksmined"`
    17  		CPUHashrate      int  `json:"cpuhashrate"`
    18  		CPUMining        bool `json:"cpumining"`
    19  		StaleBlocksMined int  `json:"staleblocksmined"`
    20  	}
    21  )
    22  
    23  // minerHandler handles the API call that queries the miner's status.
    24  func (api *API) minerHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    25  	blocksMined, staleMined := api.miner.BlocksMined()
    26  	mg := MinerGET{
    27  		BlocksMined:      blocksMined,
    28  		CPUHashrate:      api.miner.CPUHashrate(),
    29  		CPUMining:        api.miner.CPUMining(),
    30  		StaleBlocksMined: staleMined,
    31  	}
    32  	WriteJSON(w, mg)
    33  }
    34  
    35  // minerStartHandler handles the API call that starts the miner.
    36  func (api *API) minerStartHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    37  	api.miner.StartCPUMining()
    38  	WriteSuccess(w)
    39  }
    40  
    41  // minerStopHandler handles the API call to stop the miner.
    42  func (api *API) minerStopHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    43  	api.miner.StopCPUMining()
    44  	WriteSuccess(w)
    45  }
    46  
    47  // minerHeaderHandlerGET handles the API call that retrieves a block header
    48  // for work.
    49  func (api *API) minerHeaderHandlerGET(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    50  	bhfw, target, err := api.miner.HeaderForWork()
    51  	if err != nil {
    52  		WriteError(w, Error{err.Error()}, http.StatusBadRequest)
    53  		return
    54  	}
    55  	w.Write(encoding.MarshalAll(target, bhfw))
    56  }
    57  
    58  // minerHeaderHandlerPOST handles the API call to submit a block header to the
    59  // miner.
    60  func (api *API) minerHeaderHandlerPOST(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    61  	var bh types.BlockHeader
    62  	err := encoding.NewDecoder(req.Body).Decode(&bh)
    63  	if err != nil {
    64  		WriteError(w, Error{err.Error()}, http.StatusBadRequest)
    65  		return
    66  	}
    67  	err = api.miner.SubmitHeader(bh)
    68  	if err != nil {
    69  		WriteError(w, Error{err.Error()}, http.StatusBadRequest)
    70  		return
    71  	}
    72  	WriteSuccess(w)
    73  }