Skip to content
This repository was archived by the owner on Oct 20, 2023. It is now read-only.

Commit 7b6f393

Browse files
committed
fix: improve share processing
1 parent fa4dea5 commit 7b6f393

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

src/Miningcore/Blockchain/Ravencoin/RavencoinJob.cs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected byte[] SerializeHeader(Span<byte> coinbaseHash)
5151
return blockHeader.ToBytes();
5252
}
5353

54-
protected virtual (Share Share, string BlockHex) ProcessShareInternal(ILogger logger,
54+
public virtual (Share Share, string BlockHex) ProcessShareInternal(ILogger logger,
5555
StratumConnection worker, ulong nonce, string inputHeaderHash, string mixHash)
5656
{
5757
var context = worker.ContextAs<RavencoinWorkerContext>();
@@ -72,13 +72,14 @@ protected virtual (Share Share, string BlockHex) ProcessShareInternal(ILogger lo
7272
var headerHashHex = headerHash.ToHexString();
7373

7474
if(headerHashHex != inputHeaderHash)
75-
{
76-
throw new StratumException(StratumError.MinusOne, "bad header-hash");
77-
}
75+
throw new StratumException(StratumError.MinusOne, $"bad header-hash");
7876

7977
if(!kawpowHasher.Compute(logger, (int) BlockTemplate.Height, headerHash.ToArray(), nonce, out var mixHashOut, out var resultBytes))
8078
throw new StratumException(StratumError.MinusOne, "bad hash");
8179

80+
if(mixHash != mixHashOut.ToHexString())
81+
throw new StratumException(StratumError.MinusOne, $"bad mix-hash");
82+
8283
resultBytes.ReverseInPlace();
8384
mixHashOut.ReverseInPlace();
8485

@@ -247,7 +248,7 @@ protected virtual string CreateHeaderHash(RavencoinWorkerJob workerJob)
247248
var coinbaseHasher = coin.CoinbaseHasherValue;
248249
var extraNonce1 = workerJob.ExtraNonce1;
249250

250-
var coinbase = SerializeCoinbase(workerJob.ExtraNonce1);
251+
var coinbase = SerializeCoinbase(extraNonce1);
251252
Span<byte> coinbaseHash = stackalloc byte[32];
252253
coinbaseHasher.Digest(coinbase, coinbaseHash);
253254

@@ -259,29 +260,6 @@ protected virtual string CreateHeaderHash(RavencoinWorkerJob workerJob)
259260
return headerHash.ToHexString();
260261
}
261262

262-
public virtual (Share Share, string BlockHex) ProcessShare(ILogger logger, StratumConnection worker, string nonce, string headerHash, string mixHash)
263-
{
264-
Contract.RequiresNonNull(worker);
265-
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(nonce));
266-
267-
var context = worker.ContextAs<RavencoinWorkerContext>();
268-
269-
// mixHash
270-
if(mixHash.Length != 64)
271-
throw new StratumException(StratumError.Other, $"incorrect size of mixHash: {mixHash}");
272-
273-
// validate nonce
274-
if(nonce.Length != 16)
275-
throw new StratumException(StratumError.Other, $"incorrect size of nonce: {nonce}");
276-
277-
// check if nonce is within range
278-
if(nonce.IndexOf(context.ExtraNonce1.Substring(0, 4)) != 0)
279-
throw new StratumException(StratumError.Other, $"nonce out of range: {nonce}");
280-
281-
var nonceLong = ulong.Parse(nonce, NumberStyles.HexNumber);
282-
283-
return ProcessShareInternal(logger, worker, nonceLong, headerHash, mixHash);
284-
}
285263

286264
#endregion // API-Surface
287265
}

src/Miningcore/Blockchain/Ravencoin/RavencoinJobManager.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,9 @@ public virtual async ValueTask<Share> SubmitShareAsync(StratumConnection worker,
263263
if(job == null)
264264
throw new StratumException(StratumError.JobNotFound, "job not found");
265265

266-
// dupe check
267-
if(!job.RegisterSubmit(context.ExtraNonce1, nonce, headerHash, mixHash))
268-
throw new StratumException(StratumError.DuplicateShare, "duplicate share");
269266

270267
// validate & process
271-
var (share, blockHex) = job.Job.ProcessShare(logger, worker, nonce, headerHash, mixHash);
268+
var (share, blockHex) = job.ProcessShare(logger, worker, nonce, headerHash, mixHash);
272269

273270
// enrich share with common data
274271
share.PoolId = poolConfig.Id;

src/Miningcore/Blockchain/Ravencoin/RavencoinWorkerJob.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using System.Collections.Concurrent;
2+
using System.Globalization;
23
using System.Text;
4+
using Miningcore.Stratum;
5+
using NLog;
6+
using Contract = Miningcore.Contracts.Contract;
37

48
namespace Miningcore.Blockchain.Ravencoin;
59

@@ -20,15 +24,42 @@ public RavencoinWorkerJob(string jobId, string extraNonce1)
2024

2125
public readonly ConcurrentDictionary<string, bool> Submissions = new(StringComparer.OrdinalIgnoreCase);
2226

23-
public bool RegisterSubmit(string extraNonce1, string nonce, string headerHash, string mixHash)
27+
private bool RegisterSubmit(string nonce, string headerHash, string mixHash)
2428
{
2529
var key = new StringBuilder()
26-
.Append(extraNonce1)
2730
.Append(nonce) // lowercase as we don't want to accept case-sensitive values as valid.
2831
.Append(headerHash)
2932
.Append(mixHash)
3033
.ToString();
3134

3235
return Submissions.TryAdd(key, true);
3336
}
37+
38+
public virtual (Share Share, string BlockHex) ProcessShare(ILogger logger, StratumConnection worker, string nonce, string headerHash, string mixHash)
39+
{
40+
Contract.RequiresNonNull(worker);
41+
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(nonce));
42+
43+
var context = worker.ContextAs<RavencoinWorkerContext>();
44+
45+
// mixHash
46+
if(mixHash.Length != 64)
47+
throw new StratumException(StratumError.Other, $"incorrect size of mixHash: {mixHash}");
48+
49+
// validate nonce
50+
if(nonce.Length != 16)
51+
throw new StratumException(StratumError.Other, $"incorrect size of nonce: {nonce}");
52+
53+
// check if nonce is within range
54+
if(nonce.IndexOf(context.ExtraNonce1.Substring(0, 4)) != 0)
55+
throw new StratumException(StratumError.Other, $"nonce out of range: {nonce}");
56+
57+
// dupe check
58+
if(!RegisterSubmit(nonce, headerHash, mixHash))
59+
throw new StratumException(StratumError.DuplicateShare, "duplicate share");
60+
61+
var nonceLong = ulong.Parse(nonce, NumberStyles.HexNumber);
62+
63+
return Job.ProcessShareInternal(logger, worker, nonceLong, headerHash, mixHash);
64+
}
3465
}

0 commit comments

Comments
 (0)