Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made Quai CPU Miner a CLI app #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ quai-cpu-miner:

# to manually select a location to mine
run-mine:
./build/bin/quai-cpu-miner $(region) $(zone)
./build/bin/quai-cpu-miner --region $(region) --zone $(zone)

# to run in the background (manually set location)
run-mine-background:
ifeq (,$(wildcard logs))
mkdir logs
endif
@nohup ./build/bin/quai-cpu-miner $(region) $(zone) 1 >> logs/slice-$(region)-$(zone).log 2>&1 &
@nohup ./build/bin/quai-cpu-miner --region $(region) --zone $(zone) >> logs/slice-$(region)-$(zone).log 2>&1 &

stop:
ifeq ($(shell uname -s),Darwin)
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Quai CPU Miner
Djadih marked this conversation as resolved.
Show resolved Hide resolved

Quai CPU Miner is a cli app that utilizes CPU blake3 library to mine Quai Blockchain

## How to build the Quai CPU Miner
```
make quai-cpu-miner
```

## How to Run ?
Both MakeFile and command lines can be used to run the Quai CPU Miner.

For Example to run a Miner in `0,0` slice
### Running using the binary
```
./build/bin/quai-cpu-miner --region 0 --zone 0
```
It also supports `Aliases`, So running using Aliases will look like this, `--r` corresponds to `--region` and `--z` corresponds to `--zone`
```
./build/bin/quai-cpu-miner --r 0 --z 0
```

### Running using the Makefile
```
make run-mine region=0 zone=0
```

### Running using the Makefile in background
```
make run-mine-background region=0 zone=0
```
If running in Background the logs are saved in the `logs` folder

## How to stop the Miner
```
make stop
```

Additionally, all the flags can be seen in the help section using `--help` or `--h` command
```
./build/bin/quai-cpu-miner --help
```
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ require (
github.com/TwiN/go-color v1.4.0
github.com/dominant-strategies/go-quai v0.1.0-rc.0
github.com/spf13/viper v1.14.0
github.com/urfave/cli/v2 v2.24.4
)

