Repository navigation
Expand file tree
/
Copy pathMinerProcess.ps1
More file actions
391 lines (366 loc) · 14 KB
/
Copy pathMinerProcess.ps1
File metadata and controls
391 lines (366 loc) · 14 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
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
<#
MindMiner Copyright (C) 2017-2022 Oleg Samsonov aka Quake4
https://github.com/Quake4/MindMiner
License GPL-3.0
#>
. .\Code\MinerInfo.ps1
. .\Code\Config.ps1
. .\Code\StatInfo.ps1
. .\Code\MultipleUnit.ps1
enum eState {
Stopped
Failed
NoHash
Running
}
enum eAction {
Normal
Benchmark
Service
Fee
}
class MinerProcess {
[string] $State
[MinerInfo] $Miner
[Diagnostics.Stopwatch] $TotalTime
[Diagnostics.Stopwatch] $CurrentTime
[int] $Run
[Config] $Config
[int] $ErrorAnswer
[eAction] $Action
hidden [StatGroup] $Speed
hidden [StatGroup] $SpeedDual
hidden [StatInfo] $Power
[Shares] $Shares # add in get-speed
[Shares] $SharesDual # add in get-speed
hidden [decimal] $SharesCache
hidden [decimal] $SharesCacheDual
hidden [hashtable] $FlatResult
hidden [int] $NoHashCount
hidden [Diagnostics.Process] $Process
MinerProcess([MinerInfo] $miner, [Config] $config) {
$this.Miner = [MinerInfo]($miner | ConvertTo-Json | ConvertFrom-Json)
$this.Config = $config
$this.TotalTime = [Diagnostics.Stopwatch]::new()
$this.CurrentTime = [Diagnostics.Stopwatch]::new()
$this.Speed = [StatGroup]::new()
$this.SpeedDual = [StatGroup]::new()
$this.Shares = [Shares]::new()
$this.SharesDual = [Shares]::new()
}
[void] Start([bool] $nbench, $runbefore) {
$act = if ($nbench) { [eAction]::Service } else { [eAction]::Normal }
$this.StartInt($act, $runbefore)
}
[void] Benchmark([bool] $nbench, $runbefore) {
$act = if ($nbench) { [eAction]::Fee } else { [eAction]::Benchmark }
$this.StartInt($act, $runbefore)
}
[bool] CanFee() {
return $this.Miner.Arguments.IndexOf($this.Config.Wallet.BTC) -gt 0
}
[void] SetSpeed([string] $key, [decimal] $speed, [string] $interval) {
if (($speed -ge 0 -and $this.Action -eq [eAction]::Normal) -or ($speed -gt 0 -and $this.Action -ne [eAction]::Normal)) {
$spd = $this.Speed.SetValue($key, $speed * (100 - $this.Miner.Fee) / 100, $interval)
Remove-Variable spd
}
}
[void] SetSpeedDual([string] $key, [decimal] $speed, [string] $interval) {
if (($speed -ge 0 -and $this.Action -eq [eAction]::Normal) -or ($speed -gt 0 -and $this.Action -ne [eAction]::Normal)) {
$spd = $this.SpeedDual.SetValue($key, $speed, $interval)
Remove-Variable spd
}
}
[decimal] GetSpeed([bool] $dual = $false) {
if ($this.FlatResult) {
if ($dual) { return $this.FlatResult["SpeedDual"] } else { return $this.FlatResult["Speed"] }
}
$spd = $this.Speed;
$sharestime = $this.Miner.BenchmarkSeconds * 5;
$sharesvalue = $this.Shares.Get($sharestime);
if ($dual) {
$spd = $this.SpeedDual;
$sharesvalue = $this.SharesDual.Get($sharestime);
if ($this.State -eq [eState]::Running -and ($this.CurrentTime.Elapsed.TotalSeconds -gt $sharestime -or $this.SharesDual.HasValue($sharestime, 5) -or $sharesvalue -ne $this.SharesCacheDual)) {
$this.SharesCacheDual = $sharesvalue;
}
}
elseif ($this.State -eq [eState]::Running -and ($this.CurrentTime.Elapsed.TotalSeconds -gt $sharestime -or $this.Shares.HasValue($sharestime, 5) -or $sharesvalue -ne $this.SharesCache)) {
$this.SharesCache = $sharesvalue;
}
Remove-Variable sharestime
if ($dual) { $sharesvalue = $this.SharesCacheDual } else { $sharesvalue = $this.SharesCache }
# total speed by share
[decimal] $result = $spd.GetValue()
# sum speed by benchmark
[decimal] $sum = 0
$spd.Values.GetEnumerator() | Where-Object { $_.Key -ne [string]::Empty } | ForEach-Object {
$sum += $_.Value.Value
}
# if bench - need fast evaluation - get theoretical speed
if ($sum -gt 0 -and $this.Action -eq [eAction]::Benchmark) {
return $sum
}
# if both - average
if ($result -gt 0 -and $sum -gt 0) {
return ($result + $sum) / 2 * $sharesvalue
}
if ($result -gt 0) {
return $result * $sharesvalue
}
return $sum * $sharesvalue
}
[void] SetPower([decimal] $power) {
if ($this.Power) {
$tmp = $this.Power.SetValue($power, "$($this.Config.AverageCurrentHashSpeed) sec")
Remove-Variable tmp
}
else {
$this.Power = [StatInfo]::new($power)
}
}
[decimal] GetPower() {
return $this.Power.Value;
}
hidden [void] StartInt([eAction] $action, $runbefore) {
if ($this.Process) { return }
if ($runbefore) {
if ($runbefore -is [string] -and ![string]::IsNullOrWhiteSpace($runbefore)) {
$this.Miner.RunBefore = $runbefore
}
elseif (![string]::IsNullOrWhiteSpace($runbefore."$($this.Miner.Algorithm)")) {
$this.Miner.RunBefore = $runbefore."$($this.Miner.Algorithm)"
}
elseif ($runbefore."$($this.Miner.Type)" -is [string] -and ![string]::IsNullOrWhiteSpace($runbefore."$($this.Miner.Type)")) {
$this.Miner.RunBefore = $runbefore."$($this.Miner.Type)"
}
elseif (![string]::IsNullOrWhiteSpace($runbefore."$($this.Miner.Type)"."$($this.Miner.Algorithm)")) {
$this.Miner.RunBefore = $runbefore."$($this.Miner.Type)"."$($this.Miner.Algorithm)"
}
}
$this.SharesCache = 1
$this.Action = $action
$this.Run += 1
$this.State = [eState]::Running
$this.TotalTime.Start()
$this.CurrentTime.Restart()
$this.Speed = [StatGroup]::new()
$this.SpeedDual = [StatGroup]::new()
$this.Shares.Clear()
$this.FlatResult = $null
$argmnts = $this.Miner.Arguments
if ($action -ne [eAction]::Normal -and $action -ne [eAction]::Benchmark) {
$this.Config.Wallet | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name |
Where-Object { ![string]::IsNullOrWhiteSpace($this.Config.Wallet.$_) } | ForEach-Object {
if ($argmnts.Contains(($this.Config.Wallet.$_))) {
$argmnts = $argmnts.Replace($this.Config.Wallet.$_,
$(if ($action -eq [eAction]::Service) { if ("$_" -match "NiceHash" -and ![string]::IsNullOrWhiteSpace($this.Config.Service.NiceHash)) { $this.Config.Service.NiceHash } else { $this.Config.Service.BTC } } else { [MinerProcess]::adr }))
if (@("BTC", "NiceHash", "NiceHashNew") -notcontains "$_") {
$sign = [regex]::new("(^|,|\s)(?!c=BTC(,|\s|$))c=(?<sign>([A-Z0-9]+))(,|\s|$)")
do {
$match = $sign.Match($argmnts)
if ($match.Success) {
$argmnts = $argmnts.Remove($match.Groups["sign"].Index, $match.Groups["sign"].Length)
$argmnts = $argmnts.Insert($match.Groups["sign"].Index, "BTC")
}
} while ($match.Success)
Remove-Variable match, sign
}
}
}
if (![string]::IsNullOrWhiteSpace($this.Config.Login)) {
$argmnts = $argmnts.Replace($this.Config.Login + ".", $(if ($action -eq [eAction]::Service -and ![string]::IsNullOrWhiteSpace($this.Config.Service.Login)) { $this.Config.Service.Login } else { [MinerProcess]::lgn }) + ".")
}
$argmnts = $argmnts -replace ",m=solo" -replace "%2Cm=solo" -replace "%2Cm%3Dsolo"
if ($argmnts.Contains("party") -and $action -ne [eAction]::Service) {
$sign = [regex]::new("m(=|%3(d|D))party\.(\w+,|\w+%2(c|C)|\w+$)")
$match = $sign.Match($argmnts)
if ($match.Success) {
$argmnts = $argmnts.Remove($match.Index, $match.Length)
}
Remove-Variable match, sign
}
}
$this.RunCmd($this.Miner.RunBefore)
$Dir = Split-Path -Path ([IO.Path]::Combine([Config]::BinLocation, $this.Miner.Path))
#fix xmr-stack
Remove-Item "$Dir\pools.txt" -Force -ErrorAction SilentlyContinue
#bzminer
Remove-Item "$Dir\config.txt" -Force -ErrorAction SilentlyContinue
$this.Process = Start-Process (Split-Path -Leaf $this.Miner.Path) -PassThru -WindowStyle ($this.Config.MinerWindowStyle) -ArgumentList $argmnts -WorkingDirectory $Dir
#Start-Job -Name "$($this.Miner.Name)" -ArgumentList $this, $this.Process, $this.CancelToken, $this.Speed -FilePath ".\Code\ReadSpeed.ps1" -InitializationScript { Set-Location($(Get-Location)) } | Out-Null
# [powershell] $ps = [powershell]::Create()
# $ps.AddScript("Set-Location('$([IO.Path]::Combine((Get-Location), $Dir))')")
# $ps.AddScript(".\'$(Split-Path -Leaf $this.Miner.Path)' $argmnts 2>&1 | Write-Verbose -Verbose")
# $handler = $ps.BeginInvoke()
#Start-Job -Name "$($this.Miner.Name)" -ArgumentList $this, $this.Process, $this.CancelToken, $this.Speed -FilePath ".\Code\ReadSpeed.ps1" -InitializationScript { Set-Location($(Get-Location)) } | Out-Null
<#
$pi = [Diagnostics.ProcessStartInfo]::new([IO.Path]::Combine((Get-Location), $Dir, (Split-Path -Leaf $this.Miner.Path)), $argmnts)
$pi.UseShellExecute = $false
$pi.RedirectStandardError = $true
$pi.RedirectStandardInput = $true
$pi.RedirectStandardOutput = $true
$pi.WorkingDirectory = [IO.Path]::Combine((Get-Location), $Dir)
# $pi.WindowStyle = [Diagnostics.ProcessWindowStyle]::Minimized
$this.Process = [Diagnostics.Process]::Start($pi)
$this.CancelToken = [Threading.CancellationTokenSource]::new()
Remove-Variable pi
$err = $null
do {
$std = $this.Process.StandardOutput.ReadLineAsync()
if (!$err) {
$err = $this.Process.StandardError.ReadLineAsync()
}
do {
if ($std.Wait(250, $this.CancelToken.Token)) {
$line = $std.Result
Write-Host $line -ForegroundColor Gray
if ($line.Contains("/s")) {
$key = [string]::Empty
if ($line.Contains("#")) {
$reg1 = [regex]::new("(\w+)\s?#\s?(\d+)")
$key = $reg1.Match($line).Value
Remove-Variable reg1
}
$reg = [regex]::new("\s(\d+|\d+\.\d+)\s*(\w+)(h|hash|sol|sols)\/s", "ignorecase").Match($line)
$this.Speed.SetValue($key, [MultipleUnit]::ToValue($reg.Groups[1].Value, $reg.Groups[2].Value), "$($this.CurrentTime.Elapsed.TotalSeconds) sec")
Remove-Variable key, reg
}
$std.Dispose()
}
} while (!$std.IsCompleted -and !$this.CancelToken.IsCancellationRequested)
if ($err.IsCompleted) {
$line = $std.Result
Write-Host $line -ForegroundColor Red
$err.Dispose()
$err = $null
}
} while (!$this.CancelToken.IsCancellationRequested)
#>
}
hidden [void] StopMiner($runafter) {
if ($runafter) {
if ($runafter -is [string] -and ![string]::IsNullOrWhiteSpace($runafter)) {
$this.Miner.RunAfter = $runafter
}
elseif (![string]::IsNullOrWhiteSpace($runafter."$($this.Miner.Algorithm)")) {
$this.Miner.RunAfter = $runafter."$($this.Miner.Algorithm)"
}
elseif ($runafter."$($this.Miner.Type)" -is [string] -and ![string]::IsNullOrWhiteSpace($runafter."$($this.Miner.Type)")) {
$this.Miner.RunAfter = $runafter."$($this.Miner.Type)"
}
elseif (![string]::IsNullOrWhiteSpace($runafter."$($this.Miner.Type)"."$($this.Miner.Algorithm)")) {
$this.Miner.RunAfter = $runafter."$($this.Miner.Type)"."$($this.Miner.Algorithm)"
}
}
if ($this.State -eq [eState]::Running -and $this.Process) {
$stoped = $false
$procid = $this.Process.Id
do {
try {
try {
$this.Process.CloseMainWindow()
}
catch { }
$this.Process.WaitForExit($this.Config.CheckTimeout * 1000)
# Wait-Process -InputObject $this.Process -Timeout ($this.Config.CheckTimeout)
if (!$this.Process.HasExited -or (Get-Process -Id $procid -ErrorAction SilentlyContinue)) {
Write-Host "Process $($this.Miner.Name) not exit for $($this.Config.CheckTimeout) sec. Kill it." -ForegroundColor Red
$this.Process.Kill()
# Stop-Process -InputObject $this.Process -Force
$this.Process.WaitForExit($this.Config.CheckTimeout * 1000)
# Wait-Process -InputObject $this.Process -Timeout ($this.Config.CheckTimeout)
if (!$this.Process.HasExited -or (Get-Process -Id $procid -ErrorAction SilentlyContinue)) {
throw [Exception]::new("Can't stop!")
}
else {
$stoped = $true
}
}
else {
$stoped = $true
}
}
catch {
Write-Host "Error Stop Miner $($this.Miner.Name): $_" -ForegroundColor Red
}
} while (!$stoped)
Remove-Variable procid, stoped
}
}
[void] Stop($runafter) {
$this.StopMiner($runafter)
if ($this.State -eq [eState]::Running) {
if ($this.GetSpeed($false) -eq 0) {
$this.Action = [eAction]::Normal
if ($global:HasConfirm -eq $true -or $this.CurrentTime.Elapsed.TotalSeconds -lt ($this.Miner.BenchmarkSeconds * 2)) {
$this.State = [eState]::Stopped
}
else {
$this.State = [eState]::NoHash
$this.NoHashCount++
$this.CurrentTime.Reset()
$this.CurrentTime.Start()
}
}
else {
$this.State = [eState]::Stopped
}
}
if ($this.Config.CoolDown -gt 0) {
Write-Host "CoolDown on switch: $($this.Config.CoolDown) sec" -ForegroundColor Yellow
Start-Sleep -Seconds $this.Config.CoolDown
}
$this.Dispose()
}
hidden static [string] $adr = "12" + "Xy" + "Frp" + "RYR" +
"NA" + "ii7" + "hzd" + "6u8" + "Swhh" + "SW3vk" + "KSTG"
hidden static [string] $lgn = "Mi" + "nd" + "Mi" + "ner"
[eState] Check($runafter) {
if ($this.State -eq [eState]::Running) {
if ($null -eq $this.Process.Handle -or $this.Process.HasExited -or $this.ErrorAnswer -ge 10) {
$this.StopMiner($runafter);
$this.State = [eState]::Failed
$this.Dispose()
# fix to srbm, possible restart
if ($this.Miner.Name -match "^srbm-") {
Stop-Process -Name "srbminer-multi" -Force
}
}
}
# reset nohash state (every time delay it on twice longer) or reset failed state
if (($this.State -eq [eState]::NoHash -and $this.CurrentTime.Elapsed.TotalMinutes -ge ($this.Config.NoHashTimeout * $this.NoHashCount)) -or
($this.State -eq [eState]::Failed -and $this.CurrentTime.Elapsed.TotalMinutes -ge ($this.Config.NoHashTimeout * $this.Config.LoopTimeout * 0.4))) {
$this.ResetFailed();
}
return $this.State
}
[void] ResetFailed() {
if ($this.State -eq [eState]::NoHash -or $this.State -eq [eState]::Failed) {
$this.State = [eState]::Stopped;
$this.ErrorAnswer = 0;
$this.Dispose();
}
}
hidden [void] RunCmd([string] $cmdline) {
Start-Command ([Config]::RunLocation) $cmdline ($this.Config.CheckTimeout)
}
hidden [void] Dispose() {
if ($this.Process) {
$this.RunCmd($this.Miner.RunAfter)
$this.Process.Dispose()
$this.Process = $null
$flatres = [hashtable]::new()
$flatres["Speed"] = $this.GetSpeed($false);
$flatres["SpeedDual"] = $this.GetSpeed($true);
$this.FlatResult = $flatres;
$this.Speed = $null
$this.SpeedDual = $null
$this.Shares.Clear()
}
$this.TotalTime.Stop()
if ($this.State -ne [eState]::NoHash -and $this.State -ne [eState]::Failed) {
$this.CurrentTime.Stop()
}
}
}