class-transformer work

This commit is contained in:
Ben Wilson
2023-06-17 10:10:14 -04:00
parent f0dd027302
commit 00cb33c43d
7 changed files with 72 additions and 27 deletions
+1 -1
View File
@@ -145,7 +145,7 @@ export class MiningJob {
private constructResponse() { private constructResponse() {
const job = { const job = {
id: 0, id: null,
method: eResponseMethod.MINING_NOTIFY, method: eResponseMethod.MINING_NOTIFY,
params: [ params: [
this.job_id, this.job_id,
+7 -4
View File
@@ -27,6 +27,8 @@ export class StratumV1Client {
public refreshInterval: NodeJS.Timer; public refreshInterval: NodeJS.Timer;
public clientDifficulty: number = 512;
constructor( constructor(
public readonly socket: Socket, public readonly socket: Socket,
private readonly stratumV1JobsService: StratumV1JobsService private readonly stratumV1JobsService: StratumV1JobsService
@@ -131,7 +133,7 @@ export class StratumV1Client {
if (errors.length === 0) { if (errors.length === 0) {
this.clientAuthorization = authorizationMessage; this.clientAuthorization = authorizationMessage;
this.clientAuthorization.parse();
//const response = this.buildSubscriptionResponse(authorizationMessage.id); //const response = this.buildSubscriptionResponse(authorizationMessage.id);
socket.write(JSON.stringify(this.clientAuthorization.response()) + '\n'); socket.write(JSON.stringify(this.clientAuthorization.response()) + '\n');
} else { } else {
@@ -144,7 +146,7 @@ export class StratumV1Client {
const suggestDifficultyMessage = plainToInstance( const suggestDifficultyMessage = plainToInstance(
SuggestDifficulty, SuggestDifficulty,
parsedMessage, parsedMessage
); );
const validatorOptions: ValidatorOptions = { const validatorOptions: ValidatorOptions = {
@@ -155,8 +157,10 @@ export class StratumV1Client {
const errors = await validate(suggestDifficultyMessage, validatorOptions); const errors = await validate(suggestDifficultyMessage, validatorOptions);
if (errors.length === 0) { if (errors.length === 0) {
this.clientSuggestedDifficulty = suggestDifficultyMessage; this.clientSuggestedDifficulty = suggestDifficultyMessage;
socket.write(JSON.stringify(this.clientSuggestedDifficulty.response()) + '\n'); this.clientDifficulty = suggestDifficultyMessage.suggestedDifficulty;
socket.write(JSON.stringify(this.clientSuggestedDifficulty.response(this.clientDifficulty)) + '\n');
} else { } else {
console.error(errors); console.error(errors);
} }
@@ -217,7 +221,6 @@ export class StratumV1Client {
private handleMiningSubmission(submission: MiningSubmitMessage) { private handleMiningSubmission(submission: MiningSubmitMessage) {
submission.parse();
const networkDifficulty = 0; const networkDifficulty = 0;
const job = this.stratumV1JobsService.getJobById(submission.jobId); const job = this.stratumV1JobsService.getJobById(submission.jobId);
const diff = submission.testNonceValue(this.id, job, submission); const diff = submission.testNonceValue(this.id, job, submission);
@@ -1,4 +1,5 @@
import { ArrayMaxSize, ArrayMinSize, IsArray } from 'class-validator'; import { Expose, Transform } from 'class-transformer';
import { ArrayMaxSize, ArrayMinSize, IsArray, IsString } from 'class-validator';
import { eRequestMethod } from '../enums/eRequestMethod'; import { eRequestMethod } from '../enums/eRequestMethod';
import { StratumBaseMessage } from './StratumBaseMessage'; import { StratumBaseMessage } from './StratumBaseMessage';
@@ -10,8 +11,18 @@ export class AuthorizationMessage extends StratumBaseMessage {
@ArrayMaxSize(2) @ArrayMaxSize(2)
params: string[]; params: string[];
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[0];
})
public username: string; public username: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[1];
})
public password: string; public password: string;
constructor() { constructor() {
@@ -20,11 +31,6 @@ export class AuthorizationMessage extends StratumBaseMessage {
} }
public parse() {
this.username = this.params[0];
this.password = this.params[1];
console.log(`Username ${this.username}, Password: ${this.password}`);
}
public response() { public response() {
return { return {
@@ -1,5 +1,6 @@
import Big from 'big.js'; import Big from 'big.js';
import { ArrayMaxSize, ArrayMinSize, IsArray } from 'class-validator'; import { Expose, Transform } from 'class-transformer';
import { ArrayMaxSize, ArrayMinSize, IsArray, IsString } from 'class-validator';
import * as crypto from 'crypto'; import * as crypto from 'crypto';
import { eRequestMethod } from '../enums/eRequestMethod'; import { eRequestMethod } from '../enums/eRequestMethod';
@@ -13,11 +14,41 @@ export class MiningSubmitMessage extends StratumBaseMessage {
@ArrayMaxSize(6) @ArrayMaxSize(6)
public params: string[]; public params: string[];
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[0];
})
public userId: string; public userId: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[1];
})
public jobId: string; public jobId: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[2];
})
public extraNonce2: string; public extraNonce2: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[3];
})
public ntime: string; public ntime: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[4];
})
public nonce: string public nonce: string
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[5];
})
public versionMask: string; public versionMask: string;
constructor() { constructor() {
super(); super();
@@ -25,15 +56,6 @@ export class MiningSubmitMessage extends StratumBaseMessage {
} }
public parse() {
this.userId = this.params[0];
this.jobId = this.params[1];
this.extraNonce2 = this.params[2];
this.ntime = this.params[3];
this.nonce = this.params[4];
this.versionMask = this.params[5];
}
public response() { public response() {
return { return {
id: null, id: null,
@@ -22,7 +22,7 @@ export class SubscriptionMessage extends StratumBaseMessage {
error: null, error: null,
result: [ result: [
[ [
['mining.notify', '64d8c004'] // ['mining.notify', '64d8c004']
], //subscription details ], //subscription details
clientId, //Extranonce1 - Hex-encoded, per-connection unique string which will be used for coinbase serialization later. Keep it safe! clientId, //Extranonce1 - Hex-encoded, per-connection unique string which will be used for coinbase serialization later. Keep it safe!
8 //Extranonce2_size - Represents expected length of extranonce2 which will be generated by the miner. 8 //Extranonce2_size - Represents expected length of extranonce2 which will be generated by the miner.
@@ -1,3 +1,4 @@
import { Expose, Transform } from 'class-transformer';
import { ArrayMaxSize, ArrayMinSize, IsArray, IsNumber } from 'class-validator'; import { ArrayMaxSize, ArrayMinSize, IsArray, IsNumber } from 'class-validator';
import { eRequestMethod } from '../enums/eRequestMethod'; import { eRequestMethod } from '../enums/eRequestMethod';
@@ -9,18 +10,31 @@ export class SuggestDifficulty extends StratumBaseMessage {
@ArrayMinSize(1) @ArrayMinSize(1)
@ArrayMaxSize(1) @ArrayMaxSize(1)
@IsNumber({}, { each: true }) @IsNumber({}, { each: true })
params: string[]; params: string | number[];
@Expose()
@IsNumber()
@Transform(({ value, key, obj, type }) => {
return Number(obj.params[0]);
})
public suggestedDifficulty: number;
constructor() { constructor() {
super(); super();
this.method = eRequestMethod.SUGGEST_DIFFICULTY; this.method = eRequestMethod.SUGGEST_DIFFICULTY;
} }
public response() {
public response(difficulty: number) {
return { return {
id: null, id: null,
method: eResponseMethod.SET_DIFFICULTY, method: eResponseMethod.SET_DIFFICULTY,
params: [256] params: [difficulty]
} }
} }
} }
+1 -1
View File
@@ -16,6 +16,6 @@
"noImplicitAny": false, "noImplicitAny": false,
"strictBindCallApply": false, "strictBindCallApply": false,
"forceConsistentCasingInFileNames": false, "forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false "noFallthroughCasesInSwitch": false,
} }
} }