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

Commit

Permalink
fix: improve share processing
Browse files Browse the repository at this point in the history
  • Loading branch information
jon4hz committed Feb 15, 2023
1 parent fa4dea5 commit 7b6f393
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
34 changes: 6 additions & 28 deletions src/Miningcore/Blockchain/Ravencoin/RavencoinJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected byte[] SerializeHeader(Span<byte> coinbaseHash)
return blockHeader.ToBytes();
}

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

if(headerHashHex != inputHeaderHash)
{
throw new StratumException(StratumError.MinusOne, "bad header-hash");
}
throw new StratumException(StratumError.MinusOne, $"bad header-hash");

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

if(mixHash != mixHashOut.ToHexString())
throw new StratumException(StratumError.MinusOne, $"bad mix-hash");

resultBytes.ReverseInPlace();
mixHashOut.ReverseInPlace();

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

var coinbase = SerializeCoinbase(workerJob.ExtraNonce1);
var coinbase = SerializeCoinbase(extraNonce1);
Span<byte> coinbaseHash = stackalloc byte[32];
coinbaseHasher.Digest(coinbase, coinbaseHash);

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

public virtual (Share Share, string BlockHex) ProcessShare(ILogger logger, StratumConnection worker, string nonce, string headerHash, string mixHash)
{
Contract.RequiresNonNull(worker);
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(nonce));

var context = worker.ContextAs<RavencoinWorkerContext>();

// mixHash
if(mixHash.Length != 64)
throw new StratumException(StratumError.Other, $"incorrect size of mixHash: {mixHash}");

// validate nonce
if(nonce.Length != 16)
throw new StratumException(StratumError.Other, $"incorrect size of nonce: {nonce}");

// check if nonce is within range
if(nonce.IndexOf(context.ExtraNonce1.Substring(0, 4)) != 0)
throw new StratumException(StratumError.Other, $"nonce out of range: {nonce}");

var nonceLong = ulong.Parse(nonce, NumberStyles.HexNumber);

return ProcessShareInternal(logger, worker, nonceLong, headerHash, mixHash);
}

#endregion // API-Surface
}
5 changes: 1 addition & 4 deletions src/Miningcore/Blockchain/Ravencoin/RavencoinJobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,9 @@ public virtual async ValueTask<Share> SubmitShareAsync(StratumConnection worker,
if(job == null)
throw new StratumException(StratumError.JobNotFound, "job not found");

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

// validate & process
var (share, blockHex) = job.Job.ProcessShare(logger, worker, nonce, headerHash, mixHash);
var (share, blockHex) = job.ProcessShare(logger, worker, nonce, headerHash, mixHash);

// enrich share with common data
share.PoolId = poolConfig.Id;
Expand Down
35 changes: 33 additions & 2 deletions src/Miningcore/Blockchain/Ravencoin/RavencoinWorkerJob.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Collections.Concurrent;
using System.Globalization;
using System.Text;
using Miningcore.Stratum;
using NLog;
using Contract = Miningcore.Contracts.Contract;

namespace Miningcore.Blockchain.Ravencoin;

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

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

public bool RegisterSubmit(string extraNonce1, string nonce, string headerHash, string mixHash)
private bool RegisterSubmit(string nonce, string headerHash, string mixHash)
{
var key = new StringBuilder()
.Append(extraNonce1)
.Append(nonce) // lowercase as we don't want to accept case-sensitive values as valid.
.Append(headerHash)
.Append(mixHash)
.ToString();

return Submissions.TryAdd(key, true);
}

public virtual (Share Share, string BlockHex) ProcessShare(ILogger logger, StratumConnection worker, string nonce, string headerHash, string mixHash)
{
Contract.RequiresNonNull(worker);
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(nonce));

var context = worker.ContextAs<RavencoinWorkerContext>();

// mixHash
if(mixHash.Length != 64)
throw new StratumException(StratumError.Other, $"incorrect size of mixHash: {mixHash}");

// validate nonce
if(nonce.Length != 16)
throw new StratumException(StratumError.Other, $"incorrect size of nonce: {nonce}");

// check if nonce is within range
if(nonce.IndexOf(context.ExtraNonce1.Substring(0, 4)) != 0)
throw new StratumException(StratumError.Other, $"nonce out of range: {nonce}");

// dupe check
if(!RegisterSubmit(nonce, headerHash, mixHash))
throw new StratumException(StratumError.DuplicateShare, "duplicate share");

var nonceLong = ulong.Parse(nonce, NumberStyles.HexNumber);

return Job.ProcessShareInternal(logger, worker, nonceLong, headerHash, mixHash);
}
}

0 comments on commit 7b6f393

Please sign in to comment.