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

View File

@ -189,47 +189,47 @@ export class ClientStatisticsService {
}
public async getHashRateForSession(clientId: string) {
// public async getHashRateForSession(clientId: string) {
const query = `
SELECT
"createdAt",
"updatedAt",
shares
FROM
client_statistics_entity AS entry
WHERE
entry."clientId" = $1
ORDER BY time DESC
LIMIT 2;
`;
// const query = `
// SELECT
// "createdAt",
// "updatedAt",
// shares
// FROM
// client_statistics_entity AS entry
// WHERE
// entry."clientId" = $1
// ORDER BY time DESC
// LIMIT 2;
// `;
const result = await this.clientStatisticsRepository.query(query, [clientId]);
// const result = await this.clientStatisticsRepository.query(query, [clientId]);
if (result.length < 1) {
return 0;
}
// if (result.length < 1) {
// return 0;
// }
const latestStat = result[0];
// const latestStat = result[0];
if (result.length < 2) {
const time = new Date(latestStat.updatedAt).getTime() - new Date(latestStat.createdAt).getTime();
// 1min
if (time < 1000 * 60) {
return 0;
}
return (parseFloat(latestStat.shares) * 4294967296) / (time / 1000);
} else {
const secondLatestStat = result[1];
const time = new Date(latestStat.updatedAt).getTime() - new Date(secondLatestStat.createdAt).getTime();
// 1min
if (time < 1000 * 60) {
return 0;
}
return ((parseFloat(latestStat.shares) + parseFloat(secondLatestStat.shares)) * 4294967296) / (time / 1000);
}
// if (result.length < 2) {
// const time = new Date(latestStat.updatedAt).getTime() - new Date(latestStat.createdAt).getTime();
// // 1min
// if (time < 1000 * 60) {
// return 0;
// }
// return (parseFloat(latestStat.shares) * 4294967296) / (time / 1000);
// } else {
// const secondLatestStat = result[1];
// const time = new Date(latestStat.updatedAt).getTime() - new Date(secondLatestStat.createdAt).getTime();
// // 1min
// if (time < 1000 * 60) {
// return 0;
// }
// return ((parseFloat(latestStat.shares) + parseFloat(secondLatestStat.shares)) * 4294967296) / (time / 1000);
// }
}
// }
public async getChartDataForSession(clientId: string) {
var yesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000));

View File

@ -48,8 +48,8 @@ export class StratumV1Client {
public extraNonceAndSessionId: string;
public sessionStart: Date;
public noFee: boolean;
public hashRate: number = 0;
//public noFee: boolean;
//public hashRate: number = 0;
private buffer: string = '';
@ -390,29 +390,31 @@ export class StratumV1Client {
private async sendNewMiningJob(jobTemplate: IJobTemplate) {
let payoutInformation;
const devFeeAddress = this.configService.get('DEV_FEE_ADDRESS');
//50Th/s
this.noFee = false;
if (this.clientEntity) {
this.hashRate = await this.clientStatisticsService.getHashRateForSession(this.clientEntity.id);
// 250Gh/s
if(this.hashRate < 250000000000){
this.statistics.targetSubmitShareEveryNSeconds = 10;
}
this.noFee = this.hashRate != 0 && this.hashRate < 50000000000000;
}
if (this.noFee || devFeeAddress == null || devFeeAddress.length < 1) {
payoutInformation = [
{ address: this.clientAuthorization.address, percent: 100 }
];
let payoutInformation= [
{ address: this.clientAuthorization.address, percent: 100 }
];
// const devFeeAddress = this.configService.get('DEV_FEE_ADDRESS');
// //50Th/s
// this.noFee = false;
// if (this.clientEntity) {
// this.hashRate = await this.clientStatisticsService.getHashRateForSession(this.clientEntity.id);
// // 250Gh/s
// if(this.hashRate < 250000000000){
// this.statistics.targetSubmitShareEveryNSeconds = 10;
// }
// this.noFee = this.hashRate != 0 && this.hashRate < 50000000000000;
// }
// if (this.noFee || devFeeAddress == null || devFeeAddress.length < 1) {
// payoutInformation = [
// { address: this.clientAuthorization.address, percent: 100 }
// ];
} else {
payoutInformation = [
{ address: devFeeAddress, percent: 1.5 },
{ address: this.clientAuthorization.address, percent: 98.5 }
];
}
// } else {
// payoutInformation = [
// { address: devFeeAddress, percent: 1.5 },
// { address: this.clientAuthorization.address, percent: 98.5 }
// ];
// }
const networkConfig = this.configService.get('NETWORK');
let network;
@ -546,7 +548,7 @@ export class StratumV1Client {
const now = new Date();
// only update every minute
//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;
//}

View File

@ -6,7 +6,12 @@ const MIN_DIFF = 0.001;
export class StratumV1ClientStatistics {
public targetSubmitShareEveryNSeconds: number = 30;
public hashRate = 0;
private previousTimeSlotTime: Date;
private currentTimeSlotTime: Date;
private previousShares: number = 0;
private shares: number = 0;
private acceptedCount: number = 0;
@ -42,6 +47,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++;
@ -63,6 +70,9 @@ export class StratumV1ClientStatistics {
shares: this.shares,
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
this.currentTimeSlot = timeSlot;
this.shares = targetDifficulty;
@ -88,8 +98,14 @@ export class StratumV1ClientStatistics {
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) {
// miner hasn't submitted shares in one minute