mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
import { Injectable, OnModuleInit } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { Server, Socket } from 'net';
|
|
|
|
import { StratumV1Client } from '../models/StratumV1Client';
|
|
import { AddressSettingsService } from '../ORM/address-settings/address-settings.service';
|
|
import { BlocksService } from '../ORM/blocks/blocks.service';
|
|
import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service';
|
|
import { ClientService } from '../ORM/client/client.service';
|
|
import { BitcoinRpcService } from './bitcoin-rpc.service';
|
|
import { NotificationService } from './notification.service';
|
|
import { StratumV1JobsService } from './stratum-v1-jobs.service';
|
|
|
|
|
|
@Injectable()
|
|
export class StratumV1Service implements OnModuleInit {
|
|
|
|
constructor(
|
|
private readonly bitcoinRpcService: BitcoinRpcService,
|
|
private readonly clientService: ClientService,
|
|
private readonly clientStatisticsService: ClientStatisticsService,
|
|
private readonly notificationService: NotificationService,
|
|
private readonly blocksService: BlocksService,
|
|
private readonly configService: ConfigService,
|
|
private readonly stratumV1JobsService: StratumV1JobsService,
|
|
private readonly addressSettingsService: AddressSettingsService
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
async onModuleInit(): Promise<void> {
|
|
|
|
//await this.clientStatisticsService.deleteAll();
|
|
|
|
await this.clientService.deleteAll();
|
|
|
|
|
|
this.startSocketServer();
|
|
}
|
|
|
|
private startSocketServer() {
|
|
const server = new Server(async (socket: Socket) => {
|
|
|
|
|
|
|
|
const client = new StratumV1Client(
|
|
socket,
|
|
this.stratumV1JobsService,
|
|
this.bitcoinRpcService,
|
|
this.clientService,
|
|
this.clientStatisticsService,
|
|
this.notificationService,
|
|
this.blocksService,
|
|
this.configService,
|
|
this.addressSettingsService
|
|
);
|
|
|
|
|
|
socket.on('end', async (error: Error) => {
|
|
if (client.extraNonceAndSessionId != null) {
|
|
// Handle socket disconnection
|
|
await client.destroy();
|
|
console.log(`Client disconnected, ${client.extraNonceAndSessionId}`);
|
|
}
|
|
|
|
});
|
|
|
|
socket.on('error', async (error: Error) => {
|
|
if (client.extraNonceAndSessionId != null) {
|
|
await client.destroy();
|
|
console.log(`Client disconnected, socket error, ${client.extraNonceAndSessionId}`);
|
|
}
|
|
|
|
socket.destroy();
|
|
});
|
|
|
|
});
|
|
|
|
server.listen(3333, () => {
|
|
console.log(`Bitcoin Stratum server is listening on port ${3333}`);
|
|
});
|
|
|
|
}
|
|
|
|
} |