From fd775965e5f90f0814302edee8690839af67a603 Mon Sep 17 00:00:00 2001 From: KillerInk <30864229+KillerInk@users.noreply.github.com> Date: Fri, 4 Apr 2025 19:40:02 +0200 Subject: [PATCH] cherry pick hashrate fix from pqsql branch (#102) --- src/models/StratumV1Client.ts | 2 +- src/models/StratumV1ClientStatistics.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index d3cbc24..0cb2207 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -390,7 +390,7 @@ export class StratumV1Client { //50Th/s this.noFee = false; if (this.entity) { - this.hashRate = await this.clientStatisticsService.getHashRateForSession(this.clientAuthorization.address, this.clientAuthorization.worker, this.extraNonceAndSessionId); + this.hashRate = this.statistics.hashRate; this.noFee = this.hashRate != 0 && this.hashRate < 50000000000000; } if (this.noFee || devFeeAddress == null || devFeeAddress.length < 1) { diff --git a/src/models/StratumV1ClientStatistics.ts b/src/models/StratumV1ClientStatistics.ts index c5c5fc9..d00410a 100644 --- a/src/models/StratumV1ClientStatistics.ts +++ b/src/models/StratumV1ClientStatistics.ts @@ -14,6 +14,13 @@ export class StratumV1ClientStatistics { private currentTimeSlot: number = null; private lastSave: number = null; + + public hashRate = 0; + + private previousTimeSlotTime: Date; + private currentTimeSlotTime: Date; + + private previousShares: number = 0; constructor( private readonly clientStatisticsService: ClientStatisticsService @@ -42,6 +49,8 @@ export class StratumV1ClientStatistics { if (this.currentTimeSlot == null) { // First record, insert it + this.previousTimeSlotTime = new Date(); + this.currentTimeSlotTime = new Date(); this.currentTimeSlot = timeSlot; this.shares += targetDifficulty; this.acceptedCount++; @@ -65,6 +74,9 @@ export class StratumV1ClientStatistics { clientName: client.clientName, sessionId: client.sessionId }); + this.previousShares = this.shares; + this.previousTimeSlotTime = this.currentTimeSlotTime; + this.currentTimeSlotTime = new Date(); // Set the new time slot and add incoming shares then insert it this.currentTimeSlot = timeSlot; this.shares = targetDifficulty; @@ -96,6 +108,10 @@ export class StratumV1ClientStatistics { // saving to memory for storing later this.shares += targetDifficulty; this.acceptedCount++; + if(this.shares > 0) { + const time = new Date().getTime() - this.previousTimeSlotTime.getTime(); + this.hashRate = ((this.previousShares + this.shares) * 4294967296) / (time / 1000); + } } }