Skip to content

Commit

Permalink
Merge pull request #12 from btccom/nicehash-diffs
Browse files Browse the repository at this point in the history
Utility to populate Nicehash configurations in ZooKeeper
  • Loading branch information
Hanjiang Yu committed Apr 3, 2019
2 parents 6e6992a + f47db02 commit 2846b78
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -15,3 +15,7 @@
# [Merged Mining Proxy](mergedMiningProxy/)

多币种联合挖矿代理,支持域名币(Namecoin)、亦来云(Elastos)等同时与比特币联合挖矿。

# [Init NiceHash] (initNiceHash)

初始化 ZooKeeper 中的 NiceHash 配置,通过调用 NiceHash API 来获取各个算法要求的最小难度,写入 ZooKeeper 以备 sserver 来使用。
7 changes: 7 additions & 0 deletions initNiceHash/Dockerfile
@@ -0,0 +1,7 @@
FROM golang:1.12

WORKDIR /work/initNiceHash
COPY . .

RUN go install -v

10 changes: 10 additions & 0 deletions initNiceHash/README.md
@@ -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
```
5 changes: 5 additions & 0 deletions initNiceHash/go.mod
@@ -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
2 changes: 2 additions & 0 deletions initNiceHash/go.sum
@@ -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=
134 changes: 134 additions & 0 deletions initNiceHash/main.go
@@ -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))
}

0 comments on commit 2846b78

Please sign in to comment.