mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
correct merkle branches
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { AddressType, getAddressInfo } from 'bitcoin-address-validation';
|
||||
import * as bitcoinjs from 'bitcoinjs-lib';
|
||||
import * as crypto from 'crypto';
|
||||
import { MerkleTree } from 'merkletreejs';
|
||||
import * as merkle from 'merkle-lib';
|
||||
import * as merkleProof from 'merkle-lib/proof';
|
||||
|
||||
import { IBlockTemplate } from './bitcoin-rpc/IBlockTemplate';
|
||||
import { eResponseMethod } from './enums/eResponseMethod';
|
||||
@@ -38,7 +39,7 @@ export class MiningJob {
|
||||
|
||||
this.block.witnessCommit = bitcoinjs.Block.calculateMerkleRoot(this.block.transactions, true);
|
||||
|
||||
this.block.merkleRoot = bitcoinjs.Block.calculateMerkleRoot(this.block.transactions, false);
|
||||
|
||||
|
||||
|
||||
// https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki
|
||||
@@ -74,8 +75,11 @@ export class MiningJob {
|
||||
// Calculate merkle branch
|
||||
const transactionBuffers = this.block.transactions.map(tx => tx.getHash(false));
|
||||
|
||||
const tree = new MerkleTree(transactionBuffers, this.sha256, { isBitcoinTree: true });
|
||||
this.merkle_branch = tree.getProof(coinbaseTransaction.getHash(false)).map(p => p.data.toString('hex'));
|
||||
const merkleTree = merkle(transactionBuffers, bitcoinjs.crypto.hash256);
|
||||
const merkleBranches: Buffer[] = merkleProof(merkleTree, transactionBuffers[0]).filter(h => h != null);
|
||||
this.block.merkleRoot = merkleBranches.pop();
|
||||
|
||||
this.merkle_branch = merkleBranches.map(b => b.toString('hex'))
|
||||
|
||||
this.block.transactions[0] = coinbaseTransaction;
|
||||
|
||||
@@ -125,8 +129,7 @@ export class MiningJob {
|
||||
}
|
||||
|
||||
|
||||
private createCoinbaseTransaction
|
||||
(addresses: AddressObject[], reward: number): bitcoinjs.Transaction {
|
||||
private createCoinbaseTransaction(addresses: AddressObject[], reward: number): bitcoinjs.Transaction {
|
||||
// Part 1
|
||||
const coinbaseTransaction = new bitcoinjs.Transaction();
|
||||
|
||||
|
||||
@@ -223,8 +223,10 @@ export class StratumV1Client extends EasyUnsubscribe {
|
||||
const errors = await validate(miningSubmitMessage, validatorOptions);
|
||||
|
||||
if (errors.length === 0) {
|
||||
await this.handleMiningSubmission(miningSubmitMessage);
|
||||
await this.promiseSocket.write(JSON.stringify(miningSubmitMessage.response()) + '\n');
|
||||
const result = await this.handleMiningSubmission(miningSubmitMessage);
|
||||
if (result == true) {
|
||||
await this.promiseSocket.write(JSON.stringify(miningSubmitMessage.response()) + '\n');
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
@@ -324,7 +326,7 @@ export class StratumV1Client extends EasyUnsubscribe {
|
||||
'Job not found').response();
|
||||
console.error(err);
|
||||
await this.promiseSocket.write(err);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
const updatedJobBlock = job.copyAndUpdateBlock(
|
||||
parseInt(submission.versionMask, 16),
|
||||
@@ -353,6 +355,7 @@ export class StratumV1Client extends EasyUnsubscribe {
|
||||
'Duplicate share').response();
|
||||
console.error(err);
|
||||
await this.promiseSocket.write(err);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (submissionDifficulty > this.entity.bestDifficulty) {
|
||||
@@ -367,10 +370,11 @@ export class StratumV1Client extends EasyUnsubscribe {
|
||||
'Difficulty too low').response();
|
||||
console.error(err);
|
||||
await this.promiseSocket.write(err);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
await this.checkDifficulty();
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,6 @@ export class StratumErrorMessage {
|
||||
result: null,
|
||||
error: [this.errorCode, this.errorMessage, this.validationErrors.reduce((pre, cur) => `${pre}, ${cur.value}`, '')]
|
||||
}
|
||||
return JSON.stringify(error + '\n');
|
||||
return JSON.stringify(error) + '\n';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user