bitmessage-js/docs/pow.js.html

166 lines
7.3 KiB
HTML
Raw Normal View History

2015-01-03 17:04:14 +01:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: pow.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: pow.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* Implements proof of work.
* @see {@link https://bitmessage.org/wiki/Proof_of_work}
* @module bitmessage/pow
*/
2015-01-31 12:54:23 +01:00
"use strict";
var objectAssign = Object.assign || require("object-assign");
var bmcrypto = require("./crypto");
var platform = require("./platform");
var util = require("./_util");
/**
* Calculate target.
* @param {Object} opts - Target options
2015-02-12 11:44:15 +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-31 12:54:23 +01:00
* @return {number} Target.
* @function
* @static
*/
// Just a wrapper around platform-specific implementation.
var getTarget = exports.getTarget = function(opts) {
var payloadLength = opts.payloadLength || opts.payload.length;
return platform.getTarget({
ttl: opts.ttl,
payloadLength: payloadLength,
nonceTrialsPerByte: util.getTrials(opts),
payloadLengthExtraBytes: util.getExtraBytes(opts),
});
};
/**
* Check a POW.
* @param {Object} opts - Proof of work options
2015-02-12 11:44:15 +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-31 12:54:23 +01:00
* @return {boolean} Is the proof of work sufficient.
*/
exports.check = function(opts) {
var initialHash;
var nonce;
var target = opts.target;
if (target === undefined) {
target = getTarget(opts);
}
if (opts.payload) {
nonce = opts.payload.slice(0, 8);
initialHash = bmcrypto.sha512(opts.payload.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(target / 4294967296);
var targetLo = 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 &lt; targetHi) {
return true;
} else {
var trialLo = resultHash.readUInt32BE(4, true);
return trialLo &lt;= targetLo;
}
};
/**
* Do a POW.
* @param {Object} opts - Proof of work options
2015-02-12 11:44:15 +01:00
* @param {Buffer} opts.data - Object message payload without nonce to
2015-01-31 12:54:23 +01:00
* get the initial hash from
2015-02-12 11:44:15 +01:00
* @param {Buffer} opts.initialHash - ...or already computed initial
* hash
2015-01-31 12:54:23 +01:00
* @param {number} opts.target - POW target
2015-02-12 11:44:15 +01:00
* @param {number=} opts.poolSize - POW calculation pool size (by
* default equals to number of cores)
2015-01-31 12:54:23 +01:00
* @return {Promise.&lt;number>} A promise that contains computed nonce for
* the given target when fulfilled.
*/
exports.doAsync = function(opts) {
var initialHash;
if (opts.data) {
initialHash = bmcrypto.sha512(opts.data);
} else {
initialHash = opts.initialHash;
}
opts = objectAssign({}, opts, {initialHash: initialHash});
return platform.pow(opts);
};
2015-01-03 17:04:14 +01:00
</code></pre>
</article>
</section>
</div>
<nav>
2015-02-14 12:00:58 +01:00
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-bitmessage.html">bitmessage</a></li><li><a href="module-bitmessage_address.html">bitmessage/address</a></li><li><a href="module-bitmessage_crypto.html">bitmessage/crypto</a></li><li><a href="module-bitmessage_messages.html">bitmessage/messages</a></li><li><a href="module-bitmessage_net_base.html">bitmessage/net/base</a></li><li><a href="module-bitmessage_net_tcp.html">bitmessage/net/tcp</a></li><li><a href="module-bitmessage_net_ws.html">bitmessage/net/ws</a></li><li><a href="module-bitmessage_objects.html">bitmessage/objects</a></li><li><a href="module-bitmessage_pow.html">bitmessage/pow</a></li><li><a href="module-bitmessage_structs.html">bitmessage/structs</a></li><li><a href="module-bitmessage_user-agent.html">bitmessage/user-agent</a></li><li><a href="module-bitmessage_wif.html">bitmessage/wif</a></li></ul><h3>Classes</h3><ul><li><a href="module-bitmessage_address.Address.html">Address</a></li><li><a href="module-bitmessage_net_base.BaseTransport.html">BaseTransport</a></li><li><a href="module-bitmessage_net_tcp.TcpTransport.html">TcpTransport</a></li><li><a href="module-bitmessage_net_ws.WsTransport.html">WsTransport</a></li><li><a href="module-bitmessage_structs.PubkeyBitfield.html">PubkeyBitfield</a></li><li><a href="module-bitmessage_structs.ServicesBitfield.html">ServicesBitfield</a></li></ul><h3>Namespaces</h3><ul><li><a href="module-bitmessage_messages.addr.html">addr</a></li><li><a href="module-bitmessage_messages.error.html">error</a></li><li><a href="module-bitmessage_messages.getdata.html">getdata</a></li><li><a href="module-bitmessage_messages.inv.html">inv</a></li><li><a href="module-bitmessage_messages.version.html">version</a></li><li><a href="module-bitmessage_objects.broadcast.html">broadcast</a></li><li><a href="module-bitmessage_objects.getpubkey.html">getpubkey</a></li><li><a href="module-bitmessage_objects.msg.html">msg</a></li><li><a href="module-bitmessage_objects.pubkey.html">pubkey</a></li><li><a href="module-bitmessage_structs.encrypted.html">encrypted</a></li><li><a href="module-bitmessage_structs.inv_vect.html">inv_vect</a></li><li><a href="module-bitmessage_structs.message.html">message</a></li><li><a href="module-bitmessage_structs.net_addr.html">net_addr</a></li><li><a href="module-bitmessage_structs.object.html">object</a></li><li><a href="module-bitmessage_structs.var_int.html">var_int</a></li><li><a href="module-bitmessage_structs.var_int_list.html">var_int_list</a></li><li><a href="module-bitmessage_structs.var_str.html">var_str</a></li></ul>
2015-01-03 17:04:14 +01:00
</nav>
<br class="clear">
<footer>
2015-02-14 12:00:58 +01:00
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0-dev</a> on Sat Feb 14 2015 14:00:49 GMT+0300 (MSK)
2015-01-03 17:04:14 +01:00
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>