cleaning up

This commit is contained in:
Ben Wilson
2023-06-17 23:36:14 -04:00
parent c167fe9517
commit 916d5f20e6
10 changed files with 101 additions and 144 deletions
+16 -46
View File
@@ -7,7 +7,7 @@ import { eResponseMethod } from './enums/eResponseMethod';
interface AddressObject {
address: string;
percentage: number;
percent: number;
}
export class MiningJob {
public id: number;
@@ -25,7 +25,7 @@ export class MiningJob {
public version: number; // The hex-encoded block version.
public nbits: number; // The hex-encoded network difficulty required for the block.
public ntime: number; // 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.
//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.
public response: string;
@@ -33,9 +33,8 @@ export class MiningJob {
public tree: MerkleTree;
constructor(id: string, public blockTemplate: IBlockTemplate, _cleanJobs: boolean) {
constructor(id: string, payoutInformation: AddressObject[], public blockTemplate: IBlockTemplate, public networkDifficulty: number, public clean_jobs: boolean) {
//console.log(blockTemplate);
this.job_id = id;
this.target = blockTemplate.target;
@@ -44,17 +43,14 @@ export class MiningJob {
this.version = blockTemplate.version;
this.nbits = parseInt(blockTemplate.bits, 16);
this.ntime = Math.floor(new Date().getTime() / 1000);
this.clean_jobs = _cleanJobs;
const transactionFees = blockTemplate.transactions.reduce((pre, cur, i, arr) => {
return pre + cur.fee;
}, 0);
const miningReward = this.calculateMiningReward(blockTemplate.height);
//console.log('TRANSACTION FEES', transactionFees);
//console.log('MINING REWARD', miningReward);
const { coinbasePart1, coinbasePart2 } = this.createCoinbaseTransaction([{ address: 'bc1qsa5kr5s74wss7pcpmnrc7e3y5nrgyfcdzpwakk', percentage: 100 }], blockTemplate.height, transactionFees + miningReward);
const { coinbasePart1, coinbasePart2 } = this.createCoinbaseTransaction(payoutInformation, blockTemplate.height, blockTemplate.coinbasevalue);
this.coinb1 = coinbasePart1;
this.coinb2 = coinbasePart2;
@@ -62,19 +58,12 @@ export class MiningJob {
const coinbaseHash = this.sha256(this.coinb1 + this.coinb2).toString('hex');
const coinbaseBuffer = Buffer.from(coinbaseHash, 'hex');
//transactions.unshift(coinbaseHash);
// Calculate merkle branch
const transactionBuffers = blockTemplate.transactions.map(tx => Buffer.from(tx.hash, 'hex'));
transactionBuffers.unshift(coinbaseBuffer);
this.tree = new MerkleTree(transactionBuffers, this.sha256, { isBitcoinTree: true });
// // this.merkle_branch = tree.getProof(coinbaseBuffer).map(p => p.data.toString('hex'))
// this.merkle_branch = tree.getLayers().map(l => l.pop().toString('hex'));
// this.merkleRoot = this.merkle_branch.pop();
const rootBuffer = this.tree.getRoot();
this.merkleRoot = rootBuffer.toString('hex');
this.merkle_branch = this.tree.getProof(coinbaseBuffer).map(p => p.data.toString('hex'));
@@ -84,30 +73,12 @@ export class MiningJob {
}
private calculateMiningReward(blockHeight: number): number {
const initialBlockReward = 50 * 1e8; // Initial block reward in satoshis (1 BTC = 100 million satoshis)
const halvingInterval = 210000; // Number of blocks after which the reward halves
// Calculate the number of times the reward has been halved
const halvingCount = Math.floor(blockHeight / halvingInterval);
// Calculate the current block reward in satoshis
const currentReward = initialBlockReward / Math.pow(2, halvingCount);
return currentReward;
}
private createCoinbaseTransaction(addresses: AddressObject[], blockHeight: number, reward: number): { coinbasePart1: string, coinbasePart2: string } {
// Generate coinbase script
// Part 1
const blockHeightScript = `03${blockHeight.toString(16).padStart(8, '0')}`;
// Create coinbase transaction
const outputIndex = 'ffffffff';
const sequence = 'ffffffff';
const lockTime = '00000000';
const version = '01000000';
const inputCount = '01';
const fakeCoinbaseInput = '0000000000000000000000000000000000000000000000000000000000000000';
@@ -115,15 +86,14 @@ export class MiningJob {
const inputScriptBytes = ((blockHeightScript.length + 16) / 2).toString(16).padStart(2, '0');
const inputTransaction = inputCount + fakeCoinbaseInput + outputIndex + inputScriptBytes + blockHeightScript;
const coinbasePart1 = version + inputCount + fakeCoinbaseInput + outputIndex + inputScriptBytes + blockHeightScript;
//let remainingPayout = reward;
// Part 2
const outputs = addresses
.map((addressObj) => {
const percentage = addressObj.percentage / 100;
const percentage = addressObj.percent / 100;
const satoshis = Math.floor(reward * percentage);
const satoshiBuff = Buffer.alloc(4);
satoshiBuff.writeUInt32LE(satoshis);
@@ -135,14 +105,13 @@ export class MiningJob {
})
.join('');
// // Distribute any remaining satoshis to the first address
// const firstAddressSatoshis = (parseInt(outputs.substring(0, 16), 16) + remainingPayout).toString(16).padStart(16, '0');
// Combine coinbasePart1 and coinbasePart2
// Create outputs
const outputCountHex = addresses.length.toString(16).padStart(2, '0');
const coinbasePart1 = version + inputTransaction;
const sequence = 'ffffffff';
const lockTime = '00000000';
const coinbasePart2 = sequence + outputCountHex + outputs + lockTime;
return { coinbasePart1, coinbasePart2 };
@@ -184,4 +153,5 @@ export class MiningJob {
}