bitmessage-js/lib/pow.js

115 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

2015-01-02 22:45:56 +01:00
/**
* Implements proof of work.
2023-01-09 02:46:20 +01:00
* @see {@link https://pybitmessage.rtfd.io/en/v0.6/pow.html}
2015-01-02 22:45:56 +01:00
* @module bitmessage/pow
*/
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");
var util = require("./_util");
2015-01-07 22:28:13 +01:00
/**
2015-01-31 12:51:35 +01:00
* Calculate target.
2015-01-08 14:47:29 +01:00
* @param {Object} opts - Target options
2015-02-10 19:07:08 +01:00
* @param {number} opts.ttl - Time to live of the message in seconds
* @param {number} opts.payloadLength - Length of the message payload
* (with nonce)
* @param {Buffer} opts.payload - ...or payload itself
* @param {number=} opts.nonceTrialsPerByte - This number is the average
* number of nonce trials a node will have to perform to meet the Proof
* of Work requirement. 1000 is the network minimum so any lower values
* will be automatically raised to 1000.
* @param {number=} opts.payloadLengthExtraBytes - This number is added
* to the data length to make sending small messages more difficult.
* 1000 is the network minimum so any lower values will be automatically
* raised to 1000.
2015-01-07 22:28:13 +01:00
* @return {number} Target.
2015-01-31 12:51:35 +01:00
* @function
2015-01-30 20:45:45 +01:00
* @static
2015-01-07 22:28:13 +01:00
*/
// Just a wrapper around platform-specific implementation.
2015-01-30 20:45:45 +01:00
var getTarget = 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,
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
2015-02-10 19:07:08 +01:00
* @param {number} opts.target - Proof of work target or pass
* [getTarget]{@link module:bitmessage/pow.getTarget} options to `opts`
* to compute it
* @param {Buffer} opts.payload - Message payload (with nonce)
* @param {(number|Buffer)} opts.nonce - ...or already derived nonce
* @param {Buffer} opts.initialHash - ...and initial hash
2015-01-08 14:47:29 +01:00
* @return {boolean} Is the proof of work sufficient.
*/
exports.check = function(opts) {
var initialHash;
var nonce;
2015-01-30 20:45:45 +01:00
var target = opts.target;
if (target === undefined) {
target = getTarget(opts);
}
2015-01-30 18:13:09 +01:00
if (opts.payload) {
nonce = opts.payload.slice(0, 8);
initialHash = bmcrypto.sha512(opts.payload.slice(8));
2015-01-08 14:47:29 +01:00
} else {
if (typeof opts.nonce === "number") {
nonce = Buffer.alloc(8);
2015-01-08 14:47:29 +01:00
// 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;
}
2015-01-30 20:45:45 +01:00
var targetHi = Math.floor(target / 4294967296);
var targetLo = target % 4294967296;
2015-01-08 14:47:29 +01:00
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
2015-02-10 19:07:08 +01:00
* @param {Buffer} opts.data - Object message payload without nonce to
2015-01-30 20:45:45 +01:00
* get the initial hash from
2015-02-10 19:07:08 +01:00
* @param {Buffer} opts.initialHash - ...or already computed initial
* hash
2015-01-30 20:45:45 +01:00
* @param {number} opts.target - POW target
2015-02-10 19:07:08 +01:00
* @param {number=} opts.poolSize - POW calculation pool size (by
* default equals to number of cores)
2015-01-30 20:45:45 +01:00
* @return {Promise.<number>} A promise that contains computed nonce for
2015-01-09 14:12:23 +01:00
* 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;
2015-01-30 20:45:45 +01:00
if (opts.data) {
initialHash = bmcrypto.sha512(opts.data);
2015-01-09 14:12:23 +01:00
} 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
};