This commit is contained in:
Christopher Jeffrey 2016-01-07 03:24:29 -08:00
parent c56640a433
commit 42fbef82ed
3 changed files with 26 additions and 12 deletions

View File

@ -145,7 +145,7 @@ Chain.prototype._init = function _init() {
});
};
Chain.prototype._addIndex = function _addIndex(entry) {
Chain.prototype._addIndex = function _addIndex(entry, save) {
var self = this;
// Already added
@ -181,7 +181,8 @@ Chain.prototype._addIndex = function _addIndex(entry) {
this.tip = this.index.entries[this.index.entries.length - 1];
this.emit('tip', this.tip);
this._save(entry);
if (save)
this._save(entry.hash, entry);
return Chain.codes.okay;
};
@ -194,10 +195,12 @@ Chain.prototype.resetLastCheckpoint = function resetLastCheckpoint(height) {
assert(index >= 0);
assert(checkpoint);
// This is the safest way to do it, the other possibility is to simply reset
// ignore the bad checkpoint block. The likelihood of someone carrying on an
// entire fork between to checkpoints is absurd, so this is probably _a lot_
// of work for nothing.
// This is the safest way to do it, the other
// possibility is to simply reset ignore the
// bad checkpoint block. The likelihood of
// someone carrying on an entire fork between
// to checkpoints is absurd, so this is
// probably _a lot_ of work for nothing.
this.resetHeight(checkpoint.height);
};
@ -360,7 +363,7 @@ Chain.prototype.add = function add(block, peer) {
}
// Attempt to add block to the chain index.
code = this._addIndex(entry);
code = this._addIndex(entry, true);
// Result should never be `unchanged` since
// we already verified there were no

View File

@ -42,6 +42,7 @@ function Miner(options) {
this.fee = new bn(0);
this.last = this.chain.getTip();
this.block = null;
this.iterations = 0;
this._begin = utils.now();
this._init();
@ -95,6 +96,9 @@ Miner.prototype.start = function start() {
// Ask our peers for mempool txs every so often.
this.interval = setInterval(mempool, 60 * 1000);
// Reset iterations
this.iterations = 0;
// Create a new block and start hashing
this.block = this.createBlock();
this.iterate();
@ -126,7 +130,7 @@ Miner.prototype.addBlock = function addBlock(block) {
// Somebody found the next block before
// us, start over with the new target.
if (block.height > this.last.height) {
this.last = block.verify
this.last = block.type === 'block'
? this.chain.getBlock(block)
: block;
assert(this.last);
@ -209,9 +213,7 @@ Miner.prototype.createBlock = function createBlock(tx) {
// Create our block
headers = {
version: 4,
prevBlock: this.last.verify
? this.last.hash('hex')
: this.last.hash,
prevBlock: this.last.hash,
merkleRoot: utils.toHex(constants.zeroHash.slice()),
ts: ts,
bits: target,
@ -291,7 +293,7 @@ Miner.prototype.iterate = function iterate() {
};
Miner.prototype.__defineGetter__('hashes', function() {
return this.block.extraNonce.muln(0xffffffff).addn(this.block.nonce);
return new bn(this.iterations).muln(0xffffffff).addn(this.block.nonce);
});
Miner.prototype.__defineGetter__('rate', function() {
@ -333,6 +335,9 @@ Miner.prototype.findNonce = function findNonce() {
this.sendStatus();
}
// Keep track of our iterations
this.iterations++;
// Send progress report
this.sendStatus();
@ -344,6 +349,8 @@ Miner.prototype.findNonce = function findNonce() {
now = utils.now();
if (now > this.block.ts) {
this.block.ts = now;
// Overflow the nonce
this.block.nonce = 0;
return false;
}

View File

@ -835,6 +835,10 @@ utils.now = function now() {
return +new Date() / 1000 | 0;
};
utils.host = function host(addr) {
return addr.split(':')[0];
};
utils.hash = function hash(obj, enc) {
if (obj == null)
throw new Error('Cannot get hash of null');