2015-01-02 22:45:56 +01:00
|
|
|
/**
|
|
|
|
* Implements proof of work.
|
|
|
|
* @see {@link https://bitmessage.org/wiki/Proof_of_work}
|
|
|
|
* @module bitmessage/pow
|
|
|
|
*/
|
2015-01-09 14:12:23 +01:00
|
|
|
// TODO(Kagami): Find a way how to document object params properly.
|
2015-01-07 22:28:13 +01:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2015-01-09 14:12:23 +01:00
|
|
|
var objectAssign = Object.assign || require("object-assign");
|
2015-01-08 14:47:29 +01:00
|
|
|
var bmcrypto = require("./crypto");
|
2015-01-07 22:28:13 +01:00
|
|
|
var platform = require("./platform");
|
2015-01-22 01:00:58 +01:00
|
|
|
var util = require("./_util");
|
2015-01-07 22:28:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate target
|
2015-01-08 14:47:29 +01:00
|
|
|
* @param {Object} opts - Target options
|
2015-01-07 22:28:13 +01:00
|
|
|
* @return {number} Target.
|
|
|
|
*/
|
|
|
|
// Just a wrapper around platform-specific implementation.
|
|
|
|
exports.getTarget = function(opts) {
|
2015-01-08 14:47:29 +01:00
|
|
|
var payloadLength = opts.payloadLength || opts.payload.length;
|
2015-01-07 22:28:13 +01:00
|
|
|
return platform.getTarget({
|
|
|
|
ttl: opts.ttl,
|
2015-01-08 14:47:29 +01:00
|
|
|
payloadLength: payloadLength,
|
2015-01-22 01:00:58 +01:00
|
|
|
nonceTrialsPerByte: util.getTrials(opts),
|
|
|
|
payloadLengthExtraBytes: util.getExtraBytes(opts),
|
2015-01-07 22:28:13 +01:00
|
|
|
});
|
|
|
|
};
|
2015-01-08 14:47:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
};
|
2015-01-09 14:12:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do a POW.
|
|
|
|
* @param {Object} opts - Proof of work options
|
|
|
|
* @return {Promise.<Buffer>} A promise that contains computed nonce for
|
|
|
|
* the given target when fulfilled.
|
|
|
|
*/
|
2015-01-10 14:23:10 +01:00
|
|
|
exports.doAsync = function(opts) {
|
2015-01-09 14:12:23 +01:00
|
|
|
var initialHash;
|
|
|
|
if (opts.payload) {
|
|
|
|
initialHash = bmcrypto.sha512(opts.payload);
|
|
|
|
} else {
|
|
|
|
initialHash = opts.initialHash;
|
|
|
|
}
|
|
|
|
opts = objectAssign({}, opts, {initialHash: initialHash});
|
2015-01-10 14:03:14 +01:00
|
|
|
return platform.pow(opts);
|
2015-01-09 14:12:23 +01:00
|
|
|
};
|