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

Commit

Permalink
Re-introduce Ethereum Job backlog
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Weichhold committed Jan 24, 2022
1 parent 3f08a8b commit c7a59d3
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions src/Miningcore/Blockchain/Ethereum/EthereumJobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public EthereumJobManager(
private EthashFull ethash;
private readonly IMasterClock clock;
private readonly IExtraNonceProvider extraNonceProvider;

private const int MaxBlockBacklog = 8;
protected readonly Dictionary<string, EthereumJob> validJobs = new();
private EthereumPoolConfigExtra extraPoolConfig;

protected async Task<bool> UpdateJob(CancellationToken ct, string via = null)
Expand Down Expand Up @@ -93,7 +94,24 @@ protected bool UpdateJob(EthereumBlockTemplate blockTemplate, string via = null)
{
messageBus.NotifyChainHeight(poolConfig.Id, blockTemplate.Height, poolConfig.Template);

job = new EthereumJob(NextJobId("x8"), blockTemplate, logger);
var jobId = NextJobId("x8");

// update template
job = new EthereumJob(jobId, blockTemplate, logger);

lock(jobLock)
{
// add jobs
validJobs[jobId] = job;

// remove old ones
var obsoleteKeys = validJobs.Keys
.Where(key => validJobs[key].BlockTemplate.Height < job.BlockTemplate.Height - MaxBlockBacklog).ToArray();

foreach(var key in obsoleteKeys)
validJobs.Remove(key);
}

currentJob = job;

logger.Info(() => $"New work at height {currentJob.BlockTemplate.Height} and header {currentJob.BlockTemplate.Header} via [{(via ?? "Unknown")}]");
Expand Down Expand Up @@ -355,8 +373,20 @@ public async Task<Share> SubmitShareV1Async(StratumConnection worker, string[] r

var context = worker.ContextAs<EthereumWorkerContext>();
var nonce = request[0];
var header = request[1];

EthereumJob job;

return await SubmitShareAsync(worker, context, workerName, currentJob, nonce.StripHexPrefix(), ct);
// stale?
lock(jobLock)
{
job = validJobs.Values.FirstOrDefault(x => x.BlockTemplate.Header.Equals(header));

if(job == null)
throw new StratumException(StratumError.MinusOne, "stale share");
}

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

public async Task<Share> SubmitShareV2Async(StratumConnection worker, string[] request, CancellationToken ct)
Expand All @@ -368,14 +398,20 @@ public async Task<Share> SubmitShareV2Async(StratumConnection worker, string[] r
var jobId = request[1];
var nonce = request[2];

EthereumJob job;

// stale?
if(jobId != currentJob.Id)
throw new StratumException(StratumError.MinusOne, "stale share");
lock(jobLock)
{
// look up job by id
if(!validJobs.TryGetValue(jobId, out job))
throw new StratumException(StratumError.MinusOne, "stale share");
}

// assemble full-nonce
var fullNonceHex = context.ExtraNonce1 + nonce;

return await SubmitShareAsync(worker, context, context.Worker, currentJob, fullNonceHex, ct);
return await SubmitShareAsync(worker, context, context.Worker, job, fullNonceHex, ct);
}

private async Task<Share> SubmitShareAsync(StratumConnection worker,
Expand Down

0 comments on commit c7a59d3

Please sign in to comment.