mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
mining is submitting
This commit is contained in:
@@ -1,34 +1,73 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { Server, Socket } from 'net';
|
||||
import { BehaviorSubject, take } from 'rxjs';
|
||||
|
||||
import { CoinbaseConstructorService } from './coinbase-constructor.service';
|
||||
import { BitcoinRpcService } from './bitcoin-rpc.service';
|
||||
import { IBlockTempalte } from './models/IBlockTempalte';
|
||||
import { MiningJob } from './models/MiningJob';
|
||||
import { StratumV1Client } from './models/StratumV1Client';
|
||||
|
||||
@Injectable()
|
||||
export class BitcoinStratumProvider {
|
||||
export class BitcoinStratumProvider implements OnModuleInit {
|
||||
|
||||
public clients: StratumV1Client[] = [];
|
||||
|
||||
private server: Server;
|
||||
|
||||
constructor(private coinbaseConstructorService: CoinbaseConstructorService) {
|
||||
private blockTemplate: IBlockTempalte;
|
||||
|
||||
private interval: NodeJS.Timer;
|
||||
|
||||
private newMiningJobEmitter: BehaviorSubject<string> = new BehaviorSubject(null);
|
||||
|
||||
private latestJob: MiningJob;
|
||||
|
||||
|
||||
constructor(private readonly bitcoinRpcService: BitcoinRpcService) {
|
||||
|
||||
this.server = new Server((socket: Socket) => {
|
||||
console.log('New client connected:', socket.remoteAddress);
|
||||
|
||||
const client = new StratumV1Client(socket);
|
||||
this.clients.push(client);
|
||||
const client = new StratumV1Client(socket, this.newMiningJobEmitter.asObservable());
|
||||
|
||||
console.log('Number of Clients:', this.clients.length);
|
||||
client.onInitialized.pipe(take(1)).subscribe(() => {
|
||||
if (this.latestJob == null) {
|
||||
return;
|
||||
}
|
||||
const job = this.latestJob.response();
|
||||
const jobString = JSON.stringify(job);
|
||||
client.localMiningJobEmitter.next(jobString);
|
||||
});
|
||||
|
||||
// this.clients.push(client);
|
||||
|
||||
// console.log('Number of Clients:', this.clients.length);
|
||||
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
async onModuleInit(): Promise<void> {
|
||||
console.log('onModuleInit');
|
||||
this.blockTemplate = await this.bitcoinRpcService.getBlockTemplate();
|
||||
|
||||
clearInterval(this.interval);
|
||||
const job = new MiningJob(this.blockTemplate).response();
|
||||
this.newMiningJobEmitter.next(JSON.stringify(job));
|
||||
|
||||
this.interval = setInterval(() => {
|
||||
this.latestJob = new MiningJob(this.blockTemplate);
|
||||
|
||||
const job = this.latestJob.response();
|
||||
const jobString = JSON.stringify(job);
|
||||
|
||||
this.newMiningJobEmitter.next(jobString);
|
||||
}, 60000);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
listen(port: number) {
|
||||
|
||||
Reference in New Issue
Block a user