fix typo. emit resolved entries.

This commit is contained in:
Christopher Jeffrey 2016-01-05 13:00:45 -08:00
parent d0d5ab2c3b
commit 8e698673a0
4 changed files with 40 additions and 9 deletions

View File

@ -361,6 +361,16 @@ Block.prototype.getReward = function getReward() {
}; };
}; };
Block.prototype.getEntry = function getEntry(chain) {
chain = chain || bcoin.chain.global;
return chain.getBlock(this);
};
Block.prototype.isOrphan = function isOrphan(chain) {
chain = chain || bcoin.chain.global;
return chain.hasBlock(this.prevBlock);
};
Block.prototype.__defineGetter__('rhash', function() { Block.prototype.__defineGetter__('rhash', function() {
return utils.revHex(this.hash('hex')); return utils.revHex(this.hash('hex'));
}); });
@ -388,6 +398,14 @@ Block.prototype.__defineGetter__('coinbase', function() {
return tx; return tx;
}); });
Block.prototype.__defineGetter__('entry', function() {
return this.getEntry(bcoin.chain.global);
});
Block.prototype.__defineGetter__('orphan', function() {
return this.isOrphan(bcoin.chain.global);
});
Block.prototype.inspect = function inspect() { Block.prototype.inspect = function inspect() {
var copy = bcoin.block(this, this.subtype); var copy = bcoin.block(this, this.subtype);
copy.__proto__ = null; copy.__proto__ = null;

View File

@ -73,7 +73,7 @@ function Chain(options) {
// Last TS after preload, needed for fill percent // Last TS after preload, needed for fill percent
this.index.lastTs = this.index.entries[this.index.entries.length - 1].ts; this.index.lastTs = this.index.entries[this.index.entries.length - 1].ts;
bcoin.chain.global = this; Chain.global = this;
this.loading = false; this.loading = false;
this._init(); this._init();
@ -195,7 +195,7 @@ Chain.prototype.resetHeight = function resetHeight(height) {
this.orphan.map = {}; this.orphan.map = {};
this.orphan.bmap = {}; this.orphan.bmap = {};
this.orphan.count = 0; this.orphan.count = 0;
this.orhpan.size = 0; this.orphan.size = 0;
this.index.entries.length = height + 1; this.index.entries.length = height + 1;
this.index.heights = this.index.entries.reduce(function(out, entry) { this.index.heights = this.index.entries.reduce(function(out, entry) {
if (!self.options.fullNode) { if (!self.options.fullNode) {
@ -293,6 +293,9 @@ Chain.prototype.add = function add(block, peer) {
// Breaking here only works because // Breaking here only works because
// we deleted the orphan map in resetHeight. // we deleted the orphan map in resetHeight.
this.emit('block', block, peer); this.emit('block', block, peer);
this.emit('entry', entry);
if (block !== initial)
this.emit('resolved', entry);
break; break;
} }
} }
@ -300,6 +303,9 @@ Chain.prototype.add = function add(block, peer) {
// Validated known block at this point - add it to index // Validated known block at this point - add it to index
code = this._addIndex(entry); code = this._addIndex(entry);
this.emit('block', block, peer); this.emit('block', block, peer);
this.emit('entry', entry);
if (block !== initial)
this.emit('resolved', entry);
} }
// Fullfill request // Fullfill request

View File

@ -21,6 +21,8 @@ function Miner(options) {
if (!(this instanceof Miner)) if (!(this instanceof Miner))
return new Miner(options); return new Miner(options);
EventEmitter.call(this);
if (!options) if (!options)
options = {}; options = {};

View File

@ -145,6 +145,8 @@ function Pool(options) {
self.emit.apply(self, ['debug'].concat(args)); self.emit.apply(self, ['debug'].concat(args));
}); });
Pool.global = this;
if (!this.chain.loading) { if (!this.chain.loading) {
this._init(); this._init();
} else { } else {
@ -498,7 +500,12 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer) {
// Resolve orphan chain // Resolve orphan chain
if (!this.options.headers) { if (!this.options.headers) {
if (!this.chain.hasBlock(block.prevBlock)) { if (!this.chain.hasBlock(block.prevBlock)) {
this._addIndex(block, peer); // NOTE: If we were to emit new orphans here, we
// would not need to store full blocks as orphans.
// However, the listener would not be able to see
// the height until later.
if (this._addIndex(block, peer))
this.emit('pool block', block, peer);
peer.loadBlocks( peer.loadBlocks(
this.chain.locatorHashes(), this.chain.locatorHashes(),
this.chain.getOrphanRoot(block) this.chain.getOrphanRoot(block)
@ -508,7 +515,8 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer) {
} }
// Add to index and emit/save // Add to index and emit/save
this._addIndex(block, peer); if (this._addIndex(block, peer))
this.emit('pool block', block, peer);
}; };
Pool.prototype._addIndex = function _addIndex(block, peer) { Pool.prototype._addIndex = function _addIndex(block, peer) {
@ -521,11 +529,8 @@ Pool.prototype._addIndex = function _addIndex(block, peer) {
res = this.chain.add(block, peer); res = this.chain.add(block, peer);
if (res) { if (res > 0)
this.emit('debug', this.emit('chain-error', bcoin.chain.msg(res));
'Chain Error: ' + bcoin.chain.msg(res),
peer);
}
// Do not emit if nothing was added to the chain // Do not emit if nothing was added to the chain
if (this.chain.size() === size) { if (this.chain.size() === size) {