mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
optimizations and cleanup
This commit is contained in:
@@ -115,7 +115,7 @@ export class StratumV1Client {
|
|||||||
this.sessionStart = new Date();
|
this.sessionStart = new Date();
|
||||||
this.statistics = new StratumV1ClientStatistics(this.clientStatisticsService, this.clientService);
|
this.statistics = new StratumV1ClientStatistics(this.clientStatisticsService, this.clientService);
|
||||||
this.extraNonceAndSessionId = this.getRandomHexString();
|
this.extraNonceAndSessionId = this.getRandomHexString();
|
||||||
console.log(`New client ID: : ${this.extraNonceAndSessionId}`);
|
console.log(`New client ID: : ${this.extraNonceAndSessionId}, ${this.socket.remoteAddress}:${this.socket.remotePort}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (parsedMessage.method) {
|
switch (parsedMessage.method) {
|
||||||
@@ -331,7 +331,7 @@ export class StratumV1Client {
|
|||||||
|
|
||||||
|
|
||||||
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));
|
||||||
const success = await this.write(setDifficulty + '\n');
|
const success = await this.write(setDifficulty + '\n');
|
||||||
if (!success) {
|
if (!success) {
|
||||||
@@ -521,7 +521,7 @@ export class StratumV1Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (targetDiff != this.sessionDifficulty) {
|
if (targetDiff != this.sessionDifficulty) {
|
||||||
console.log(`Adjusting difficulty from ${this.sessionDifficulty} to ${targetDiff}`);
|
console.log(`Adjusting ${this.extraNonceAndSessionId} difficulty from ${this.sessionDifficulty} to ${targetDiff}`);
|
||||||
this.sessionDifficulty = targetDiff;
|
this.sessionDifficulty = targetDiff;
|
||||||
|
|
||||||
const data = JSON.stringify({
|
const data = JSON.stringify({
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import * as bitcoinjs from 'bitcoinjs-lib';
|
import * as bitcoinjs from 'bitcoinjs-lib';
|
||||||
import * as merkle from 'merkle-lib';
|
import * as merkle from 'merkle-lib';
|
||||||
import * as merkleProof from 'merkle-lib/proof';
|
import * as merkleProof from 'merkle-lib/proof';
|
||||||
import { combineLatest, filter, from, interval, map, Observable, shareReplay, startWith, switchMap, tap } from 'rxjs';
|
import { combineLatest, delay, filter, from, interval, map, Observable, shareReplay, startWith, switchMap, tap } from 'rxjs';
|
||||||
|
|
||||||
import { MiningJob } from '../models/MiningJob';
|
import { MiningJob } from '../models/MiningJob';
|
||||||
import { BitcoinRpcService } from './bitcoin-rpc.service';
|
import { BitcoinRpcService } from './bitcoin-rpc.service';
|
||||||
@@ -35,11 +35,16 @@ export class StratumV1JobsService {
|
|||||||
|
|
||||||
public blocks: { [id: number]: IJobTemplate } = {};
|
public blocks: { [id: number]: IJobTemplate } = {};
|
||||||
|
|
||||||
|
// offset the interval so that all the cluster processes don't try and refresh at the same time.
|
||||||
|
private delay = process.env.NODE_APP_INSTANCE == null ? 0 : parseInt(process.env.NODE_APP_INSTANCE) * 5000;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly bitcoinRpcService: BitcoinRpcService
|
private readonly bitcoinRpcService: BitcoinRpcService
|
||||||
) {
|
) {
|
||||||
|
|
||||||
this.newMiningJob$ = combineLatest([this.bitcoinRpcService.newBlock$, interval(60000).pipe(startWith(-1))]).pipe(
|
console.log(this.delay)
|
||||||
|
|
||||||
|
this.newMiningJob$ = combineLatest([this.bitcoinRpcService.newBlock$, interval(60000).pipe(delay(this.delay), startWith(-1))]).pipe(
|
||||||
switchMap(([miningInfo, interval]) => {
|
switchMap(([miningInfo, interval]) => {
|
||||||
return from(this.bitcoinRpcService.getBlockTemplate()).pipe(map((blockTemplate) => {
|
return from(this.bitcoinRpcService.getBlockTemplate()).pipe(map((blockTemplate) => {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -57,23 +57,18 @@ export class StratumV1Service implements OnModuleInit {
|
|||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
socket.on('end', async (error: Error) => {
|
socket.on('close', async (hadError: boolean) => {
|
||||||
if (client.extraNonceAndSessionId != null) {
|
if (client.extraNonceAndSessionId != null) {
|
||||||
// Handle socket disconnection
|
// Handle socket disconnection
|
||||||
await client.destroy();
|
await client.destroy();
|
||||||
console.log(`Client disconnected, ${client.extraNonceAndSessionId}`);
|
console.log(`Client ${client.extraNonceAndSessionId} disconnected, hadError?:${hadError}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('error', async (error: Error) => {
|
socket.on('error', async (error: Error) => { });
|
||||||
if (client.extraNonceAndSessionId != null) {
|
|
||||||
await client.destroy();
|
// //console.log(`Client disconnected, socket error, ${client.extraNonceAndSessionId}`);
|
||||||
console.log(`Client disconnected, socket error, ${client.extraNonceAndSessionId}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
socket.destroy();
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user