-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpool.go
296 lines (235 loc) · 7.04 KB
/
pool.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
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
package main
//
// main pool functionality
//
import "math/big"
import "time"
import "sync"
import "log"
import "errors"
type BlockState struct {
blockNumber *big.Int
blockDifficulty *big.Int
headerHash *big.Int
blockStart time.Time
submissions map[string]bool // check here to see if someone submitted the solution already
}
type BlockSolution struct {
nonce *big.Int
blockNumber *big.Int
}
type MinerStat struct {
Address string `json:"address"`
Hashes string `json:"hashes"`
Payout string `json:"payout"`
OnlineTime time.Duration `json:"online"`
Shares uint64 `json:"shares"`
Blocks uint64 `json:"blocks"`
}
type MinerPool struct {
miners map[string]*Miner
submissions map[string]bool // check here to see if someone submitted the solution already
solutions map[string]*BlockSolution // check here to see if someone submitted the solution already
stateLock *sync.Mutex
tick time.Time
blockStart time.Time
db Database
workingBlocks []BlockState
totalHashrate *big.Int
blockDifficulty *big.Int
blockNumber *big.Int
headerHash *big.Int
seedHash *big.Int
}
func newMinerPool(db Database) *MinerPool {
blockDif := big.NewInt(0)
blockDif.SetString("5000000000000", 10)
ret := &MinerPool{miners: make(map[string]*Miner),
submissions: make(map[string]bool),
stateLock: &sync.Mutex{},
tick: time.Now(),
blockStart: time.Now(),
db: db,
totalHashrate: big.NewInt(0),
blockDifficulty: blockDif,
blockNumber: big.NewInt(0),
headerHash: big.NewInt(0),
seedHash: big.NewInt(0)}
if db != nil {
db.Connect()
}
return ret
}
func (self *MinerPool) destroy() {
for key, mr := range(self.miners) {
self.removeMiner(mr, key)
}
if self.db != nil {
self.db.Disconnect()
}
}
func (self *MinerPool) lock() {
self.stateLock.Lock()
}
func (self *MinerPool) unlock() {
self.stateLock.Unlock()
}
func (self *MinerPool) getBlockNumber() *big.Int {
return self.blockNumber
}
func (self *MinerPool) getDifficulty() *big.Int {
return self.blockDifficulty
}
func (self *MinerPool) getHeaderHash() *big.Int {
return self.headerHash
}
func (self *MinerPool) getSeedHash() *big.Int {
return self.seedHash
}
func (self *MinerPool) getTotalHashes() *big.Int {
ret := big.NewInt(0)
for _, mr := range self.miners {
ret.Add(ret, mr.getHashes())
}
return ret
}
func (self *MinerPool) getAverageHashrate() *big.Int {
ret := big.NewInt(0)
for _, mr := range self.miners {
ret.Add(ret, mr.getTrueHashrate())
}
return ret
}
func (self *MinerPool) addSolution(blockNum, nonce *big.Int) {
solution := &BlockSolution{blockNumber: blockNum, nonce: nonce}
self.solutions[nonce.String()] = solution
}
func (self *MinerPool) removeMiner(miner *Miner, key string) {
pool.lock()
defer pool.unlock()
self.writeMinerStats(miner)
delete(self.miners, key)
}
func (self *MinerPool) update() {
now := time.Now()
dt := now.Sub(self.tick)
dstep := int64(dt.Seconds())
// do not update more than once a second
if dstep < 1 {
return
}
for key, mr := range self.miners {
mr.Update(dstep)
if now.Sub(mr.lastPost) > CLIENT_TIMEOUT * time.Second {
log.Println("pool: removing idle miner - ", key)
self.removeMiner(mr, key)
} else if now.Sub(mr.lastStat) > CLIENT_DB_WRITEBACK * time.Second {
self.writeMinerStats(mr)
mr.lastStat = now
}
}
staleBlockNum := big.NewInt(0)
staleBlockNum.Set(self.blockNumber)
staleBlockNum.Sub(staleBlockNum, big.NewInt(8))
for key, sub := range(self.solutions) {
if sub.blockNumber.Cmp(staleBlockNum) < 0 {
delete(self.solutions, key)
}
}
self.tick = self.tick.Add(time.Duration(dstep) * time.Second)
}
func (self *MinerPool) getHashrate() *big.Int {
ret := big.NewInt(0)
for _, mr := range self.miners {
ret.Add(ret, mr.getTrueHashrate())
}
return ret
}
func (self *MinerPool) resetHashcounts() {
self.stateLock.Lock()
for _, mr := range self.miners {
mr.hashes = big.NewInt(0)
}
self.stateLock.Unlock()
}
func (self *MinerPool) writeMinerStats(miner *Miner) error {
dt := time.Since(miner.lastStat)
miner.onlineTime += dt
minerStat := &MinerStat{Address: getHexString(miner.address, 40),
Hashes: miner.hashes.String(),
Payout: miner.payout.String(),
OnlineTime: miner.onlineTime,
Shares: miner.shares.Uint64(),
Blocks: miner.blocks.Uint64()}
if self.db == nil {
return errors.New("no database")
}
err := self.db.Connect()
if err != nil {
log.Println("could not connect to database")
return err
}
log.Println("writing miner stats - ", getHexString(miner.address, 40))
self.db.Update(minerStat)
self.db.Disconnect()
miner.lastStat = time.Now()
return nil
}
func (self *MinerPool) getMinerStats(miner *Miner) *MinerStat {
minerStat := &MinerStat{Address: getHexString(miner.address, 40),
Hashes: "0",
Payout: "0",
OnlineTime: 0,
Shares: 0,
Blocks: 0}
if self.db == nil {
return minerStat
}
err := self.db.Connect()
if err != nil {
log.Println("could not connect to database")
return minerStat
}
self.db.Get(minerStat, getHexString(miner.address, 40))
self.db.Disconnect()
log.Println("reading miner stats: ", getHexString(miner.address, 40))
return minerStat
}
func (self *MinerPool) getMiner(address *big.Int) *Miner {
minerStr := getHexString(address, 40)
ret, ok := self.miners[minerStr]
if !ok {
self.miners[minerStr] = MinerNew(self, address, self.tick)
ret = self.miners[minerStr]
minerStats := self.getMinerStats(ret)
ret.hashes.SetString(minerStats.Hashes, 10)
ret.shares = big.NewInt(int64(minerStats.Shares))
ret.blocks = big.NewInt(int64(minerStats.Blocks))
ret.payout.SetString(minerStats.Payout, 10)
ret.onlineTime = minerStats.OnlineTime
teraHashes := big.NewRat(1,1)
teraHashes.SetFrac(ret.hashes, big.NewInt(1000000000000))
fteraHashes, _ := teraHashes.Float64()
log.Println("new miner joined: ", minerStr)
log.Println("info: ", ret.onlineTime, " - terahashes: ", fteraHashes, " blocks: ", ret.blocks.String())
}
return ret
}
func (self *MinerPool) getTotalHashrate() *big.Int {
return self.totalHashrate
}
func (self *MinerPool) start(finished chan bool) {
for !SHUTDOWN {
time.Sleep(time.Duration(POOL_POLL_TIME) * time.Second)
pool.update()
for _, mr := range pool.miners {
if server != nil {
pool.lock()
server.SubmitHashrate(mr)
pool.unlock()
}
}
}
self.destroy()
finished <- true
}