forked from yoshuki43/node-stratum-pool-yescrypt
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1fc8c92
commit 27bde74
Showing
1 changed file
with
33 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,48 @@ | ||
/* | ||
Ported from https://github.com/slush0/stratum-mining/blob/master/lib/merkletree.py | ||
*/ | ||
|
||
var util = require('./util.js'); | ||
|
||
var MerkleTree = module.exports = function MerkleTree(data){ | ||
this.data = data.filter(element => element != null); | ||
this.steps = calculateSteps(this.data); | ||
}; | ||
|
||
function merkleJoin(h1, h2){ | ||
var joined = Buffer.concat([h1, h2]); | ||
var dhashed = util.sha256d(joined); | ||
return dhashed; | ||
function merkleJoin(h1, h2){ | ||
if (h1 === null || h2 === null) { | ||
throw new Error('Merkle join received null input'); | ||
} | ||
var joined = Buffer.concat([h1, h2]); | ||
var dhashed = util.sha256d(joined); | ||
return dhashed; | ||
} | ||
|
||
function calculateSteps(data){ | ||
var L = data; | ||
var steps = []; | ||
var PreL = [null]; | ||
var StartL = 2; | ||
var Ll = L.length; | ||
|
||
if (Ll > 1){ | ||
while (true){ | ||
function calculateSteps(data){ | ||
var L = data; | ||
var steps = []; | ||
var Ll = L.length; | ||
|
||
if (Ll === 1) | ||
break; | ||
if (Ll > 1){ | ||
while (true){ | ||
if (Ll === 1) break; | ||
|
||
steps.push(L[1]); | ||
steps.push(L[1]); | ||
|
||
if (Ll % 2) | ||
L.push(L[L.length - 1]); | ||
if (Ll % 2) L.push(L[L.length - 1]); | ||
|
||
var Ld = []; | ||
var r = util.range(StartL, Ll, 2); | ||
r.forEach(function(i){ | ||
var Ld = []; | ||
for (var i = 0; i < L.length; i += 2) { | ||
if (i + 1 < L.length) { | ||
Ld.push(merkleJoin(L[i], L[i + 1])); | ||
}); | ||
L = PreL.concat(Ld); | ||
Ll = L.length; | ||
} | ||
} | ||
L = Ld; | ||
Ll = L.length; | ||
} | ||
return steps; | ||
} | ||
|
||
this.data = data; | ||
this.steps = calculateSteps(data); | ||
|
||
return steps; | ||
} | ||
MerkleTree.prototype = { | ||
withFirst: function(f){ | ||
this.steps.forEach(function(s){ | ||
f = util.sha256d(Buffer.concat([f, s])); | ||
}); | ||
return f; | ||
} | ||
}; | ||
|
||
MerkleTree.prototype.withFirst = function(f){ | ||
this.steps.forEach(function(s){ | ||
f = util.sha256d(Buffer.concat([f, s])); | ||
}); | ||
return f; | ||
}; |