Skip to content

Commit

Permalink
Merge branch 'unified-nicehash-v1' of https://github.com/EthersocialN…
Browse files Browse the repository at this point in the history
  • Loading branch information
iquidus committed Jan 22, 2021
2 parents bd71587 + 62392c6 commit 3515a5e
Show file tree
Hide file tree
Showing 52 changed files with 7,333 additions and 6,765 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,13 @@ otherwise you will get errors on start because of JSON comments.**
// Bind stratum mining socket to this IP:PORT
"listen": "0.0.0.0:8008",
"timeout": "120s",
"maxConn": 8192
"maxConn": 8192,
"tls": false,
"certFile": "/path/to/cert.pem",
"keyFile": "/path/to/key.pem"
},

// Try to get new job from geth in this interval
// Try to get new job from node in this interval
"blockRefreshInterval": "120ms",
"stateUpdateInterval": "3s",
// Require this share difficulty from miners
Expand Down Expand Up @@ -169,10 +172,10 @@ otherwise you will get errors on start because of JSON comments.**
"purgeOnly": false
},

// Check health of each geth node in this interval
// Check health of each node in this interval
"upstreamCheckInterval": "5s",

/* List of geth nodes to poll for new jobs. Pool will try to get work from
/* List of parity nodes to poll for new jobs. Pool will try to get work from
first alive one and check in background for failed to back up.
Current block template of the pool is always cached in RAM indeed.
*/
Expand Down Expand Up @@ -215,9 +218,9 @@ otherwise you will get errors on start because of JSON comments.**
"keepTxFees": false,
// Run unlocker in this interval
"interval": "10m",
// Geth instance node rpc endpoint for unlocking blocks
// Parity node rpc endpoint for unlocking blocks
"daemon": "http://127.0.0.1:8545",
// Rise error if can't reach geth in this amount of time
// Rise error if can't reach parity
"timeout": "10s"
},

Expand All @@ -228,13 +231,13 @@ otherwise you will get errors on start because of JSON comments.**
"requirePeers": 25,
// Run payouts in this interval
"interval": "12h",
// Geth instance node rpc endpoint for payouts processing
// Parity node rpc endpoint for payouts processing
"daemon": "http://127.0.0.1:8545",
// Rise error if can't reach geth in this amount of time
// Rise error if can't reach parity
"timeout": "10s",
// Address with pool balance
"address": "0x0",
// Let geth to determine gas and gasPrice
// Let parity to determine gas and gasPrice
"autoGas": true,
// Gas amount and price for payout tx (advanced users only)
"gas": "21000",
Expand Down
5 changes: 3 additions & 2 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"hashrateExpiration": "3h",

"healthCheck": true,
"debug": false,
"maxFails": 100,

"stratum": {
Expand All @@ -24,8 +25,8 @@
"timeout": "120s",
"maxConn": 8192,
"tls": false,
"certFile": "/path/to/cert.pem",
"keyFile": "/path/to/key.pem"
"certFile": "/path/to/cert.pem",
"keyFile": "/path/to/key.pem"
},

