This repository was archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.go
More file actions
163 lines (137 loc) · 3.9 KB
/
main.go
File metadata and controls
163 lines (137 loc) · 3.9 KB
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
package main
import (
"math/rand"
"miningPoolCli/config"
"miningPoolCli/utils/api"
"miningPoolCli/utils/boc"
"miningPoolCli/utils/files"
"miningPoolCli/utils/gpuwrk"
"miningPoolCli/utils/helpers"
"miningPoolCli/utils/initp"
"miningPoolCli/utils/logreport"
"miningPoolCli/utils/mlog"
"miningPoolCli/utils/server"
"os/exec"
"strconv"
"strings"
"time"
)
var gpuGoroutines []gpuwrk.GpuGoroutine
var globalTasks []api.Task
func startTask(i int, task api.Task) {
// gpuGoroutines[i].startTimestamp = time.Now().Unix()
mineResultFilename := "mined_" + strconv.Itoa(task.Id) + ".boc"
pathToBoc := config.MinerGetter.MinerDirectory + "/" + mineResultFilename
minerArgs := []string{
"-vv",
"-g" + strconv.Itoa(gpuGoroutines[i].GpuData.GpuId),
"-p" + strconv.Itoa(gpuGoroutines[i].GpuData.PlatformId),
"-F" + strconv.Itoa(config.StaticBeforeMinerSettings.BoostFactor),
"-t" + strconv.Itoa(config.StaticBeforeMinerSettings.TimeoutT),
"-e" + strconv.FormatInt(task.Expire, 10),
config.StaticBeforeMinerSettings.PoolAddress,
helpers.ConvertHexData(task.Seed),
helpers.ConvertHexData(task.Complexity),
config.StaticBeforeMinerSettings.Iterations,
task.Giver,
pathToBoc,
}
cmd := exec.Command(config.MinerGetter.StartPath, minerArgs...)
gpuGoroutines[i].ProcStderr.Reset()
cmd.Stderr = &gpuGoroutines[i].ProcStderr
unblockFunc := make(chan struct{}, 1)
var killedByNotActual bool
var done bool
if err := cmd.Start(); err != nil {
mlog.LogFatal("failed to start miner cmd; err: " + err.Error() + "; args: " + strings.Join(cmd.Args, " "))
}
gpuGoroutines[i].PPid = cmd.Process.Pid
gpuGoroutines[i].KeepAlive = true
go func() {
cmd.Wait()
done = true
if helpers.StringInSlice(mineResultFilename, files.GetDir(config.MinerGetter.MinerDirectory)) {
// found
bocFileInHex, _ := boc.ReadBocFileToHex(pathToBoc)
if !killedByNotActual {
go func() {
bocServerResp, err := api.SendHexBocToServer(bocFileInHex, task.Seed, strconv.Itoa(task.Id))
if err == nil {
if bocServerResp.Data == "Found" && bocServerResp.Status == "ok" {
logreport.ShareFound(gpuGoroutines[i].GpuData.Model, gpuGoroutines[i].GpuData.GpuId, task.Id)
} else {
logreport.ShareServerError(task, bocServerResp, gpuGoroutines[i].GpuData.GpuId)
}
}
}()
}
files.RemovePath(pathToBoc)
}
if gpuGoroutines[i].KeepAlive {
enableTask(i)
}
unblockFunc <- struct{}{}
}()
go func() {
for !done {
if checkTaskAlreadyFound(task.Id) {
killedByNotActual = true
if err := cmd.Process.Kill(); err != nil {
mlog.LogError(err.Error())
}
break
}
time.Sleep(10 * time.Microsecond)
}
}()
<-unblockFunc
}
func checkTaskAlreadyFound(checkId int) bool {
for _, task := range globalTasks {
if task.Id == checkId {
return false
}
}
return true
}
func syncTasks(firstSync *chan struct{}) {
var firstSyncIsOk bool
for {
if t := api.GetTasks().Tasks; len(t) > 0 {
globalTasks = t
if !firstSyncIsOk {
*firstSync <- struct{}{}
firstSyncIsOk = true
}
}
time.Sleep(1 * time.Second)
}
}
func enableTask(gpuGoIndex int) {
if tLen := len(globalTasks); tLen > 0 {
go startTask(gpuGoIndex, globalTasks[rand.Intn(len(globalTasks))])
} else {
mlog.LogError("can't start task, because the len of globalTasks <= 0")
}
}
func main() {
rand.Seed(time.Now().Unix())
gpus := initp.InitProgram()
gpuGoroutines = make([]gpuwrk.GpuGoroutine, len(gpus))
firstSync := make(chan struct{})
go syncTasks(&firstSync)
<-firstSync
for gpuGoIndex := range gpuGoroutines {
gpuGoroutines[gpuGoIndex].GpuData = gpus[gpuGoIndex]
enableTask(gpuGoIndex)
}
if !config.NetSrv.RunThis && config.NetSrv.HandleKill {
mlog.LogInfo("Unable to apply -handle-kill because flag -serve-stat is not specified")
} else if config.NetSrv.RunThis {
go server.Entrypoint(&gpuGoroutines)
}
for {
time.Sleep(10 * time.Second)
gpuwrk.CalcHashrate(&gpuGoroutines)
}
}