-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from btccom/nicehash-diffs
Utility to populate Nicehash configurations in ZooKeeper
- Loading branch information
Showing
6 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM golang:1.12 | ||
|
||
WORKDIR /work/initNiceHash | ||
COPY . . | ||
|
||
RUN go install -v | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Init NiceHash | ||
|
||
``` | ||
# Build a Docker image for the initNiceHash utility | ||
docker build --rm -t init_nicehash . | ||
# Run the initNiceHash utility to retrieve NiceHash configurations | ||
# (using default NiceHash API and ZooKeeper is running on localhost) | ||
docker run --rm init_nicehash initNiceHash -zookeeper 127.0.0.1:2181 -path /nicehash | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/btccom/btcpool-go-modules/initNiceHash | ||
|
||
go 1.12 | ||
|
||
require github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec h1:6ncX5ko6B9LntYM0YBRXkiSaZMmLYeZ/NWcmeB43mMY= | ||
github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/samuel/go-zookeeper/zk" | ||
) | ||
|
||
type Algorithm struct { | ||
Name string `json:"name"` | ||
MinDiff string `json:"min_diff_working"` | ||
} | ||
|
||
type Configuration struct { | ||
Algorithms []Algorithm | ||
} | ||
|
||
type Reply struct { | ||
Result Configuration `json:"result"` | ||
} | ||
|
||
func getNiceHashConfiguration(url string) Configuration { | ||
log.Printf("Calling NiceHash API %s", url) | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
log.Fatalf("Failed to call NiceHash API: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Fatalf("Failed to read NiceHash API response: %v", err) | ||
} | ||
log.Printf("NiceHash API response: %s", body) | ||
|
||
var reply Reply | ||
err = json.Unmarshal(body, &reply) | ||
if err != nil { | ||
log.Fatalf("Failed to unmarshal NiceHash API response: %v", err) | ||
} | ||
|
||
return reply.Result | ||
} | ||
|
||
func populateNiceHashNodes(zookeeper string, path string, config Configuration) { | ||
servers := strings.Split(zookeeper, ",") | ||
if len(zookeeper) == 0 || len(servers) == 0 { | ||
log.Print("ZooKeeper servers are not specificed, exit now") | ||
return | ||
} | ||
c, _, err := zk.Connect(servers, time.Second*5) | ||
if err != nil { | ||
log.Fatalf("Failed to connect to ZooKeeper: %v", err) | ||
} | ||
defer c.Close() | ||
|
||
dirs := strings.Split(path, "/") | ||
prefix := "" | ||
for _, dir := range dirs { | ||
if len(dir) != 0 { | ||
prefix += "/" + strings.ToLower(dir) | ||
exists, _, err := c.Exists(prefix) | ||
if err != nil { | ||
log.Fatalf("Failed to check Zookeeper node %s: %v", prefix, err) | ||
} | ||
|
||
if !exists { | ||
_, err = c.Create(prefix, []byte{}, 0, zk.WorldACL(zk.PermAll)) | ||
|
||
if err != nil { | ||
log.Fatalf("Failed to create Zookeeper node %s: %v", prefix, err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
for _, algo := range config.Algorithms { | ||
minDiff, err := strconv.ParseFloat(algo.MinDiff, 64) | ||
if err != nil { | ||
log.Printf("Minimal required difficulty for algorithm %s is not a number: %s", algo.Name, algo.MinDiff) | ||
continue | ||
} else { | ||
log.Printf("Minimal required difficulty for algorithm %s is %s", algo.Name, algo.MinDiff) | ||
} | ||
|
||
algoName := strings.ToLower(algo.Name) | ||
if algoName == "daggerhashimoto" { | ||
minDiff *= 4294967296 | ||
} | ||
|
||
nodeAlgo := prefix + "/" + algoName | ||
exists, _, err := c.Exists(nodeAlgo) | ||
if err != nil { | ||
log.Fatalf("Failed to check ZooKeeper node %s: %v", nodeAlgo, err) | ||
} | ||
if !exists { | ||
_, err := c.Create(nodeAlgo, []byte{}, 0, zk.WorldACL(zk.PermAll)) | ||
if err != nil { | ||
log.Fatalf("Failed to create ZooKeeper node %s: %v", nodeAlgo, err) | ||
} | ||
} | ||
|
||
nodeMinDiff := nodeAlgo + "/min_difficulty" | ||
exists, _, err = c.Exists(nodeMinDiff) | ||
data := []byte(strconv.FormatUint(uint64(minDiff), 10)) | ||
if exists { | ||
_, err := c.Set(nodeMinDiff, data, -1) | ||
if err != nil { | ||
log.Fatalf("Failed to write ZooKeeper node %s: %v", nodeMinDiff, err) | ||
} | ||
} else { | ||
_, err := c.Create(nodeMinDiff, data, 0, zk.WorldACL(zk.PermAll)) | ||
if err != nil { | ||
log.Fatalf("Failed to create ZooKeeper node %s: %v", nodeMinDiff, err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
url := flag.String("url", "https://api.nicehash.com/api?method=buy.info", "NiceHash API URL") | ||
zookeeper := flag.String("zookeeper", "", "ZooKeeper servers separated by comma") | ||
path := flag.String("path", "/nicehash", "ZooKeeper path to store NiceHash configurations") | ||
flag.Parse() | ||
|
||
populateNiceHashNodes(*zookeeper, *path, getNiceHashConfiguration(*url)) | ||
} |