separate background tasks

This commit is contained in:
Ben 2023-12-24 17:54:40 -05:00
parent ab1e7bb8a0
commit d97dafe185
2 changed files with 52 additions and 43 deletions

View File

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

View File

@ -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;
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.backgroundWork = setInterval(async () => {
await this.checkDifficulty();
await this.statistics.saveShares(this.entity);
}, 60 * 1000);
await this.initStratum();
}
}
private async initStratum() {
this.stratumInitialized = true;
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.backgroundWork.push(
setInterval(async () => {
await this.checkDifficulty();
}, 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 }
];