Skip to content

Commit

Permalink
fix: improve nonce validation on ethereum stratum v1
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaolin1579 committed Aug 4, 2023
1 parent 28094e9 commit aed2a3d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Miningcore/Blockchain/Ethereum/EthereumJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void RegisterNonce(StratumConnection worker, string nonce)
else
{
if(nonces.Contains(nonceLower))
throw new StratumException(StratumError.MinusOne, "duplicate share");
throw new StratumException(StratumError.DuplicateShare, "duplicate share");

nonces.Add(nonceLower);
}
Expand Down
20 changes: 19 additions & 1 deletion src/Miningcore/Blockchain/Ethereum/EthereumJobManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Concurrent;
using System.Globalization;
using System.Numerics;
using System.Reactive.Linq;
Expand All @@ -21,6 +22,7 @@
using EC = Miningcore.Blockchain.Ethereum.EthCommands;
using static Miningcore.Util.ActionUtils;
using System.Reactive;
using Microsoft.Extensions.Primitives;
using Miningcore.Mining;
using Miningcore.Rpc;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -54,6 +56,7 @@ public EthereumJobManager(
private readonly IExtraNonceProvider extraNonceProvider;
private const int MaxBlockBacklog = 6;
protected readonly Dictionary<string, EthereumJob> validJobs = new();
protected readonly ConcurrentDictionary<string, bool> submissions = new(StringComparer.OrdinalIgnoreCase);
private EthereumPoolConfigExtra extraPoolConfig;

protected async Task<bool> UpdateJob(CancellationToken ct, string via = null)
Expand Down Expand Up @@ -400,10 +403,25 @@ public async Task<Share> SubmitShareV1Async(StratumConnection worker, string[] r
if(job == null)
throw new StratumException(StratumError.MinusOne, "stale share");
}


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

return await SubmitShareAsync(worker, context, workerName, job, nonce.StripHexPrefix(), ct);
}

private bool RegisterSubmit(string nonce, string header, string address)
{
var key = new StringBuilder()
.Append(nonce)
.Append(header)
.Append(address)
.ToString();

return submissions.TryAdd(key, true);
}

public async Task<Share> SubmitShareV2Async(StratumConnection worker, string[] request, CancellationToken ct)
{
Contract.RequiresNonNull(worker);
Expand Down

0 comments on commit aed2a3d

Please sign in to comment.