misc
This commit is contained in:
parent
c56640a433
commit
42fbef82ed
@ -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;
|
var self = this;
|
||||||
|
|
||||||
// Already added
|
// Already added
|
||||||
@ -181,7 +181,8 @@ Chain.prototype._addIndex = function _addIndex(entry) {
|
|||||||
this.tip = this.index.entries[this.index.entries.length - 1];
|
this.tip = this.index.entries[this.index.entries.length - 1];
|
||||||
this.emit('tip', this.tip);
|
this.emit('tip', this.tip);
|
||||||
|
|
||||||
this._save(entry);
|
if (save)
|
||||||
|
this._save(entry.hash, entry);
|
||||||
|
|
||||||
return Chain.codes.okay;
|
return Chain.codes.okay;
|
||||||
};
|
};
|
||||||
@ -194,10 +195,12 @@ Chain.prototype.resetLastCheckpoint = function resetLastCheckpoint(height) {
|
|||||||
assert(index >= 0);
|
assert(index >= 0);
|
||||||
assert(checkpoint);
|
assert(checkpoint);
|
||||||
|
|
||||||
// This is the safest way to do it, the other possibility is to simply reset
|
// This is the safest way to do it, the other
|
||||||
// ignore the bad checkpoint block. The likelihood of someone carrying on an
|
// possibility is to simply reset ignore the
|
||||||
// entire fork between to checkpoints is absurd, so this is probably _a lot_
|
// bad checkpoint block. The likelihood of
|
||||||
// of work for nothing.
|
// 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);
|
this.resetHeight(checkpoint.height);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -360,7 +363,7 @@ Chain.prototype.add = function add(block, peer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to add block to the chain index.
|
// Attempt to add block to the chain index.
|
||||||
code = this._addIndex(entry);
|
code = this._addIndex(entry, true);
|
||||||
|
|
||||||
// Result should never be `unchanged` since
|
// Result should never be `unchanged` since
|
||||||
// we already verified there were no
|
// we already verified there were no
|
||||||
|
|||||||
@ -42,6 +42,7 @@ function Miner(options) {
|
|||||||
this.fee = new bn(0);
|
this.fee = new bn(0);
|
||||||
this.last = this.chain.getTip();
|
this.last = this.chain.getTip();
|
||||||
this.block = null;
|
this.block = null;
|
||||||
|
this.iterations = 0;
|
||||||
this._begin = utils.now();
|
this._begin = utils.now();
|
||||||
|
|
||||||
this._init();
|
this._init();
|
||||||
@ -95,6 +96,9 @@ Miner.prototype.start = function start() {
|
|||||||
// Ask our peers for mempool txs every so often.
|
// Ask our peers for mempool txs every so often.
|
||||||
this.interval = setInterval(mempool, 60 * 1000);
|
this.interval = setInterval(mempool, 60 * 1000);
|
||||||
|
|
||||||
|
// Reset iterations
|
||||||
|
this.iterations = 0;
|
||||||
|
|
||||||
// Create a new block and start hashing
|
// Create a new block and start hashing
|
||||||
this.block = this.createBlock();
|
this.block = this.createBlock();
|
||||||
this.iterate();
|
this.iterate();
|
||||||
@ -126,7 +130,7 @@ Miner.prototype.addBlock = function addBlock(block) {
|
|||||||
// Somebody found the next block before
|
// Somebody found the next block before
|
||||||
// us, start over with the new target.
|
// us, start over with the new target.
|
||||||
if (block.height > this.last.height) {
|
if (block.height > this.last.height) {
|
||||||
this.last = block.verify
|
this.last = block.type === 'block'
|
||||||
? this.chain.getBlock(block)
|
? this.chain.getBlock(block)
|
||||||
: block;
|
: block;
|
||||||
assert(this.last);
|
assert(this.last);
|
||||||
@ -209,9 +213,7 @@ Miner.prototype.createBlock = function createBlock(tx) {
|
|||||||
// Create our block
|
// Create our block
|
||||||
headers = {
|
headers = {
|
||||||
version: 4,
|
version: 4,
|
||||||
prevBlock: this.last.verify
|
prevBlock: this.last.hash,
|
||||||
? this.last.hash('hex')
|
|
||||||
: this.last.hash,
|
|
||||||
merkleRoot: utils.toHex(constants.zeroHash.slice()),
|
merkleRoot: utils.toHex(constants.zeroHash.slice()),
|
||||||
ts: ts,
|
ts: ts,
|
||||||
bits: target,
|
bits: target,
|
||||||
@ -291,7 +293,7 @@ Miner.prototype.iterate = function iterate() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Miner.prototype.__defineGetter__('hashes', function() {
|
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() {
|
Miner.prototype.__defineGetter__('rate', function() {
|
||||||
@ -333,6 +335,9 @@ Miner.prototype.findNonce = function findNonce() {
|
|||||||
this.sendStatus();
|
this.sendStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Keep track of our iterations
|
||||||
|
this.iterations++;
|
||||||
|
|
||||||
// Send progress report
|
// Send progress report
|
||||||
this.sendStatus();
|
this.sendStatus();
|
||||||
|
|
||||||
@ -344,6 +349,8 @@ Miner.prototype.findNonce = function findNonce() {
|
|||||||
now = utils.now();
|
now = utils.now();
|
||||||
if (now > this.block.ts) {
|
if (now > this.block.ts) {
|
||||||
this.block.ts = now;
|
this.block.ts = now;
|
||||||
|
// Overflow the nonce
|
||||||
|
this.block.nonce = 0;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -835,6 +835,10 @@ utils.now = function now() {
|
|||||||
return +new Date() / 1000 | 0;
|
return +new Date() / 1000 | 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
utils.host = function host(addr) {
|
||||||
|
return addr.split(':')[0];
|
||||||
|
};
|
||||||
|
|
||||||
utils.hash = function hash(obj, enc) {
|
utils.hash = function hash(obj, enc) {
|
||||||
if (obj == null)
|
if (obj == null)
|
||||||
throw new Error('Cannot get hash of null');
|
throw new Error('Cannot get hash of null');
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user