From d97dafe1850e5c4c247c264bd8a75ebe018e3958 Mon Sep 17 00:00:00 2001 From: Ben Date: Sun, 24 Dec 2023 17:54:40 -0500 Subject: [PATCH] separate background tasks --- src/models/MiningJob.ts | 4 +- src/models/StratumV1Client.ts | 77 +++++++++++++++++++---------------- 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/src/models/MiningJob.ts b/src/models/MiningJob.ts index 02bccf0..775842e 100644 --- a/src/models/MiningJob.ts +++ b/src/models/MiningJob.ts @@ -27,9 +27,9 @@ export class MiningJob { jobTemplate: IJobTemplate ) { - this.jobTemplateId = jobTemplate.blockData.id, + this.jobTemplateId = jobTemplate.blockData.id; - this.coinbaseTransaction = this.createCoinbaseTransaction(payoutInformation, jobTemplate.blockData.coinbasevalue); + this.coinbaseTransaction = this.createCoinbaseTransaction(payoutInformation, jobTemplate.blockData.coinbasevalue); //The commitment is recorded in a scriptPubKey of the coinbase transaction. It must be at least 38 bytes, with the first 6-byte of 0x6a24aa21a9ed, that is: // 1-byte - OP_RETURN (0x6a) diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index 8619bb7..52d0ce2 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -6,6 +6,7 @@ import { validate, ValidatorOptions } from 'class-validator'; import * as crypto from 'crypto'; import { Socket } from 'net'; import { firstValueFrom, Subscription } from 'rxjs'; +import { clearInterval } from 'timers'; import { AddressSettingsService } from '../ORM/address-settings/address-settings.service'; import { BlocksService } from '../ORM/blocks/blocks.service'; @@ -35,7 +36,7 @@ export class StratumV1Client { private clientAuthorization: AuthorizationMessage; private clientSuggestedDifficulty: SuggestDifficulty; private stratumSubscription: Subscription; - private backgroundWork: NodeJS.Timer; + private backgroundWork: NodeJS.Timer[] = []; private statistics: StratumV1ClientStatistics; private stratumInitialized = false; @@ -87,9 +88,10 @@ export class StratumV1Client { if (this.stratumSubscription != null) { this.stratumSubscription.unsubscribe(); } - if (this.backgroundWork != null) { - clearInterval(this.backgroundWork); - } + + this.backgroundWork.forEach(work => { + clearInterval(work); + }); } private getRandomHexString() { @@ -333,55 +335,62 @@ export class StratumV1Client { && this.clientAuthorization != null && this.stratumInitialized == false) { - this.stratumInitialized = true; + await this.initStratum(); - switch (this.clientSubscription.userAgent) { - case 'cpuminer': { - this.sessionDifficulty = 0.1; - } - } + } + } + private async initStratum() { + this.stratumInitialized = true; - if (this.clientSuggestedDifficulty == null) { - //console.log(`Setting difficulty to ${this.sessionDifficulty}`) - const setDifficulty = JSON.stringify(new SuggestDifficulty().response(this.sessionDifficulty)); - const success = await this.write(setDifficulty + '\n'); - if (!success) { - return; - } + switch (this.clientSubscription.userAgent) { + case 'cpuminer': { + this.sessionDifficulty = 0.1; } + } + if (this.clientSuggestedDifficulty == null) { + //console.log(`Setting difficulty to ${this.sessionDifficulty}`) + const setDifficulty = JSON.stringify(new SuggestDifficulty().response(this.sessionDifficulty)); + const success = await this.write(setDifficulty + '\n'); + if (!success) { + return; + } + } - this.stratumSubscription = this.stratumV1JobsService.newMiningJob$.subscribe(async (jobTemplate) => { - try { - await this.sendNewMiningJob(jobTemplate); - } catch (e) { - await this.socket.end(); - console.error(e); - } - }); + this.stratumSubscription = this.stratumV1JobsService.newMiningJob$.subscribe(async (jobTemplate) => { + try { + await this.sendNewMiningJob(jobTemplate); + } catch (e) { + await this.socket.end(); + console.error(e); + } + }); - this.backgroundWork = setInterval(async () => { + this.backgroundWork.push( + setInterval(async () => { await this.checkDifficulty(); - await this.statistics.saveShares(this.entity); - }, 60 * 1000); + }, 60 * 1000) + ); - } + this.backgroundWork.push( + setInterval(async () => { + await this.statistics.saveShares(this.entity); + }, 60 * 1000) + ); } private async sendNewMiningJob(jobTemplate: IJobTemplate) { - - let payoutInformation; const devFeeAddress = this.configService.get('DEV_FEE_ADDRESS'); //50Th/s - this.noFee = false; - if (this.entity) { + this.noFee = devFeeAddress == null || devFeeAddress.length < 1; + if (this.noFee == false && this.entity) { this.hashRate = await this.clientStatisticsService.getHashRateForSession(this.clientAuthorization.address, this.clientAuthorization.worker, this.extraNonceAndSessionId); this.noFee = this.hashRate != 0 && this.hashRate < 50000000000000; } - if (this.noFee || devFeeAddress == null || devFeeAddress.length < 1) { + if (this.noFee) { payoutInformation = [ { address: this.clientAuthorization.address, percent: 100 } ];