gitlab.com/SiaPrime/SiaPrime@v1.4.1/node/api/miningpool.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"time"
     7  
     8  	"github.com/julienschmidt/httprouter"
     9  	"gitlab.com/SiaPrime/SiaPrime/modules"
    10  	"gitlab.com/SiaPrime/SiaPrime/types"
    11  )
    12  
    13  type (
    14  	// MiningPoolGET contains the stats that are returned after a GET request
    15  	// to /pool.
    16  	MiningPoolGET struct {
    17  		BlocksMined  int `json:"blocksmined"`
    18  		PoolHashrate int `json:"poolhashrate"`
    19  	}
    20  	// MiningPoolConfig contains the parameters you can set to config your pool
    21  	MiningPoolConfig struct {
    22  		NetworkPort    int              `json:"networkport"`
    23  		DBConnection   string           `json:"dbconnection"`
    24  		Name           string           `json:"name"`
    25  		PoolID         uint64           `json:"poolid"`
    26  		PoolWallet     types.UnlockHash `json:"poolwallet"`
    27  		OperatorWallet types.UnlockHash `json:"operatorwallet"`
    28  	}
    29  	// MiningPoolClientsInfo returns the stats are return after a GET request
    30  	// to /pool/clients
    31  	MiningPoolClientsInfo struct {
    32  		NumberOfClients uint64                 `json:"numberofclients"`
    33  		NumberOfWorkers uint64                 `json:"numberofworkers"`
    34  		Clients         []MiningPoolClientInfo `json:"clientinfo"`
    35  	}
    36  	// MiningPoolClientInfo returns the stats for a single client
    37  	MiningPoolClientInfo struct {
    38  		ClientName  string           `json:"clientname"`
    39  		BlocksMined uint64           `json:"blocksminer"`
    40  		Balance     string           `json:"balance"`
    41  		Workers     []PoolWorkerInfo `json:"workers"`
    42  	}
    43  	// MiningPoolClientTransaction returns info for a single transaction
    44  	MiningPoolClientTransaction struct {
    45  		BalanceChange string    `json:"balancechange"`
    46  		TxTime        time.Time `json:"txtime"`
    47  		Memo          string    `json:"memo"`
    48  	}
    49  	// PoolWorkerInfo returns info about one of a client's workers
    50  	PoolWorkerInfo struct {
    51  		WorkerName             string    `json:"workername"`
    52  		LastShareTime          time.Time `json:"lastsharetime"`
    53  		CurrentDifficulty      float64   `json:"currentdifficult"`
    54  		CumulativeDifficulty   float64   `json:"cumulativedifficulty"`
    55  		SharesThisBlock        uint64    `json:"sharesthisblock"`
    56  		InvalidSharesThisBlock uint64    `json:"invalidsharesthisblock"`
    57  		StaleSharesThisBlock   uint64    `json:"stalesharesthisblock"`
    58  		BlocksFound            uint64    `json:"blocksfound"`
    59  	}
    60  	// MiningPoolBlockInfo returns info about one of the pool's blocks
    61  	MiningPoolBlockInfo struct {
    62  		BlockNumber uint64    `json:"blocknumber"`
    63  		BlockHeight uint64    `json:"blockheight"`
    64  		BlockReward string    `json:"blockreward"`
    65  		BlockTime   time.Time `json:"blocktime"`
    66  		BlockStatus string    `json:"blockstatus"`
    67  	}
    68  	// MiningPoolBlockClientInfo returns info about one of the pool's block's clients
    69  	MiningPoolBlockClientInfo struct {
    70  		ClientName       string  `json:"clientname"`
    71  		ClientPercentage float64 `json:"clientpercentage"`
    72  		ClientReward     string  `json:"clientreward"`
    73  	}
    74  )
    75  
    76  // poolHandler handles the API call that queries the pool's status.
    77  func (api *API) poolHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    78  	pg := MiningPoolGET{
    79  		BlocksMined:  0,
    80  		PoolHashrate: 0,
    81  	}
    82  	WriteJSON(w, pg)
    83  }
    84  
    85  // poolConfigHandlerPOST handles POST request to the /pool API endpoint, which sets
    86  // the internal settings of the pool.
    87  func (api *API) poolConfigHandlerPOST(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
    88  	settings, err := api.parsePoolSettings(req)
    89  	if err != nil {
    90  		WriteError(w, Error{"error parsing pool settings: " + err.Error()}, http.StatusBadRequest)
    91  		return
    92  	}
    93  	err = api.pool.SetInternalSettings(settings)
    94  	if err != nil {
    95  		WriteError(w, Error{err.Error()}, http.StatusBadRequest)
    96  		return
    97  	}
    98  	WriteSuccess(w)
    99  }
   100  
   101  // poolConfigHandler handles the API call that queries the pool's status.
   102  func (api *API) poolConfigHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
   103  	settings, err := api.parsePoolSettings(req)
   104  	if err != nil {
   105  		WriteError(w, Error{"error parsing pool settings: " + err.Error()}, http.StatusBadRequest)
   106  		return
   107  	}
   108  	pg := MiningPoolConfig{
   109  		Name:         settings.PoolName,
   110  		NetworkPort:  settings.PoolNetworkPort,
   111  		DBConnection: settings.PoolDBConnection,
   112  		PoolID:       settings.PoolID,
   113  		PoolWallet:   settings.PoolWallet,
   114  	}
   115  	WriteJSON(w, pg)
   116  }
   117  
   118  // parsePoolSettings a request's query strings and returns a
   119  // modules.PoolInternalSettings configured with the request's query string
   120  // parameters.
   121  func (api *API) parsePoolSettings(req *http.Request) (modules.PoolInternalSettings, error) {
   122  	settings := api.pool.InternalSettings()
   123  
   124  	if req.FormValue("poolwallet") != "" {
   125  		var x types.UnlockHash
   126  		x, err := scanAddress(req.FormValue("poolwallet"))
   127  		if err != nil {
   128  			fmt.Println(err)
   129  			return modules.PoolInternalSettings{}, nil
   130  		}
   131  		settings.PoolWallet = x
   132  	}
   133  	if req.FormValue("networkport") != "" {
   134  		var x int
   135  		_, err := fmt.Sscan(req.FormValue("networkport"), &x)
   136  		if err != nil {
   137  			return modules.PoolInternalSettings{}, nil
   138  		}
   139  		settings.PoolNetworkPort = x
   140  
   141  	}
   142  	if req.FormValue("name") != "" {
   143  		settings.PoolName = req.FormValue("name")
   144  	}
   145  	if req.FormValue("poolid") != "" {
   146  		var x int
   147  		_, err := fmt.Sscan(req.FormValue("poolid"), &x)
   148  		if err != nil {
   149  			return modules.PoolInternalSettings{}, nil
   150  		}
   151  		settings.PoolID = uint64(x)
   152  	}
   153  	if req.FormValue("dbconnection") != "" {
   154  		settings.PoolDBConnection = req.FormValue("dbconnection")
   155  	}
   156  	err := api.pool.SetInternalSettings(settings)
   157  	return settings, err
   158  }