batch save share statistics

This commit is contained in:
Ben Wilson 2023-12-15 00:25:01 -05:00 committed by Ben
parent 76285ba728
commit ab1e7bb8a0
2 changed files with 34 additions and 21 deletions

View File

@ -133,7 +133,7 @@ export class StratumV1Client {
if (this.sessionStart == null) {
this.sessionStart = new Date();
this.statistics = new StratumV1ClientStatistics(this.clientStatisticsService, this.clientService);
this.statistics = new StratumV1ClientStatistics(this.clientStatisticsService);
this.extraNonceAndSessionId = this.getRandomHexString();
console.log(`New client ID: : ${this.extraNonceAndSessionId}, ${this.socket.remoteAddress}:${this.socket.remotePort}`);
}
@ -363,6 +363,7 @@ export class StratumV1Client {
this.backgroundWork = setInterval(async () => {
await this.checkDifficulty();
await this.statistics.saveShares(this.entity);
}, 60 * 1000);
}
@ -505,7 +506,7 @@ export class StratumV1Client {
}
}
try {
await this.statistics.addSubmission(this.entity, this.sessionDifficulty);
await this.statistics.addShares(this.sessionDifficulty);
await this.clientService.heartbeat(this.entity.address, this.entity.clientName, this.entity.sessionId, this.hashRate);
} catch (e) {
console.log(e);

View File

@ -1,23 +1,48 @@
import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service';
import { ClientEntity } from '../ORM/client/client.entity';
import { ClientService } from '../ORM/client/client.service';
const CACHE_SIZE = 30;
const TARGET_SUBMISSION_PER_SECOND = 10;
const MIN_DIFF = 0.00001;
export class StratumV1ClientStatistics {
private shareBacklog: number = 0;
private submissionCacheStart: Date;
private submissionCache = [];
constructor(
private readonly clientStatisticsService: ClientStatisticsService,
private readonly clientService: ClientService
private readonly clientStatisticsService: ClientStatisticsService
) {
this.submissionCacheStart = new Date();
}
public async addSubmission(client: ClientEntity, targetDifficulty: number) {
public async saveShares(client: ClientEntity) {
if (client == null || client.address == null || client.clientName == null || client.sessionId == null) {
return;
}
// 10 min
var coeff = 1000 * 60 * 10;
var date = new Date();
var rounded = new Date(Math.floor(date.getTime() / coeff) * coeff);
await this.clientStatisticsService.save({
time: rounded.getTime(),
shares: this.shareBacklog,
address: client.address,
clientName: client.clientName,
sessionId: client.sessionId
});
this.shareBacklog = 0;
}
// We don't want to save them here because it can be DB intensive, stead do it every once in
// awhile with saveShares()
public async addShares(targetDifficulty: number) {
if (this.submissionCache.length > CACHE_SIZE) {
this.submissionCache.shift();
@ -28,23 +53,10 @@ export class StratumV1ClientStatistics {
difficulty: targetDifficulty,
});
// 10 min
var coeff = 1000 * 60 * 10;
var date = new Date();
var rounded = new Date(Math.floor(date.getTime() / coeff) * coeff);
await this.clientStatisticsService.save({
time: rounded.getTime(),
shares: targetDifficulty,
address: client.address,
clientName: client.clientName,
sessionId: client.sessionId
});
this.shareBacklog += targetDifficulty;
}
public getLastSubmissionTime(): Date | null {
return this.submissionCache[this.submissionCache.length - 1]?.time;
}
public getSuggestedDifficulty(clientDifficulty: number) {
// miner hasn't submitted shares in one minute