-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminer.go
93 lines (78 loc) · 2.6 KB
/
miner.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
package mine
import (
"fmt"
"time"
"github.com/pegnet/LXRPow/accumulate"
"github.com/pegnet/LXRPow/cfg"
"github.com/pegnet/LXRPow/hashing"
)
type Miner struct {
Cfg *cfg.Config
Hashers *hashing.HasherSet
Started bool
Solutions chan hashing.PoWSolution
Control chan string
MinersIdx uint64
}
func (m *Miner) Init(cfg *cfg.Config) {
m.Cfg = cfg
// Input to the Miner
m.Control = make(chan string, 1) // The Hasher stops when told on this channel
// Outputs of hashers to the Miners
m.Solutions = make(chan hashing.PoWSolution, 10) // Solutions are written to this channel
m.Hashers = hashing.NewHashers(cfg.Instances, cfg.Seed, cfg.LX) // Allocate the Hashers
m.Hashers.SetSolutions(m.Solutions) // Override their Solutions channel
m.MinersIdx = accumulate.MiningADI.RegisterMiner(m.Cfg.TokenURL)
}
func (m *Miner) Stop() {
fmt.Printf("Miner %2d has stopped", m.Cfg.Index)
m.Hashers.Stop()
}
// Run
// The job of the miner is to find the best hash it can from its hashers
// When hashers find a solution, those are fed to WriteSolution. WriteSolution
// implements strategies for optimal submission of mining records
func (m *Miner) Run() {
if m.Started {
return
}
m.Started = true
var limit uint64 = 0xFFF0000000000000
var settings accumulate.Settings
HashCounts := make(map[int]uint64)
for {
select {
case solution := <-m.Solutions: // New solutions have to be graded
HashCounts[int(solution.Instance)] = solution.HashCnt // Collect all the hashing counts from hashers
if solution.Pow > limit { // If the best so far on the block
solution.TokenURL = m.Cfg.TokenURL // Save the TokenURL
submission := new(accumulate.Submission)
submission.TimeStamp = time.Now()
submission.BlockIndex = settings.BlockIndex
submission.DNHash = settings.DNHash
submission.DNIndex = settings.DNIndex
submission.MinerIdx = m.MinersIdx
submission.Nonce = solution.Nonce
submission.PoW = solution.Pow
accumulate.MiningADI.AddSubmission(*submission)
}
continue
case cmd := <-m.Control:
if cmd == "stop" {
m.Stop()
return
}
default:
}
newSettings := accumulate.MiningADI.Sync() // Get the current state of mining
if newSettings.DNHash != settings.DNHash {
settings = newSettings
m.Hashers.BlockHashes <- hashing.Hash{Hash:settings.DNHash,Limit:limit} // Send the hash to the hashers
if !m.Hashers.Started { // If hashers are not started, do so after we have a hash set to them.
m.Hashers.Start()
}
} else {
time.Sleep(time.Second / 100) // Sleep for 1/10 a second
}
}
}