delete old templates and jobs

This commit is contained in:
Benjamin Wilson 2025-02-10 00:01:55 -05:00
parent ddb7a89615
commit 0201703e35
3 changed files with 21 additions and 3 deletions

View File

@ -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);

View File

@ -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,

View File

@ -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;
}