"policy": {
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.15
require (
github.com/btcsuite/btcd v0.21.0-beta // indirect
github.com/edsrzf/mmap-go v1.0.0
github.com/etclabscore/core-pool v0.0.0-00010101000000-000000000000
github.com/etclabscore/go-etchash v0.0.0-20201116172336-ee6bd2e29b29
github.com/ethereum/go-ethereum v1.9.24
github.com/garyburd/redigo v1.6.2 // indirect
Expand All @@ -18,3 +19,7 @@ require (
gopkg.in/bsm/ratelimit.v1 v1.0.0-20160220154919-db14e161995a // indirect
gopkg.in/redis.v3 v3.6.4
)

// develop
// replace github.com/etclabscore/go-etchash => /home/xocel/go/src/github.com/etclabscore/go-etchash
// replace github.com/etclabscore/core-pool => /home/xocel/go/src/github.com/etclabscore/core-pool
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277/go.mod h1
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hackmod/ethereum-ethash v0.0.0-20181221122758-d53632c3f91d h1:eV5L9fGrxvVjtokumbnxxC7BIOd1OkDGHyMtov5UpUc=
github.com/hackmod/ethereum-ethash v0.0.0-20181221122758-d53632c3f91d/go.mod h1:EVWn9m+4GpYFmo9BFPJn5dQJGEXuw+1yPjHo1mnBRQM=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -303,6 +305,8 @@ github.com/rs/xhandler v0.0.0-20160618193221-ed27b6fd6521/go.mod h1:RvLn4FgxWubr
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sammy007/open-ethereum-pool v0.0.0-20171111114927-bcfc0eb0e683 h1:5sGr1dJAyelixsyB3eBjftKl3+cvNeDsIYsFIfTwS78=
github.com/sammy007/open-ethereum-pool v0.0.0-20171111114927-bcfc0eb0e683/go.mod h1:ceG8iCx4zh1bdt1zplyFb0YQQrAmWcBcClT2Quo7KXQ=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shirou/gopsutil v2.20.5+incompatible h1:tYH07UPoQt0OCQdgWWMgYHy3/a9bcxNpBIysykNIP7I=
github.com/shirou/gopsutil v2.20.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func startApi() {
}

func startBlockUnlocker() {
u := payouts.NewBlockUnlocker(&cfg.BlockUnlocker, backend, &cfg.Network)
u := payouts.NewBlockUnlocker(&cfg.BlockUnlocker, backend, cfg.Network)
u.Start()
}

Expand Down
4 changes: 2 additions & 2 deletions payouts/unlocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (u *BlockUnlocker) unlockPendingBlocks() {
return
}

current, err := u.rpc.GetPendingBlock()
current, err := u.rpc.GetLatestBlock()
if err != nil {
u.halt = true
u.lastFail = err
Expand Down Expand Up @@ -421,7 +421,7 @@ func (u *BlockUnlocker) unlockAndCreditMiners() {
return
}

current, err := u.rpc.GetPendingBlock()
current, err := u.rpc.GetLatestBlock()
if err != nil {
u.halt = true
u.lastFail = err
Expand Down
44 changes: 12 additions & 32 deletions proxy/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,37 @@ func (b Block) MixDigest() common.Hash { return b.mixDigest }
func (b Block) NumberU64() uint64 { return b.number }

func (s *ProxyServer) fetchBlockTemplate() {
rpc := s.rpc()
r := s.rpc()
t := s.currentBlockTemplate()
pendingReply, height, diff, err := s.fetchPendingBlock()
reply, err := r.GetWork()
if err != nil {
log.Printf("Error while refreshing pending block on %s: %s", rpc.Name, err)
return
}
reply, err := rpc.GetWork()
if err != nil {
log.Printf("Error while refreshing block template on %s: %s", rpc.Name, err)
log.Printf("Error while refreshing block template on %s: %s", r.Name, err)
return
}
// No need to update, we have fresh job
if t != nil && t.Header == reply[0] {
return
}
diff := util.TargetHexToDiff(reply[2])
height, err := strconv.ParseUint(strings.Replace(reply[3], "0x", "", -1), 16, 64)

pendingReply.Difficulty = util.ToHex(s.config.Proxy.Difficulty)
pendingReply := &rpc.GetBlockReplyPart{
Difficulty: util.ToHex(s.config.Proxy.Difficulty),
Number: reply[3],
}

newTemplate := BlockTemplate{
Header: reply[0],
Seed: reply[1],
Target: reply[2],
Height: height,
Difficulty: big.NewInt(diff),
Difficulty: diff,
GetPendingBlockCache: pendingReply,
headers: make(map[string]heightDiffPair),
}
// Copy job backlog and add current one
newTemplate.headers[reply[0]] = heightDiffPair{
diff: util.TargetHexToDiff(reply[2]),
diff: diff,
height: height,
}
if t != nil {
Expand All @@ -88,30 +88,10 @@ func (s *ProxyServer) fetchBlockTemplate() {
}
}
s.blockTemplate.Store(&newTemplate)
log.Printf("New block to mine on %s at height %d / %s", rpc.Name, height, reply[0][0:10])
log.Printf("New block to mine on %s at height %d / %s / %d", r.Name, height, reply[0][0:10], diff)

// Stratum
if s.config.Proxy.Stratum.Enabled {
go s.broadcastNewJobs()
}
}

func (s *ProxyServer) fetchPendingBlock() (*rpc.GetBlockReplyPart, uint64, int64, error) {
rpc := s.rpc()
reply, err := rpc.GetPendingBlock()
if err != nil {
log.Printf("Error while refreshing pending block on %s: %s", rpc.Name, err)
return nil, 0, 0, err
}
blockNumber, err := strconv.ParseUint(strings.Replace(reply.Number, "0x", "", -1), 16, 64)
if err != nil {
log.Println("Can't parse pending block number")
return nil, 0, 0, err
}
blockDiff, err := strconv.ParseInt(strings.Replace(reply.Difficulty, "0x", "", -1), 16, 64)
if err != nil {
log.Println("Can't parse pending block difficulty")
return nil, 0, 0, err
}
return reply, blockNumber, blockDiff, nil
}
16 changes: 4 additions & 12 deletions proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,19 @@ type Proxy struct {

MaxFails int64 `json:"maxFails"`
HealthCheck bool `json:"healthCheck"`
Debug bool `json:"debug"`

Stratum Stratum `json:"stratum"`

StratumNiceHash StratumNiceHash `json:"stratum_nice_hash"`
}

type Stratum struct {
Enabled bool `json:"enabled"`
Listen string `json:"listen"`
Timeout string `json:"timeout"`
MaxConn int `json:"maxConn"`
TLS bool `json:"tls`
CertFile string `json:"certFile`
KeyFile string `json:"keyFile`
}

type StratumNiceHash struct {
Enabled bool `json:"enabled"`
Listen string `json:"listen"`
Timeout string `json:"timeout"`
MaxConn int `json:"maxConn"`
TLS bool `json:"tls"`
CertFile string `json:"certFile"`
KeyFile string `json:"keyFile"`
}

type Upstream struct {
Expand Down
23 changes: 19 additions & 4 deletions proxy/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (s *ProxyServer) handleGetWorkRPC(cs *Session) ([]string, *ErrorReply) {
if t == nil || len(t.Header) == 0 || s.isSick() {
return nil, &ErrorReply{Code: 0, Message: "Work not ready"}
}
return []string{t.Header, t.Seed, s.diff}, nil
return []string{t.Header, t.Seed, s.diff, util.ToHex(int64(t.Height))}, nil
}

// Stratum
Expand All @@ -63,18 +63,31 @@ func (s *ProxyServer) handleSubmitRPC(cs *Session, login, id string, params []st
return false, &ErrorReply{Code: -1, Message: "Invalid params"}
}

stratumMode := cs.stratumMode()
if stratumMode == NiceHash {
for i := 0; i <= 2; i++ {
if params[i][0:2] != "0x" {
params[i] = "0x" + params[i]
}
}
}

if !noncePattern.MatchString(params[0]) || !hashPattern.MatchString(params[1]) || !hashPattern.MatchString(params[2]) {
s.policy.ApplyMalformedPolicy(cs.ip)
log.Printf("Malformed PoW result from %s@%s %v", login, cs.ip, params)
return false, &ErrorReply{Code: -1, Message: "Malformed PoW result"}
}
t := s.currentBlockTemplate()
exist, validShare := s.processShare(login, id, cs.ip, t, params)
exist, validShare := s.processShare(login, id, cs.ip, t, params, stratumMode != EthProxy)
ok := s.policy.ApplySharePolicy(cs.ip, !exist && validShare)

if exist {
log.Printf("Duplicate share from %s@%s %v", login, cs.ip, params)
return false, &ErrorReply{Code: 22, Message: "Duplicate share"}
// see https://github.com/sammy007/open-ethereum-pool/compare/master...nicehashdev:patch-1
if !ok {
return false, &ErrorReply{Code: 23, Message: "Invalid share"}
}
return false, nil
}

if !validShare {
Expand All @@ -85,7 +98,9 @@ func (s *ProxyServer) handleSubmitRPC(cs *Session, login, id string, params []st
}
return false, nil
}
log.Printf("Valid share from %s@%s", login, cs.ip)
if s.config.Proxy.Debug {
log.Printf("Valid share from %s@%s", login, cs.ip)
}

if !ok {
return true, &ErrorReply{Code: -1, Message: "High rate of invalid shares"}
Expand Down
Loading

0 comments on commit 3515a5e

Please sign in to comment.