-
Notifications
You must be signed in to change notification settings - Fork 33
/
pool.go
415 lines (355 loc) · 12.4 KB
/
pool.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// (c) 2018 PoC Consortium ALL RIGHTS RESERVED
package pool
import (
"encoding/json"
"fmt"
"go.uber.org/zap"
"net"
"net/http"
"runtime"
"strconv"
"time"
"github.com/PoC-Consortium/Nogrod/pkg/burstmath"
. "github.com/PoC-Consortium/Nogrod/pkg/config"
. "github.com/PoC-Consortium/Nogrod/pkg/logger"
. "github.com/PoC-Consortium/Nogrod/pkg/modelx"
"github.com/PoC-Consortium/Nogrod/pkg/nodecom"
"github.com/PoC-Consortium/Nogrod/pkg/wallethandler"
"github.com/throttled/throttled"
"github.com/throttled/throttled/store/memstore"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/reflection"
)
const (
submitBefore = 30 * time.Second
nonceSubmissionRetries = 3
)
type Pool struct {
modelx *Modelx
walletHandler wallethandler.WalletHandler
nonceSubmissions chan *NonceSubmission
deadlineRequestHandler *burstmath.DeadlineRequestHandler
}
type nodeServer struct {
modelx *Modelx
nonceSubmissions chan *NonceSubmission
}
func NewPool(modelx *Modelx, walletHandler wallethandler.WalletHandler) *Pool {
pool := &Pool{
walletHandler: walletHandler,
modelx: modelx,
nonceSubmissions: make(chan *NonceSubmission),
deadlineRequestHandler: burstmath.NewDeadlineRequestHandler(runtime.NumCPU())}
currentBlock := Cache.CurrentBlock()
go pool.checkAndAddNewBlockJob()
go pool.forge(currentBlock)
return pool
}
func (pool *Pool) forge(currentBlock Block) {
var bestNonceSubmission *NonceSubmission
maxTime := time.Duration(1<<63 - 1)
var after <-chan time.Time
updateSubmitTimer := func(deadline uint64, roundStart time.Time) {
due := time.Duration(deadline)*time.Second - time.Since(roundStart) - submitBefore
Logger.Info("planning submitNonce", zap.Duration("delay", due))
after = time.After(due)
}
if currentBlock.BestNonceSubmissionID.Valid {
var err error
bestNonceSubmission, err = pool.modelx.GetBestNonceSubmissionOnBlock(currentBlock.Height)
if err != nil {
Logger.Fatal("error getting best nonce submission", zap.Error(err))
}
Cache.StoreBestNonceSubmission(*bestNonceSubmission)
updateSubmitTimer(bestNonceSubmission.Deadline, bestNonceSubmission.RoundStart)
} else {
// if we don't have a current time wait forever and don't submit anything
bestNonceSubmission = &NonceSubmission{Deadline: ^uint64(0)}
after = time.After(maxTime)
}
for {
select {
case nonceSubmission := <-pool.nonceSubmissions:
// ignore old blocks
if nonceSubmission.Height < bestNonceSubmission.Height {
continue
}
// ignore worse deadlines
if nonceSubmission.Height == bestNonceSubmission.Height &&
nonceSubmission.Deadline >= bestNonceSubmission.Deadline {
continue
}
Logger.Info("new best deadline", zap.Uint64("deadline", nonceSubmission.Deadline))
bestNonceSubmission = nonceSubmission
pool.modelx.UpdateBestSubmission(nonceSubmission.MinerID, nonceSubmission.Height)
Cache.StoreBestNonceSubmission(*bestNonceSubmission)
updateSubmitTimer(nonceSubmission.Deadline, nonceSubmission.RoundStart)
case <-after:
pool.submitNonce(bestNonceSubmission)
}
}
}
func (pool *Pool) submitNonce(nonceSubmission *NonceSubmission) {
Logger.Info("submitting best nonce")
for try := 0; try < nonceSubmissionRetries; try++ {
err := pool.walletHandler.SubmitNonce(nonceSubmission.Nonce, nonceSubmission.MinerID,
nonceSubmission.Deadline)
if err == nil {
return
}
Logger.Error("Submitting nonce failed", zap.Int("try", try),
zap.Int("max-tries", nonceSubmissionRetries))
}
Logger.Error("Submitting nonce failed", zap.Int("max-tries", nonceSubmissionRetries))
}
func (pool *Pool) checkAndAddNewBlock() {
miningInfo, err := pool.walletHandler.GetMiningInfo()
if err != nil {
return
}
oldBlock := Cache.CurrentBlock()
if oldBlock.Height < miningInfo.Height {
Logger.Info("got new Block with height", zap.Uint64("height", miningInfo.Height))
err := pool.modelx.NewBlock(miningInfo.BaseTarget, miningInfo.GenerationSignature,
miningInfo.Height)
if err != nil {
Logger.Error("creating new block", zap.Error(err))
return
}
}
}
func (pool *Pool) checkAndAddNewBlockJob() {
pool.checkAndAddNewBlock()
ticker := time.NewTicker(time.Second)
for range ticker.C {
pool.checkAndAddNewBlock()
}
}
func (pool *Pool) jobs() {
payTicker := time.NewTicker(Cfg.PayoutIntervalDur)
rereadMinerNamesTicker := time.NewTicker(12 * time.Hour)
cleanDBTicker := time.NewTicker(24 * time.Hour)
for {
select {
case <-payTicker.C:
pool.modelx.RewardBlocks()
pool.modelx.Payout()
case <-rereadMinerNamesTicker.C:
pool.modelx.RereadMinerNames()
case <-cleanDBTicker.C:
pool.modelx.CleanDB()
}
}
}
func formatJSONError(errorCode int64, errorMsg string) []uint8 {
bytes, _ := json.Marshal(map[string]string{
"errorCode": strconv.FormatInt(errorCode, 10),
"errorDescription": errorMsg})
return bytes
}
func (pool *Pool) processSubmitNonceRequest(w http.ResponseWriter, req *http.Request) {
ri := Cache.GetRoundInfo()
requestLogger := RequestLogger(req)
if minerHeight, err := strconv.ParseUint(req.Form.Get("blockheight"), 10, 64); err == nil {
if minerHeight != ri.Height {
requestLogger.Warn("Miner submitted on invalid height",
zap.Uint64("got", minerHeight), zap.Uint64("expected", ri.Height))
w.WriteHeader(http.StatusBadRequest)
w.Write(formatJSONError(1005, "Submitted on wrong height"))
return
}
}
// Extract params and check for errors
nonceStr := req.Form.Get("nonce")
nonce, err := strconv.ParseUint(nonceStr, 10, 64)
if err != nil {
requestLogger.Warn("malformed nonce", zap.Error(err))
w.WriteHeader(http.StatusBadRequest)
w.Write(formatJSONError(1012, "submitNonce request has bad 'nonce' parameter - should be uint64"))
return
}
accountIDStr := req.Form.Get("accountId")
accountID, err := strconv.ParseUint(accountIDStr, 10, 64)
if err != nil || accountID == 0 {
requestLogger.Warn("malformed accountId", zap.Error(err))
w.WriteHeader(http.StatusBadRequest)
w.Write(formatJSONError(1013, "submitNonce request has bad 'accountId' parameter - should be uint64"))
return
}
if _, blacklisted := Cfg.AccountIDBlacklist[accountID]; blacklisted {
return
}
requestLogger.Info("processing formal valid request", zap.Uint64("accountID", accountID),
zap.Uint64("nonce", nonce))
// Check if the reward recepient is correct and cache it for this round
correctRewardRecepient, _ := Cache.IsRewardRecipient(accountID)
if !correctRewardRecepient {
w.WriteHeader(http.StatusForbidden)
w.Write(formatJSONError(1004, "Account's reward recipient doesn't match the pool's"))
requestLogger.Warn("reward recipient doesn't match pools", zap.Uint64("accountID", accountID))
return
}
requestLogger.Info("valid reward recipient")
// Create a new miner or get it from cache
miner := pool.modelx.FirstOrCreateMiner(accountID)
if miner == nil {
// most likely wrong reward recipient, can also be error in db
requestLogger.Warn("invalid reward recipient", zap.Error(err))
w.WriteHeader(http.StatusForbidden)
w.Write(formatJSONError(1004, "Account's reward recipient doesn't match the pool's"))
return
}
if ua := req.Header.Get("User-Agent"); ua == "" {
miner.UserAgent = req.Header.Get("X-Miner")
} else {
miner.UserAgent = ua
}
// Calculate deadline and check against limit
deadlineReq := burstmath.NewCalcDeadlineRequest(accountID, nonce, ri.BaseTarget, ri.Scoop, ri.GenSig)
deadline := pool.deadlineRequestHandler.CalcDeadline(deadlineReq)
if Cfg.DeadlineLimit != 0 && deadline > Cfg.DeadlineLimit {
requestLogger.Warn("calculated deadline exceeds pool limit", zap.Uint64("got", deadline),
zap.Uint64("expected-max", Cfg.DeadlineLimit))
w.WriteHeader(http.StatusBadRequest)
w.Write(formatJSONError(1008, "deadline exceeds deadline limit of the pool"))
return
}
requestLogger.Info("valid deadline", zap.Uint64("deadline", deadline))
w.Write([]byte(fmt.Sprintf("{\"deadline\":%d,\"result\":\"success\"}", deadline)))
err = pool.modelx.UpdateOrCreateNonceSubmission(miner, ri.Height, deadline, nonce, ri.BaseTarget, "")
if err != nil {
requestLogger.Error("updating deadline failed", zap.Error(err))
return
}
// Check if this is the best deadline and submit it to the wallet as soon as it comes close
nonceSubmission := NonceSubmission{
MinerID: accountID,
Name: miner.Name,
Address: miner.Address,
Deadline: deadline,
Nonce: nonce,
RoundStart: ri.RoundStart,
Height: ri.Height}
pool.nonceSubmissions <- &nonceSubmission
}
func rateLimitDeniedHandler(w http.ResponseWriter, req *http.Request) {
logIncomingRequest(req)
ip, _, _ := net.SplitHostPort(req.RemoteAddr)
RequestLogger(req).Info("rate limit exceeded", zap.String("ip", ip), zap.String("uri", req.RequestURI),
zap.String("user-agent", req.UserAgent()))
http.Error(w, "limit exceeded", 429)
}
func generateLimiterKey(req *http.Request) string {
req.ParseForm()
ip, _, _ := net.SplitHostPort(req.RemoteAddr)
return ip + req.Form.Get("requestType")
}
func logIncomingRequest(req *http.Request) {
// ip, _, _ := net.SplitHostPort(req.RemoteAddr)
// RequestLogger(req).Info("incoming request", zap.String("ip", ip), zap.String("uri", req.RequestURI),
// zap.String("user-agent", req.UserAgent()))
}
func (pool *Pool) serve() {
store, err := memstore.New(65536)
if err != nil {
Logger.Fatal("", zap.Error(err))
}
quota := throttled.RateQuota{
MaxRate: throttled.PerSec(Cfg.AllowRequestsPerSecond),
MaxBurst: 2}
rateLimiter, err := throttled.NewGCRARateLimiter(store, quota)
if err != nil {
Logger.Fatal("", zap.Error(err))
}
httpRateLimiter := throttled.HTTPRateLimiter{
RateLimiter: rateLimiter,
VaryBy: &throttled.VaryBy{Custom: generateLimiterKey},
DeniedHandler: http.Handler(http.HandlerFunc(rateLimitDeniedHandler))}
http.Handle("/burst", httpRateLimiter.RateLimit(http.HandlerFunc(
func(w http.ResponseWriter, req *http.Request) {
logIncomingRequest(req)
switch req.Form.Get("requestType") {
case "getMiningInfo":
w.Write(Cache.GetMiningInfoJSON())
case "submitNonce":
pool.processSubmitNonceRequest(w, req)
}
})))
http.ListenAndServe(fmt.Sprintf("%s:%d", Cfg.PoolListenAddress, Cfg.PoolPort), nil)
}
func (s *nodeServer) SubmitNonce(ctx context.Context, msg *nodecom.SubmitNonceRequest) (*nodecom.SubmitNonceReply,
error) {
ri := Cache.GetRoundInfo()
m := s.modelx.FirstOrCreateMiner(msg.AccountID)
err := s.modelx.UpdateOrCreateNonceSubmission(m, msg.BlockHeight, msg.Deadline, msg.Nonce, msg.BaseTarget,
msg.GenSig)
if err != nil {
return nil, err
}
// TODO: probably cache best deadline of roundinfo, there might be too much going
// through that channel after some time
if ri.Height <= msg.BlockHeight {
ri := Cache.GetRoundInfo()
// TODO: we need to get the round start of exactly the block on which
// was submitted
nonceSubmission := NonceSubmission{
MinerID: msg.AccountID,
Name: m.Name,
Address: m.Address,
Deadline: msg.Deadline,
Nonce: msg.Nonce,
RoundStart: ri.RoundStart,
Height: ri.Height}
s.nonceSubmissions <- &nonceSubmission
}
return &nodecom.SubmitNonceReply{}, nil
}
func authInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
t := ctx.Value("token")
if t == nil {
return nil, grpc.Errorf(codes.Unauthenticated, "incorrect access token")
}
token := t.(string)
if token != "valid-token" {
return nil, grpc.Errorf(codes.Unauthenticated, "incorrect access token")
}
return handler(ctx, req)
}
func (pool *Pool) serveNode() {
if Cfg.NodePort == 0 {
return
}
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", Cfg.NodeListenAddress, Cfg.NodePort))
if err != nil {
Logger.Fatal("failed to listen", zap.Error(err))
}
var s *grpc.Server
var creds credentials.TransportCredentials
if Cfg.NodeComCert != "" {
creds, err = credentials.NewClientTLSFromFile(Cfg.NodeComCert, "")
if err != nil {
Logger.Fatal("create credentials", zap.Error(err))
}
s = grpc.NewServer(
grpc.Creds(creds),
grpc.UnaryInterceptor(authInterceptor),
)
} else {
s = grpc.NewServer()
}
nodecom.RegisterNodeComServer(s, &nodeServer{
modelx: pool.modelx,
nonceSubmissions: pool.nonceSubmissions})
reflection.Register(s)
if err := s.Serve(lis); err != nil {
Logger.Fatal("failed to server", zap.Error(err))
}
}
func (pool *Pool) Run() {
go pool.jobs()
go pool.serve()
go pool.serveNode()
}