Skip to content

Commit

Permalink
separate background tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-wilson committed Dec 24, 2023
1 parent ab1e7bb commit d97dafe
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/models/MiningJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
77 changes: 43 additions & 34 deletions src/models/StratumV1Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 }
];
Expand Down

0 comments on commit d97dafe

Please sign in to comment.