mining is submitting

This commit is contained in:
Ben Wilson
2023-06-10 13:58:27 -04:00
parent deb3aa30d4
commit 2aad736628
6 changed files with 164 additions and 80 deletions
+50 -44
View File
@@ -1,17 +1,17 @@
import { plainToInstance } from 'class-transformer';
import { validate, ValidatorOptions } from 'class-validator';
import { Socket } from 'net';
import { interval } from 'rxjs';
import { BehaviorSubject, combineLatest, concat, merge, Observable, takeUntil } from 'rxjs';
import { EasyUnsubscribe } from '../utils/AutoUnsubscribe';
import { eRequestMethod } from './enums/eRequestMethod';
import { eResponseMethod } from './enums/eResponseMethod';
import { AuthorizationMessage } from './stratum-messages/AuthorizationMessage';
import { ConfigurationMessage } from './stratum-messages/ConfigurationMessage';
import { MiningSubmitMessage } from './stratum-messages/MiningSubmitMessage';
import { SubscriptionMessage } from './stratum-messages/SubscriptionMessage';
import { SuggestDifficulty } from './stratum-messages/SuggestDifficultyMessage';
export class StratumV1Client {
export class StratumV1Client extends EasyUnsubscribe {
private clientSubscription: SubscriptionMessage;
private clientConfiguration: ConfigurationMessage;
@@ -20,17 +20,23 @@ export class StratumV1Client {
public initialized = false;
private interval: NodeJS.Timer;
public onInitialized: BehaviorSubject<void> = new BehaviorSubject(null);
public localMiningJobEmitter: BehaviorSubject<string> = new BehaviorSubject(null);
constructor(private readonly socket: Socket) {
constructor(
private readonly socket: Socket,
private readonly globalMiningJobEmitter: Observable<string>
) {
super();
this.socket.on('data', this.handleData.bind(this, this.socket));
this.socket.on('end', () => {
// Handle socket disconnection
console.log('Client disconnected:', socket.remoteAddress);
this.unsubscribeAll();
});
this.socket.on('error', (error: Error) => {
@@ -38,6 +44,13 @@ export class StratumV1Client {
console.error('Socket error:', error);
});
merge(this.globalMiningJobEmitter, this.localMiningJobEmitter).pipe(takeUntil(this.easyUnsubscribe)).subscribe((job: string) => {
if (!this.initialized) {
return;
}
this.socket.write(job + '\n');
})
}
@@ -187,7 +200,7 @@ export class StratumV1Client {
&& this.clientSuggestedDifficulty != null) {
this.initialized = true;
this.manualMiningNotify();
this.onInitialized.next();
}
@@ -195,46 +208,39 @@ export class StratumV1Client {
}
private manualMiningNotify() {
clearInterval(this.interval);
this.miningNotify();
this.interval = setInterval(() => {
this.miningNotify();
}, 60000);
}
private miningNotify() {
const notification = {
id: null,
method: eResponseMethod.MINING_NOTIFY,
params: [
'64756fab0000442e',
'39dbb5b4e173e1f9ac6f6ad92e9dde300effce6b0003ea860000000000000000',
'01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff35033e1c0c00048fef83640447483e060c',
'0a636b706f6f6c112f736f6c6f2e636b706f6f6c2e6f72672fffffffff03e1c7872600000000160014876961d21eaba10f0701dcc78f6624a4c682270d384dc900000000001976a914f4cbe6c6bb3a8535c963169c22963d3a20e7686988ac0000000000000000266a24aa21a9ed751521cd94e7780a9a13ac8bb1da5410d30acdd2f6348876835623b04b2dc83b00000000',
[
'c0c9d351b9e094dd85bc64c8909dece6226269ebfe173bb74ba2f89c51df7066',
'6c56f47cbfaef5688bb338bc56c4189530f12cdb98f8cc46b6a12053f1e69fdd',
'697cdaa8d15691f7b30dfe7f6c33957f04cfc8126fed16d2fe38c96adaa59c41',
'cbe8aa6e0343884a40b850df8fd3c2ffcc026acec392ce93f9e28619eb0d3dac',
'e023091cf0fc02684c77730a34791181a44be92f966ba579aa4cf6e98d754548',
'6b8fea64efe363e02ff42a4257d76a77381006eade804c8a8c9b96c9c98b1d9e',
'1c936bc5320cdbbe7348cdd4bf272529822e9d34dfa8e0ee0041eae635891cc3',
'8fed2682a3c95863c6b9440b1a47abdd3d4230181f61edf0daa2e5f0befbcf65',
'0837c4d162e1086ec553ea90af4be4f9747958e556598cc38cb08149e58227b1',
'0b5287e647c7cb6f2fcdf13d5ef3bf091d1137773d7695405d0b2768f442ee78',
'71eaff9247b5556fa88bca6da2e055d5db8aef2969a2d5c68f8e7efd7d39a283'
],
'20000000',
'17057e69',
'6483ef8f',
true
],
// private miningNotify() {
// const notification = {
// id: null,
// method: eResponseMethod.MINING_NOTIFY,
// params: [
// '64756fab0000442e',
// '39dbb5b4e173e1f9ac6f6ad92e9dde300effce6b0003ea860000000000000000',
// '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff35033e1c0c00048fef83640447483e060c',
// '0a636b706f6f6c112f736f6c6f2e636b706f6f6c2e6f72672fffffffff03e1c7872600000000160014876961d21eaba10f0701dcc78f6624a4c682270d384dc900000000001976a914f4cbe6c6bb3a8535c963169c22963d3a20e7686988ac0000000000000000266a24aa21a9ed751521cd94e7780a9a13ac8bb1da5410d30acdd2f6348876835623b04b2dc83b00000000',
// [
// 'c0c9d351b9e094dd85bc64c8909dece6226269ebfe173bb74ba2f89c51df7066',
// '6c56f47cbfaef5688bb338bc56c4189530f12cdb98f8cc46b6a12053f1e69fdd',
// '697cdaa8d15691f7b30dfe7f6c33957f04cfc8126fed16d2fe38c96adaa59c41',
// 'cbe8aa6e0343884a40b850df8fd3c2ffcc026acec392ce93f9e28619eb0d3dac',
// 'e023091cf0fc02684c77730a34791181a44be92f966ba579aa4cf6e98d754548',
// '6b8fea64efe363e02ff42a4257d76a77381006eade804c8a8c9b96c9c98b1d9e',
// '1c936bc5320cdbbe7348cdd4bf272529822e9d34dfa8e0ee0041eae635891cc3',
// '8fed2682a3c95863c6b9440b1a47abdd3d4230181f61edf0daa2e5f0befbcf65',
// '0837c4d162e1086ec553ea90af4be4f9747958e556598cc38cb08149e58227b1',
// '0b5287e647c7cb6f2fcdf13d5ef3bf091d1137773d7695405d0b2768f442ee78',
// '71eaff9247b5556fa88bca6da2e055d5db8aef2969a2d5c68f8e7efd7d39a283'
// ],
// '20000000',
// '17057e69',
// '6483ef8f',
// true
// ],
};
// };
this.socket.write(JSON.stringify(notification) + '\n');
}
// this.socket.write(JSON.stringify(notification) + '\n');
// }
}