From b1cfe07222d56aa82eca0fc9f774326a9909b99e Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Mon, 7 Aug 2023 23:12:52 -0400 Subject: [PATCH] optimizations and cleanup --- src/models/StratumV1Client.ts | 6 +++--- src/services/stratum-v1-jobs.service.ts | 9 +++++++-- src/services/stratum-v1.service.ts | 15 +++++---------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index cdf96a4..785af6a 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -115,7 +115,7 @@ export class StratumV1Client { this.sessionStart = new Date(); this.statistics = new StratumV1ClientStatistics(this.clientStatisticsService, this.clientService); this.extraNonceAndSessionId = this.getRandomHexString(); - console.log(`New client ID: : ${this.extraNonceAndSessionId}`); + console.log(`New client ID: : ${this.extraNonceAndSessionId}, ${this.socket.remoteAddress}:${this.socket.remotePort}`); } switch (parsedMessage.method) { @@ -331,7 +331,7 @@ export class StratumV1Client { if (this.clientSuggestedDifficulty == null) { - console.log(`Setting difficulty to ${this.sessionDifficulty}`) + //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) { @@ -521,7 +521,7 @@ export class StratumV1Client { } if (targetDiff != this.sessionDifficulty) { - console.log(`Adjusting difficulty from ${this.sessionDifficulty} to ${targetDiff}`); + console.log(`Adjusting ${this.extraNonceAndSessionId} difficulty from ${this.sessionDifficulty} to ${targetDiff}`); this.sessionDifficulty = targetDiff; const data = JSON.stringify({ diff --git a/src/services/stratum-v1-jobs.service.ts b/src/services/stratum-v1-jobs.service.ts index 041d5d9..f121eaf 100644 --- a/src/services/stratum-v1-jobs.service.ts +++ b/src/services/stratum-v1-jobs.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common'; import * as bitcoinjs from 'bitcoinjs-lib'; import * as merkle from 'merkle-lib'; import * as merkleProof from 'merkle-lib/proof'; -import { combineLatest, filter, from, interval, map, Observable, shareReplay, startWith, switchMap, tap } from 'rxjs'; +import { combineLatest, delay, filter, from, interval, map, Observable, shareReplay, startWith, switchMap, tap } from 'rxjs'; import { MiningJob } from '../models/MiningJob'; import { BitcoinRpcService } from './bitcoin-rpc.service'; @@ -35,11 +35,16 @@ export class StratumV1JobsService { public blocks: { [id: number]: IJobTemplate } = {}; + // offset the interval so that all the cluster processes don't try and refresh at the same time. + private delay = process.env.NODE_APP_INSTANCE == null ? 0 : parseInt(process.env.NODE_APP_INSTANCE) * 5000; + constructor( private readonly bitcoinRpcService: BitcoinRpcService ) { - this.newMiningJob$ = combineLatest([this.bitcoinRpcService.newBlock$, interval(60000).pipe(startWith(-1))]).pipe( + console.log(this.delay) + + this.newMiningJob$ = combineLatest([this.bitcoinRpcService.newBlock$, interval(60000).pipe(delay(this.delay), startWith(-1))]).pipe( switchMap(([miningInfo, interval]) => { return from(this.bitcoinRpcService.getBlockTemplate()).pipe(map((blockTemplate) => { return { diff --git a/src/services/stratum-v1.service.ts b/src/services/stratum-v1.service.ts index 0a27779..b7320e0 100644 --- a/src/services/stratum-v1.service.ts +++ b/src/services/stratum-v1.service.ts @@ -57,23 +57,18 @@ export class StratumV1Service implements OnModuleInit { ); - socket.on('end', async (error: Error) => { + socket.on('close', async (hadError: boolean) => { if (client.extraNonceAndSessionId != null) { // Handle socket disconnection await client.destroy(); - console.log(`Client disconnected, ${client.extraNonceAndSessionId}`); + console.log(`Client ${client.extraNonceAndSessionId} disconnected, hadError?:${hadError}`); } - }); - socket.on('error', async (error: Error) => { - if (client.extraNonceAndSessionId != null) { - await client.destroy(); - console.log(`Client disconnected, socket error, ${client.extraNonceAndSessionId}`); - } + socket.on('error', async (error: Error) => { }); + + // //console.log(`Client disconnected, socket error, ${client.extraNonceAndSessionId}`); - socket.destroy(); - }); });