correct merkle branches

This commit is contained in:
Ben
2023-07-03 14:19:30 -04:00
parent 66187f476a
commit 04c859782c
5 changed files with 34 additions and 928 deletions
+15 -916
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -32,7 +32,7 @@
"bs58": "^5.0.0", "bs58": "^5.0.0",
"class-transformer": "^0.5.1", "class-transformer": "^0.5.1",
"class-validator": "^0.14.0", "class-validator": "^0.14.0",
"merkletreejs": "^0.3.10", "merkle-lib": "^2.0.10",
"promise-socket": "^7.0.0", "promise-socket": "^7.0.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"rpc-bitcoin": "^2.0.0", "rpc-bitcoin": "^2.0.0",
+9 -6
View File
@@ -1,7 +1,8 @@
import { AddressType, getAddressInfo } from 'bitcoin-address-validation'; import { AddressType, getAddressInfo } from 'bitcoin-address-validation';
import * as bitcoinjs from 'bitcoinjs-lib'; import * as bitcoinjs from 'bitcoinjs-lib';
import * as crypto from 'crypto'; 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 { IBlockTemplate } from './bitcoin-rpc/IBlockTemplate';
import { eResponseMethod } from './enums/eResponseMethod'; import { eResponseMethod } from './enums/eResponseMethod';
@@ -38,7 +39,7 @@ export class MiningJob {
this.block.witnessCommit = bitcoinjs.Block.calculateMerkleRoot(this.block.transactions, true); 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 // https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki
@@ -74,8 +75,11 @@ export class MiningJob {
// Calculate merkle branch // Calculate merkle branch
const transactionBuffers = this.block.transactions.map(tx => tx.getHash(false)); const transactionBuffers = this.block.transactions.map(tx => tx.getHash(false));
const tree = new MerkleTree(transactionBuffers, this.sha256, { isBitcoinTree: true }); const merkleTree = merkle(transactionBuffers, bitcoinjs.crypto.hash256);
this.merkle_branch = tree.getProof(coinbaseTransaction.getHash(false)).map(p => p.data.toString('hex')); 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; this.block.transactions[0] = coinbaseTransaction;
@@ -125,8 +129,7 @@ export class MiningJob {
} }
private createCoinbaseTransaction private createCoinbaseTransaction(addresses: AddressObject[], reward: number): bitcoinjs.Transaction {
(addresses: AddressObject[], reward: number): bitcoinjs.Transaction {
// Part 1 // Part 1
const coinbaseTransaction = new bitcoinjs.Transaction(); const coinbaseTransaction = new bitcoinjs.Transaction();
+8 -4
View File
@@ -223,8 +223,10 @@ export class StratumV1Client extends EasyUnsubscribe {
const errors = await validate(miningSubmitMessage, validatorOptions); const errors = await validate(miningSubmitMessage, validatorOptions);
if (errors.length === 0) { if (errors.length === 0) {
await this.handleMiningSubmission(miningSubmitMessage); const result = await this.handleMiningSubmission(miningSubmitMessage);
await this.promiseSocket.write(JSON.stringify(miningSubmitMessage.response()) + '\n'); if (result == true) {
await this.promiseSocket.write(JSON.stringify(miningSubmitMessage.response()) + '\n');
}
} else { } else {
@@ -324,7 +326,7 @@ export class StratumV1Client extends EasyUnsubscribe {
'Job not found').response(); 'Job not found').response();
console.error(err); console.error(err);
await this.promiseSocket.write(err); await this.promiseSocket.write(err);
return; return false;
} }
const updatedJobBlock = job.copyAndUpdateBlock( const updatedJobBlock = job.copyAndUpdateBlock(
parseInt(submission.versionMask, 16), parseInt(submission.versionMask, 16),
@@ -353,6 +355,7 @@ export class StratumV1Client extends EasyUnsubscribe {
'Duplicate share').response(); 'Duplicate share').response();
console.error(err); console.error(err);
await this.promiseSocket.write(err); await this.promiseSocket.write(err);
return false;
} }
if (submissionDifficulty > this.entity.bestDifficulty) { if (submissionDifficulty > this.entity.bestDifficulty) {
@@ -367,10 +370,11 @@ export class StratumV1Client extends EasyUnsubscribe {
'Difficulty too low').response(); 'Difficulty too low').response();
console.error(err); console.error(err);
await this.promiseSocket.write(err); await this.promiseSocket.write(err);
return false;
} }
await this.checkDifficulty(); await this.checkDifficulty();
return true;
} }
@@ -20,6 +20,6 @@ export class StratumErrorMessage {
result: null, result: null,
error: [this.errorCode, this.errorMessage, this.validationErrors.reduce((pre, cur) => `${pre}, ${cur.value}`, '')] error: [this.errorCode, this.errorMessage, this.validationErrors.reduce((pre, cur) => `${pre}, ${cur.value}`, '')]
} }
return JSON.stringify(error + '\n'); return JSON.stringify(error) + '\n';
} }
} }