Skip to content

Commit

Permalink
prepare for #49 by separating computation with DOM update
Browse files Browse the repository at this point in the history
  • Loading branch information
ethers committed Jul 12, 2015
1 parent 901de25 commit b6644f3
Showing 1 changed file with 53 additions and 45 deletions.
98 changes: 53 additions & 45 deletions meteor-dapp/client/templates/elements/pow.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ Template.pow.viewmodel({
ticketId: 2,
nonce: 2460830,


findPoWClicked: function() {
console.log(this.btcTxHash())
var powNonce = computePow(this.btcTxHash(), this.ticketId());

this.nonce(powNonce);
},

verifyPoWClicked: function() {
var hexTicketId = new BigNumber(this.ticketId()).toString(16);
var padLen = 16 - hexTicketId.length;
var leadZerosForTicketId = Array(padLen + 1).join('0');

var bnSrc = new BigNumber('0x' + this.btcTxHash() + leadZerosForTicketId + hexTicketId + "0000000000000000");
var hexNonce = new BigNumber(this.nonce()).toString(16);
padLen = 16 - hexNonce.length;
var leadZerosForNonce = Array(padLen + 1).join('0');

var bnSrc = new BigNumber('0x' + this.btcTxHash() + leadZerosForTicketId + hexTicketId + leadZerosForNonce + hexNonce);
var src;
var bnHash;
var strHash;
Expand All @@ -32,68 +41,67 @@ Template.pow.viewmodel({
strHash = ku.wordsToHexString(dst);
bnHash = new BigNumber('0x' + strHash);

var isPowValid = bnHash.lt(bnTarget);
console.log('@@@ isPowValid: ', isPowValid, ' pow: ', bnHash.toString(16), ' target: ', bnTarget.toString(16))

startTime = new Date().getTime();
console.log("startTime: ", startTime)
if (isPowValid) {
swal('Proof of Work', 'Valid', 'success');
}
else {
swal('Proof of Work', 'Invalid', 'error');
}
}
});

var i=0;
while (bnHash.gte(bnTarget) && i < 100000000) {
bnSrc = bnSrc.add(1);

src = ku.hexStringToBytes(bnSrc.toString(16));
src = new Uint32Array(src.buffer);
kecc.digestWords(dst, 0, 8, src, 0, srcLen);
function computePow(btcTxHash, ticketId) {
console.log('@@@ computePow txhash: ', btcTxHash)

strHash = ku.wordsToHexString(dst);
bnHash = new BigNumber('0x' + strHash);
var hexTicketId = new BigNumber(ticketId).toString(16);
var padLen = 16 - hexTicketId.length;
var leadZerosForTicketId = Array(padLen + 1).join('0');

var bnSrc = new BigNumber('0x' + btcTxHash + leadZerosForTicketId + hexTicketId + "0000000000000000");
var src;
var bnHash;
var strHash;

i+= 1;
}
console.log('@@@ bnSrc: ', bnSrc.toString(16))

console.log("endTime: ", new Date().getTime())
console.log("duration: ", (new Date().getTime() - startTime) / 1000.0)

console.log('@@@@ i: ', i)
console.log('@@@ strHash: ', strHash)
src = ku.hexStringToBytes(bnSrc.toString(16));
src = new Uint32Array(src.buffer);
var srcLen = src.length;
var dst = new Uint32Array(8);
kecc.digestWords(dst, 0, 8, src, 0, srcLen);

this.nonce(i);
},
strHash = ku.wordsToHexString(dst);
bnHash = new BigNumber('0x' + strHash);

verifyPoWClicked: function() {
var hexTicketId = new BigNumber(this.ticketId()).toString(16);
var padLen = 16 - hexTicketId.length;
var leadZerosForTicketId = Array(padLen + 1).join('0');

var hexNonce = new BigNumber(this.nonce()).toString(16);
padLen = 16 - hexNonce.length;
var leadZerosForNonce = Array(padLen + 1).join('0');

var bnSrc = new BigNumber('0x' + this.btcTxHash() + leadZerosForTicketId + hexTicketId + leadZerosForNonce + hexNonce);
var src;
var bnHash;
var strHash;

console.log('@@@ bnSrc: ', bnSrc.toString(16))
startTime = new Date().getTime();
console.log("startTime: ", startTime)

var i=0;
while (bnHash.gte(bnTarget) && i < 100000000) {
bnSrc = bnSrc.add(1);

src = ku.hexStringToBytes(bnSrc.toString(16));
src = new Uint32Array(src.buffer);
var srcLen = src.length;
var dst = new Uint32Array(8);
kecc.digestWords(dst, 0, 8, src, 0, srcLen);

strHash = ku.wordsToHexString(dst);
bnHash = new BigNumber('0x' + strHash);

var isPowValid = bnHash.lt(bnTarget);
console.log('@@@ isPowValid: ', isPowValid, ' pow: ', bnHash.toString(16), ' target: ', bnTarget.toString(16))

if (isPowValid) {
swal('Proof of Work', 'Valid', 'success');
}
else {
swal('Proof of Work', 'Invalid', 'error');
}
i+= 1;
}
});

console.log("endTime: ", new Date().getTime())
console.log("duration: ", (new Date().getTime() - startTime) / 1000.0)

console.log('@@@@ i: ', i)
console.log('@@@ strHash: ', strHash)

return i;
}

0 comments on commit b6644f3

Please sign in to comment.