diff --git a/src/models/MiningJob.ts b/src/models/MiningJob.ts index e3fc279..cbe6e36 100644 --- a/src/models/MiningJob.ts +++ b/src/models/MiningJob.ts @@ -20,7 +20,7 @@ export class MiningJob { public jobTemplateId: string; public networkDifficulty: number; - + public creation: number; constructor( configService: ConfigService, @@ -30,6 +30,7 @@ export class MiningJob { jobTemplate: IJobTemplate ) { + this.creation = new Date().getTime(); this.jobTemplateId = jobTemplate.blockData.id; this.coinbaseTransaction = this.createCoinbaseTransaction(payoutInformation, jobTemplate.blockData.coinbasevalue); diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index bd2efc9..cb83d15 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -468,7 +468,7 @@ export class StratumV1Client { const job = this.stratumV1JobsService.getJobById(submission.jobId); - // a miner may submit a job that doesn't exist anymore if it was removed by a new block notification + // a miner may submit a job that doesn't exist anymore if it was removed by a new block notification (or expired, 5 min) if (job == null) { const err = new StratumErrorMessage( submission.id, diff --git a/src/services/stratum-v1-jobs.service.ts b/src/services/stratum-v1-jobs.service.ts index 8c0c1dd..ca1bea4 100644 --- a/src/services/stratum-v1-jobs.service.ts +++ b/src/services/stratum-v1-jobs.service.ts @@ -13,6 +13,7 @@ export interface IJobTemplate { merkle_branch: string[]; blockData: { id: string, + creation: number, coinbasevalue: number; networkDifficulty: number; height: number; @@ -114,6 +115,7 @@ export class StratumV1JobsService { merkle_branch, blockData: { id, + creation: new Date().getTime(), coinbasevalue, networkDifficulty, height, @@ -125,6 +127,20 @@ export class StratumV1JobsService { if (data.blockData.clearJobs) { this.blocks = {}; this.jobs = {}; + }else{ + const now = new Date().getTime(); + // Delete old templates (5 minutes) + for(const templateId in this.blocks){ + if(now - this.blocks[templateId].blockData.creation > (1000 * 60 * 5)){ + delete this.blocks[templateId]; + } + } + // Delete old jobs (5 minutes) + for (const jobId in this.jobs) { + if(now - this.jobs[jobId].creation > (1000 * 60 * 5)){ + delete this.jobs[jobId]; + } + } } this.blocks[data.blockData.id] = data; }), @@ -138,7 +154,8 @@ export class StratumV1JobsService { const target: number = mantissa * Math.pow(256, (exponent - 3)); // Calculate the target value - const difficulty: number = (Math.pow(2, 208) * 65535) / target; // Calculate the difficulty + const maxTarget = Math.pow(2, 208) * 65535; // Easiest target (max_target) + const difficulty: number = maxTarget / target; // Calculate the difficulty return difficulty; }