improvements

This commit is contained in:
Ben Wilson 2023-06-29 11:59:17 -04:00
parent 8878b31377
commit cef9114c83
4 changed files with 32 additions and 20 deletions

View File

@ -3,7 +3,7 @@ import { plainToInstance } from 'class-transformer';
import { validate, ValidatorOptions } from 'class-validator';
import * as crypto from 'crypto';
import { Socket } from 'net';
import { combineLatest, firstValueFrom, interval, startWith } from 'rxjs';
import { combineLatest, firstValueFrom, interval, startWith, takeUntil } from 'rxjs';
import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service';
import { ClientEntity } from '../ORM/client/client.entity';
@ -85,6 +85,7 @@ export class StratumV1Client extends EasyUnsubscribe {
parsedMessage = JSON.parse(message);
} catch (e) {
console.log(e);
this.socket.end();
}
switch (parsedMessage.method) {
@ -229,27 +230,29 @@ export class StratumV1Client extends EasyUnsubscribe {
let lastIntervalCount = undefined;
let skipNext = false;
combineLatest([this.blockTemplateService.currentBlockTemplate$, interval(60000).pipe(startWith(-1))]).subscribe(async ([{ blockTemplate }, interValCount]) => {
let clearJobs = false;
if (lastIntervalCount === interValCount) {
clearJobs = true;
skipNext = true;
console.log('new block')
}
combineLatest([this.blockTemplateService.currentBlockTemplate$, interval(60000).pipe(startWith(-1))])
.pipe(takeUntil(this.easyUnsubscribe))
.subscribe(async ([{ blockTemplate }, interValCount]) => {
let clearJobs = false;
if (lastIntervalCount === interValCount) {
clearJobs = true;
skipNext = true;
console.log('new block')
}
if (skipNext == true && clearJobs == false) {
skipNext = false;
return;
}
if (skipNext == true && clearJobs == false) {
skipNext = false;
return;
}
lastIntervalCount = interValCount;
lastIntervalCount = interValCount;
this.sendNewMiningJob(blockTemplate, clearJobs);
this.sendNewMiningJob(blockTemplate, clearJobs);
await this.checkDifficulty();
await this.checkDifficulty();
});
});
}
}

View File

@ -72,8 +72,16 @@ export class StratumV1ClientStatistics {
x = x | (x >> 32);
const res = x - (x >> 1);
if (res < 1) {
return 1;
//return this.blpo2(val * 100) / 100;
if (val < 0.01) {
return 0.01;
}
let y = (val * 100) | ((val * 100) >> 1);
y = y | (y >> 2);
y = y | (y >> 4);
y = y | (y >> 8);
y = y | (y >> 16);
y = y | (y >> 32);
return (y - (y >> 1)) / 100;
}
return res;
}

View File

@ -47,6 +47,7 @@ export class StratumV1Service implements OnModuleInit {
socket.on('end', async () => {
// Handle socket disconnection
client.destroy();
await this.clientService.delete(client.extraNonce);
const clientCount = await this.clientService.connectedClientCount();
@ -55,8 +56,8 @@ export class StratumV1Service implements OnModuleInit {
socket.on('error', async (error: Error) => {
client.destroy();
await this.clientService.delete(client.extraNonce);
const clientCount = await this.clientService.connectedClientCount();
console.error(`Socket error:`, error);
console.log(`Client disconnected: ${socket.remoteAddress}, ${clientCount} total clients`);

View File

@ -3,7 +3,7 @@ import { Subject } from 'rxjs';
export class EasyUnsubscribe {
protected easyUnsubscribe = new Subject<void>();
public unsubscribeAll(): void {
public destroy(): void {
this.easyUnsubscribe.next();
this.easyUnsubscribe.complete();
}