gitlab.com/SkynetLabs/skyd@v1.6.9/cmd/skyc/minercmd.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/cobra"
     7  	"gitlab.com/NebulousLabs/errors"
     8  	"gitlab.com/SkynetLabs/skyd/node/api"
     9  )
    10  
    11  var (
    12  	minerCmd = &cobra.Command{
    13  		Use:   "miner",
    14  		Short: "Perform miner actions",
    15  		Long:  "Perform miner actions and view miner status.",
    16  		Run:   wrap(minercmd),
    17  	}
    18  
    19  	minerStartCmd = &cobra.Command{
    20  		Use:   "start",
    21  		Short: "Start cpu mining",
    22  		Long:  "Start cpu mining, if the miner is already running, this command does nothing",
    23  		Run:   wrap(minerstartcmd),
    24  	}
    25  
    26  	minerStopCmd = &cobra.Command{
    27  		Use:   "stop",
    28  		Short: "Stop mining",
    29  		Long:  "Stop mining (this may take a few moments).",
    30  		Run:   wrap(minerstopcmd),
    31  	}
    32  )
    33  
    34  // minerstartcmd is the handler for the command `skyc miner start`.
    35  // Starts the CPU miner.
    36  func minerstartcmd() {
    37  	err := httpClient.MinerStartGet()
    38  	if err != nil {
    39  		die("Could not start miner:", err)
    40  	}
    41  	fmt.Println("CPU Miner is now running.")
    42  }
    43  
    44  // minercmd is the handler for the command `skyc miner`.
    45  // Prints the status of the miner.
    46  func minercmd() {
    47  	status, err := httpClient.MinerGet()
    48  	if errors.Contains(err, api.ErrAPICallNotRecognized) {
    49  		// Assume module is not loaded if status command is not recognized.
    50  		fmt.Printf("Miner:\n  Status: %s\n\n", moduleNotReadyStatus)
    51  		return
    52  	} else if err != nil {
    53  		die("Could not get miner status:", err)
    54  	}
    55  
    56  	miningStr := "off"
    57  	if status.CPUMining {
    58  		miningStr = "on"
    59  	}
    60  	fmt.Printf(`Miner status:
    61  CPU Mining:   %s
    62  CPU Hashrate: %v KH/s
    63  Blocks Mined: %d (%d stale)
    64  `, miningStr, status.CPUHashrate/1000, status.BlocksMined, status.StaleBlocksMined)
    65  }
    66  
    67  // minerstopcmd is the handler for the command `skyc miner stop`.
    68  // Stops the CPU miner.
    69  func minerstopcmd() {
    70  	err := httpClient.MinerStopGet()
    71  	if err != nil {
    72  		die("Could not stop miner:", err)
    73  	}
    74  	fmt.Println("Stopped mining.")
    75  }