From cef9114c83ee620da34aeeea9c5300c738a25f30 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Thu, 29 Jun 2023 11:59:17 -0400 Subject: [PATCH] improvements --- src/models/StratumV1Client.ts | 35 ++++++++++++++----------- src/models/StratumV1ClientStatistics.ts | 12 +++++++-- src/services/stratum-v1.service.ts | 3 ++- src/utils/AutoUnsubscribe.ts | 2 +- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index 6a3e865..f603301 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -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(); - }); + }); } } diff --git a/src/models/StratumV1ClientStatistics.ts b/src/models/StratumV1ClientStatistics.ts index 621123b..e18aaa8 100644 --- a/src/models/StratumV1ClientStatistics.ts +++ b/src/models/StratumV1ClientStatistics.ts @@ -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; } diff --git a/src/services/stratum-v1.service.ts b/src/services/stratum-v1.service.ts index 53125df..2c6bd76 100644 --- a/src/services/stratum-v1.service.ts +++ b/src/services/stratum-v1.service.ts @@ -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`); diff --git a/src/utils/AutoUnsubscribe.ts b/src/utils/AutoUnsubscribe.ts index 5a03c40..9020cb8 100644 --- a/src/utils/AutoUnsubscribe.ts +++ b/src/utils/AutoUnsubscribe.ts @@ -3,7 +3,7 @@ import { Subject } from 'rxjs'; export class EasyUnsubscribe { protected easyUnsubscribe = new Subject(); - public unsubscribeAll(): void { + public destroy(): void { this.easyUnsubscribe.next(); this.easyUnsubscribe.complete(); }