This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 689
/
Copy pathPPLNSPaymentScheme.cs
244 lines (196 loc) · 9.09 KB
/
PPLNSPaymentScheme.cs
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
using System.Data;
using System.Data.Common;
using System.Net.Sockets;
using Miningcore.Configuration;
using Miningcore.Extensions;
using Miningcore.Mining;
using Miningcore.Persistence;
using Miningcore.Persistence.Model;
using Miningcore.Persistence.Repositories;
using Miningcore.Util;
using NLog;
using Polly;
using Contract = Miningcore.Contracts.Contract;
namespace Miningcore.Payments.PaymentSchemes;
/// <summary>
/// PPLNS payout scheme implementation
/// </summary>
// ReSharper disable once InconsistentNaming
public class PPLNSPaymentScheme : IPayoutScheme
{
public PPLNSPaymentScheme(IConnectionFactory cf,
IShareRepository shareRepo,
IBlockRepository blockRepo,
IBalanceRepository balanceRepo)
{
Contract.RequiresNonNull(cf, nameof(cf));
Contract.RequiresNonNull(shareRepo, nameof(shareRepo));
Contract.RequiresNonNull(blockRepo, nameof(blockRepo));
Contract.RequiresNonNull(balanceRepo, nameof(balanceRepo));
this.cf = cf;
this.shareRepo = shareRepo;
this.blockRepo = blockRepo;
this.balanceRepo = balanceRepo;
BuildFaultHandlingPolicy();
}
private readonly IBalanceRepository balanceRepo;
private readonly IBlockRepository blockRepo;
private readonly IConnectionFactory cf;
private readonly IShareRepository shareRepo;
private static readonly ILogger logger = LogManager.GetLogger("PPLNS Payment", typeof(PPLNSPaymentScheme));
private const int RetryCount = 4;
private IAsyncPolicy shareReadFaultPolicy;
private class Config
{
public decimal Factor { get; set; }
}
#region IPayoutScheme
public async Task UpdateBalancesAsync(IDbConnection con, IDbTransaction tx, IMiningPool pool, IPayoutHandler payoutHandler, Block block, decimal blockReward, CancellationToken ct)
{
var poolConfig = pool.Config;
var payoutConfig = poolConfig.PaymentProcessing.PayoutSchemeConfig;
// PPLNS window (see https://bitcointalk.org/index.php?topic=39832)
var window = payoutConfig?.ToObject<Config>()?.Factor ?? 2.0m;
// calculate rewards
var shares = new Dictionary<string, double>();
var rewards = new Dictionary<string, decimal>();
var shareCutOffDate = await CalculateRewardsAsync(pool, payoutHandler, window, block, blockReward, shares, rewards, ct);
// update balances
foreach(var address in rewards.Keys)
{
var amount = rewards[address];
if(amount > 0)
{
logger.Info(() => $"Adding {payoutHandler.FormatAmount(amount)} to balance of {address} for {FormatUtil.FormatQuantity(shares[address])} ({shares[address]}) shares for block {block.BlockHeight}");
await balanceRepo.AddAmountAsync(con, tx, poolConfig.Id, address, amount, $"Reward for {FormatUtil.FormatQuantity(shares[address])} shares for block {block.BlockHeight}");
}
}
// delete discarded shares
if(shareCutOffDate.HasValue)
{
var cutOffCount = await shareRepo.CountSharesBeforeCreatedAsync(con, tx, poolConfig.Id, shareCutOffDate.Value);
if(cutOffCount > 0)
{
await LogDiscardedSharesAsync(poolConfig, block, shareCutOffDate.Value);
#if !DEBUG
logger.Info(() => $"Deleting {cutOffCount} discarded shares before {shareCutOffDate.Value:O}");
await shareRepo.DeleteSharesBeforeCreatedAsync(con, tx, poolConfig.Id, shareCutOffDate.Value);
#endif
}
}
// diagnostics
var totalShareCount = shares.Values.ToList().Sum(x => new decimal(x));
var totalRewards = rewards.Values.ToList().Sum(x => x);
if(totalRewards > 0)
logger.Info(() => $"{FormatUtil.FormatQuantity((double) totalShareCount)} ({Math.Round(totalShareCount, 2)}) shares contributed to a total payout of {payoutHandler.FormatAmount(totalRewards)} ({totalRewards / blockReward * 100:0.00}% of block reward) to {rewards.Keys.Count} addresses");
}
private async Task LogDiscardedSharesAsync(PoolConfig poolConfig, Block block, DateTime value)
{
var before = value;
var pageSize = 50000;
var currentPage = 0;
var shares = new Dictionary<string, double>();
while(true)
{
logger.Info(() => $"Fetching page {currentPage} of discarded shares for pool {poolConfig.Id}, block {block.BlockHeight}");
var page = await shareReadFaultPolicy.ExecuteAsync(() =>
cf.Run(con => shareRepo.ReadSharesBeforeCreatedAsync(con, poolConfig.Id, before, false, pageSize)));
currentPage++;
for(var i = 0; i < page.Length; i++)
{
var share = page[i];
var address = share.Miner;
// record attributed shares for diagnostic purposes
if(!shares.ContainsKey(address))
shares[address] = share.Difficulty;
else
shares[address] += share.Difficulty;
}
if(page.Length < pageSize)
break;
before = page[^1].Created;
}
if(shares.Keys.Count > 0)
{
// sort addresses by shares
var addressesByShares = shares.Keys.OrderByDescending(x => shares[x]);
logger.Info(() => $"{FormatUtil.FormatQuantity(shares.Values.Sum())} ({shares.Values.Sum()}) total discarded shares, block {block.BlockHeight}");
foreach(var address in addressesByShares)
logger.Info(() => $"{address} = {FormatUtil.FormatQuantity(shares[address])} ({shares[address]}) discarded shares, block {block.BlockHeight}");
}
}
#endregion // IPayoutScheme
private async Task<DateTime?> CalculateRewardsAsync(IMiningPool pool, IPayoutHandler payoutHandler,decimal window, Block block, decimal blockReward,
Dictionary<string, double> shares, Dictionary<string, decimal> rewards, CancellationToken ct)
{
var poolConfig = pool.Config;
var done = false;
var before = block.Created;
var inclusive = true;
var pageSize = 50000;
var currentPage = 0;
var accumulatedScore = 0.0m;
var blockRewardRemaining = blockReward;
DateTime? shareCutOffDate = null;
while(!done && !ct.IsCancellationRequested)
{
logger.Info(() => $"Fetching page {currentPage} of shares for pool {poolConfig.Id}, block {block.BlockHeight}");
var page = await shareReadFaultPolicy.ExecuteAsync(() =>
cf.Run(con => shareRepo.ReadSharesBeforeCreatedAsync(con, poolConfig.Id, before, inclusive, pageSize))); //, sw, logger));
inclusive = false;
currentPage++;
for(var i = 0; !done && i < page.Length; i++)
{
var share = page[i];
var address = share.Miner;
var shareDiffAdjusted = payoutHandler.AdjustShareDifficulty(share.Difficulty);
// record attributed shares for diagnostic purposes
if(!shares.ContainsKey(address))
shares[address] = shareDiffAdjusted;
else
shares[address] += shareDiffAdjusted;
var score = (decimal) (shareDiffAdjusted / share.NetworkDifficulty);
// if accumulated score would cross threshold, cap it to the remaining value
if(accumulatedScore + score >= window)
{
score = window - accumulatedScore;
shareCutOffDate = share.Created;
done = true;
}
// calculate reward
var reward = score * blockReward / window;
accumulatedScore += score;
blockRewardRemaining -= reward;
// this should never happen
if(blockRewardRemaining <= 0 && !done)
throw new OverflowException("blockRewardRemaining < 0");
if(reward > 0)
{
// accumulate miner reward
if(!rewards.ContainsKey(address))
rewards[address] = reward;
else
rewards[address] += reward;
}
}
if(page.Length < pageSize)
break;
before = page[^1].Created;
}
logger.Info(() => $"Balance-calculation for pool {poolConfig.Id}, block {block.BlockHeight} completed with accumulated score {accumulatedScore:0.####} ({(accumulatedScore / window) * 100:0.#}%)");
return shareCutOffDate;
}
private void BuildFaultHandlingPolicy()
{
var retry = Policy
.Handle<DbException>()
.Or<SocketException>()
.Or<TimeoutException>()
.RetryAsync(RetryCount, OnPolicyRetry);
shareReadFaultPolicy = retry;
}
private static void OnPolicyRetry(Exception ex, int retry, object context)
{
logger.Warn(() => $"Retry {retry} due to {ex.Source}: {ex.GetType().Name} ({ex.Message})");
}
}