This repository was archived by the owner on Oct 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 755
Expand file tree
/
Copy pathBitcoinJobManagerBase.cs
More file actions
580 lines (470 loc) · 22.2 KB
/
BitcoinJobManagerBase.cs
File metadata and controls
580 lines (470 loc) · 22.2 KB
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
using System.Globalization;
using System.Reactive;
using System.Reactive.Linq;
using Autofac;
using Miningcore.Blockchain.Bitcoin.Configuration;
using Miningcore.Blockchain.Bitcoin.DaemonResponses;
using Miningcore.Configuration;
using Miningcore.Contracts;
using Miningcore.Extensions;
using Miningcore.Messaging;
using Miningcore.Mining;
using Miningcore.Notifications.Messages;
using Miningcore.Rpc;
using Miningcore.Time;
using NBitcoin;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using static Miningcore.Util.ActionUtils;
namespace Miningcore.Blockchain.Bitcoin;
public abstract class BitcoinJobManagerBase<TJob> : JobManagerBase<TJob>
{
protected BitcoinJobManagerBase(
IComponentContext ctx,
IMasterClock clock,
IMessageBus messageBus,
IExtraNonceProvider extraNonceProvider) :
base(ctx, messageBus)
{
Contract.RequiresNonNull(ctx);
Contract.RequiresNonNull(clock);
Contract.RequiresNonNull(messageBus);
Contract.RequiresNonNull(extraNonceProvider);
this.clock = clock;
this.extraNonceProvider = extraNonceProvider;
}
protected readonly IMasterClock clock;
protected RpcClient rpc;
protected readonly IExtraNonceProvider extraNonceProvider;
protected const int ExtranonceBytes = 4;
protected int maxActiveJobs = 4;
protected bool hasLegacyDaemon;
protected BitcoinPoolConfigExtra extraPoolConfig;
protected BitcoinPoolPaymentProcessingConfigExtra extraPoolPaymentProcessingConfig;
protected readonly List<TJob> validJobs = new();
protected DateTime? lastJobRebroadcast;
protected bool hasSubmitBlockMethod;
protected bool isPoS;
protected TimeSpan jobRebroadcastTimeout;
protected Network network;
protected IDestination poolAddressDestination;
protected virtual object[] GetBlockTemplateParams()
{
return new object[]
{
new
{
rules = new[] {"segwit"},
}
};
}
protected virtual void SetupJobUpdates(CancellationToken ct)
{
jobRebroadcastTimeout = TimeSpan.FromSeconds(Math.Max(1, poolConfig.JobRebroadcastTimeout));
var blockFound = blockFoundSubject.Synchronize();
var pollTimerRestart = blockFoundSubject.Synchronize();
var triggers = new List<IObservable<(bool Force, string Via, string Data)>>
{
blockFound.Select(_ => (false, JobRefreshBy.BlockFound, (string) null))
};
if(extraPoolConfig?.BtStream == null)
{
// collect ports
var zmq = poolConfig.Daemons
.Where(x => !string.IsNullOrEmpty(x.Extra.SafeExtensionDataAs<BitcoinDaemonEndpointConfigExtra>()?.ZmqBlockNotifySocket))
.ToDictionary(x => x, x =>
{
var extra = x.Extra.SafeExtensionDataAs<BitcoinDaemonEndpointConfigExtra>();
var topic = !string.IsNullOrEmpty(extra.ZmqBlockNotifyTopic?.Trim()) ? extra.ZmqBlockNotifyTopic.Trim() : BitcoinConstants.ZmqPublisherTopicBlockHash;
return (Socket: extra.ZmqBlockNotifySocket, Topic: topic);
});
if(zmq.Count > 0)
{
logger.Info(() => $"Subscribing to ZMQ push-updates from {string.Join(", ", zmq.Values)}");
var blockNotify = rpc.ZmqSubscribe(logger, ct, zmq)
.Select(msg =>
{
using(msg)
{
// We just take the second frame's raw data and turn it into a hex string.
// If that string changes, we got an update (DistinctUntilChanged)
var result = msg[1].Read().ToHexString();
return result;
}
})
.DistinctUntilChanged()
.Select(_ => (false, JobRefreshBy.PubSub, (string) null))
.Publish()
.RefCount();
pollTimerRestart = blockFound
.Merge(blockNotify.Select(_ => Unit.Default))
.Publish()
.RefCount();
triggers.Add(blockNotify);
}
if(poolConfig.BlockRefreshInterval > 0)
{
// periodically update block-template
var pollingInterval = poolConfig.BlockRefreshInterval > 0 ? poolConfig.BlockRefreshInterval : 1000;
triggers.Add(Observable.Timer(TimeSpan.FromMilliseconds(pollingInterval))
.TakeUntil(pollTimerRestart)
.Select(_ => (false, JobRefreshBy.Poll, (string) null))
.Repeat());
}
else
{
// get initial blocktemplate
triggers.Add(Observable.Interval(TimeSpan.FromMilliseconds(1000))
.Select(_ => (false, JobRefreshBy.Initial, (string) null))
.TakeWhile(_ => !hasInitialBlockTemplate));
}
// periodically update transactions for current template
if(poolConfig.JobRebroadcastTimeout > 0)
{
triggers.Add(Observable.Timer(jobRebroadcastTimeout)
.TakeUntil(pollTimerRestart)
.Select(_ => (true, JobRefreshBy.PollRefresh, (string) null))
.Repeat());
}
}
else
{
var btStream = BtStreamSubscribe(extraPoolConfig.BtStream);
if(poolConfig.JobRebroadcastTimeout > 0)
{
var interval = TimeSpan.FromSeconds(Math.Max(1, poolConfig.JobRebroadcastTimeout - 0.1d));
triggers.Add(btStream
.Select(json =>
{
var force = !lastJobRebroadcast.HasValue || (clock.Now - lastJobRebroadcast >= interval);
return (force, !force ? JobRefreshBy.BlockTemplateStream : JobRefreshBy.BlockTemplateStreamRefresh, json);
})
.Publish()
.RefCount());
}
else
{
triggers.Add(btStream
.Select(json => (false, JobRefreshBy.BlockTemplateStream, json))
.Publish()
.RefCount());
}
// get initial blocktemplate
triggers.Add(Observable.Interval(TimeSpan.FromMilliseconds(1000))
.Select(_ => (false, JobRefreshBy.Initial, (string) null))
.TakeWhile(_ => !hasInitialBlockTemplate));
}
Jobs = triggers.Merge()
.Select(x => Observable.FromAsync(() => UpdateJob(ct, x.Force, x.Via, x.Data)))
.Concat()
.Where(x => x.IsNew || x.Force)
.Do(x =>
{
if(x.IsNew)
hasInitialBlockTemplate = true;
})
.Select(x => GetJobParamsForStratum(x.IsNew))
.Publish()
.RefCount();
}
protected virtual async Task ShowDaemonSyncProgressAsync(CancellationToken ct)
{
if(hasLegacyDaemon)
{
await ShowDaemonSyncProgressLegacyAsync(ct);
return;
}
var info = await rpc.ExecuteAsync<BlockchainInfo>(logger, BitcoinCommands.GetBlockchainInfo, ct);
if(info != null)
{
var blockCount = info.Response?.Blocks;
if(blockCount.HasValue)
{
// get list of peers and their highest block height to compare to ours
var peerInfo = await rpc.ExecuteAsync<PeerInfo[]>(logger, BitcoinCommands.GetPeerInfo, ct);
var peers = peerInfo.Response;
var totalBlocks = Math.Max(info.Response.Headers, peers.Any() ? peers.Max(y => y.StartingHeight) : 0);
var percent = totalBlocks > 0 ? (double) blockCount / totalBlocks * 100 : 0;
logger.Info(() => $"Daemon has downloaded {percent:0.00}% of blockchain from {peers.Length} peers");
}
}
}
private async Task UpdateNetworkStatsAsync(CancellationToken ct)
{
try
{
var results = await rpc.ExecuteBatchAsync(logger, ct,
new RpcRequest(BitcoinCommands.GetMiningInfo),
new RpcRequest(BitcoinCommands.GetNetworkInfo),
new RpcRequest(BitcoinCommands.GetNetworkHashPS)
);
if(results.Any(x => x.Error != null))
{
var errors = results.Where(x => x.Error != null).ToArray();
if(errors.Any())
logger.Warn(() => $"Error(s) refreshing network stats: {string.Join(", ", errors.Select(y => y.Error.Message))}");
}
var miningInfoResponse = results[0].Response.ToObject<MiningInfo>();
var networkInfoResponse = results[1].Response.ToObject<NetworkInfo>();
BlockchainStats.NetworkHashrate = miningInfoResponse.NetworkHashps;
BlockchainStats.ConnectedPeers = networkInfoResponse.Connections;
// Fall back to alternative RPC if coin does not report Network HPS (Digibyte)
if(BlockchainStats.NetworkHashrate == 0 && results[2].Error == null)
BlockchainStats.NetworkHashrate = results[2].Response.Value<double>();
}
catch(Exception e)
{
logger.Error(e);
}
}
protected record SubmitResult(bool Accepted, string CoinbaseTx);
protected async Task<SubmitResult> SubmitBlockAsync(Share share, string blockHex, CancellationToken ct)
{
var submitBlockRequest = hasSubmitBlockMethod
? new RpcRequest(BitcoinCommands.SubmitBlock, new[] { blockHex })
: new RpcRequest(BitcoinCommands.GetBlockTemplate, new { mode = "submit", data = blockHex });
var batch = new []
{
submitBlockRequest,
new RpcRequest(BitcoinCommands.GetBlock, new[] { share.BlockHash })
};
var results = await rpc.ExecuteBatchAsync(logger, ct, batch);
// did submission succeed?
var submitResult = results[0];
var submitError = submitResult.Error?.Message ??
submitResult.Error?.Code.ToString(CultureInfo.InvariantCulture) ??
submitResult.Response?.ToString();
if(!string.IsNullOrEmpty(submitError))
{
logger.Warn(() => $"Block {share.BlockHeight} submission failed with: {submitError}");
messageBus.SendMessage(new AdminNotification("Block submission failed", $"Pool {poolConfig.Id} {(!string.IsNullOrEmpty(share.Source) ? $"[{share.Source.ToUpper()}] " : string.Empty)}failed to submit block {share.BlockHeight}: {submitError}"));
return new SubmitResult(false, null);
}
// was it accepted?
var acceptResult = results[1];
var block = acceptResult.Response?.ToObject<DaemonResponses.Block>();
var accepted = acceptResult.Error == null && block?.Hash == share.BlockHash;
if(!accepted)
{
logger.Warn(() => $"Block {share.BlockHeight} submission failed for pool {poolConfig.Id} because block was not found after submission");
messageBus.SendMessage(new AdminNotification($"[{share.PoolId.ToUpper()}]-[{share.Source}] Block submission failed", $"[{share.PoolId.ToUpper()}]-[{share.Source}] Block {share.BlockHeight} submission failed for pool {poolConfig.Id} because block was not found after submission"));
}
return new SubmitResult(accepted, block?.Transactions.FirstOrDefault());
}
protected async Task<bool> AreDaemonsHealthyLegacyAsync(CancellationToken ct)
{
var response = await rpc.ExecuteAsync<DaemonInfo>(logger, BitcoinCommands.GetInfo, ct);
return response.Error == null;
}
protected async Task<bool> AreDaemonsConnectedLegacyAsync(CancellationToken ct)
{
var response = await rpc.ExecuteAsync<DaemonInfo>(logger, BitcoinCommands.GetInfo, ct);
return response.Error == null && response.Response.Connections > 0;
}
protected async Task ShowDaemonSyncProgressLegacyAsync(CancellationToken ct)
{
var info = await rpc.ExecuteAsync<DaemonInfo>(logger, BitcoinCommands.GetInfo, ct);
if(info != null)
{
var blockCount = info.Response?.Blocks;
if(blockCount.HasValue)
{
// get list of peers and their highest block height to compare to ours
var peerInfo = await rpc.ExecuteAsync<PeerInfo[]>(logger, BitcoinCommands.GetPeerInfo, ct);
var peers = peerInfo.Response;
if(peers != null && peers.Length > 0)
{
var totalBlocks = peers.Max(x => x.StartingHeight);
var percent = totalBlocks > 0 ? (double) blockCount / totalBlocks * 100 : 0;
logger.Info(() => $"Daemon has downloaded {percent:0.00}% of blockchain from {peers.Length} peers");
}
}
}
}
private async Task UpdateNetworkStatsLegacyAsync(CancellationToken ct)
{
try
{
var results = await rpc.ExecuteBatchAsync(logger, ct,
new RpcRequest(BitcoinCommands.GetConnectionCount)
);
if(results.Any(x => x.Error != null))
{
var errors = results.Where(x => x.Error != null).ToArray();
if(errors.Any())
logger.Warn(() => $"Error(s) refreshing network stats: {string.Join(", ", errors.Select(y => y.Error.Message))}");
}
var connectionCountResponse = results[0].Response.ToObject<object>();
//BlockchainStats.NetworkHashrate = miningInfoResponse.NetworkHashps;
BlockchainStats.ConnectedPeers = (int) (long) connectionCountResponse!;
}
catch(Exception e)
{
logger.Error(e);
}
}
protected virtual void PostChainIdentifyConfigure()
{
}
protected override void ConfigureDaemons()
{
var jsonSerializerSettings = ctx.Resolve<JsonSerializerSettings>();
rpc = new RpcClient(poolConfig.Daemons.First(), jsonSerializerSettings, messageBus, poolConfig.Id);
}
protected override async Task<bool> AreDaemonsHealthyAsync(CancellationToken ct)
{
if(hasLegacyDaemon)
return await AreDaemonsHealthyLegacyAsync(ct);
var response = await rpc.ExecuteAsync<BlockchainInfo>(logger, BitcoinCommands.GetBlockchainInfo, ct);
return response.Error == null;
}
protected override async Task<bool> AreDaemonsConnectedAsync(CancellationToken ct)
{
if(hasLegacyDaemon)
return await AreDaemonsConnectedLegacyAsync(ct);
var response = await rpc.ExecuteAsync<NetworkInfo>(logger, BitcoinCommands.GetNetworkInfo, ct);
return response.Error == null && response.Response?.Connections > 0;
}
protected override async Task EnsureDaemonsSynchedAsync(CancellationToken ct)
{
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(5));
var syncPendingNotificationShown = false;
do
{
var response = await rpc.ExecuteAsync<BlockTemplate>(logger,
BitcoinCommands.GetBlockTemplate, ct, GetBlockTemplateParams());
var isSynched = response.Error == null;
if(isSynched)
{
logger.Info(() => "All daemons synched with blockchain");
break;
}
if(!syncPendingNotificationShown)
{
logger.Info(() => "Daemon is still syncing with network. Manager will be started once synced.");
syncPendingNotificationShown = true;
}
await ShowDaemonSyncProgressAsync(ct);
} while(await timer.WaitForNextTickAsync(ct));
}
protected override async Task PostStartInitAsync(CancellationToken ct)
{
var requests = new[]
{
new RpcRequest(BitcoinCommands.ValidateAddress, new[] { poolConfig.Address }),
new RpcRequest(BitcoinCommands.SubmitBlock),
new RpcRequest(!hasLegacyDaemon ? BitcoinCommands.GetBlockchainInfo : BitcoinCommands.GetInfo),
new RpcRequest(BitcoinCommands.GetDifficulty),
new RpcRequest(BitcoinCommands.GetAddressInfo, new[] { poolConfig.Address }),
};
var responses = await rpc.ExecuteBatchAsync(logger, ct, requests);
if(responses.Any(x => x.Error != null))
{
// filter out optional RPCs
var errors = responses
.Where((x, i) => x.Error != null &&
requests[i].Method != BitcoinCommands.SubmitBlock &&
requests[i].Method != BitcoinCommands.GetAddressInfo)
.ToArray();
if(errors.Any())
throw new PoolStartupException($"Init RPC failed: {string.Join(", ", errors.Select(y => y.Error.Message))}", poolConfig.Id);
}
// extract results
var validateAddressResponse = responses[0].Error == null ? responses[0].Response.ToObject<ValidateAddressResponse>() : null;
var submitBlockResponse = responses[1];
var blockchainInfoResponse = !hasLegacyDaemon ? responses[2].Response.ToObject<BlockchainInfo>() : null;
var daemonInfoResponse = hasLegacyDaemon ? responses[2].Response.ToObject<DaemonInfo>() : null;
var difficultyResponse = responses[3].Response.ToObject<JToken>();
var addressInfoResponse = responses[4].Error == null ? responses[4].Response.ToObject<AddressInfo>() : null;
// chain detection
if(!hasLegacyDaemon)
network = Network.GetNetwork(blockchainInfoResponse.Chain.ToLower());
else
network = daemonInfoResponse.Testnet ? Network.TestNet : Network.Main;
PostChainIdentifyConfigure();
// ensure pool owns wallet
if(validateAddressResponse is not {IsValid: true})
throw new PoolStartupException($"Daemon reports pool-address '{poolConfig.Address}' as invalid", poolConfig.Id);
isPoS = poolConfig.Template is BitcoinTemplate {IsPseudoPoS: true} ||
(difficultyResponse.Values().Any(x => x.Path == "proof-of-stake" && !difficultyResponse.Values().Any(x => x.Path == "proof-of-work")));
// Create pool address script from response
if(!isPoS)
{
if(extraPoolConfig != null && extraPoolConfig.AddressType != BitcoinAddressType.Legacy)
logger.Info(()=> $"Interpreting pool address {poolConfig.Address} as type {extraPoolConfig?.AddressType.ToString()}");
poolAddressDestination = AddressToDestination(poolConfig.Address, extraPoolConfig?.AddressType);
}
else
poolAddressDestination = new PubKey(poolConfig.PubKey ?? validateAddressResponse.PubKey);
// Payment-processing setup
if(clusterConfig.PaymentProcessing?.Enabled == true && poolConfig.PaymentProcessing?.Enabled == true)
{
// ensure pool owns wallet
if(validateAddressResponse is {IsMine: false} && addressInfoResponse is {IsMine: false})
logger.Warn(()=> $"Daemon does not own pool-address '{poolConfig.Address}'");
}
// update stats
BlockchainStats.NetworkType = network.Name;
BlockchainStats.RewardType = isPoS ? "POS" : "POW";
// block submission RPC method
if(submitBlockResponse.Error?.Message?.ToLower() == "method not found")
hasSubmitBlockMethod = false;
else if(submitBlockResponse.Error?.Code == -1)
hasSubmitBlockMethod = true;
else
throw new PoolStartupException("Unable detect block submission RPC method", poolConfig.Id);
if(!hasLegacyDaemon)
await UpdateNetworkStatsAsync(ct);
else
await UpdateNetworkStatsLegacyAsync(ct);
// Periodically update network stats
Observable.Interval(TimeSpan.FromMinutes(10))
.Select(_ => Observable.FromAsync(() =>
Guard(()=> !hasLegacyDaemon ? UpdateNetworkStatsAsync(ct) : UpdateNetworkStatsLegacyAsync(ct),
ex => logger.Error(ex))))
.Concat()
.Subscribe();
SetupCrypto();
SetupJobUpdates(ct);
}
protected virtual IDestination AddressToDestination(string address, BitcoinAddressType? addressType)
{
if(!addressType.HasValue)
return BitcoinUtils.AddressToDestination(address, network);
switch(addressType.Value)
{
case BitcoinAddressType.BechSegwit:
return BitcoinUtils.BechSegwitAddressToDestination(poolConfig.Address, network);
case BitcoinAddressType.BCash:
return BitcoinUtils.BCashAddressToDestination(poolConfig.Address, network);
default:
return BitcoinUtils.AddressToDestination(poolConfig.Address, network);
}
}
protected void SetupCrypto()
{
}
protected abstract Task<(bool IsNew, bool Force)> UpdateJob(CancellationToken ct, bool forceUpdate, string via = null, string json = null);
protected abstract object GetJobParamsForStratum(bool isNew);
#region API-Surface
public Network Network => network;
public IObservable<object> Jobs { get; private set; }
public BlockchainStats BlockchainStats { get; } = new();
public override void Configure(PoolConfig pc, ClusterConfig cc)
{
extraPoolConfig = pc.Extra.SafeExtensionDataAs<BitcoinPoolConfigExtra>();
extraPoolPaymentProcessingConfig = pc.PaymentProcessing?.Extra?.SafeExtensionDataAs<BitcoinPoolPaymentProcessingConfigExtra>();
if(extraPoolConfig?.MaxActiveJobs.HasValue == true)
maxActiveJobs = extraPoolConfig.MaxActiveJobs.Value;
hasLegacyDaemon = extraPoolConfig?.HasLegacyDaemon == true;
base.Configure(pc, cc);
}
public virtual async Task<bool> ValidateAddressAsync(string address, CancellationToken ct)
{
if(string.IsNullOrEmpty(address))
return false;
var result = await rpc.ExecuteAsync<ValidateAddressResponse>(logger, BitcoinCommands.ValidateAddress, ct, new[] { address });
return result.Response is {IsValid: true};
}
#endregion // API-Surface
}