forked from decred/dcrpool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcrpool.go
277 lines (250 loc) · 6.72 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// 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"
"decred.org/dcrwallet/rpc/walletrpc"
"github.com/decred/dcrd/rpcclient/v6"
"github.com/decred/dcrpool/gui"
"github.com/decred/dcrpool/pool"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// miningPool represents a decred Proof-of-Work mining pool.
type miningPool struct {
cfg *config
ctx context.Context
cancel context.CancelFunc
hub *pool.Hub
gui *gui.GUI
}
// newPool initializes the mining pool.
func newPool(cfg *config) (*miningPool, error) {
p := new(miningPool)
p.cfg = cfg
dcrdRPCCfg := &rpcclient.ConnConfig{
Host: cfg.DcrdRPCHost,
Endpoint: "ws",
User: cfg.RPCUser,
Pass: cfg.RPCPass,
Certificates: cfg.dcrdRPCCerts,
}
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", 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
}
err = addPort(minerPorts, pool.ObeliskDCR1, cfg.DCR1Port)
if err != nil {
return nil, err
}
db, err := pool.InitDB(cfg.DBFile, cfg.SoloPool)
if err != nil {
return nil, err
}
hcfg := &pool.HubConfig{
DB: db,
ActiveNet: cfg.net.Params,
PoolFee: cfg.PoolFee,
MaxGenTime: cfg.MaxGenTime,
PaymentMethod: cfg.PaymentMethod,
LastNPeriod: cfg.LastNPeriod,
WalletPass: cfg.WalletPass,
PoolFeeAddrs: cfg.poolFeeAddrs,
SoloPool: cfg.SoloPool,
NonceIterations: iterations,
MinerPorts: minerPorts,
MaxConnectionsPerHost: cfg.MaxConnectionsPerHost,
WalletAccount: cfg.WalletAccount,
}
p.hub, err = pool.NewHub(p.cancel, hcfg)
if err != nil {
return nil, err
}
// Establish a connection to the mining node.
ntfnHandlers := p.hub.CreateNotificationHandlers()
nodeConn, err := rpcclient.New(dcrdRPCCfg, ntfnHandlers)
if err != nil {
return nil, err
}
if err := nodeConn.NotifyWork(p.ctx); err != nil {
nodeConn.Shutdown()
return nil, err
}
if err := nodeConn.NotifyBlocks(p.ctx); err != nil {
nodeConn.Shutdown()
return nil, err
}
p.hub.SetNodeConnection(nodeConn)
// Establish a connection to the wallet if the pool is mining as a
// publicly available mining pool.
if !cfg.SoloPool {
creds, err := credentials.
NewClientTLSFromFile(cfg.WalletRPCCert, "localhost")
if err != nil {
return nil, err
}
grpc, err := grpc.Dial(cfg.WalletGRPCHost,
grpc.WithTransportCredentials(creds))
if err != nil {
return nil, err
}
// Perform a Balance request to check connectivity and account
// existence.
walletConn := walletrpc.NewWalletServiceClient(grpc)
req := &walletrpc.BalanceRequest{
AccountNumber: cfg.WalletAccount,
RequiredConfirmations: 1,
}
_, err = walletConn.Balance(p.ctx, req)
if err != nil {
return nil, err
}
p.hub.SetWalletConnection(walletConn, grpc.Close)
confNotifs, err := walletConn.ConfirmationNotifications(p.ctx)
if err != nil {
return nil, err
}
p.hub.SetTxConfNotifClient(confNotifs)
}
err = p.hub.FetchWork(p.ctx)
if err != nil {
return nil, err
}
err = p.hub.Listen()
if err != nil {
return nil, err
}
csrfSecret, err := p.hub.CSRFSecret()
if err != nil {
return nil, err
}
gcfg := &gui.Config{
SoloPool: cfg.SoloPool,
GUIDir: cfg.GUIDir,
AdminPass: cfg.AdminPass,
GUIPort: cfg.GUIPort,
UseLEHTTPS: cfg.UseLEHTTPS,
Domain: cfg.Domain,
TLSCertFile: cfg.TLSCert,
TLSKeyFile: cfg.TLSKey,
ActiveNet: cfg.net.Params,
PaymentMethod: cfg.PaymentMethod,
Designation: cfg.Designation,
PoolFee: cfg.PoolFee,
CSRFSecret: csrfSecret,
MinerPorts: minerPorts,
WithinLimit: p.hub.WithinLimit,
FetchLastWorkHeight: p.hub.FetchLastWorkHeight,
FetchLastPaymentHeight: p.hub.FetchLastPaymentHeight,
FetchMinedWork: p.hub.FetchMinedWork,
FetchWorkQuotas: p.hub.FetchWorkQuotas,
BackupDB: p.hub.BackupDB,
FetchClients: p.hub.FetchClients,
AccountExists: p.hub.AccountExists,
FetchArchivedPayments: p.hub.FetchArchivedPayments,
FetchPendingPayments: p.hub.FetchPendingPayments,
FetchCacheChannel: p.hub.FetchCacheChannel,
}
p.gui, err = gui.NewGUI(gcfg)
if err != nil {
p.hub.CloseListeners()
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 {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer func() {
if logRotator != nil {
logRotator.Close()
}
}()
p, err := newPool(cfg)
if err != nil {
mpLog.Error(err)
os.Exit(1)
}
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)
}