testNonceValue work

This commit is contained in:
Ben Wilson
2023-06-11 14:30:30 -04:00
parent e8a5e263b0
commit edd2845829
2 changed files with 30 additions and 22 deletions
+4 -2
View File
@@ -15,7 +15,7 @@ export class MiningJob {
public params: string[]; public params: string[];
public target: string; public target: string;
public merkleRoot: Buffer; public merkleRoot: string;
public job_id: string; // ID of the job. Use this ID while submitting share generated from this job. public job_id: string; // ID of the job. Use this ID while submitting share generated from this job.
public prevhash: string; // The hex-encoded previous block hash. public prevhash: string; // The hex-encoded previous block hash.
@@ -31,6 +31,8 @@ export class MiningJob {
constructor(blockTemplate: IBlockTemplate) { constructor(blockTemplate: IBlockTemplate) {
console.log(blockTemplate);
this.job_id = randomUUID(); this.job_id = randomUUID();
this.target = blockTemplate.target; this.target = blockTemplate.target;
this.prevhash = blockTemplate.previousblockhash; this.prevhash = blockTemplate.previousblockhash;
@@ -71,7 +73,7 @@ export class MiningJob {
this.merkle_branch = branch; this.merkle_branch = branch;
this.merkleRoot = tree.getRoot(); this.merkleRoot = tree.getRoot().toString('hex')
} }
@@ -5,6 +5,7 @@ import { StratumBaseMessage } from './StratumBaseMessage';
import { MiningJob } from '../MiningJob'; import { MiningJob } from '../MiningJob';
import * as crypto from 'crypto'; import * as crypto from 'crypto';
const trueDiffOne = Number('26959535291011309493156476344723991336010898738574164086137773096960'); const trueDiffOne = Number('26959535291011309493156476344723991336010898738574164086137773096960');
export class MiningSubmitMessage extends StratumBaseMessage { export class MiningSubmitMessage extends StratumBaseMessage {
@@ -43,42 +44,47 @@ export class MiningSubmitMessage extends StratumBaseMessage {
testNonceValue(job: MiningJob, nonce: string): number { testNonceValue(job: MiningJob, nonce: string): number {
const header = Buffer.alloc(80);
const header: Buffer = Buffer.alloc(80); // TODO: Use the midstate hash instead of hashing the whole header
// TODO: use the midstate hash instead of hashing the whole header // Copy data from job to header
header.set(Buffer.from(job.version).subarray(0, 4), 0); header.set(Buffer.from(job.version, 'hex').subarray(0, 4), 0);
header.set(Buffer.from(job.prevhash).subarray(0, 32), 4); header.set(Buffer.from(job.prevhash, 'hex').subarray(0, 32), 4);
header.set(Buffer.from(job.merkleRoot).subarray(0, 32), 36); header.set(Buffer.from(job.merkleRoot, 'hex').subarray(0, 32), 36);
header.set(Buffer.from(job.ntime).subarray(0, 4), 68); header.set(Buffer.from(job.ntime).subarray(0, 4), 68);
header.set(Buffer.from(job.target).subarray(0, 4), 72); header.set(Buffer.from(job.target, 'hex').subarray(0, 4), 72);
header.set(Buffer.from(nonce).subarray(0, 4), 76); header.set(Buffer.from(nonce, 'hex').subarray(0, 4), 76);
const hashBuffer = crypto.createHash('sha256').update(header).digest(); const hashBuffer = crypto.createHash('sha256').update(header).digest();
const hashResult = crypto.createHash('sha256').update(hashBuffer).digest(); const hashResult = crypto.createHash('sha256').update(hashBuffer).digest();
const s64 = this.le256toDouble(hashResult); const d64 = 1.0;
const ds = trueDiffOne / s64; const s64 = this.littleEndianToDouble(hashResult);
console.log(trueDiffOne + '/ ' + s64) const ds = d64 / s64;
return ds; return ds;
} }
private le256toDouble(target: Buffer): number { private littleEndianToDouble(buffer: Buffer): number {
const bits192 = 6277101735386680763835789423207666416102355444464034512896n; // Replace with the actual value of bits192 // Reverse the byte order to big-endian
const bits128 = 340282366920938463463374607431768211456n; // Replace with the actual value of bits128 const reversedBytes = Buffer.from(buffer).reverse();
const bits64 = 18446744073709551616n; // Replace with the actual value of bits64
const data64 = target.readBigUInt64LE(24); // Convert the reversed bytes to a hexadecimal string
let dcut64 = Number(data64) * Number(bits192); const hexString = `0x${reversedBytes.toString('hex')}`;
dcut64 += Number(target.readBigUInt64LE(16)) * Number(bits128); // Interpret the hexadecimal string as a double
dcut64 += Number(target.readBigUInt64LE(8)) * Number(bits64); const doubleValue = Number(hexString);
dcut64 += Number(target.readBigUInt64LE(0));
return dcut64; // Check for NaN and Infinity
if (!Number.isFinite(doubleValue)) {
throw new Error('Conversion resulted in NaN or Infinity');
}
return doubleValue;
} }
} }