-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaggregator.go
800 lines (730 loc) · 23.9 KB
/
aggregator.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/google/go-querystring/query"
jsoniter "github.com/json-iterator/go"
cache "github.com/patrickmn/go-cache"
"github.com/spf13/viper"
"github.com/throttled/throttled"
"github.com/throttled/throttled/store/memstore"
"github.com/valyala/fasthttp"
)
const (
version = "1.2.3"
defaultCacheExpiration = 15 * time.Minute
minerCacheExpiration = 60 * time.Second
exceededMinersPerIP = 0
notUpdated = 1
updated = 2
remoteErr = 3
wrongHeight = 4
)
// modules
var jsonx = jsoniter.ConfigCompatibleWithStandardLibrary
var client *fasthttp.Client
var websocketClient *websocketAPI
// config
var listenAddr string
var displayMiners bool
var primarySubmitURL string
var primTDL uint64
var primBest uint64
var primaryPassphrase string
var primaryIPForwarding bool
var primaryIgnoreWorseDeadlines bool
var primaryAccountKey string
var primaryws bool
var secondarySubmitURL string
var secTDL uint64
var secBest uint64
var secondaryPassphrase string
var secondaryIPForwarding bool
var secondaryIgnoreWorseDeadlines bool
var secondaryAccountKey string
var secondaryws bool
var minerName string
var minerAlias string
var fileLogging bool
var scanTime int64
var rateLimit int
var burstRate int
var minersPerIP int
var lieDetector bool
// state variables
var currentPrimChain atomicBool
var currentHeight uint64
var currentBaseTarget uint64 = 1
var curPrimaryMiningInfo atomic.Value
// last state variables
var lastPrimChain atomicBool
var lastHeight uint64
var lastBaseTarget uint64 = 1
var curSecondaryMiningInfo atomic.Value
// caches
var primc *cache.Cache
var secc *cache.Cache
var liarsCache *cache.Cache
// errors
var errSubmissionWrongFormatDeadline = errors.New("deadline submission has wrong format")
var errSubmissionWrongFormatNonce = errors.New("nonce submission has wrong format")
var errSubmissionWrongFormatBlockHeight = errors.New("blockheight submission has wrong format")
var errSubmissionWrongFormatAccountID = errors.New("account id submission has wrong format")
var errTooManySubmissionsDifferentMiners = errors.New("too many submissions from different account ids by same ip")
var errUnknownRequestType = errors.New("unknown request type")
type minerRound struct {
AccountID uint64 `url:"accountId"`
Height uint64 `url:"blockheight"`
Deadline uint64 `url:"deadline"`
Nonce uint64 `url:"nonce"`
Passphrase string `url:"secretPhrase"`
Adjusted bool
}
type miningInfo struct {
Height FlexUInt64 `json:"height"`
BaseTarget FlexUInt64 `json:"baseTarget"`
TargetDeadline FlexUInt64 `json:"targetDeadline"`
GenSig string `json:"generationSignature"`
bytes []byte
StartTime time.Time
}
type submitResponse struct {
Deadline FlexUInt64 `json:"deadline"`
}
type ipData struct {
accountIDtoRound map[uint64]*minerRound
sync.Mutex
}
func tryUpdateRound(w *http.ResponseWriter, r *http.Request, ip string, round *minerRound) int {
accountID := round.AccountID
// check if submission is late (height mismatch) if chain wasn't switched.
if round.Height != atomic.LoadUint64(¤tHeight) && currentPrimChain.Get() == lastPrimChain.Get() {
log.Println("DL out-dated:", round.Height, round.AccountID, round.Nonce, "X"+strconv.FormatUint(round.Deadline, 10))
return wrongHeight
}
// check if submission belong to previous block.
if round.Height != atomic.LoadUint64(¤tHeight) && round.Height != atomic.LoadUint64(&lastHeight) {
log.Println("DL out-dated:", round.Height, round.AccountID, round.Nonce, "X"+strconv.FormatUint(round.Deadline, 10))
return wrongHeight
}
// you lie I lie
_, exists := liarsCache.Get(ip)
if exists {
return notUpdated
}
// load relevant data
var primChain = true
var baseTarget uint64 = 1
if round.Height == atomic.LoadUint64(¤tHeight) {
primChain = currentPrimChain.Get()
baseTarget = atomic.LoadUint64(¤tBaseTarget)
} else {
primChain = lastPrimChain.Get()
baseTarget = atomic.LoadUint64(&lastBaseTarget)
}
deadline := round.Deadline
if !round.Adjusted {
deadline /= baseTarget
}
// deadlines filter
if (primChain && (deadline > primTDL)) || (!primChain && (deadline > secTDL)) {
log.Println("DL filtered:", round.Height, round.AccountID, round.Nonce, deadline)
return notUpdated
}
if (primChain && (deadline > atomic.LoadUint64(&primBest)) && primaryIgnoreWorseDeadlines) || (!primChain && (deadline > atomic.LoadUint64(&secBest) && secondaryIgnoreWorseDeadlines)) {
log.Println("DL discarded:", round.Height, round.AccountID, round.Nonce, deadline)
return notUpdated
}
var ipDataV interface{}
if primChain {
ipDataV, exists = primc.Get(ip)
} else {
ipDataV, exists = secc.Get(ip)
}
if !exists {
err := proxySubmitRound(w, r, ip, round, primChain, baseTarget)
if err != nil {
return remoteErr
}
if primChain {
primc.SetDefault(ip, &ipData{
accountIDtoRound: map[uint64]*minerRound{
accountID: round,
},
})
} else {
secc.SetDefault(ip, &ipData{
accountIDtoRound: map[uint64]*minerRound{
accountID: round,
},
})
}
if primChain {
atomic.StoreUint64(&primBest, deadline)
} else {
atomic.StoreUint64(&secBest, deadline)
}
log.Println("DL response:", round.Height, round.AccountID, round.Nonce, deadline)
return updated
}
ipData := ipDataV.(*ipData)
ipData.Lock()
defer ipData.Unlock()
existingRound, exists := ipData.accountIDtoRound[accountID]
if !exists {
minerCount := len(ipData.accountIDtoRound)
if minerCount == minersPerIP {
for _, otherRound := range ipData.accountIDtoRound {
if otherRound.Height < round.Height {
delete(ipData.accountIDtoRound, otherRound.AccountID)
goto update
}
}
log.Println("DL rejected:", round.Height, round.AccountID, round.Nonce, deadline)
return exceededMinersPerIP
}
} else {
existingDeadline := existingRound.Deadline
if !existingRound.Adjusted {
existingDeadline /= baseTarget
}
if existingRound.Height > round.Height || existingRound.Height == round.Height &&
existingDeadline < deadline {
log.Println("DL ignored:", round.Height, round.AccountID, round.Nonce, deadline)
return notUpdated
}
}
update:
if err := proxySubmitRound(w, r, ip, round, primChain, baseTarget); err != nil {
return remoteErr
}
ipData.accountIDtoRound[accountID] = round
if primChain {
atomic.StoreUint64(&primBest, deadline)
} else {
atomic.StoreUint64(&secBest, deadline)
}
log.Println("DL response:", round.Height, round.AccountID, round.Nonce, deadline)
return updated
}
func parseRound(r *http.Request) (*minerRound, error) {
adjusted := false
deadline, err := strconv.ParseUint((*r).FormValue("deadline"), 10, 64)
if err != nil {
// inefficient mining software detected :p
deadline, err = strconv.ParseUint((*r).Header.Get("X-Deadline"), 10, 64)
if err != nil {
return nil, errSubmissionWrongFormatDeadline
}
adjusted = true
}
nonce, err := strconv.ParseUint((*r).FormValue("nonce"), 10, 64)
if err != nil {
return nil, errSubmissionWrongFormatNonce
}
height, err := strconv.ParseUint((*r).FormValue("blockheight"), 10, 64)
if err != nil {
return nil, errSubmissionWrongFormatBlockHeight
}
accountID, err := strconv.ParseUint((*r).FormValue("accountId"), 10, 64)
if err != nil {
return nil, errSubmissionWrongFormatAccountID
}
passphrase := (*r).FormValue("secretPhrase")
return &minerRound{
Deadline: deadline,
Nonce: nonce,
Height: height,
AccountID: accountID,
Passphrase: passphrase,
Adjusted: adjusted,
}, nil
}
func proxySubmitRound(w *http.ResponseWriter, r *http.Request, ip string, round *minerRound, primary bool, baseTarget uint64) error {
// websocket api handling
if (primary && primaryws) || (!primary && secondaryws) {
// fire submission
websocketClient.submitNonce(round.AccountID, round.Height, round.Nonce, round.Deadline)
log.Println("DL fired:", round.Height, round.AccountID, round.Nonce, round.Deadline)
// fake answer
var baseTarget = atomic.LoadUint64(¤tBaseTarget)
if round.Height != atomic.LoadUint64(¤tHeight) {
baseTarget = atomic.LoadUint64(&lastBaseTarget)
}
deadline := round.Deadline
if !round.Adjusted {
deadline /= baseTarget
}
(*w).Write([]byte(fmt.Sprintf("{\"deadline\":%d,\"result\":\"success\"}", deadline)))
return nil
}
// passphrase overwrites
if primary && primaryPassphrase != "" {
round.Passphrase = primaryPassphrase
}
if !primary && secondaryPassphrase != "" {
round.Passphrase = secondaryPassphrase
}
v, _ := query.Values(round)
// treat unadj dl
if round.Adjusted {
v.Del("deadline")
}
// treat pool
if round.Passphrase == "" {
v.Del("secretPhrase")
} else {
v.Del("deadline")
}
v.Del("Adjusted")
var submitURL string
if primary {
submitURL = primarySubmitURL
} else {
submitURL = secondarySubmitURL
}
req := fasthttp.AcquireRequest()
req.URI().Update(submitURL + "/burst?requestType=submitNonce&" + v.Encode())
var miner string
if ua := r.Header.Get("User-Agent"); ua == "" {
miner = r.Header.Get("X-Miner")
} else {
miner = ua
}
req.Header.Set("User-Agent", "Aggregator/"+version+"/"+miner)
req.Header.Set("X-Miner", "Aggregator/"+version+"/"+miner)
req.Header.Set("X-MinerAlias", minerAlias)
req.Header.Set("X-Capacity", strconv.FormatInt(TotalCapacity(), 10))
if primary {
req.Header.Set("X-Account", primaryAccountKey)
} else {
req.Header.Set("X-Account", secondaryAccountKey)
}
// x-forwarded-for
if (primary && primaryIPForwarding) || (!primary && secondaryIPForwarding) {
ip, _, err := net.SplitHostPort(ip)
if err == nil {
req.Header.Set("X-Forwarded-For", ip)
}
}
req.Header.SetMethodBytes([]byte("POST"))
resp := fasthttp.AcquireResponse()
err := client.Do(req, resp)
if err != nil {
(*w).Write(formatJSONError(3, "error reaching pool or wallet"))
return err
}
// lie detector
if lieDetector {
var mi submitResponse
if err := jsonx.Unmarshal(resp.Body(), &mi); err == nil {
deadline := round.Deadline
if !round.Adjusted {
deadline /= baseTarget
}
if uint64(mi.Deadline) != deadline {
var liar = true
liarsCache.SetDefault(ip, &liar)
log.Println("Liar detected:", round.Height, ip, mi.Deadline, deadline)
}
}
}
(*w).Write(resp.Body())
return nil
}
func refreshMiningInfo() error {
// primary chain
var mi miningInfo
var errchain1 error
if primaryws {
if available.Get() {
mi = *currentMiningInfo.Load().(*miningInfo)
} else {
// initial mining info missing
errchain1 = fmt.Errorf("primary chain: initial mining info missing")
}
} else {
req := fasthttp.AcquireRequest()
req.URI().Update(primarySubmitURL + "/burst?requestType=getMiningInfo")
req.Header.Set("User-Agent", "Aggregator/"+version)
req.Header.Set("X-Miner", "Aggregator/"+version)
req.Header.Set("X-Capacity", strconv.FormatInt(TotalCapacity(), 10))
req.Header.SetMethodBytes([]byte("GET"))
resp := fasthttp.AcquireResponse()
err1 := client.Do(req, resp)
errchain1 = err1
if errchain1 == nil {
if err := jsonx.Unmarshal(resp.Body(), &mi); err != nil {
return err
}
}
}
var curPrimMi *miningInfo
if curPrimMiV := curPrimaryMiningInfo.Load(); curPrimMiV != nil {
curPrimMi = curPrimMiV.(*miningInfo)
}
var curSecMi *miningInfo
if curSecMiV := curSecondaryMiningInfo.Load(); curSecMiV != nil {
curSecMi = curSecMiV.(*miningInfo)
}
var lastPrimaryStart = time.Time{}
if curPrimMi != nil {
lastPrimaryStart = curPrimMi.StartTime
}
var lastSecondaryStart = time.Time{}
if curSecMi != nil {
lastSecondaryStart = curSecMi.StartTime
}
if errchain1 == nil {
switch {
case curPrimMi == nil || curPrimMi.Height < mi.Height:
log.Println("New Block", mi.Height, mi.BaseTarget, mi.TargetDeadline, mi.GenSig)
if displayMiners {
DisplayMiners()
}
mi.bytes, _ = json.Marshal(map[string]string{
"height": fmt.Sprintf("%d", mi.Height),
"baseTarget": fmt.Sprintf("%d", mi.BaseTarget),
"generationSignature": mi.GenSig})
mi.StartTime = time.Now()
curPrimaryMiningInfo.Store(&mi)
if !currentPrimChain.Get() {
atomic.StoreUint64(&lastBaseTarget, atomic.LoadUint64(¤tBaseTarget))
atomic.StoreUint64(&lastHeight, atomic.LoadUint64(¤tHeight))
lastPrimChain.Set(false)
}
atomic.StoreUint64(¤tBaseTarget, uint64(mi.BaseTarget))
atomic.StoreUint64(¤tHeight, uint64(mi.Height))
currentPrimChain.Set(true)
atomic.StoreUint64(&primBest, ^uint64(0))
// reschedule secondary chain on interrupt
if int64(time.Now().Sub(lastSecondaryStart).Seconds()) < scanTime {
reset := miningInfo{0, 0, 0, "", []byte{0}, time.Time{}}
curSecondaryMiningInfo.Store(&reset)
}
return nil
case curPrimMi.Height > mi.Height: // fork handling
log.Println("New Block", mi.Height, mi.BaseTarget, mi.TargetDeadline, mi.GenSig)
if displayMiners {
DisplayMiners()
}
mi.bytes, _ = json.Marshal(map[string]string{
"height": fmt.Sprintf("%d", mi.Height),
"baseTarget": fmt.Sprintf("%d", mi.BaseTarget),
"generationSignature": mi.GenSig})
mi.StartTime = time.Now()
curPrimaryMiningInfo.Store(&mi)
primc.Flush()
if !currentPrimChain.Get() {
atomic.StoreUint64(&lastBaseTarget, atomic.LoadUint64(¤tBaseTarget))
atomic.StoreUint64(&lastHeight, atomic.LoadUint64(¤tHeight))
lastPrimChain.Set(false)
}
atomic.StoreUint64(¤tBaseTarget, uint64(mi.BaseTarget))
atomic.StoreUint64(¤tHeight, uint64(mi.Height))
currentPrimChain.Set(true)
atomic.StoreUint64(&primBest, ^uint64(0))
// reschedule secondary chain on interrupt
if int64(time.Now().Sub(lastSecondaryStart).Seconds()) < scanTime {
reset := miningInfo{0, 0, 0, "", []byte{0}, time.Time{}}
curSecondaryMiningInfo.Store(&reset)
}
return nil
case curPrimMi.BaseTarget != mi.BaseTarget: // fork handling
log.Println("New Block", mi.Height, mi.BaseTarget, mi.TargetDeadline, mi.GenSig)
if displayMiners {
DisplayMiners()
}
mi.bytes, _ = json.Marshal(map[string]string{
"height": fmt.Sprintf("%d", mi.Height),
"baseTarget": fmt.Sprintf("%d", mi.BaseTarget),
"generationSignature": mi.GenSig})
mi.StartTime = time.Now()
curPrimaryMiningInfo.Store(&mi)
primc.Flush()
if !currentPrimChain.Get() {
atomic.StoreUint64(&lastBaseTarget, atomic.LoadUint64(¤tBaseTarget))
atomic.StoreUint64(&lastHeight, atomic.LoadUint64(¤tHeight))
lastPrimChain.Set(false)
}
atomic.StoreUint64(¤tBaseTarget, uint64(mi.BaseTarget))
atomic.StoreUint64(¤tHeight, uint64(mi.Height))
currentPrimChain.Set(true)
atomic.StoreUint64(&primBest, ^uint64(0))
// reschedule secondary chain on interrupt
if int64(time.Now().Sub(lastSecondaryStart).Seconds()) < scanTime {
reset := miningInfo{0, 0, 0, "", []byte{0}, time.Time{}}
curSecondaryMiningInfo.Store(&reset)
}
return nil
}
}
// single chain
if secondarySubmitURL == "" {
return nil
}
// skip secondary if primary is scanning
if int64(time.Now().Sub(lastPrimaryStart).Seconds()) < scanTime {
return nil
}
// secondary chain
var errchain2 error
if secondaryws {
if available.Get() {
mi = *currentMiningInfo.Load().(*miningInfo)
} else {
// initial mining info missing
errchain2 = fmt.Errorf("secondary chain: initial mining info missing")
return errchain2
}
} else {
req := fasthttp.AcquireRequest()
req.URI().Update(secondarySubmitURL + "/burst?requestType=getMiningInfo")
req.Header.Set("User-Agent", "Aggregator/"+version)
req.Header.Set("X-Miner", "Aggregator/"+version)
req.Header.Set("X-Capacity", strconv.FormatInt(TotalCapacity(), 10))
req.Header.SetMethodBytes([]byte("GET"))
resp := fasthttp.AcquireResponse()
err2 := client.Do(req, resp)
errchain2 = err2
if errchain2 == nil {
if err := jsonx.Unmarshal(resp.Body(), &mi); err != nil {
return err
}
} else {
return errchain2
}
}
switch {
case curSecMi == nil || curSecMi.Height < mi.Height:
log.Println("New Block", mi.Height, mi.BaseTarget, mi.TargetDeadline, mi.GenSig)
if displayMiners {
DisplayMiners()
}
mi.bytes, _ = json.Marshal(map[string]string{
"height": fmt.Sprintf("%d", mi.Height),
"baseTarget": fmt.Sprintf("%d", mi.BaseTarget),
"generationSignature": mi.GenSig})
mi.StartTime = time.Now()
curSecondaryMiningInfo.Store(&mi)
if currentPrimChain.Get() {
atomic.StoreUint64(&lastBaseTarget, atomic.LoadUint64(¤tBaseTarget))
atomic.StoreUint64(&lastHeight, atomic.LoadUint64(¤tHeight))
lastPrimChain.Set(true)
}
atomic.StoreUint64(¤tBaseTarget, uint64(mi.BaseTarget))
atomic.StoreUint64(¤tHeight, uint64(mi.Height))
currentPrimChain.Set(false)
atomic.StoreUint64(&secBest, ^uint64(0))
return nil
case curSecMi.Height > mi.Height: // fork handling
log.Println("New Block", mi.Height, mi.BaseTarget, mi.TargetDeadline, mi.GenSig)
if displayMiners {
DisplayMiners()
}
mi.bytes, _ = json.Marshal(map[string]string{
"height": fmt.Sprintf("%d", mi.Height),
"baseTarget": fmt.Sprintf("%d", mi.BaseTarget),
"generationSignature": mi.GenSig})
mi.StartTime = time.Now()
curSecondaryMiningInfo.Store(&mi)
secc.Flush()
if currentPrimChain.Get() {
atomic.StoreUint64(&lastBaseTarget, atomic.LoadUint64(¤tBaseTarget))
atomic.StoreUint64(&lastHeight, atomic.LoadUint64(¤tHeight))
lastPrimChain.Set(true)
}
atomic.StoreUint64(¤tBaseTarget, uint64(mi.BaseTarget))
atomic.StoreUint64(¤tHeight, uint64(mi.Height))
currentPrimChain.Set(true)
atomic.StoreUint64(&secBest, ^uint64(0))
return nil
case curSecMi.BaseTarget != mi.BaseTarget: // fork handling
log.Println("New Block", mi.Height, mi.BaseTarget, mi.TargetDeadline, mi.GenSig)
if displayMiners {
DisplayMiners()
}
mi.bytes, _ = json.Marshal(map[string]string{
"height": fmt.Sprintf("%d", mi.Height),
"baseTarget": fmt.Sprintf("%d", mi.BaseTarget),
"generationSignature": mi.GenSig})
mi.StartTime = time.Now()
curSecondaryMiningInfo.Store(&mi)
secc.Flush()
if currentPrimChain.Get() {
atomic.StoreUint64(&lastBaseTarget, atomic.LoadUint64(¤tBaseTarget))
atomic.StoreUint64(&lastHeight, atomic.LoadUint64(¤tHeight))
lastPrimChain.Set(true)
}
atomic.StoreUint64(¤tBaseTarget, uint64(mi.BaseTarget))
atomic.StoreUint64(¤tHeight, uint64(mi.Height))
currentPrimChain.Set(false)
atomic.StoreUint64(&secBest, ^uint64(0))
return nil
}
return nil
}
func requestHandler(w http.ResponseWriter, r *http.Request) {
ipport := r.RemoteAddr
ip, port, _ := net.SplitHostPort(ipport)
switch reqType := string(r.FormValue("requestType")); reqType {
case "getMiningInfo":
if currentPrimChain.Get() {
w.Write(curPrimaryMiningInfo.Load().(*miningInfo).bytes)
} else {
w.Write(curSecondaryMiningInfo.Load().(*miningInfo).bytes)
}
// log client
var miner string
if ua := r.Header.Get("User-Agent"); ua == "" {
miner = r.Header.Get("X-Miner")
} else {
miner = ua
}
size, _ := strconv.ParseInt(r.Header.Get("X-Capacity"), 10, 64)
UpdateClient(ip, port, miner, size)
if primaryws || secondaryws {
websocketClient.UpdateSize(TotalCapacity())
}
case "submitNonce":
round, err := parseRound(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write(formatJSONError(1, err.Error()))
return
}
switch res := tryUpdateRound(&w, r, ip, round); res {
case updated:
case notUpdated:
var baseTarget = atomic.LoadUint64(¤tBaseTarget)
if round.Height != atomic.LoadUint64(¤tHeight) {
baseTarget = atomic.LoadUint64(&lastBaseTarget)
}
deadline := round.Deadline
if !round.Adjusted {
deadline /= baseTarget
}
w.Write([]byte(fmt.Sprintf("{\"deadline\":%d,\"result\":\"success\"}", deadline)))
case wrongHeight:
w.WriteHeader(http.StatusBadRequest)
w.Write(formatJSONError(1005, "Submitted on wrong height"))
case exceededMinersPerIP:
w.WriteHeader(http.StatusBadRequest)
w.Write(formatJSONError(2, errTooManySubmissionsDifferentMiners.Error()))
}
default:
w.WriteHeader(http.StatusBadRequest)
w.Write(formatJSONError(4, errUnknownRequestType.Error()))
}
}
func main() {
log.Println("Aggregator v." + version)
viper.SetConfigName("config")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %s", err))
}
client = &fasthttp.Client{NoDefaultUserAgentHeader: true}
client.MaxIdleConnDuration = 0 * time.Millisecond
listenAddr = viper.GetString("listenAddr")
displayMiners = viper.GetBool("displayMiners")
log.Println("Proxy address:", listenAddr)
minersPerIP = viper.GetInt("minersPerIP")
primarySubmitURL = viper.GetString("primarySubmitURL")
primaryPassphrase = viper.GetString("primaryPassphrase")
primaryIPForwarding = viper.GetBool("primaryIpForwarding")
primaryIgnoreWorseDeadlines = viper.GetBool("primaryIgnoreWorseDeadlines")
primaryAccountKey = viper.GetString("primaryAccountKey")
primTDL = uint64(viper.GetInt64("primaryTargetDeadline"))
secondarySubmitURL = viper.GetString("secondarySubmitURL")
secondaryPassphrase = viper.GetString("secondaryPassphrase")
secondaryIPForwarding = viper.GetBool("secondaryIpForwarding")
secondaryIgnoreWorseDeadlines = viper.GetBool("secondaryIgnoreWorseDeadlines")
secondaryAccountKey = viper.GetString("secondaryAccountKey")
secTDL = uint64(viper.GetInt64("secondaryTargetDeadline"))
fileLogging = viper.GetBool("fileLogging")
scanTime = viper.GetInt64("scanTime")
rateLimit = viper.GetInt("rateLimit")
burstRate = viper.GetInt("burstRate")
lieDetector = viper.GetBool("lieDetector")
log.Println("Primary chain:", primarySubmitURL)
log.Println("Secondary chain:", secondarySubmitURL)
log.Println("Rate Limiter:", "limit="+strconv.Itoa(rateLimit), "per second, burstrate="+strconv.Itoa(burstRate))
minerName = viper.GetString("minerName")
minerAlias = viper.GetString("minerAlias")
// todo check exactly one url is wss
primaryws = strings.HasPrefix(primarySubmitURL, "wss")
secondaryws = strings.HasPrefix(secondarySubmitURL, "wss")
if primaryws && secondaryws {
panic("can only have a single websocket upstream")
}
// launch api
if primaryws {
websocketClient = newWebsocketAPI(primarySubmitURL, primaryAccountKey, minerName, 0)
websocketClient.Connect()
}
if secondaryws {
websocketClient = newWebsocketAPI(secondarySubmitURL, secondaryAccountKey, minerName, 0)
websocketClient.Connect()
}
// amend submit & getMiningInfo
if fileLogging {
logFile, err := os.OpenFile("log.txt", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
panic(err)
}
mw := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(mw)
}
clients = cache.New(minerCacheExpiration, minerCacheExpiration)
if err := refreshMiningInfo(); err != nil {
log.Fatalln("get initial mining info: ", err)
}
go func() {
t := time.NewTicker(1 * time.Second)
for {
for range t.C {
_ = refreshMiningInfo()
}
}
}()
primc = cache.New(defaultCacheExpiration, defaultCacheExpiration)
secc = cache.New(defaultCacheExpiration, defaultCacheExpiration)
liarsCache = cache.New(defaultCacheExpiration, defaultCacheExpiration)
store, err := memstore.New(65536)
if err != nil {
log.Fatal(err)
}
quota := throttled.RateQuota{MaxRate: throttled.PerSec(rateLimit), MaxBurst: burstRate}
rateLimiter, err := throttled.NewGCRARateLimiter(store, quota)
if err != nil {
log.Fatal(err)
}
httpRateLimiter := throttled.HTTPRateLimiter{
RateLimiter: rateLimiter,
VaryBy: &throttled.VaryBy{Path: true},
}
h := http.HandlerFunc(requestHandler)
err = fasthttp.ListenAndServe(listenAddr, NewFastHTTPHandler(httpRateLimiter.RateLimit(h)))
if err != nil {
log.Fatalf("listen and serve: %s", err)
}
}
func formatJSONError(errorCode int64, errorMsg string) []uint8 {
bytes, _ := json.Marshal(map[string]string{
"errorCode": strconv.FormatInt(errorCode, 10),
"errorDescription": errorMsg})
return bytes
}