/
share.go
391 lines (326 loc) · 10.6 KB
/
share.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
// Copyright (c) 2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package pool
import (
"bytes"
"encoding/json"
"fmt"
"math"
"math/big"
"time"
bolt "github.com/coreos/bbolt"
"github.com/davecgh/go-spew/spew"
"github.com/HcashOrg/hcd/chaincfg"
"github.com/HcashOrg/hcd/hcutil"
)
var (
// PPS represents the pay per share payment method.
PPS = "pps"
// PPLNS represents the pay per last n shares payment method.
PPLNS = "pplns"
)
// ShareWeights reprsents the associated weights for each known DCR miner.
// With the share weight of the lowest hash DCR miner (LHM) being 1, the
// rest were calculated as :
// (Hash of Miner X * Weight of LHM)/ Hash of LHM
var ShareWeights = map[string]*big.Rat{
CPU: new(big.Rat).SetFloat64(1.0), // Reserved for testing.
// ObeliskDCR1: new(big.Rat).SetFloat64(1.0),
InnosiliconD9: new(big.Rat).SetFloat64(2.182),
AntminerDR3: new(big.Rat).SetFloat64(7.091),
AntminerDR5: new(big.Rat).SetFloat64(31.181),
WhatsminerD1: new(big.Rat).SetFloat64(43.636),
}
// calculatePoolDifficulty determines the difficulty at which the provided
// hashrate can generate a pool share by the provided target time.
func calculatePoolDifficulty(net *chaincfg.Params, hashRate *big.Int, targetTimeSecs *big.Int) (*big.Int, error) {
hashesPerTargetTime := new(big.Int).Mul(hashRate, targetTimeSecs)
powLimit := net.PowLimit
powLimitFloat, _ := new(big.Float).SetInt(powLimit).Float64()
// The number of possible iterations is calculated as:
//
// iterations := 2^(256 - floor(log2(pow_limit)))
iterations := math.Pow(2, 256-math.Floor(math.Log2(powLimitFloat)))
// The difficulty at which the provided hashrate can mine a block is
// calculated as:
//
// difficulty = (hashes_per_sec * target_in_seconds) / iterations
difficulty := new(big.Rat).Quo(new(big.Rat).SetInt(hashesPerTargetTime),
new(big.Rat).SetFloat64(iterations))
diff := new(big.Int).Quo(difficulty.Num(), difficulty.Denom())
// Clamp the difficulty to 1 if needed.
if diff.Cmp(ZeroInt) == 0 {
diff = new(big.Int).SetInt64(1)
}
return diff, nil
}
// DifficultyToTarget converts the provided difficulty to a target based on the
// active network.
func DifficultyToTarget(net *chaincfg.Params, difficulty *big.Int) (*big.Int, error) {
powLimit := net.PowLimit
// The corresponding target is calculated as:
//
// target = pow_limit / difficulty
//
// The result is clamped to the pow limit if it exceeds it.
target := new(big.Int).Div(powLimit, difficulty)
if target.Cmp(powLimit) > 0 {
target = powLimit
}
return target, nil
}
// calculatePoolTarget determines the target difficulty at which the provided
// hashrate can generate a pool share by the provided target time.
func calculatePoolTarget(net *chaincfg.Params, hashRate *big.Int, targetTimeSecs *big.Int) (*big.Int, *big.Int, error) {
difficulty, err := calculatePoolDifficulty(net, hashRate, targetTimeSecs)
if err != nil {
return nil, nil, err
}
target, err := DifficultyToTarget(net, difficulty)
return target, difficulty, err
}
// Share represents verifiable work performed by a pool client.
type Share struct {
Account string `json:"account"`
Weight *big.Rat `json:"weight"`
CreatedOn int64 `json:"createdOn"`
}
// NewShare creates a share with the provided account and weight.
func NewShare(account string, weight *big.Rat) *Share {
return &Share{
Account: account,
Weight: weight,
CreatedOn: time.Now().UnixNano(),
}
}
// fetchShareBucket is a helper function for getting the share bucket.
func fetchShareBucket(tx *bolt.Tx) (*bolt.Bucket, error) {
pbkt := tx.Bucket(poolBkt)
if pbkt == nil {
desc := fmt.Sprintf("bucket %s not found", string(poolBkt))
return nil, MakeError(ErrBucketNotFound, desc, nil)
}
bkt := pbkt.Bucket(shareBkt)
if bkt == nil {
desc := fmt.Sprintf("bucket %s not found", string(shareBkt))
return nil, MakeError(ErrBucketNotFound, desc, nil)
}
return bkt, nil
}
// Create persists a share to the database.
func (s *Share) Create(db *bolt.DB) error {
err := db.Update(func(tx *bolt.Tx) error {
bkt, err := fetchShareBucket(tx)
if err != nil {
return err
}
sBytes, err := json.Marshal(s)
if err != nil {
return err
}
err = bkt.Put(nanoToBigEndianBytes(s.CreatedOn), sBytes)
return err
})
return err
}
// Update is not supported for shares.
func (s *Share) Update(db *bolt.DB) error {
desc := "share update not supported"
return MakeError(ErrNotSupported, desc, nil)
}
// Delete is not supported for shares.
func (s *Share) Delete(db *bolt.DB) error {
desc := "share deletion not supported"
return MakeError(ErrNotSupported, desc, nil)
}
// PPSEligibleShares fetches all shares within the provided inclusive bounds.
func PPSEligibleShares(db *bolt.DB, min []byte, max []byte) ([]*Share, error) {
eligibleShares := make([]*Share, 0)
err := db.View(func(tx *bolt.Tx) error {
bkt, err := fetchShareBucket(tx)
if err != nil {
return err
}
c := bkt.Cursor()
if min == nil {
for k, v := c.First(); k != nil; k, v = c.Next() {
var share Share
err := json.Unmarshal(v, &share)
if err != nil {
return err
}
eligibleShares = append(eligibleShares, &share)
}
}
if min != nil {
for k, v := c.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() {
var share Share
err := json.Unmarshal(v, &share)
if err != nil {
return err
}
eligibleShares = append(eligibleShares, &share)
}
}
return nil
})
if err != nil {
return nil, err
}
return eligibleShares, err
}
// PPLNSEligibleShares fetches all shares keyed greater than the provided
// minimum.
func PPLNSEligibleShares(db *bolt.DB, min []byte) ([]*Share, error) {
eligibleShares := make([]*Share, 0)
err := db.View(func(tx *bolt.Tx) error {
bkt, err := fetchShareBucket(tx)
if err != nil {
return err
}
c := bkt.Cursor()
for k, v := c.Last(); k != nil && bytes.Compare(k, min) > 0; k, v = c.Prev() {
var share Share
err := json.Unmarshal(v, &share)
if err != nil {
return err
}
eligibleShares = append(eligibleShares, &share)
}
return nil
})
if err != nil {
return nil, err
}
return eligibleShares, err
}
// sharePercentages calculates the percentages due each account according
// to their weighted shares.
func sharePercentages(shares []*Share) (map[string]*big.Rat, error) {
totalShares := new(big.Rat)
tally := make(map[string]*big.Rat)
dividends := make(map[string]*big.Rat)
// Tally all share weights for each participation account.
for _, share := range shares {
totalShares = totalShares.Add(totalShares, share.Weight)
if _, ok := tally[share.Account]; ok {
tally[share.Account] = tally[share.Account].
Add(tally[share.Account], share.Weight)
continue
}
tally[share.Account] = share.Weight
}
// Calculate each participating account to be claimed.
for account, shareCount := range tally {
if tally[account].Cmp(ZeroRat) == 0 {
return nil, MakeError(ErrDivideByZero, "division by zero", nil)
}
dividend := new(big.Rat).Quo(shareCount, totalShares)
dividends[account] = dividend
}
return dividends, nil
}
// CalculatePayments calculates the payments due participating accounts.
func CalculatePayments(percentages map[string]*big.Rat, total hcutil.Amount,
poolFee float64, height uint32, estMaturity uint32) ([]*Payment, error) {
// Deduct pool fee from the amount to be shared.
fee := total.MulF64(poolFee)
amtSansFees := total - fee
// Calculate each participating account's portion of the amount after fees.
payments := make([]*Payment, 0)
for account, percentage := range percentages {
percent, _ := percentage.Float64()
amt := amtSansFees.MulF64(percent)
payments = append(payments, NewPayment(account, amt, height, estMaturity))
}
// Add a payout entry for pool fees.
payments = append(payments, NewPayment(poolFeesK, fee, height, estMaturity))
return payments, nil
}
// PruneShares removes invalidated shares from the db.
func PruneShares(db *bolt.DB, minNano int64) error {
minBytes := nanoToBigEndianBytes(minNano)
err := db.Update(func(tx *bolt.Tx) error {
bkt, err := fetchShareBucket(tx)
if err != nil {
return err
}
toDelete := [][]byte{}
cursor := bkt.Cursor()
for k, _ := cursor.First(); k != nil; k, _ = cursor.Next() {
if bytes.Compare(minBytes, k) > 0 {
toDelete = append(toDelete, k)
}
}
for _, entry := range toDelete {
err := bkt.Delete(entry)
if err != nil {
return err
}
}
return nil
})
return err
}
// PPLNSSharePercentages computes the current mining reward percentages
// due pool accounts based on work performed measured by the PPS payment scheme.
func PPSSharePercentages(db *bolt.DB, poolFee float64, height uint32) (map[string]*big.Rat, error) {
now := time.Now()
nowNano := nanoToBigEndianBytes(now.UnixNano())
// Fetch the last payment created time.
var lastPaymentTimeNano []byte
err := db.View(func(tx *bolt.Tx) error {
pbkt := tx.Bucket(poolBkt)
if pbkt == nil {
desc := fmt.Sprintf("bucket %s not found", string(poolBkt))
return MakeError(ErrBucketNotFound, desc, nil)
}
v := pbkt.Get(lastPaymentCreatedOn)
lastPaymentTimeNano = make([]byte, len(v))
copy(lastPaymentTimeNano, v)
return nil
})
if err != nil {
return nil, err
}
// Fetch all eligible shares for payment calculations.
shares, err := PPSEligibleShares(db, lastPaymentTimeNano, nowNano)
if err != nil {
return nil, err
}
if len(shares) == 0 {
return make(map[string]*big.Rat), nil
}
// Deduct pool fees and calculate the payment due each participating
// account.
percentages, err := sharePercentages(shares)
if err != nil {
return nil, err
}
log.Tracef("Share PPS percentages are: %v", spew.Sdump(percentages))
return percentages, nil
}
// PPLNSSharePercentages computes the current mining reward percentages due pool
// accounts based on work performed measured by the PPLNS payment scheme.
func PPLNSSharePercentages(db *bolt.DB, poolFee float64, height uint32, periodSecs uint32) (map[string]*big.Rat, error) {
now := time.Now()
min := now.Add(-(time.Second * time.Duration(periodSecs)))
minNano := nanoToBigEndianBytes(min.UnixNano())
// Fetch all eligible shares within the specified period.
shares, err := PPLNSEligibleShares(db, minNano)
if err != nil {
return nil, err
}
if len(shares) == 0 {
return make(map[string]*big.Rat), nil
}
// Deduct pool fees and calculate the payment due each participating
// account.
percentages, err := sharePercentages(shares)
if err != nil {
return nil, err
}
log.Tracef("Share PPLNS percentages are: %v", spew.Sdump(percentages))
return percentages, nil
}