forked from sammy007/open-ethereum-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unlocker.go
522 lines (459 loc) · 15.2 KB
/
unlocker.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package payouts
import (
"fmt"
"log"
"math/big"
"strconv"
"strings"
"time"
"github.com/ethereum/go-ethereum/common/math"
"github.com/sammy007/open-ethereum-pool/rpc"
"github.com/sammy007/open-ethereum-pool/storage"
"github.com/sammy007/open-ethereum-pool/util"
)
type UnlockerConfig struct {
Enabled bool `json:"enabled"`
PoolFee float64 `json:"poolFee"`
PoolFeeAddress string `json:"poolFeeAddress"`
Donate bool `json:"donate"`
Depth int64 `json:"depth"`
ImmatureDepth int64 `json:"immatureDepth"`
KeepTxFees bool `json:"keepTxFees"`
Interval string `json:"interval"`
Daemon string `json:"daemon"`
Timeout string `json:"timeout"`
}
const minDepth = 16
var constReward = math.MustParseBig256("5000000000000000000")
var uncleReward = new(big.Int).Div(constReward, new(big.Int).SetInt64(32))
// Donate 10% from pool fees to developers
const donationFee = 10.0
const donationAccount = "0xb85150eb365e7df0941f0cf08235f987ba91506a"
type BlockUnlocker struct {
config *UnlockerConfig
backend *storage.RedisClient
rpc *rpc.RPCClient
halt bool
lastFail error
}
func NewBlockUnlocker(cfg *UnlockerConfig, backend *storage.RedisClient) *BlockUnlocker {
if len(cfg.PoolFeeAddress) != 0 && !util.IsValidHexAddress(cfg.PoolFeeAddress) {
log.Fatalln("Invalid poolFeeAddress", cfg.PoolFeeAddress)
}
if cfg.Depth < minDepth*2 {
log.Fatalf("Block maturity depth can't be < %v, your depth is %v", minDepth*2, cfg.Depth)
}
if cfg.ImmatureDepth < minDepth {
log.Fatalf("Immature depth can't be < %v, your depth is %v", minDepth, cfg.ImmatureDepth)
}
u := &BlockUnlocker{config: cfg, backend: backend}
u.rpc = rpc.NewRPCClient("BlockUnlocker", cfg.Daemon, cfg.Timeout)
return u
}
func (u *BlockUnlocker) Start() {
log.Println("Starting block unlocker")
intv := util.MustParseDuration(u.config.Interval)
timer := time.NewTimer(intv)
log.Printf("Set block unlock interval to %v", intv)
// Immediately unlock after start
u.unlockPendingBlocks()
u.unlockAndCreditMiners()
timer.Reset(intv)
go func() {
for {
select {
case <-timer.C:
u.unlockPendingBlocks()
u.unlockAndCreditMiners()
timer.Reset(intv)
}
}
}()
}
type UnlockResult struct {
maturedBlocks []*storage.BlockData
orphanedBlocks []*storage.BlockData
orphans int
uncles int
blocks int
}
/* Geth does not provide consistent state when you need both new height and new job,
* so in redis I am logging just what I have in a pool state on the moment when block found.
* Having very likely incorrect height in database results in a weird block unlocking scheme,
* when I have to check what the hell we actually found and traversing all the blocks with height-N and height+N
* to make sure we will find it. We can't rely on round height here, it's just a reference point.
* ISSUE: https://github.com/ethereum/go-ethereum/issues/2333
*/
func (u *BlockUnlocker) unlockCandidates(candidates []*storage.BlockData) (*UnlockResult, error) {
result := &UnlockResult{}
// Data row is: "height:nonce:powHash:mixDigest:timestamp:diff:totalShares"
for _, candidate := range candidates {
orphan := true
/* Search for a normal block with wrong height here by traversing 16 blocks back and forward.
* Also we are searching for a block that can include this one as uncle.
*/
for i := int64(minDepth * -1); i < minDepth; i++ {
height := candidate.Height + i
block, err := u.rpc.GetBlockByHeight(height)
if err != nil {
log.Printf("Error while retrieving block %v from node: %v", height, err)
return nil, err
}
if block == nil {
return nil, fmt.Errorf("Error while retrieving block %v from node, wrong node height", height)
}
if matchCandidate(block, candidate) {
orphan = false
result.blocks++
err = u.handleBlock(block, candidate)
if err != nil {
u.halt = true
u.lastFail = err
return nil, err
}
result.maturedBlocks = append(result.maturedBlocks, candidate)
log.Printf("Mature block %v with %v tx, hash: %v", candidate.Height, len(block.Transactions), candidate.Hash[0:10])
break
}
if len(block.Uncles) == 0 {
continue
}
// Trying to find uncle in current block during our forward check
for uncleIndex, uncleHash := range block.Uncles {
uncle, err := u.rpc.GetUncleByBlockNumberAndIndex(height, uncleIndex)
if err != nil {
return nil, fmt.Errorf("Error while retrieving uncle of block %v from node: %v", uncleHash, err)
}
if uncle == nil {
return nil, fmt.Errorf("Error while retrieving uncle of block %v from node", height)
}
// Found uncle
if matchCandidate(uncle, candidate) {
orphan = false
result.uncles++
err := handleUncle(height, uncle, candidate)
if err != nil {
u.halt = true
u.lastFail = err
return nil, err
}
result.maturedBlocks = append(result.maturedBlocks, candidate)
log.Printf("Mature uncle %v/%v of reward %v with hash: %v", candidate.Height, candidate.UncleHeight,
util.FormatReward(candidate.Reward), uncle.Hash[0:10])
break
}
}
// Found block or uncle
if !orphan {
break
}
}
// Block is lost, we didn't find any valid block or uncle matching our data in a blockchain
if orphan {
result.orphans++
candidate.Orphan = true
result.orphanedBlocks = append(result.orphanedBlocks, candidate)
log.Printf("Orphaned block %v:%v", candidate.RoundHeight, candidate.Nonce)
}
}
return result, nil
}
func matchCandidate(block *rpc.GetBlockReply, candidate *storage.BlockData) bool {
// Just compare hash if block is unlocked as immature
if len(candidate.Hash) > 0 && strings.EqualFold(candidate.Hash, block.Hash) {
return true
}
// Geth-style candidate matching
if len(block.Nonce) > 0 {
return strings.EqualFold(block.Nonce, candidate.Nonce)
}
// Parity's EIP: https://github.com/ethereum/EIPs/issues/95
if len(block.SealFields) == 2 {
return strings.EqualFold(candidate.Nonce, block.SealFields[1])
}
return false
}
func (u *BlockUnlocker) handleBlock(block *rpc.GetBlockReply, candidate *storage.BlockData) error {
// Initial 5 Ether static reward
reward := new(big.Int).Set(constReward)
correctHeight, err := strconv.ParseInt(strings.Replace(block.Number, "0x", "", -1), 16, 64)
if err != nil {
return err
}
candidate.Height = correctHeight
// Add TX fees
extraTxReward, err := u.getExtraRewardForTx(block)
if err != nil {
return fmt.Errorf("Error while fetching TX receipt: %v", err)
}
if u.config.KeepTxFees {
candidate.ExtraReward = extraTxReward
} else {
reward.Add(reward, extraTxReward)
}
// Add reward for including uncles
rewardForUncles := big.NewInt(0).Mul(uncleReward, big.NewInt(int64(len(block.Uncles))))
reward.Add(reward, rewardForUncles)
candidate.Orphan = false
candidate.Hash = block.Hash
candidate.Reward = reward
return nil
}
func handleUncle(height int64, uncle *rpc.GetBlockReply, candidate *storage.BlockData) error {
uncleHeight, err := strconv.ParseInt(strings.Replace(uncle.Number, "0x", "", -1), 16, 64)
if err != nil {
return err
}
reward := getUncleReward(uncleHeight, height)
candidate.Height = height
candidate.UncleHeight = uncleHeight
candidate.Orphan = false
candidate.Hash = uncle.Hash
candidate.Reward = reward
return nil
}
func (u *BlockUnlocker) unlockPendingBlocks() {
if u.halt {
log.Println("Unlocking suspended due to last critical error:", u.lastFail)
return
}
current, err := u.rpc.GetPendingBlock()
if err != nil {
u.halt = true
u.lastFail = err
log.Printf("Unable to get current blockchain height from node: %v", err)
return
}
currentHeight, err := strconv.ParseInt(strings.Replace(current.Number, "0x", "", -1), 16, 64)
if err != nil {
u.halt = true
u.lastFail = err
log.Printf("Can't parse pending block number: %v", err)
return
}
candidates, err := u.backend.GetCandidates(currentHeight - u.config.ImmatureDepth)
if err != nil {
u.halt = true
u.lastFail = err
log.Printf("Failed to get block candidates from backend: %v", err)
return
}
if len(candidates) == 0 {
log.Println("No block candidates to unlock")
return
}
result, err := u.unlockCandidates(candidates)
if err != nil {
u.halt = true
u.lastFail = err
log.Printf("Failed to unlock blocks: %v", err)
return
}
log.Printf("Immature %v blocks, %v uncles, %v orphans", result.blocks, result.uncles, result.orphans)
err = u.backend.WritePendingOrphans(result.orphanedBlocks)
if err != nil {
u.halt = true
u.lastFail = err
log.Printf("Failed to insert orphaned blocks into backend: %v", err)
return
} else {
log.Printf("Inserted %v orphaned blocks to backend", result.orphans)
}
totalRevenue := new(big.Rat)
totalMinersProfit := new(big.Rat)
totalPoolProfit := new(big.Rat)
for _, block := range result.maturedBlocks {
revenue, minersProfit, poolProfit, roundRewards, err := u.calculateRewards(block)
if err != nil {
u.halt = true
u.lastFail = err
log.Printf("Failed to calculate rewards for round %v: %v", block.RoundKey(), err)
return
}
err = u.backend.WriteImmatureBlock(block, roundRewards)
if err != nil {
u.halt = true
u.lastFail = err
log.Printf("Failed to credit rewards for round %v: %v", block.RoundKey(), err)
return
}
totalRevenue.Add(totalRevenue, revenue)
totalMinersProfit.Add(totalMinersProfit, minersProfit)
totalPoolProfit.Add(totalPoolProfit, poolProfit)
logEntry := fmt.Sprintf(
"IMMATURE %v: revenue %v, miners profit %v, pool profit: %v",
block.RoundKey(),
util.FormatRatReward(revenue),
util.FormatRatReward(minersProfit),
util.FormatRatReward(poolProfit),
)
entries := []string{logEntry}
for login, reward := range roundRewards {
entries = append(entries, fmt.Sprintf("\tREWARD %v: %v: %v Shannon", block.RoundKey(), login, reward))
}
log.Println(strings.Join(entries, "\n"))
}
log.Printf(
"IMMATURE SESSION: revenue %v, miners profit %v, pool profit: %v",
util.FormatRatReward(totalRevenue),
util.FormatRatReward(totalMinersProfit),
util.FormatRatReward(totalPoolProfit),
)
}
func (u *BlockUnlocker) unlockAndCreditMiners() {
if u.halt {
log.Println("Unlocking suspended due to last critical error:", u.lastFail)
return
}
current, err := u.rpc.GetPendingBlock()
if err != nil {
u.halt = true
u.lastFail = err
log.Printf("Unable to get current blockchain height from node: %v", err)
return
}
currentHeight, err := strconv.ParseInt(strings.Replace(current.Number, "0x", "", -1), 16, 64)
if err != nil {
u.halt = true
u.lastFail = err
log.Printf("Can't parse pending block number: %v", err)
return
}
immature, err := u.backend.GetImmatureBlocks(currentHeight - u.config.Depth)
if err != nil {
u.halt = true
u.lastFail = err
log.Printf("Failed to get block candidates from backend: %v", err)
return
}
if len(immature) == 0 {
log.Println("No immature blocks to credit miners")
return
}
result, err := u.unlockCandidates(immature)
if err != nil {
u.halt = true
u.lastFail = err
log.Printf("Failed to unlock blocks: %v", err)
return
}
log.Printf("Unlocked %v blocks, %v uncles, %v orphans", result.blocks, result.uncles, result.orphans)
for _, block := range result.orphanedBlocks {
err = u.backend.WriteOrphan(block)
if err != nil {
u.halt = true
u.lastFail = err
log.Printf("Failed to insert orphaned block into backend: %v", err)
return
}
}
log.Printf("Inserted %v orphaned blocks to backend", result.orphans)
totalRevenue := new(big.Rat)
totalMinersProfit := new(big.Rat)
totalPoolProfit := new(big.Rat)
for _, block := range result.maturedBlocks {
revenue, minersProfit, poolProfit, roundRewards, err := u.calculateRewards(block)
if err != nil {
u.halt = true
u.lastFail = err
log.Printf("Failed to calculate rewards for round %v: %v", block.RoundKey(), err)
return
}
err = u.backend.WriteMaturedBlock(block, roundRewards)
if err != nil {
u.halt = true
u.lastFail = err
log.Printf("Failed to credit rewards for round %v: %v", block.RoundKey(), err)
return
}
totalRevenue.Add(totalRevenue, revenue)
totalMinersProfit.Add(totalMinersProfit, minersProfit)
totalPoolProfit.Add(totalPoolProfit, poolProfit)
logEntry := fmt.Sprintf(
"MATURED %v: revenue %v, miners profit %v, pool profit: %v",
block.RoundKey(),
util.FormatRatReward(revenue),
util.FormatRatReward(minersProfit),
util.FormatRatReward(poolProfit),
)
entries := []string{logEntry}
for login, reward := range roundRewards {
entries = append(entries, fmt.Sprintf("\tREWARD %v: %v: %v Shannon", block.RoundKey(), login, reward))
}
log.Println(strings.Join(entries, "\n"))
}
log.Printf(
"MATURE SESSION: revenue %v, miners profit %v, pool profit: %v",
util.FormatRatReward(totalRevenue),
util.FormatRatReward(totalMinersProfit),
util.FormatRatReward(totalPoolProfit),
)
}
func (u *BlockUnlocker) calculateRewards(block *storage.BlockData) (*big.Rat, *big.Rat, *big.Rat, map[string]int64, error) {
revenue := new(big.Rat).SetInt(block.Reward)
minersProfit, poolProfit := chargeFee(revenue, u.config.PoolFee)
shares, err := u.backend.GetRoundShares(block.RoundHeight, block.Nonce)
if err != nil {
return nil, nil, nil, nil, err
}
rewards := calculateRewardsForShares(shares, block.TotalShares, minersProfit)
if block.ExtraReward != nil {
extraReward := new(big.Rat).SetInt(block.ExtraReward)
poolProfit.Add(poolProfit, extraReward)
revenue.Add(revenue, extraReward)
}
if u.config.Donate {
var donation = new(big.Rat)
poolProfit, donation = chargeFee(poolProfit, donationFee)
login := strings.ToLower(donationAccount)
rewards[login] += weiToShannonInt64(donation)
}
if len(u.config.PoolFeeAddress) != 0 {
address := strings.ToLower(u.config.PoolFeeAddress)
rewards[address] += weiToShannonInt64(poolProfit)
}
return revenue, minersProfit, poolProfit, rewards, nil
}
func calculateRewardsForShares(shares map[string]int64, total int64, reward *big.Rat) map[string]int64 {
rewards := make(map[string]int64)
for login, n := range shares {
percent := big.NewRat(n, total)
workerReward := new(big.Rat).Mul(reward, percent)
rewards[login] += weiToShannonInt64(workerReward)
}
return rewards
}
// Returns new value after fee deduction and fee value.
func chargeFee(value *big.Rat, fee float64) (*big.Rat, *big.Rat) {
feePercent := new(big.Rat).SetFloat64(fee / 100)
feeValue := new(big.Rat).Mul(value, feePercent)
return new(big.Rat).Sub(value, feeValue), feeValue
}
func weiToShannonInt64(wei *big.Rat) int64 {
shannon := new(big.Rat).SetInt(util.Shannon)
inShannon := new(big.Rat).Quo(wei, shannon)
value, _ := strconv.ParseInt(inShannon.FloatString(0), 10, 64)
return value
}
func getUncleReward(uHeight, height int64) *big.Int {
reward := new(big.Int).Set(constReward)
reward.Mul(big.NewInt(uHeight+8-height), reward)
reward.Div(reward, big.NewInt(8))
return reward
}
func (u *BlockUnlocker) getExtraRewardForTx(block *rpc.GetBlockReply) (*big.Int, error) {
amount := new(big.Int)
for _, tx := range block.Transactions {
receipt, err := u.rpc.GetTxReceipt(tx.Hash)
if err != nil {
return nil, err
}
if receipt != nil {
gasUsed := util.String2Big(receipt.GasUsed)
gasPrice := util.String2Big(tx.GasPrice)
fee := new(big.Int).Mul(gasUsed, gasPrice)
amount.Add(amount, fee)
}
}
return amount, nil
}