address best diff

This commit is contained in:
Ben Wilson
2023-07-24 22:23:29 -04:00
parent 8369e9eef2
commit a4b4e517cb
7 changed files with 48 additions and 16 deletions
@@ -8,6 +8,9 @@ export class AddressSettingsEntity extends TrackedEntity {
@PrimaryColumn({ length: 62, type: 'varchar' }) @PrimaryColumn({ length: 62, type: 'varchar' })
address: string; address: string;
@Column({ type: 'real', default: 0 })
bestDifficulty: number
@Column() @Column()
miscCoinbaseScriptData: string; miscCoinbaseScriptData: string;
@@ -14,4 +14,15 @@ export class AddressSettingsService {
} }
public async getSettings(address: string) {
return await this.addressSettingsRepository.findOne({ where: { address } });
}
public async updateBestDifficulty(address: string, bestDifficulty: number) {
return await this.addressSettingsRepository.update({ address }, { bestDifficulty });
}
public async createNew(address: string) {
return await this.addressSettingsRepository.save({ address });
}
} }
-8
View File
@@ -45,14 +45,6 @@ export class ClientService {
return await this.clientRepository.count(); return await this.clientRepository.count();
} }
public async getBestDiff(address: string) {
const res = await this.clientRepository.createQueryBuilder('client')
.select('MAX(client.bestDifficulty)', 'bestDifficulty')
.where('client.address = :address', { address })
.withDeleted()
.getRawOne();
return res?.bestDifficulty;
}
public async getByAddress(address: string): Promise<ClientEntity[]> { public async getByAddress(address: string): Promise<ClientEntity[]> {
return await this.clientRepository.find({ return await this.clientRepository.find({
where: { where: {
+7 -3
View File
@@ -1,5 +1,6 @@
import { Controller, Get, NotFoundException, Param } from '@nestjs/common'; import { Controller, Get, NotFoundException, Param } from '@nestjs/common';
import { AddressSettingsService } from '../../ORM/address-settings/address-settings.service';
import { ClientStatisticsService } from '../../ORM/client-statistics/client-statistics.service'; import { ClientStatisticsService } from '../../ORM/client-statistics/client-statistics.service';
import { ClientService } from '../../ORM/client/client.service'; import { ClientService } from '../../ORM/client/client.service';
@@ -9,7 +10,8 @@ export class ClientController {
constructor( constructor(
private readonly clientService: ClientService, private readonly clientService: ClientService,
private readonly clientStatisticsService: ClientStatisticsService private readonly clientStatisticsService: ClientStatisticsService,
private readonly addressSettingsService: AddressSettingsService
) { } ) { }
@@ -20,10 +22,12 @@ export class ClientController {
const chartData = await this.clientStatisticsService.getChartDataForAddress(address); const chartData = await this.clientStatisticsService.getChartDataForAddress(address);
const bestDifficulty = await this.clientService.getBestDiff(address); const addressSettings = await this.addressSettingsService.getSettings(address);
return { return {
bestDifficulty, bestDifficulty: addressSettings?.bestDifficulty,
workersCount: workers.length, workersCount: workers.length,
workers: await Promise.all( workers: await Promise.all(
workers.map(async (worker) => { workers.map(async (worker) => {
+8 -2
View File
@@ -6,6 +6,8 @@ import { BehaviorSubject } from 'rxjs';
import { DataSource } from 'typeorm'; import { DataSource } from 'typeorm';
import { MockRecording1 } from '../../test/models/MockRecording1'; import { MockRecording1 } from '../../test/models/MockRecording1';
import { AddressSettingsModule } from '../ORM/address-settings/address-settings.module';
import { AddressSettingsService } from '../ORM/address-settings/address-settings.service';
import { BlocksService } from '../ORM/blocks/blocks.service'; import { BlocksService } from '../ORM/blocks/blocks.service';
import { ClientStatisticsEntity } from '../ORM/client-statistics/client-statistics.entity'; import { ClientStatisticsEntity } from '../ORM/client-statistics/client-statistics.entity';
import { ClientStatisticsModule } from '../ORM/client-statistics/client-statistics.module'; import { ClientStatisticsModule } from '../ORM/client-statistics/client-statistics.module';
@@ -65,7 +67,8 @@ describe('StratumV1Client', () => {
logging: false logging: false
}), }),
ClientModule, ClientModule,
ClientStatisticsModule ClientStatisticsModule,
AddressSettingsModule
], ],
providers: [ providers: [
{ {
@@ -119,6 +122,8 @@ describe('StratumV1Client', () => {
promiseSocket.end = jest.fn(); promiseSocket.end = jest.fn();
const addressSettings = moduleRef.get<AddressSettingsService>(AddressSettingsService);
client = new StratumV1Client( client = new StratumV1Client(
promiseSocket, promiseSocket,
@@ -128,7 +133,8 @@ describe('StratumV1Client', () => {
clientStatisticsService, clientStatisticsService,
notificationService, notificationService,
blocksService, blocksService,
configService configService,
addressSettings
); );
client.extraNonceAndSessionId = MockRecording1.EXTRA_NONCE; client.extraNonceAndSessionId = MockRecording1.EXTRA_NONCE;
+14 -1
View File
@@ -8,6 +8,8 @@ import { Socket } from 'net';
import PromiseSocket from 'promise-socket'; import PromiseSocket from 'promise-socket';
import { firstValueFrom, takeUntil } from 'rxjs'; import { firstValueFrom, takeUntil } from 'rxjs';
import { AddressSettingsEntity } from '../ORM/address-settings/address-settings.entity';
import { AddressSettingsService } from '../ORM/address-settings/address-settings.service';
import { BlocksService } from '../ORM/blocks/blocks.service'; import { BlocksService } from '../ORM/blocks/blocks.service';
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';
@@ -41,6 +43,7 @@ export class StratumV1Client extends EasyUnsubscribe {
private usedSuggestedDifficulty = false; private usedSuggestedDifficulty = false;
private sessionDifficulty: number = 16384; private sessionDifficulty: number = 16384;
private entity: ClientEntity; private entity: ClientEntity;
private addressSettings: AddressSettingsEntity;
public extraNonceAndSessionId: string; public extraNonceAndSessionId: string;
@@ -54,7 +57,8 @@ export class StratumV1Client extends EasyUnsubscribe {
private readonly clientStatisticsService: ClientStatisticsService, private readonly clientStatisticsService: ClientStatisticsService,
private readonly notificationService: NotificationService, private readonly notificationService: NotificationService,
private readonly blocksService: BlocksService, private readonly blocksService: BlocksService,
private readonly configService: ConfigService private readonly configService: ConfigService,
private readonly addressSettingsService: AddressSettingsService
) { ) {
super(); super();
@@ -259,6 +263,11 @@ export class StratumV1Client extends EasyUnsubscribe {
this.stratumInitialized = true; this.stratumInitialized = true;
this.addressSettings = await this.addressSettingsService.getSettings(this.clientAuthorization.address);
if (this.addressSettings == null) {
this.addressSettingsService.createNew(this.clientAuthorization.address);
}
if (this.clientSuggestedDifficulty == null) { if (this.clientSuggestedDifficulty == null) {
console.log(`Setting difficulty to ${this.sessionDifficulty}`) console.log(`Setting difficulty to ${this.sessionDifficulty}`)
const setDifficulty = JSON.stringify(new SuggestDifficulty().response(this.sessionDifficulty)); const setDifficulty = JSON.stringify(new SuggestDifficulty().response(this.sessionDifficulty));
@@ -402,6 +411,10 @@ export class StratumV1Client extends EasyUnsubscribe {
this.entity.bestDifficulty = submissionDifficulty; this.entity.bestDifficulty = submissionDifficulty;
} }
if (submissionDifficulty > this.addressSettings.bestDifficulty) {
await this.addressSettingsService.updateBestDifficulty(this.extraNonceAndSessionId, submissionDifficulty);
}
} else { } else {
const err = new StratumErrorMessage( const err = new StratumErrorMessage(
submission.id, submission.id,
+5 -2
View File
@@ -4,6 +4,7 @@ import { Server, Socket } from 'net';
import { PromiseSocket } from 'promise-socket'; import { PromiseSocket } from 'promise-socket';
import { StratumV1Client } from '../models/StratumV1Client'; import { StratumV1Client } from '../models/StratumV1Client';
import { AddressSettingsService } from '../ORM/address-settings/address-settings.service';
import { BlocksService } from '../ORM/blocks/blocks.service'; import { BlocksService } from '../ORM/blocks/blocks.service';
import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service'; import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service';
import { ClientService } from '../ORM/client/client.service'; import { ClientService } from '../ORM/client/client.service';
@@ -22,7 +23,8 @@ export class StratumV1Service implements OnModuleInit {
private readonly notificationService: NotificationService, private readonly notificationService: NotificationService,
private readonly blocksService: BlocksService, private readonly blocksService: BlocksService,
private readonly configService: ConfigService, private readonly configService: ConfigService,
private readonly stratumV1JobsService: StratumV1JobsService private readonly stratumV1JobsService: StratumV1JobsService,
private readonly addressSettingsService: AddressSettingsService
) { ) {
} }
@@ -49,7 +51,8 @@ export class StratumV1Service implements OnModuleInit {
this.clientStatisticsService, this.clientStatisticsService,
this.notificationService, this.notificationService,
this.blocksService, this.blocksService,
this.configService this.configService,
this.addressSettingsService
); );