diff --git a/src/ORM/client/client.entity.ts b/src/ORM/client/client.entity.ts index 9e43168..ddaf54a 100644 --- a/src/ORM/client/client.entity.ts +++ b/src/ORM/client/client.entity.ts @@ -29,7 +29,8 @@ export class ClientEntity extends TrackedEntity { @Column({ type: 'real', default: 0 }) bestDifficulty: number - + @Column({ default: 0 }) + hashRate: number; } diff --git a/src/ORM/client/client.service.ts b/src/ORM/client/client.service.ts index e68899a..2f9926e 100644 --- a/src/ORM/client/client.service.ts +++ b/src/ORM/client/client.service.ts @@ -30,8 +30,8 @@ export class ClientService { .execute(); } - public async heartbeat(address: string, clientName: string, sessionId: string) { - return await this.clientRepository.update({ address, clientName, sessionId }, { deletedAt: null, updatedAt: new Date() }); + public async heartbeat(address: string, clientName: string, sessionId: string, hashRate: number) { + return await this.clientRepository.update({ address, clientName, sessionId }, { hashRate, deletedAt: null, updatedAt: new Date() }); } // public async save(client: Partial) { @@ -111,6 +111,7 @@ export class ClientService { .select('client.userAgent as userAgent') .addSelect('COUNT(client.userAgent)', 'count') .addSelect('MAX(client.bestDifficulty)', 'bestDifficulty') + .addSelect('SUM(client.hashRate)', 'totalHashRate') .groupBy('client.userAgent') .orderBy('count', 'DESC') .getRawMany(); diff --git a/src/controllers/client/client.controller.ts b/src/controllers/client/client.controller.ts index 4d10f74..7a3d2c2 100644 --- a/src/controllers/client/client.controller.ts +++ b/src/controllers/client/client.controller.ts @@ -31,8 +31,9 @@ export class ClientController { sessionId: worker.sessionId, name: worker.clientName, bestDifficulty: worker.bestDifficulty.toFixed(2), - hashRate: (await this.clientStatisticsService.getHashRateForSession(worker.address, worker.clientName, worker.sessionId)).toFixed(2), - startTime: worker.startTime + hashRate: worker.hashRate, + startTime: worker.startTime, + lastSeen: worker.updatedAt }; }) ) diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index 6e3410a..1bfbcf6 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -44,8 +44,9 @@ export class StratumV1Client { private entity: ClientEntity; public extraNonceAndSessionId: string; - public sessionStart: Date; + public noFee: boolean; + public hashRate: number; constructor( @@ -326,7 +327,6 @@ export class StratumV1Client { } - this.entity = await this.clientService.insert({ sessionId: this.extraNonceAndSessionId, address: this.clientAuthorization.address, @@ -348,35 +348,20 @@ export class StratumV1Client { this.backgroundWork = setInterval(async () => { await this.checkDifficulty(); - //await this.watchdog(); }, 60 * 1000); } } - // private async watchdog() { - // let time = await this.statistics.getLastSubmissionTime(); - // if (time == null) { - // time = this.sessionStart; - // } - // const now = Date.now(); - // const diffSeconds = (now - time.getTime()) / 1000; - // // one hour - // if (diffSeconds > 60 * 60) { - // console.log(`Watchdog ending session ${this.extraNonceAndSessionId}}`); - // await this.promiseSocket.end(); - // } - // } - private async sendNewMiningJob(jobTemplate: IJobTemplate) { - const hashRate = await this.clientStatisticsService.getHashRateForSession(this.clientAuthorization.address, this.clientAuthorization.worker, this.extraNonceAndSessionId); + this.hashRate = await this.clientStatisticsService.getHashRateForSession(this.clientAuthorization.address, this.clientAuthorization.worker, this.extraNonceAndSessionId); let payoutInformation; const devFeeAddress = this.configService.get('DEV_FEE_ADDRESS'); //50Th/s - const noFee = hashRate != 0 && hashRate < 50000000000000; - if (noFee || devFeeAddress == null || devFeeAddress.length < 1) { + this.noFee = this.hashRate != 0 && this.hashRate < 50000000000000; + if (this.noFee || devFeeAddress == null || devFeeAddress.length < 1) { payoutInformation = [ { address: this.clientAuthorization.address, percent: 100 } ]; @@ -405,7 +390,7 @@ export class StratumV1Client { } - console.log(`Sent new job to ${this.clientAuthorization.worker}.${this.extraNonceAndSessionId}. (clearJobs: ${jobTemplate.blockData.clearJobs}, fee?: ${!noFee})`) + console.log(`Sent new job to ${this.clientAuthorization.worker}.${this.extraNonceAndSessionId}. (clearJobs: ${jobTemplate.blockData.clearJobs}, fee?: ${!this.noFee})`) } @@ -465,7 +450,7 @@ export class StratumV1Client { } try { await this.statistics.addSubmission(this.entity, submissionHash, this.sessionDifficulty); - //await this.addressSettingsService.addShares(this.clientAuthorization.address, this.sessionDifficulty); + await this.clientService.heartbeat(this.entity.address, this.entity.clientName, this.entity.sessionId, this.hashRate); } catch (e) { console.log(e); const err = new StratumErrorMessage( diff --git a/src/models/StratumV1ClientStatistics.ts b/src/models/StratumV1ClientStatistics.ts index 83012f4..f0f3c5e 100644 --- a/src/models/StratumV1ClientStatistics.ts +++ b/src/models/StratumV1ClientStatistics.ts @@ -42,9 +42,6 @@ export class StratumV1ClientStatistics { submissionHash }); - await this.clientService.heartbeat(client.address, client.clientName, client.sessionId) - - } public getLastSubmissionTime(): Date | null { return this.submissionCache[this.submissionCache.length - 1]?.time;