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 671
/
BitcoinJob.cs
705 lines (556 loc) · 23.8 KB
/
BitcoinJob.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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
using System.Collections.Concurrent;
using System.Globalization;
using System.Text;
using Miningcore.Blockchain.Bitcoin.Configuration;
using Miningcore.Blockchain.Bitcoin.DaemonResponses;
using Miningcore.Configuration;
using Miningcore.Crypto;
using Miningcore.Extensions;
using Miningcore.Stratum;
using Miningcore.Time;
using Miningcore.Util;
using NBitcoin;
using NBitcoin.DataEncoders;
using Newtonsoft.Json.Linq;
using Contract = Miningcore.Contracts.Contract;
using Transaction = NBitcoin.Transaction;
namespace Miningcore.Blockchain.Bitcoin;
public class BitcoinJob
{
protected IHashAlgorithm blockHasher;
protected IMasterClock clock;
protected IHashAlgorithm coinbaseHasher;
protected double shareMultiplier;
protected int extraNoncePlaceHolderLength;
protected IHashAlgorithm headerHasher;
protected bool isPoS;
protected string txComment;
protected PayeeBlockTemplateExtra payeeParameters;
protected Network network;
protected IDestination poolAddressDestination;
protected BitcoinTemplate coin;
private BitcoinTemplate.BitcoinNetworkParams networkParams;
protected readonly ConcurrentDictionary<string, bool> submissions = new(StringComparer.OrdinalIgnoreCase);
protected uint256 blockTargetValue;
protected byte[] coinbaseFinal;
protected string coinbaseFinalHex;
protected byte[] coinbaseInitial;
protected string coinbaseInitialHex;
protected string[] merkleBranchesHex;
protected MerkleTree mt;
///////////////////////////////////////////
// GetJobParams related properties
protected object[] jobParams;
protected string previousBlockHashReversedHex;
protected Money rewardToPool;
protected Transaction txOut;
// serialization constants
protected byte[] scriptSigFinalBytes;
protected static byte[] sha256Empty = new byte[32];
protected uint txVersion = 1u; // transaction version (currently 1) - see https://en.bitcoin.it/wiki/Transaction
protected static uint txInputCount = 1u;
protected static uint txInPrevOutIndex = (uint) (Math.Pow(2, 32) - 1);
protected static uint txInSequence;
protected static uint txLockTime;
protected virtual void BuildMerkleBranches()
{
var transactionHashes = BlockTemplate.Transactions
.Select(tx => (tx.TxId ?? tx.Hash)
.HexToByteArray()
.ReverseInPlace())
.ToArray();
mt = new MerkleTree(transactionHashes);
merkleBranchesHex = mt.Steps
.Select(x => x.ToHexString())
.ToArray();
}
protected virtual void BuildCoinbase()
{
// generate script parts
var sigScriptInitial = GenerateScriptSigInitial();
var sigScriptInitialBytes = sigScriptInitial.ToBytes();
var sigScriptLength = (uint) (
sigScriptInitial.Length +
extraNoncePlaceHolderLength +
scriptSigFinalBytes.Length);
// output transaction
txOut = CreateOutputTransaction();
// build coinbase initial
using(var stream = new MemoryStream())
{
var bs = new BitcoinStream(stream, true);
// version
bs.ReadWrite(ref txVersion);
// timestamp for POS coins
if(isPoS)
{
var timestamp = BlockTemplate.CurTime;
bs.ReadWrite(ref timestamp);
}
// serialize (simulated) input transaction
bs.ReadWriteAsVarInt(ref txInputCount);
bs.ReadWrite(ref sha256Empty);
bs.ReadWrite(ref txInPrevOutIndex);
// signature script initial part
bs.ReadWriteAsVarInt(ref sigScriptLength);
bs.ReadWrite(ref sigScriptInitialBytes);
// done
coinbaseInitial = stream.ToArray();
coinbaseInitialHex = coinbaseInitial.ToHexString();
}
// build coinbase final
using(var stream = new MemoryStream())
{
var bs = new BitcoinStream(stream, true);
// signature script final part
bs.ReadWrite(ref scriptSigFinalBytes);
// tx in sequence
bs.ReadWrite(ref txInSequence);
// serialize output transaction
var txOutBytes = SerializeOutputTransaction(txOut);
bs.ReadWrite(ref txOutBytes);
// misc
bs.ReadWrite(ref txLockTime);
// Extension point
AppendCoinbaseFinal(bs);
// done
coinbaseFinal = stream.ToArray();
coinbaseFinalHex = coinbaseFinal.ToHexString();
}
}
protected virtual void AppendCoinbaseFinal(BitcoinStream bs)
{
if(!string.IsNullOrEmpty(txComment))
{
var data = Encoding.ASCII.GetBytes(txComment);
bs.ReadWriteAsVarString(ref data);
}
if(coin.HasMasterNodes && !string.IsNullOrEmpty(masterNodeParameters.CoinbasePayload))
{
var data = masterNodeParameters.CoinbasePayload.HexToByteArray();
bs.ReadWriteAsVarString(ref data);
}
}
protected virtual byte[] SerializeOutputTransaction(Transaction tx)
{
var withDefaultWitnessCommitment = !string.IsNullOrEmpty(BlockTemplate.DefaultWitnessCommitment);
var outputCount = (uint) tx.Outputs.Count;
if(withDefaultWitnessCommitment)
outputCount++;
using(var stream = new MemoryStream())
{
var bs = new BitcoinStream(stream, true);
// write output count
bs.ReadWriteAsVarInt(ref outputCount);
long amount;
byte[] raw;
uint rawLength;
// serialize witness (segwit)
if(withDefaultWitnessCommitment)
{
amount = 0;
raw = BlockTemplate.DefaultWitnessCommitment.HexToByteArray();
rawLength = (uint) raw.Length;
bs.ReadWrite(ref amount);
bs.ReadWriteAsVarInt(ref rawLength);
bs.ReadWrite(ref raw);
}
// serialize outputs
foreach(var output in tx.Outputs)
{
amount = output.Value.Satoshi;
var outScript = output.ScriptPubKey;
raw = outScript.ToBytes(true);
rawLength = (uint) raw.Length;
bs.ReadWrite(ref amount);
bs.ReadWriteAsVarInt(ref rawLength);
bs.ReadWrite(ref raw);
}
return stream.ToArray();
}
}
protected virtual Script GenerateScriptSigInitial()
{
var now = ((DateTimeOffset) clock.Now).ToUnixTimeSeconds();
// script ops
var ops = new List<Op>();
// push block height
ops.Add(Op.GetPushOp(BlockTemplate.Height));
// optionally push aux-flags
if(!coin.CoinbaseIgnoreAuxFlags && !string.IsNullOrEmpty(BlockTemplate.CoinbaseAux?.Flags))
ops.Add(Op.GetPushOp(BlockTemplate.CoinbaseAux.Flags.HexToByteArray()));
// push timestamp
ops.Add(Op.GetPushOp(now));
// push placeholder
ops.Add(Op.GetPushOp(0));
return new Script(ops);
}
protected virtual Transaction CreateOutputTransaction()
{
rewardToPool = new Money(BlockTemplate.CoinbaseValue, MoneyUnit.Satoshi);
var tx = Transaction.Create(network);
if(coin.HasPayee)
rewardToPool = CreatePayeeOutput(tx, rewardToPool);
if(coin.HasMasterNodes)
rewardToPool = CreateMasternodeOutputs(tx, rewardToPool);
if (coin.HasFounderFee)
rewardToPool = CreateFounderOutputs(tx, rewardToPool);
if (coin.HasMinerFund)
rewardToPool = CreateMinerFundOutputs(tx, rewardToPool);
// Remaining amount goes to pool
tx.Outputs.Add(rewardToPool, poolAddressDestination);
return tx;
}
protected virtual Money CreatePayeeOutput(Transaction tx, Money reward)
{
if(payeeParameters?.PayeeAmount != null && payeeParameters.PayeeAmount.Value > 0)
{
var payeeReward = new Money(payeeParameters.PayeeAmount.Value, MoneyUnit.Satoshi);
reward -= payeeReward;
tx.Outputs.Add(payeeReward, BitcoinUtils.AddressToDestination(payeeParameters.Payee, network));
}
return reward;
}
protected bool RegisterSubmit(string extraNonce1, string extraNonce2, string nTime, string nonce)
{
var key = new StringBuilder()
.Append(extraNonce1)
.Append(extraNonce2) // lowercase as we don't want to accept case-sensitive values as valid.
.Append(nTime)
.Append(nonce) // lowercase as we don't want to accept case-sensitive values as valid.
.ToString();
return submissions.TryAdd(key, true);
}
protected byte[] SerializeHeader(Span<byte> coinbaseHash, uint nTime, uint nonce, uint? versionMask, uint? versionBits)
{
// build merkle-root
var merkleRoot = mt.WithFirst(coinbaseHash.ToArray());
// Build version
var version = BlockTemplate.Version;
// Overt-ASIC boost
if(versionMask.HasValue && versionBits.HasValue)
version = (version & ~versionMask.Value) | (versionBits.Value & versionMask.Value);
#pragma warning disable 618
var blockHeader = new BlockHeader
#pragma warning restore 618
{
Version = unchecked((int) version),
Bits = new Target(Encoders.Hex.DecodeData(BlockTemplate.Bits)),
HashPrevBlock = uint256.Parse(BlockTemplate.PreviousBlockhash),
HashMerkleRoot = new uint256(merkleRoot),
BlockTime = DateTimeOffset.FromUnixTimeSeconds(nTime),
Nonce = nonce
};
return blockHeader.ToBytes();
}
protected virtual (Share Share, string BlockHex) ProcessShareInternal(
StratumConnection worker, string extraNonce2, uint nTime, uint nonce, uint? versionBits)
{
var context = worker.ContextAs<BitcoinWorkerContext>();
var extraNonce1 = context.ExtraNonce1;
// build coinbase
var coinbase = SerializeCoinbase(extraNonce1, extraNonce2);
Span<byte> coinbaseHash = stackalloc byte[32];
coinbaseHasher.Digest(coinbase, coinbaseHash);
// hash block-header
var headerBytes = SerializeHeader(coinbaseHash, nTime, nonce, context.VersionRollingMask, versionBits);
Span<byte> headerHash = stackalloc byte[32];
headerHasher.Digest(headerBytes, headerHash, (ulong) nTime, BlockTemplate, coin, networkParams);
var headerValue = new uint256(headerHash);
// calc share-diff
var shareDiff = (double) new BigRational(BitcoinConstants.Diff1, headerHash.ToBigInteger()) * shareMultiplier;
var stratumDifficulty = context.Difficulty;
var ratio = shareDiff / stratumDifficulty;
// check if the share meets the much harder block difficulty (block candidate)
var isBlockCandidate = headerValue <= blockTargetValue;
// test if share meets at least workers current difficulty
if(!isBlockCandidate && ratio < 0.99)
{
// check if share matched the previous difficulty from before a vardiff retarget
if(context.VarDiff?.LastUpdate != null && context.PreviousDifficulty.HasValue)
{
ratio = shareDiff / context.PreviousDifficulty.Value;
if(ratio < 0.99)
throw new StratumException(StratumError.LowDifficultyShare, $"low difficulty share ({shareDiff})");
// use previous difficulty
stratumDifficulty = context.PreviousDifficulty.Value;
}
else
throw new StratumException(StratumError.LowDifficultyShare, $"low difficulty share ({shareDiff})");
}
var result = new Share
{
BlockHeight = BlockTemplate.Height,
NetworkDifficulty = Difficulty,
Difficulty = stratumDifficulty / shareMultiplier,
};
if(isBlockCandidate)
{
result.IsBlockCandidate = true;
Span<byte> blockHash = stackalloc byte[32];
blockHasher.Digest(headerBytes, blockHash, nTime);
result.BlockHash = blockHash.ToHexString();
var blockBytes = SerializeBlock(headerBytes, coinbase);
var blockHex = blockBytes.ToHexString();
return (result, blockHex);
}
return (result, null);
}
protected virtual byte[] SerializeCoinbase(string extraNonce1, string extraNonce2)
{
var extraNonce1Bytes = extraNonce1.HexToByteArray();
var extraNonce2Bytes = extraNonce2.HexToByteArray();
using(var stream = new MemoryStream())
{
stream.Write(coinbaseInitial);
stream.Write(extraNonce1Bytes);
stream.Write(extraNonce2Bytes);
stream.Write(coinbaseFinal);
return stream.ToArray();
}
}
protected virtual byte[] SerializeBlock(byte[] header, byte[] coinbase)
{
var rawTransactionBuffer = BuildRawTransactionBuffer();
var transactionCount = (uint) BlockTemplate.Transactions.Length + 1; // +1 for prepended coinbase tx
using(var stream = new MemoryStream())
{
var bs = new BitcoinStream(stream, true);
bs.ReadWrite(ref header);
bs.ReadWriteAsVarInt(ref transactionCount);
bs.ReadWrite(ref coinbase);
bs.ReadWrite(ref rawTransactionBuffer);
// POS coins require a zero byte appended to block which the daemon replaces with the signature
if(isPoS)
bs.ReadWrite((byte) 0);
return stream.ToArray();
}
}
protected virtual byte[] BuildRawTransactionBuffer()
{
using(var stream = new MemoryStream())
{
foreach(var tx in BlockTemplate.Transactions)
{
var txRaw = tx.Data.HexToByteArray();
stream.Write(txRaw);
}
return stream.ToArray();
}
}
#region Masternodes
protected MasterNodeBlockTemplateExtra masterNodeParameters;
protected virtual Money CreateMasternodeOutputs(Transaction tx, Money reward)
{
if(masterNodeParameters.Masternode != null)
{
Masternode[] masternodes;
// Dash v13 Multi-Master-Nodes
if(masterNodeParameters.Masternode.Type == JTokenType.Array)
masternodes = masterNodeParameters.Masternode.ToObject<Masternode[]>();
else
masternodes = new[] { masterNodeParameters.Masternode.ToObject<Masternode>() };
if(masternodes != null)
{
foreach(var masterNode in masternodes)
{
if(!string.IsNullOrEmpty(masterNode.Payee))
{
var payeeDestination = BitcoinUtils.AddressToDestination(masterNode.Payee, network);
var payeeReward = masterNode.Amount;
tx.Outputs.Add(payeeReward, payeeDestination);
reward -= payeeReward;
}
}
}
}
if(masterNodeParameters.SuperBlocks is { Length: > 0 })
{
foreach(var superBlock in masterNodeParameters.SuperBlocks)
{
var payeeAddress = BitcoinUtils.AddressToDestination(superBlock.Payee, network);
var payeeReward = superBlock.Amount;
tx.Outputs.Add(payeeReward, payeeAddress);
reward -= payeeReward;
}
}
if(!coin.HasPayee && !string.IsNullOrEmpty(masterNodeParameters.Payee))
{
var payeeAddress = BitcoinUtils.AddressToDestination(masterNodeParameters.Payee, network);
var payeeReward = masterNodeParameters.PayeeAmount;
tx.Outputs.Add(payeeReward, payeeAddress);
reward -= payeeReward;
}
return reward;
}
#endregion // Masternodes
#region Founder
protected FounderBlockTemplateExtra founderParameters;
protected virtual Money CreateFounderOutputs(Transaction tx, Money reward)
{
if (founderParameters.Founder != null)
{
Founder[] founders;
if (founderParameters.Founder.Type == JTokenType.Array)
founders = founderParameters.Founder.ToObject<Founder[]>();
else
founders = new[] { founderParameters.Founder.ToObject<Founder>() };
if(founders != null)
{
foreach(var Founder in founders)
{
if(!string.IsNullOrEmpty(Founder.Payee))
{
var payeeAddress = BitcoinUtils.AddressToDestination(Founder.Payee, network);
var payeeReward = Founder.Amount;
tx.Outputs.Add(payeeReward, payeeAddress);
reward -= payeeReward;
}
}
}
}
return reward;
}
#endregion // Founder
#region Minerfund
protected MinerFundTemplateExtra minerFundParameters;
protected virtual Money CreateMinerFundOutputs(Transaction tx, Money reward)
{
var payeeReward = minerFundParameters.MinimumValue;
if (!string.IsNullOrEmpty(minerFundParameters.Addresses?.FirstOrDefault()))
{
var payeeAddress = BitcoinUtils.AddressToDestination(minerFundParameters.Addresses[0], network);
tx.Outputs.Add(payeeReward, payeeAddress);
}
reward -= payeeReward;
return reward;
}
#endregion // Founder
#region API-Surface
public BlockTemplate BlockTemplate { get; protected set; }
public double Difficulty { get; protected set; }
public string JobId { get; protected set; }
public void Init(BlockTemplate blockTemplate, string jobId,
PoolConfig pc, BitcoinPoolConfigExtra extraPoolConfig,
ClusterConfig cc, IMasterClock clock,
IDestination poolAddressDestination, Network network,
bool isPoS, double shareMultiplier, IHashAlgorithm coinbaseHasher,
IHashAlgorithm headerHasher, IHashAlgorithm blockHasher)
{
Contract.RequiresNonNull(blockTemplate);
Contract.RequiresNonNull(pc);
Contract.RequiresNonNull(cc);
Contract.RequiresNonNull(clock);
Contract.RequiresNonNull(poolAddressDestination);
Contract.RequiresNonNull(coinbaseHasher);
Contract.RequiresNonNull(headerHasher);
Contract.RequiresNonNull(blockHasher);
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(jobId));
coin = pc.Template.As<BitcoinTemplate>();
networkParams = coin.GetNetwork(network.ChainName);
txVersion = coin.CoinbaseTxVersion;
this.network = network;
this.clock = clock;
this.poolAddressDestination = poolAddressDestination;
BlockTemplate = blockTemplate;
JobId = jobId;
var coinbaseString = !string.IsNullOrEmpty(cc.PaymentProcessing?.CoinbaseString) ?
cc.PaymentProcessing?.CoinbaseString.Trim() : "Miningcore";
scriptSigFinalBytes = new Script(Op.GetPushOp(Encoding.UTF8.GetBytes(coinbaseString))).ToBytes();
Difficulty = new Target(System.Numerics.BigInteger.Parse(BlockTemplate.Target, NumberStyles.HexNumber)).Difficulty;
extraNoncePlaceHolderLength = BitcoinConstants.ExtranoncePlaceHolderLength;
this.isPoS = isPoS;
this.shareMultiplier = shareMultiplier;
txComment = !string.IsNullOrEmpty(extraPoolConfig?.CoinbaseTxComment) ?
extraPoolConfig.CoinbaseTxComment : coin.CoinbaseTxComment;
if(coin.HasMasterNodes)
{
masterNodeParameters = BlockTemplate.Extra.SafeExtensionDataAs<MasterNodeBlockTemplateExtra>();
if((coin.Symbol == "RTM") || (coin.Symbol == "THOON") || (coin.Symbol == "YERB") || (coin.Symbol == "BTRM"))
{
if(masterNodeParameters.Extra?.ContainsKey("smartnode") == true)
{
masterNodeParameters.Masternode = JToken.FromObject(masterNodeParameters.Extra["smartnode"]);
}
}
if(!string.IsNullOrEmpty(masterNodeParameters.CoinbasePayload))
{
txVersion = 3;
const uint txType = 5;
txVersion += txType << 16;
}
}
if(coin.HasPayee)
payeeParameters = BlockTemplate.Extra.SafeExtensionDataAs<PayeeBlockTemplateExtra>();
if (coin.HasFounderFee)
founderParameters = BlockTemplate.Extra.SafeExtensionDataAs<FounderBlockTemplateExtra>();
if (coin.HasMinerFund)
minerFundParameters = BlockTemplate.Extra.SafeExtensionDataAs<MinerFundTemplateExtra>("coinbasetxn", "minerfund");
this.coinbaseHasher = coinbaseHasher;
this.headerHasher = headerHasher;
this.blockHasher = blockHasher;
if(!string.IsNullOrEmpty(BlockTemplate.Target))
blockTargetValue = new uint256(BlockTemplate.Target);
else
{
var tmp = new Target(BlockTemplate.Bits.HexToByteArray());
blockTargetValue = tmp.ToUInt256();
}
previousBlockHashReversedHex = BlockTemplate.PreviousBlockhash
.HexToByteArray()
.ReverseByteOrder()
.ToHexString();
BuildMerkleBranches();
BuildCoinbase();
jobParams = new object[]
{
JobId,
previousBlockHashReversedHex,
coinbaseInitialHex,
coinbaseFinalHex,
merkleBranchesHex,
BlockTemplate.Version.ToStringHex8(),
BlockTemplate.Bits,
BlockTemplate.CurTime.ToStringHex8(),
false
};
}
public object GetJobParams(bool isNew)
{
jobParams[^1] = isNew;
return jobParams;
}
public virtual (Share Share, string BlockHex) ProcessShare(StratumConnection worker,
string extraNonce2, string nTime, string nonce, string versionBits = null)
{
Contract.RequiresNonNull(worker);
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(extraNonce2));
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(nTime));
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(nonce));
var context = worker.ContextAs<BitcoinWorkerContext>();
// validate nTime
if(nTime.Length != 8)
throw new StratumException(StratumError.Other, "incorrect size of ntime");
var nTimeInt = uint.Parse(nTime, NumberStyles.HexNumber);
if(nTimeInt < BlockTemplate.CurTime || nTimeInt > ((DateTimeOffset) clock.Now).ToUnixTimeSeconds() + 7200)
throw new StratumException(StratumError.Other, "ntime out of range");
// validate nonce
if(nonce.Length != 8)
throw new StratumException(StratumError.Other, "incorrect size of nonce");
var nonceInt = uint.Parse(nonce, NumberStyles.HexNumber);
// validate version-bits (overt ASIC boost)
uint versionBitsInt = 0;
if(context.VersionRollingMask.HasValue && versionBits != null)
{
versionBitsInt = uint.Parse(versionBits, NumberStyles.HexNumber);
// enforce that only bits covered by current mask are changed by miner
if((versionBitsInt & ~context.VersionRollingMask.Value) != 0)
throw new StratumException(StratumError.Other, "rolling-version mask violation");
}
// dupe check
if(!RegisterSubmit(context.ExtraNonce1, extraNonce2, nTime, nonce))
throw new StratumException(StratumError.DuplicateShare, "duplicate share");
return ProcessShareInternal(worker, extraNonce2, nTimeInt, nonceInt, versionBitsInt);
}
#endregion // API-Surface
}