This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
/
miningManager.go
executable file
·147 lines (129 loc) · 3.43 KB
/
miningManager.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
package ops
import (
"context"
"fmt"
"os"
"time"
tellorCommon "github.com/tellor-io/TellorMiner/common"
"github.com/tellor-io/TellorMiner/config"
"github.com/tellor-io/TellorMiner/db"
"github.com/tellor-io/TellorMiner/pow"
"github.com/tellor-io/TellorMiner/util"
)
type WorkSource interface {
GetWork(input chan *pow.Work) (*pow.Work,bool)
}
type SolutionSink interface {
Submit(context.Context, *pow.Result) bool
}
//MiningMgr holds items for mining and requesting data
type MiningMgr struct {
//primary exit channel
exitCh chan os.Signal
log *util.Logger
Running bool
group *pow.MiningGroup
tasker WorkSource
solHandler SolutionSink
solution *pow.Result
dataRequester *DataRequester
//data requester's exit channel
requesterExit chan os.Signal
}
//CreateMiningManager creates a new manager that mananges mining and data requests
func CreateMiningManager(ctx context.Context, exitCh chan os.Signal, submitter tellorCommon.TransactionSubmitter) (*MiningMgr, error) {
cfg := config.GetConfig()
group, err := pow.SetupMiningGroup(cfg)
if err != nil {
return nil, fmt.Errorf("failed to setup miners: %s", err.Error())
}
mng := &MiningMgr{
exitCh: exitCh,
log: util.NewLogger("ops", "MiningMgr"),
Running: false,
group: group,
tasker: nil,
solution: nil,
solHandler: nil,
}
if cfg.EnablePoolWorker {
pool := pow.CreatePool(cfg, group)
mng.tasker = pool
mng.solHandler = pool
} else {
proxy := ctx.Value(tellorCommon.DataProxyKey).(db.DataServerProxy)
mng.tasker = pow.CreateTasker(cfg, proxy)
mng.solHandler = pow.CreateSolutionHandler(cfg, submitter, proxy)
if cfg.RequestData > 0 {
fmt.Println("dataRequester created")
mng.dataRequester = CreateDataRequester(exitCh, submitter, cfg.RequestDataInterval.Duration, proxy)
}
}
return mng, nil
}
//Start will start the mining run loop
func (mgr *MiningMgr) Start(ctx context.Context) {
mgr.Running = true
go func(ctx context.Context) {
cfg := config.GetConfig()
ticker := time.NewTicker(cfg.MiningInterruptCheckInterval.Duration)
//if you make these buffered, think about the effects on synchronization!
input := make(chan *pow.Work)
output := make(chan *pow.Result)
if cfg.RequestData > 0 {
mgr.dataRequester.Start(ctx)
}
//start the mining group
go mgr.group.Mine(input, output)
// sends work to the mining group
sendWork := func() {
if cfg.EnablePoolWorker {
mgr.tasker.GetWork(input)
} else {
work,instantSubmit := mgr.tasker.GetWork(input)
if instantSubmit{
if mgr.solution == nil {
fmt.Println("Instant Submit Called! ")
mgr.solution = &pow.Result{Work:work, Nonce:"1"}
} else{
fmt.Println("Trying Resubmit...")
}
}else if work != nil {
mgr.solution = nil
input <- work
}
if mgr.solution != nil{
goodSubmit := mgr.solHandler.Submit(ctx,mgr.solution)
if goodSubmit {
mgr.solution = nil
}
}
}
}
//send the initial challenge
sendWork()
for {
select {
//boss wants us to quit for the day
case <-mgr.exitCh:
//exit
input <- nil
//found a solution
case result := <-output:
if result == nil {
mgr.Running = false
return
}
mgr.solution = result
goodSubmit := mgr.solHandler.Submit(ctx,mgr.solution)
if goodSubmit {
mgr.solution = nil
}
sendWork()
//time to check for a new challenge
case _ = <-ticker.C:
sendWork()
}
}
}(ctx)
}