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

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