forked from NebulousLabs/Sia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
69 lines (61 loc) · 2.07 KB
/
info.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
package miner
import (
"math/big"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/types"
)
// Info() returns a MinerInfo struct which can be converted to JSON to be
// parsed by frontends for displaying information to the user.
//
// State is a string indicating what the miner is currently doing with respect
// to the number of threads it currently has vs. the number of threads it wants
// to have.
//
// Threads is the number of threads that the miner currently wants to have.
//
// RunningThreads is the number of threads that the miner currently has.
//
// Address is the current address that is receiving block payouts.
func (m *Miner) MinerInfo() modules.MinerInfo {
m.mu.Lock()
defer m.mu.Unlock()
// Using the hashrate and target, determine the number of blocks per month
// that could be mined.
blocksPerWeek := float64(0)
if m.target.Int().Sign() > 0 {
hashesPerWeek := new(big.Int).Mul(big.NewInt(60*60*24*7), big.NewInt(m.hashRate))
hashesPerBlock := new(big.Int).Div(types.RootDepth.Int(), m.target.Int())
blocksPerWeek, _ = new(big.Rat).SetFrac(hashesPerWeek, hashesPerBlock).Float64()
}
info := modules.MinerInfo{
Threads: m.threads,
RunningThreads: m.runningThreads,
Address: m.address,
HashRate: m.hashRate,
BlocksPerWeek: blocksPerWeek,
}
// Using the reference of all blocks that have been mined, determine how
// many blocks have been mined successfully and how many have been mined as
// orphans.
for _, blockID := range m.blocksFound {
if m.cs.InCurrentPath(blockID) {
info.BlocksMined++
} else {
info.OrphansMined++
}
}
// Set the running info based on desiredThreads vs. runningThreads.
if info.RunningThreads != 0 {
info.Mining = true
}
if m.desiredThreads == 0 && m.runningThreads == 0 {
info.State = "Off"
} else if m.desiredThreads == 0 && m.runningThreads > 0 {
// If there are bugs or if the computer is slow (i.e. raspi), turning
// off can take multiple seconds.
info.State = "Turning Off"
} else if m.desiredThreads == m.runningThreads {
info.State = "On"
}
return info
}