itns-sidechain/lib/mining/mine.js

52 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-10-05 16:16:08 -07:00
/*!
* mine.js - mining function for bcoin
2017-02-03 22:47:26 -08:00
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
2016-10-05 16:16:08 -07:00
* https://github.com/bcoin-org/bcoin
*/
2017-06-24 05:11:54 -07:00
'use strict';
2018-01-02 20:24:56 -08:00
const {Miner} = require('thc/cuckoo');
const consensus = require('../protocol/consensus');
const {rcmp} = require('./common');
2016-10-05 16:16:08 -07:00
/**
* Hash until the nonce overflows.
2017-02-03 22:47:26 -08:00
* @alias module:mining.mine
2018-01-02 20:24:56 -08:00
* @param {Buffer} hdr
* @param {Buffer} target
* @param {Number} rounds
* @param {Object} params
* @returns {Buffer|null}
2016-10-05 16:16:08 -07:00
*/
2018-01-02 20:24:56 -08:00
function mine(hdr, target, rounds, params) {
const {bits, size, ease} = params;
const miner = new Miner(bits, size, ease);
const nonce = hdr.slice(consensus.NONCE_POS);
2016-10-05 16:16:08 -07:00
// The heart and soul of the miner: match the target.
2018-01-02 20:24:56 -08:00
for (let i = 0; i < rounds; i++) {
const sol = miner.mineHeader(hdr);
if (sol && rcmp(sol.hash(), target) <= 0)
return [nonce, sol];
for (let j = 0; j < consensus.NONCE_SIZE; j++) {
if (nonce[j] !== 0xff) {
nonce[j] += 1;
break;
}
nonce[j] = 0;
}
2016-10-05 16:16:08 -07:00
}
2018-01-02 20:24:56 -08:00
return [nonce, null];
2016-10-05 16:16:08 -07:00
}
/*
* Expose
*/
module.exports = mine;