-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcrpool.go
214 lines (186 loc) · 4.78 KB
/
dcrpool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (c) 2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"context"
"fmt"
"math"
"math/big"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"runtime"
"github.com/HcashOrg/hcd/hcutil"
"github.com/decred/dcrd/rpcclient"
"github.com/btclinux/hcpool/gui"
"github.com/btclinux/hcpool/pool"
)
// miningPool represents a decred Proof-of-Work mining pool.
type miningPool struct {
cfg *config
httpc *http.Client
ctx context.Context
cancel context.CancelFunc
hub *pool.Hub
gui *gui.GUI
limiter *pool.RateLimiter
}
// newPool initializes the mining pool.
func newPool(cfg *config) (*miningPool, error) {
p := new(miningPool)
p.cfg = cfg
p.limiter = pool.NewRateLimiter()
dcrdRPCCfg := &rpcclient.ConnConfig{
Host: cfg.DcrdRPCHost,
Endpoint: "ws",
User: cfg.RPCUser,
Pass: cfg.RPCPass,
Certificates: cfg.dcrdRPCCerts,
}
minPmt, err := hcutil.NewAmount(cfg.MinPayment)
if err != nil {
return nil, err
}
maxTxFeeReserve, err := hcutil.NewAmount(cfg.MaxTxFeeReserve)
if err != nil {
return nil, err
}
p.ctx, p.cancel = context.WithCancel(context.Background())
powLimit := cfg.net.PowLimit
powLimitF, _ := new(big.Float).SetInt(powLimit).Float64()
iterations := math.Pow(2, 256-math.Floor(math.Log2(powLimitF)))
addPort := func(ports map[string]uint32, key string, entry uint32) error {
var match bool
var miner string
for m, port := range ports {
if port == entry {
match = true
miner = m
break
}
}
if match {
return fmt.Errorf("%s and %s share port :%d. "+
"Miner listening ports must be unique.", key, miner, entry)
}
ports[key] = entry
return nil
}
// Ensure provided miner ports are unique.
minerPorts := make(map[string]uint32)
_ = addPort(minerPorts, pool.CPU, cfg.CPUPort)
err = addPort(minerPorts, pool.InnosiliconD9, cfg.D9Port)
if err != nil {
return nil, err
}
err = addPort(minerPorts, pool.AntminerDR3, cfg.DR3Port)
if err != nil {
return nil, err
}
err = addPort(minerPorts, pool.AntminerDR5, cfg.DR5Port)
if err != nil {
return nil, err
}
err = addPort(minerPorts, pool.WhatsminerD1, cfg.D1Port)
if err != nil {
return nil, err
}
hcfg := &pool.HubConfig{
ActiveNet: cfg.net,
WalletRPCCertFile: cfg.WalletRPCCert,
WalletGRPCHost: cfg.WalletGRPCHost,
DcrdRPCCfg: dcrdRPCCfg,
PoolFee: cfg.PoolFee,
MaxTxFeeReserve: maxTxFeeReserve,
MaxGenTime: cfg.MaxGenTime,
PaymentMethod: cfg.PaymentMethod,
DBFile: cfg.DBFile,
LastNPeriod: cfg.LastNPeriod,
WalletPass: cfg.WalletPass,
MinPayment: minPmt,
PoolFeeAddrs: cfg.poolFeeAddrs,
SoloPool: cfg.SoloPool,
NonceIterations: iterations,
MinerPorts: minerPorts,
MaxConnectionsPerHost: cfg.MaxConnectionsPerHost,
}
p.hub, err = pool.NewHub(p.ctx, p.cancel, p.httpc, hcfg, p.limiter)
if err != nil {
return nil, err
}
gcfg := &gui.Config{
Ctx: p.ctx,
SoloPool: cfg.SoloPool,
GUIDir: cfg.GUIDir,
BackupPass: cfg.BackupPass,
GUIPort: cfg.GUIPort,
UseLEHTTPS: cfg.UseLEHTTPS,
Domain: cfg.Domain,
TLSCertFile: cfg.TLSCert,
TLSKeyFile: cfg.TLSKey,
ActiveNet: cfg.net,
PaymentMethod: cfg.PaymentMethod,
Designation: cfg.Designation,
PoolFee: cfg.PoolFee,
MinerPorts: minerPorts,
}
p.gui, err = gui.NewGUI(gcfg, p.hub, p.limiter)
if err != nil {
return nil, err
}
return p, nil
}
func main() {
// Listen for interrupt signals.
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
// Load configuration and parse command line. This also initializes logging
// and configures it accordingly.
cfg, _, err := loadConfig()
if err != nil {
mpLog.Error(err)
return
}
defer func() {
if logRotator != nil {
logRotator.Close()
}
}()
p, err := newPool(cfg)
if err != nil {
mpLog.Error(err)
return
}
if cfg.Profile != "" {
// Start the profiler.
go func() {
listenAddr := cfg.Profile
mpLog.Infof("Creating profiling server listening "+
"on %s", listenAddr)
profileRedirect := http.RedirectHandler("/debug/pprof",
http.StatusSeeOther)
http.Handle("/", profileRedirect)
err := http.ListenAndServe(listenAddr, nil)
if err != nil {
mpLog.Criticalf(err.Error())
p.cancel()
}
}()
}
mpLog.Infof("Version: %s", version())
mpLog.Infof("Runtime: Go version %s", runtime.Version())
mpLog.Infof("Home dir: %s", cfg.HomeDir)
mpLog.Infof("Started dcrpool.")
go func() {
select {
case <-p.ctx.Done():
return
case <-interrupt:
p.cancel()
}
}()
p.gui.Run(p.ctx)
p.hub.Run(p.ctx)
}