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

View File

@ -145,7 +145,7 @@ export class MiningJob {
private constructResponse() {
const job = {
id: 0,
id: null,
method: eResponseMethod.MINING_NOTIFY,
params: [
this.job_id,

View File

@ -27,6 +27,8 @@ export class StratumV1Client {
public refreshInterval: NodeJS.Timer;
public clientDifficulty: number = 512;
constructor(
public readonly socket: Socket,
private readonly stratumV1JobsService: StratumV1JobsService
@ -131,7 +133,7 @@ export class StratumV1Client {
if (errors.length === 0) {
this.clientAuthorization = authorizationMessage;
this.clientAuthorization.parse();
//const response = this.buildSubscriptionResponse(authorizationMessage.id);
socket.write(JSON.stringify(this.clientAuthorization.response()) + '\n');
} else {
@ -144,7 +146,7 @@ export class StratumV1Client {
const suggestDifficultyMessage = plainToInstance(
SuggestDifficulty,
parsedMessage,
parsedMessage
);
const validatorOptions: ValidatorOptions = {
@ -155,8 +157,10 @@ export class StratumV1Client {
const errors = await validate(suggestDifficultyMessage, validatorOptions);
if (errors.length === 0) {
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 {
console.error(errors);
}
@ -217,7 +221,6 @@ export class StratumV1Client {
private handleMiningSubmission(submission: MiningSubmitMessage) {
submission.parse();
const networkDifficulty = 0;
const job = this.stratumV1JobsService.getJobById(submission.jobId);
const diff = submission.testNonceValue(this.id, job, submission);

View File

@ -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 { StratumBaseMessage } from './StratumBaseMessage';
@ -10,8 +11,18 @@ export class AuthorizationMessage extends StratumBaseMessage {
@ArrayMaxSize(2)
params: string[];
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[0];
})
public username: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[1];
})
public password: string;
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() {
return {

View File

@ -1,5 +1,6 @@
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 { eRequestMethod } from '../enums/eRequestMethod';
@ -13,11 +14,41 @@ export class MiningSubmitMessage extends StratumBaseMessage {
@ArrayMaxSize(6)
public params: string[];
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[0];
})
public userId: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[1];
})
public jobId: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[2];
})
public extraNonce2: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[3];
})
public ntime: string;
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[4];
})
public nonce: string
@Expose()
@IsString()
@Transform(({ value, key, obj, type }) => {
return obj.params[5];
})
public versionMask: string;
constructor() {
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() {
return {
id: null,

View File

@ -22,7 +22,7 @@ export class SubscriptionMessage extends StratumBaseMessage {
error: null,
result: [
[
['mining.notify', '64d8c004']
// ['mining.notify', '64d8c004']
], //subscription details
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.

View File

@ -1,3 +1,4 @@
import { Expose, Transform } from 'class-transformer';
import { ArrayMaxSize, ArrayMinSize, IsArray, IsNumber } from 'class-validator';
import { eRequestMethod } from '../enums/eRequestMethod';
@ -9,18 +10,31 @@ export class SuggestDifficulty extends StratumBaseMessage {
@ArrayMinSize(1)
@ArrayMaxSize(1)
@IsNumber({}, { each: true })
params: string[];
params: string | number[];
@Expose()
@IsNumber()
@Transform(({ value, key, obj, type }) => {
return Number(obj.params[0]);
})
public suggestedDifficulty: number;
constructor() {
super();
this.method = eRequestMethod.SUGGEST_DIFFICULTY;
}
public response() {
public response(difficulty: number) {
return {
id: null,
method: eResponseMethod.SET_DIFFICULTY,
params: [256]
params: [difficulty]
}
}
}
}

View File

@ -16,6 +16,6 @@
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
"noFallthroughCasesInSwitch": false,
}
}