refactor hashrate calculation

This commit is contained in:
Benjamin Wilson
2025-02-27 12:10:43 -05:00
parent 17881f70d6
commit 9b5402f1b0
3 changed files with 78 additions and 60 deletions
@@ -189,47 +189,47 @@ export class ClientStatisticsService {
} }
public async getHashRateForSession(clientId: string) { // public async getHashRateForSession(clientId: string) {
const query = ` // const query = `
SELECT // SELECT
"createdAt", // "createdAt",
"updatedAt", // "updatedAt",
shares // shares
FROM // FROM
client_statistics_entity AS entry // client_statistics_entity AS entry
WHERE // WHERE
entry."clientId" = $1 // entry."clientId" = $1
ORDER BY time DESC // ORDER BY time DESC
LIMIT 2; // LIMIT 2;
`; // `;
const result = await this.clientStatisticsRepository.query(query, [clientId]); // const result = await this.clientStatisticsRepository.query(query, [clientId]);
if (result.length < 1) { // if (result.length < 1) {
return 0; // return 0;
} // }
const latestStat = result[0]; // const latestStat = result[0];
if (result.length < 2) { // if (result.length < 2) {
const time = new Date(latestStat.updatedAt).getTime() - new Date(latestStat.createdAt).getTime(); // const time = new Date(latestStat.updatedAt).getTime() - new Date(latestStat.createdAt).getTime();
// 1min // // 1min
if (time < 1000 * 60) { // if (time < 1000 * 60) {
return 0; // return 0;
} // }
return (parseFloat(latestStat.shares) * 4294967296) / (time / 1000); // return (parseFloat(latestStat.shares) * 4294967296) / (time / 1000);
} else { // } else {
const secondLatestStat = result[1]; // const secondLatestStat = result[1];
const time = new Date(latestStat.updatedAt).getTime() - new Date(secondLatestStat.createdAt).getTime(); // const time = new Date(latestStat.updatedAt).getTime() - new Date(secondLatestStat.createdAt).getTime();
// 1min // // 1min
if (time < 1000 * 60) { // if (time < 1000 * 60) {
return 0; // return 0;
} // }
return ((parseFloat(latestStat.shares) + parseFloat(secondLatestStat.shares)) * 4294967296) / (time / 1000); // return ((parseFloat(latestStat.shares) + parseFloat(secondLatestStat.shares)) * 4294967296) / (time / 1000);
} // }
} // }
public async getChartDataForSession(clientId: string) { public async getChartDataForSession(clientId: string) {
var yesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000)); var yesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000));
+27 -25
View File
@@ -48,8 +48,8 @@ export class StratumV1Client {
public extraNonceAndSessionId: string; public extraNonceAndSessionId: string;
public sessionStart: Date; public sessionStart: Date;
public noFee: boolean; //public noFee: boolean;
public hashRate: number = 0; //public hashRate: number = 0;
private buffer: string = ''; private buffer: string = '';
@@ -390,29 +390,31 @@ export class StratumV1Client {
private async sendNewMiningJob(jobTemplate: IJobTemplate) { private async sendNewMiningJob(jobTemplate: IJobTemplate) {
let payoutInformation; let payoutInformation= [
const devFeeAddress = this.configService.get('DEV_FEE_ADDRESS'); { address: this.clientAuthorization.address, percent: 100 }
//50Th/s ];
this.noFee = false; // const devFeeAddress = this.configService.get('DEV_FEE_ADDRESS');
if (this.clientEntity) { // //50Th/s
this.hashRate = await this.clientStatisticsService.getHashRateForSession(this.clientEntity.id); // this.noFee = false;
// 250Gh/s // if (this.clientEntity) {
if(this.hashRate < 250000000000){ // this.hashRate = await this.clientStatisticsService.getHashRateForSession(this.clientEntity.id);
this.statistics.targetSubmitShareEveryNSeconds = 10; // // 250Gh/s
} // if(this.hashRate < 250000000000){
this.noFee = this.hashRate != 0 && this.hashRate < 50000000000000; // this.statistics.targetSubmitShareEveryNSeconds = 10;
} // }
if (this.noFee || devFeeAddress == null || devFeeAddress.length < 1) { // this.noFee = this.hashRate != 0 && this.hashRate < 50000000000000;
payoutInformation = [ // }
{ address: this.clientAuthorization.address, percent: 100 } // if (this.noFee || devFeeAddress == null || devFeeAddress.length < 1) {
]; // payoutInformation = [
// { address: this.clientAuthorization.address, percent: 100 }
// ];
} else { // } else {
payoutInformation = [ // payoutInformation = [
{ address: devFeeAddress, percent: 1.5 }, // { address: devFeeAddress, percent: 1.5 },
{ address: this.clientAuthorization.address, percent: 98.5 } // { address: this.clientAuthorization.address, percent: 98.5 }
]; // ];
} // }
const networkConfig = this.configService.get('NETWORK'); const networkConfig = this.configService.get('NETWORK');
let network; let network;
@@ -546,7 +548,7 @@ export class StratumV1Client {
const now = new Date(); const now = new Date();
// only update every minute // only update every minute
//if (this.clientEntity.updatedAt == null || now.getTime() - this.clientEntity.updatedAt.getTime() > 1000 * 60) { //if (this.clientEntity.updatedAt == null || now.getTime() - this.clientEntity.updatedAt.getTime() > 1000 * 60) {
await this.clientService.heartbeatBulkAsync(this.clientEntity.id, this.hashRate, now); await this.clientService.heartbeatBulkAsync(this.clientEntity.id, this.statistics.hashRate, now);
this.clientEntity.updatedAt = now; this.clientEntity.updatedAt = now;
//} //}
+16
View File
@@ -6,7 +6,12 @@ const MIN_DIFF = 0.001;
export class StratumV1ClientStatistics { export class StratumV1ClientStatistics {
public targetSubmitShareEveryNSeconds: number = 30; public targetSubmitShareEveryNSeconds: number = 30;
public hashRate = 0;
private previousTimeSlotTime: Date;
private currentTimeSlotTime: Date;
private previousShares: number = 0;
private shares: number = 0; private shares: number = 0;
private acceptedCount: number = 0; private acceptedCount: number = 0;
@@ -42,6 +47,8 @@ export class StratumV1ClientStatistics {
if (this.currentTimeSlot == null) { if (this.currentTimeSlot == null) {
// First record, insert it // First record, insert it
this.previousTimeSlotTime = new Date();
this.currentTimeSlotTime = new Date();
this.currentTimeSlot = timeSlot; this.currentTimeSlot = timeSlot;
this.shares += targetDifficulty; this.shares += targetDifficulty;
this.acceptedCount++; this.acceptedCount++;
@@ -63,6 +70,9 @@ export class StratumV1ClientStatistics {
shares: this.shares, shares: this.shares,
acceptedCount: this.acceptedCount, acceptedCount: this.acceptedCount,
}); });
this.previousShares = this.shares;
this.previousTimeSlotTime = this.currentTimeSlotTime;
this.currentTimeSlotTime = new Date();
// Set the new time slot and add incoming shares then insert it // Set the new time slot and add incoming shares then insert it
this.currentTimeSlot = timeSlot; this.currentTimeSlot = timeSlot;
this.shares = targetDifficulty; this.shares = targetDifficulty;
@@ -88,8 +98,14 @@ export class StratumV1ClientStatistics {
acceptedCount: this.acceptedCount, acceptedCount: this.acceptedCount,
}); });
} }
if(this.shares > 0) {
const time = new Date().getTime() - this.previousTimeSlotTime.getTime();
this.hashRate = ((this.previousShares + this.shares) * 4294967296) / (time / 1000);
}
} }
public getSuggestedDifficulty(clientDifficulty: number) { public getSuggestedDifficulty(clientDifficulty: number) {
// miner hasn't submitted shares in one minute // miner hasn't submitted shares in one minute