mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
batch save share statistics
This commit is contained in:
@@ -133,7 +133,7 @@ export class StratumV1Client {
|
|||||||
|
|
||||||
if (this.sessionStart == null) {
|
if (this.sessionStart == null) {
|
||||||
this.sessionStart = new Date();
|
this.sessionStart = new Date();
|
||||||
this.statistics = new StratumV1ClientStatistics(this.clientStatisticsService, this.clientService);
|
this.statistics = new StratumV1ClientStatistics(this.clientStatisticsService);
|
||||||
this.extraNonceAndSessionId = this.getRandomHexString();
|
this.extraNonceAndSessionId = this.getRandomHexString();
|
||||||
console.log(`New client ID: : ${this.extraNonceAndSessionId}, ${this.socket.remoteAddress}:${this.socket.remotePort}`);
|
console.log(`New client ID: : ${this.extraNonceAndSessionId}, ${this.socket.remoteAddress}:${this.socket.remotePort}`);
|
||||||
}
|
}
|
||||||
@@ -363,6 +363,7 @@ export class StratumV1Client {
|
|||||||
|
|
||||||
this.backgroundWork = setInterval(async () => {
|
this.backgroundWork = setInterval(async () => {
|
||||||
await this.checkDifficulty();
|
await this.checkDifficulty();
|
||||||
|
await this.statistics.saveShares(this.entity);
|
||||||
}, 60 * 1000);
|
}, 60 * 1000);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -505,7 +506,7 @@ export class StratumV1Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
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);
|
await this.clientService.heartbeat(this.entity.address, this.entity.clientName, this.entity.sessionId, this.hashRate);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
|
|||||||
@@ -1,23 +1,48 @@
|
|||||||
import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service';
|
import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service';
|
||||||
import { ClientEntity } from '../ORM/client/client.entity';
|
import { ClientEntity } from '../ORM/client/client.entity';
|
||||||
import { ClientService } from '../ORM/client/client.service';
|
|
||||||
|
|
||||||
const CACHE_SIZE = 30;
|
const CACHE_SIZE = 30;
|
||||||
const TARGET_SUBMISSION_PER_SECOND = 10;
|
const TARGET_SUBMISSION_PER_SECOND = 10;
|
||||||
const MIN_DIFF = 0.00001;
|
const MIN_DIFF = 0.00001;
|
||||||
export class StratumV1ClientStatistics {
|
export class StratumV1ClientStatistics {
|
||||||
|
|
||||||
|
private shareBacklog: number = 0;
|
||||||
|
|
||||||
private submissionCacheStart: Date;
|
private submissionCacheStart: Date;
|
||||||
private submissionCache = [];
|
private submissionCache = [];
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly clientStatisticsService: ClientStatisticsService,
|
private readonly clientStatisticsService: ClientStatisticsService
|
||||||
private readonly clientService: ClientService
|
|
||||||
) {
|
) {
|
||||||
this.submissionCacheStart = new Date();
|
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) {
|
if (this.submissionCache.length > CACHE_SIZE) {
|
||||||
this.submissionCache.shift();
|
this.submissionCache.shift();
|
||||||
@@ -28,23 +53,10 @@ export class StratumV1ClientStatistics {
|
|||||||
difficulty: targetDifficulty,
|
difficulty: targetDifficulty,
|
||||||
});
|
});
|
||||||
|
|
||||||
// 10 min
|
this.shareBacklog += targetDifficulty;
|
||||||
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
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
public getLastSubmissionTime(): Date | null {
|
|
||||||
return this.submissionCache[this.submissionCache.length - 1]?.time;
|
|
||||||
}
|
|
||||||
public getSuggestedDifficulty(clientDifficulty: number) {
|
public getSuggestedDifficulty(clientDifficulty: number) {
|
||||||
|
|
||||||
// miner hasn't submitted shares in one minute
|
// miner hasn't submitted shares in one minute
|
||||||
|
|||||||
Reference in New Issue
Block a user