mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 17:15:03 -07:00
data persistance init
This commit is contained in:
@@ -8,6 +8,9 @@ import { combineLatest, interval, startWith } from 'rxjs';
|
||||
|
||||
import { BitcoinRpcService } from '../bitcoin-rpc.service';
|
||||
import { BlockTemplateService } from '../BlockTemplateService';
|
||||
import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service';
|
||||
import { ClientEntity } from '../ORM/client/client.entity';
|
||||
import { ClientService } from '../ORM/client/client.service';
|
||||
import { StratumV1JobsService } from '../stratum-v1-jobs.service';
|
||||
import { EasyUnsubscribe } from '../utils/AutoUnsubscribe';
|
||||
import { eRequestMethod } from './enums/eRequestMethod';
|
||||
@@ -22,12 +25,14 @@ import { StratumV1ClientStatistics } from './StratumV1ClientStatistics';
|
||||
|
||||
export class StratumV1Client extends EasyUnsubscribe {
|
||||
|
||||
public startTime: Date;
|
||||
|
||||
public clientSubscription: SubscriptionMessage;
|
||||
public clientConfiguration: ConfigurationMessage;
|
||||
public clientAuthorization: AuthorizationMessage;
|
||||
public clientSuggestedDifficulty: SuggestDifficulty;
|
||||
|
||||
public statistics: StratumV1ClientStatistics = new StratumV1ClientStatistics();
|
||||
public statistics: StratumV1ClientStatistics;
|
||||
|
||||
public id: string;
|
||||
public stratumInitialized = false;
|
||||
@@ -38,16 +43,20 @@ export class StratumV1Client extends EasyUnsubscribe {
|
||||
|
||||
public jobRefreshInterval: NodeJS.Timer;
|
||||
|
||||
|
||||
public entity: ClientEntity;
|
||||
|
||||
|
||||
constructor(
|
||||
public readonly socket: Socket,
|
||||
private readonly stratumV1JobsService: StratumV1JobsService,
|
||||
private readonly blockTemplateService: BlockTemplateService,
|
||||
private readonly bitcoinRpcService: BitcoinRpcService
|
||||
private readonly bitcoinRpcService: BitcoinRpcService,
|
||||
private readonly clientService: ClientService,
|
||||
private readonly clientStatisticsService: ClientStatisticsService
|
||||
) {
|
||||
super();
|
||||
this.startTime = new Date();
|
||||
this.statistics = new StratumV1ClientStatistics(this.clientStatisticsService);
|
||||
this.id = this.getRandomHexString();
|
||||
|
||||
console.log(`New client ID: : ${this.id}`);
|
||||
@@ -194,7 +203,7 @@ export class StratumV1Client extends EasyUnsubscribe {
|
||||
const errors = await validate(miningSubmitMessage, validatorOptions);
|
||||
|
||||
if (errors.length === 0) {
|
||||
this.handleMiningSubmission(miningSubmitMessage);
|
||||
await this.handleMiningSubmission(miningSubmitMessage);
|
||||
socket.write(JSON.stringify(miningSubmitMessage.response()) + '\n');
|
||||
} else {
|
||||
console.error(errors);
|
||||
@@ -213,6 +222,15 @@ export class StratumV1Client extends EasyUnsubscribe {
|
||||
this.stratumInitialized = true;
|
||||
|
||||
|
||||
this.entity = await this.clientService.save({
|
||||
id: this.id,
|
||||
address: this.clientAuthorization.address,
|
||||
clientName: this.clientAuthorization.worker,
|
||||
startTime: new Date(),
|
||||
});
|
||||
|
||||
|
||||
|
||||
let lastIntervalCount = undefined;
|
||||
combineLatest([this.blockTemplateService.currentBlockTemplate$, interval(60000).pipe(startWith(-1))]).subscribe(([{ miningInfo, blockTemplate }, interValCount]) => {
|
||||
let clearJobs = false;
|
||||
@@ -235,7 +253,7 @@ export class StratumV1Client extends EasyUnsubscribe {
|
||||
}
|
||||
|
||||
|
||||
private handleMiningSubmission(submission: MiningSubmitMessage) {
|
||||
private async handleMiningSubmission(submission: MiningSubmitMessage) {
|
||||
|
||||
const job = this.stratumV1JobsService.getJobById(submission.jobId);
|
||||
// a miner may submit a job that doesn't exist anymore if it was removed by a new block notification
|
||||
@@ -247,8 +265,11 @@ export class StratumV1Client extends EasyUnsubscribe {
|
||||
|
||||
if (diff >= this.clientDifficulty) {
|
||||
const networkDifficulty = this.calculateNetworkDifficulty(parseInt(job.blockTemplate.bits, 16));
|
||||
this.statistics.addSubmission(this.clientDifficulty, diff, networkDifficulty);
|
||||
if (diff >= networkDifficulty) {
|
||||
await this.statistics.addSubmission(this.entity, this.clientDifficulty);
|
||||
if (diff > this.entity.bestDifficulty) {
|
||||
await this.clientService.updateBestDifficulty(this.id, diff);
|
||||
}
|
||||
if (diff >= (networkDifficulty / 2)) {
|
||||
console.log('!!! BOCK FOUND !!!');
|
||||
this.constructBlockAndBroadcast(job, submission);
|
||||
}
|
||||
|
||||
@@ -1,45 +1,22 @@
|
||||
import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service';
|
||||
import { ClientEntity } from '../ORM/client/client.entity';
|
||||
|
||||
export class StratumV1ClientStatistics {
|
||||
|
||||
public bestDifficulty: number = 0;
|
||||
|
||||
public historicSubmissions = [];
|
||||
|
||||
constructor() {
|
||||
constructor(private readonly clientStatisticsService: ClientStatisticsService) {
|
||||
|
||||
}
|
||||
|
||||
public addSubmission(targetDifficulty: number, submissionDifficulty: number, networkDifficulty: number) {
|
||||
public async addSubmission(client: ClientEntity, targetDifficulty: number) {
|
||||
|
||||
if (this.historicSubmissions.length >= 1000) {
|
||||
this.historicSubmissions.shift();
|
||||
}
|
||||
|
||||
this.historicSubmissions.push({
|
||||
await this.clientStatisticsService.save({
|
||||
time: new Date(),
|
||||
difficulty: targetDifficulty,
|
||||
time: new Date()
|
||||
client
|
||||
});
|
||||
|
||||
if (submissionDifficulty > this.bestDifficulty) {
|
||||
this.bestDifficulty = submissionDifficulty;
|
||||
}
|
||||
}
|
||||
|
||||
public getHashRate() {
|
||||
return this.historicSubmissions.reduce((pre, cur, idx, arr) => {
|
||||
if (idx === 0) {
|
||||
pre.time = cur.time;
|
||||
}
|
||||
pre.difficulty += cur.difficulty;
|
||||
|
||||
if (arr.length - 1 === idx) {
|
||||
const duration = cur.time.getTime() - pre.time.getTime();
|
||||
const sumDifficulty = pre.difficulty;
|
||||
return (sumDifficulty * 4294967296) / (duration * 1000000)
|
||||
}
|
||||
return pre;
|
||||
}, {
|
||||
difficulty: 0,
|
||||
time: undefined
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user