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
+52 -17
View File
@@ -1,6 +1,8 @@
import * as crypto from 'crypto';
import { eResponseMethod } from './enums/eResponseMethod';
import { IBlockTempalte } from './IBlockTempalte';
import { randomUUID } from 'crypto';
export class MiningJob {
public id: number;
@@ -18,29 +20,38 @@ export class MiningJob {
public ntime: string; // Current ntime/
public clean_jobs: boolean; // When true, server indicates that submitting shares from previous jobs don't have a sense and such shares will be rejected. When this flag is set, miner should also drop all previous jobs too.
constructor() {
constructor(blockTemplate: IBlockTempalte) {
}
this.job_id = randomUUID();
this.prevhash = blockTemplate.previousblockhash;
public response() {
this.job_id = null;
this.prevhash = null;
this.coinb1 = null;
this.coinb2 = null;
this.merkle_branch = null;
this.version = null;
this.nbits = null;
this.version = blockTemplate.version.toString();
this.nbits = blockTemplate.bits;
this.ntime = Math.floor(new Date().getTime() / 1000).toString();
this.clean_jobs = false;
// Construct coinbase transaction
const coinbaseTransaction = blockTemplate.transactions[0];
const coinbaseHashBin = this.buildCoinbaseHashBin(coinbaseTransaction.data);
this.coinb1 = this.coinbasePrefix(coinbaseTransaction.data, coinbaseHashBin);
this.coinb2 = ''; // Assuming no suffix is required
// Calculate merkle branch
const merkleBranch = blockTemplate.transactions.slice(1).map((transaction) => transaction.hash);
this.merkle_branch = this.buildMerkleBranch(merkleBranch, coinbaseHashBin);
}
public response() {
return {
id: this.id,
method: this.method,
id: 0,
method: eResponseMethod.MINING_NOTIFY,
params: [
this.job_id,
'123',///this.job_id,
this.prevhash,
this.coinb1,
this.coinb2,
@@ -55,7 +66,18 @@ export class MiningJob {
}
public buildCoinbaseHashBin(coinbase: string): Buffer {
private coinbasePrefix(coinbase: string, coinbaseHashBin: Buffer): string {
const coinbaseData = Buffer.from(coinbase, 'hex');
const coinbaseSize = Buffer.alloc(1, coinbaseData.length);
const extraNoncePlaceholder = Buffer.alloc(4); // Assuming 4 bytes for extra nonce
const concatenatedBuffer = Buffer.concat([coinbaseSize, coinbaseData, extraNoncePlaceholder]);
const merkleRoot = this.doubleSHA(Buffer.concat([coinbaseHashBin, concatenatedBuffer]));
return merkleRoot.toString('hex');
}
private buildCoinbaseHashBin(coinbase: string): Buffer {
const sha256 = crypto.createHash('sha256');
const sha256Digest = sha256.update(Buffer.from(coinbase, 'hex')).digest();
@@ -65,15 +87,28 @@ export class MiningJob {
return coinbaseHash;
}
public buildMerkleRoot(merkleBranch: string[], coinbaseHashBin: Buffer): string {
private buildMerkleBranch(merkleBranch: string[], coinbaseHashBin: Buffer): string[] {
const merkleRoots: string[] = [];
let merkleRoot = coinbaseHashBin;
for (const h of merkleBranch) {
const concatenatedBuffer = Buffer.concat([merkleRoot, Buffer.from(h, 'hex')]);
merkleRoot = this.doubleSHA(concatenatedBuffer);
merkleRoots.push(merkleRoot.toString('hex'));
}
return merkleRoot.toString('hex');
return merkleRoots.slice(0, 1);
}
// private buildMerkleRoot(merkleBranch: string[], coinbaseHashBin: Buffer): string {
// let merkleRoot = coinbaseHashBin;
// for (const h of merkleBranch) {
// const concatenatedBuffer = Buffer.concat([merkleRoot, Buffer.from(h, 'hex')]);
// merkleRoot = this.doubleSHA(concatenatedBuffer);
// }
// return merkleRoot.toString('hex');
// }
private doubleSHA(data: Buffer): Buffer {
const sha256 = crypto.createHash('sha256');
const sha256Digest = sha256.update(data).digest();