require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/VictoriaMetrics/fastcache v1.6.0 // indirect
github.com/btcsuite/btcd v0.20.1-beta // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea // indirect
github.com/dominant-strategies/bn256 v0.0.0-20220930122411-fbf930a7493d // indirect
Expand All @@ -39,6 +41,7 @@ require (
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/tsdb v0.7.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
Expand All @@ -48,6 +51,7 @@ require (
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnht
github.com/consensys/bavard v0.1.8-0.20210406032232-f3452dc9b572/go.mod h1:Bpd0/3mZuaj6Sj+PqrmIquiOKy397AKGThQPaGzNXAQ=
github.com/consensys/gnark-crypto v0.4.1-0.20210426202927-39ac3d4b3f1f/go.mod h1:815PAHg3wvysy0SyIqanF8gZ0Y1wjk/hrDHD/iT88+Q=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -377,6 +379,8 @@ github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBO
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
Expand Down Expand Up @@ -425,8 +429,12 @@ github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefld
github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM=
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/urfave/cli/v2 v2.24.4 h1:0gyJJEBYtCV87zI/x2nZCPyDxD51K6xM8SkwjHFCNEU=
github.com/urfave/cli/v2 v2.24.4/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/willf/bitset v1.1.3/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
89 changes: 69 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"math/big"
"os"
"strconv"
"time"

"github.com/TwiN/go-color"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/quaiclient/ethclient"
"github.com/dominant-strategies/quai-cpu-miner/util"
"github.com/urfave/cli/v2"
)

const (
Expand All @@ -25,7 +25,17 @@ const (
)

var (
exit = make(chan bool)
exit = make(chan bool)
RegionFlag = cli.IntFlag{
Name: "region",
Aliases: []string{"r"},
Usage: "CPU Miner Region flag",
}
ZoneFlag = cli.IntFlag{
Name: "zone",
Aliases: []string{"z"},
Usage: "CPU Miner Zone flag",
}
)

type Miner struct {
Expand All @@ -34,7 +44,7 @@ type Miner struct {

// Blake3pow consensus engine used to seal a block
engine *blake3pow.Blake3pow

// Current header to mine
header *types.Header

Expand All @@ -45,7 +55,7 @@ type Miner struct {
updateCh chan *types.Header

// Channel to submit completed work
resultCh chan *types.Header
resultCh chan *types.Header

// Track previous block number for pretty printing
previousNumber [common.HierarchyDepth]uint64
Expand Down Expand Up @@ -99,15 +109,55 @@ func main() {
if err != nil {
log.Fatal("cannot load config:", err)
}
// Parse mining location from args
if len(os.Args) > 2 {
raw := os.Args[1:3]
region, _ := strconv.Atoi(raw[0])
zone, _ := strconv.Atoi(raw[1])
config.Location = common.Location{byte(region), byte(zone)}
} else {
log.Fatal("Not enough arguments supplied")
app := &cli.App{
Name: "Quai CPU Miner",
Flags: []cli.Flag{
&RegionFlag,
&ZoneFlag,
},
Commands: []*cli.Command{
&cli.Command{
Name: "help",
Aliases: []string{"h"},
After: func(cCtx *cli.Context) error {
return nil
},
},
},
Action: func(ctx *cli.Context) error {
err := verifyFlags(ctx)
if err != nil {
return err
}
config.Location = common.Location{byte(ctx.Int(RegionFlag.Name)), byte(ctx.Int(ZoneFlag.Name))}
StartMiner(config)
return nil
},
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
<-exit
}

func verifyFlags(ctx *cli.Context) error {
// If the Region or Zone flags are not set, or improperly set returns
// Appropriate errors
if !ctx.IsSet(RegionFlag.Name) {
return errors.New("region flag is not set")
} else if ctx.Int(RegionFlag.Name) < 0 || ctx.Int(RegionFlag.Name) > 2 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use helper common.HierarchyDepth

return errors.New("current ontology prime only has 3 regions, so please input between 0 and 2")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the messages too

}
if !ctx.IsSet(ZoneFlag.Name) {
return errors.New("zone flag is not set")
} else if ctx.Int(ZoneFlag.Name) < 0 || ctx.Int(ZoneFlag.Name) > 2 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as ^

return errors.New("current ontology each region only has 3 zones, so please input between 0 and 2")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as ^

}
return nil
}

func StartMiner(config util.Config) {
// Build manager config
blake3Config := blake3pow.Config{
NotifyFull: true,
Expand All @@ -118,20 +168,19 @@ func main() {
engine: blake3Engine,
sliceClients: connectToSlice(config),
header: types.EmptyHeader(),
updateCh: make(chan *types.Header, resultQueueSize),
updateCh: make(chan *types.Header, resultQueueSize),
resultCh: make(chan *types.Header, resultQueueSize),
previousNumber: [common.HierarchyDepth]uint64{0,0,0},
previousNumber: [common.HierarchyDepth]uint64{0, 0, 0},
}
log.Println("Starting Quai cpu miner in location ", config.Location)
m.fetchPendingHeader()
go m.subscribePendingHeader()
go m.resultLoop()
go m.miningLoop()
go m.hashratePrinter()
<-exit
}

func (m *Miner) client(ctx int) *ethclient.Client {return m.sliceClients[ctx]}
func (m *Miner) client(ctx int) *ethclient.Client { return m.sliceClients[ctx] }

// subscribePendingHeader subscribes to the head of the mining nodes in order to pass
// the most up to date block to the miner within the manager.
Expand Down Expand Up @@ -181,7 +230,7 @@ func (m *Miner) miningLoop() error {
// Interrupt previous sealing operation
interrupt()
stopCh = make(chan struct{})
number := [common.HierarchyDepth]uint64{header.NumberU64(common.PRIME_CTX),header.NumberU64(common.REGION_CTX),header.NumberU64(common.ZONE_CTX)}
number := [common.HierarchyDepth]uint64{header.NumberU64(common.PRIME_CTX), header.NumberU64(common.REGION_CTX), header.NumberU64(common.ZONE_CTX)}
primeStr := fmt.Sprint(number[common.PRIME_CTX])
regionStr := fmt.Sprint(number[common.REGION_CTX])
zoneStr := fmt.Sprint(number[common.ZONE_CTX])
Expand All @@ -198,7 +247,7 @@ func (m *Miner) miningLoop() error {
}
log.Println("Mining Block: ", fmt.Sprintf("[%s %s %s]", primeStr, regionStr, zoneStr), "location", header.Location(), "difficulty", header.DifficultyArray())
}
m.previousNumber = [common.HierarchyDepth]uint64{header.NumberU64(common.PRIME_CTX),header.NumberU64(common.REGION_CTX),header.NumberU64(common.ZONE_CTX)}
m.previousNumber = [common.HierarchyDepth]uint64{header.NumberU64(common.PRIME_CTX), header.NumberU64(common.REGION_CTX), header.NumberU64(common.ZONE_CTX)}
header.SetTime(uint64(time.Now().Unix()))
if err := m.engine.Seal(header, m.resultCh, stopCh); err != nil {
log.Println("Block sealing failed", "err", err)
Expand All @@ -210,15 +259,15 @@ func (m *Miner) miningLoop() error {
// WatchHashRate is a simple method to watch the hashrate of our miner and log the output.
func (m *Miner) hashratePrinter() {
ticker := time.NewTicker(60 * time.Second)
toSiUnits := func (hr float64) (float64, string) {
toSiUnits := func(hr float64) (float64, string) {
reduced := hr
order := 0
for {
if reduced >= 1000 {
reduced /= 1000
order += 3
} else {
break;
break
}
}
switch order {
Expand Down