Check a POW

This commit is contained in:
Kagami Hiiragi 2015-01-08 16:47:29 +03:00
parent 8ef3168af1
commit 9407e27529
4 changed files with 52 additions and 5 deletions

View File

@ -91,8 +91,8 @@ function keys2ripe(signKey, encKey) {
} else {
encPublicKey = encKey;
}
var concat = Buffer.concat([signPublicKey, encPublicKey]);
return bmcrypto.ripemd160(bmcrypto.sha512(concat));
var dataToHash = Buffer.concat([signPublicKey, encPublicKey]);
return bmcrypto.ripemd160(bmcrypto.sha512(dataToHash));
}
/**

View File

@ -6,6 +6,7 @@
"use strict";
var bmcrypto = require("./crypto");
var platform = require("./platform");
var DEFAULT_TRIALS_PER_BYTE = 1000;
@ -13,11 +14,13 @@ var DEFAULT_EXTRA_BYTES = 1000;
/**
* Calculate target
* @param {{ttl: number, payloadLength: number}} opts - Target options
* @param {Object} opts - Target options
* @return {number} Target.
*/
// TODO(Kagami): Find a way how to document object params properly.
// Just a wrapper around platform-specific implementation.
exports.getTarget = function(opts) {
var payloadLength = opts.payloadLength || opts.payload.length;
var nonceTrialsPerByte = opts.nonceTrialsPerByte;
// Automatically raise lower values per spec.
if (!nonceTrialsPerByte || nonceTrialsPerByte < DEFAULT_TRIALS_PER_BYTE) {
@ -29,8 +32,46 @@ exports.getTarget = function(opts) {
}
return platform.getTarget({
ttl: opts.ttl,
payloadLength: opts.payloadLength,
payloadLength: payloadLength,
nonceTrialsPerByte: nonceTrialsPerByte,
payloadLengthExtraBytes: payloadLengthExtraBytes,
});
};
/**
* Check a POW.
* @param {Object} opts - Proof of work options
* @return {boolean} Is the proof of work sufficient.
*/
exports.check = function(opts) {
var initialHash;
var nonce;
if (opts.data) {
nonce = opts.data.slice(0, 8);
initialHash = bmcrypto.sha512(opts.data.slice(8));
} else {
if (typeof opts.nonce === "number") {
nonce = new Buffer(8);
// High 32 bits.
nonce.writeUInt32BE(Math.floor(opts.nonce / 4294967296), 0, true);
// Low 32 bits.
nonce.writeUInt32BE(opts.nonce % 4294967296, 4, true);
} else {
nonce = opts.nonce;
}
initialHash = opts.initialHash;
}
var targetHi = Math.floor(opts.target / 4294967296);
var targetLo = opts.target % 4294967296;
var dataToHash = Buffer.concat([nonce, initialHash]);
var resultHash = bmcrypto.sha512(bmcrypto.sha512(dataToHash));
var trialHi = resultHash.readUInt32BE(0, true);
if (trialHi > targetHi) {
return false;
} else if (trialHi < targetHi) {
return true;
} else {
var trialLo = resultHash.readUInt32BE(4, true);
return trialLo <= targetLo;
}
};

View File

@ -173,7 +173,7 @@ var var_int = exports.var_int = {
targetStart = 1 + (8 - value.length);
value.copy(buf, targetStart);
} else {
throw new Error("Value encode error");
throw new Error("Unknown value type");
}
return buf;
},

View File

@ -272,6 +272,12 @@ describe("POW", function() {
expect(POW.getTarget({ttl: 2418984, payloadLength: 628, nonceTrialsPerByte: 1000, payloadLengthExtraBytes: 1000})).to.equal(297422593171);
expect(POW.getTarget({ttl: 86400, payloadLength: 628})).to.equal(4864647698763);
});
it("should check a POW", function() {
expect(POW.check({nonce: 21997550, target: 297422593171, initialHash: Buffer("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})).to.be.true;
expect(POW.check({nonce: 3122437, target: 4864647698763, initialHash: Buffer("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})).to.be.true;
expect(POW.check({nonce: 3122436, target: 4864647698763, initialHash: Buffer("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})).to.be.false;
});
});
describe("High-level classes", function() {