better events.

This commit is contained in:
Christopher Jeffrey 2016-02-17 17:07:12 -08:00
parent f7ac99d24d
commit 6ef09f39ec
4 changed files with 59 additions and 34 deletions

View File

@ -1050,7 +1050,9 @@ BlockData.prototype.closeAsync = function closeAsync(callback) {
};
BlockData.prototype._malloc = function _malloc(size) {
return new Buffer(size);
if (!this.options.usePool)
return new Buffer(size);
if (size > 500)
return new Buffer(size);
@ -1066,7 +1068,9 @@ BlockData.prototype._malloc = function _malloc(size) {
};
BlockData.prototype._free = function _free(buf) {
return;
if (!this.options.usePool)
return;
if (this._bufferPool.used[buf.length] === buf) {
assert(this._bufferPool[buf.length] === buf);
delete this._bufferPool.used[buf.length];

View File

@ -69,7 +69,25 @@ Chain.prototype._init = function _init() {
var self = this;
// Hook into events for debugging
this.on('fork', function(data, peer) {
this.on('block', function(block, entry, peer) {
var host = peer ? peer.host : 'unknown';
// utils.debug('Block %s (%d) added to chain (%s)',
// utils.revHex(entry.hash), entry.height, host);
});
this.on('resolved', function(block, entry, peer) {
var host = peer ? peer.host : 'unknown';
utils.debug('Orphan %s (%d) was resolved (%s)',
utils.revHex(entry.hash), entry.height, host);
});
this.on('checkpoint', function(block, data, peer) {
var host = peer ? peer.host : 'unknown';
utils.debug('Hit checkpoint block %s (%d) (%s)',
utils.revHex(data.checkpoint), data.height, host);
});
this.on('fork', function(block, data, peer) {
var host = peer ? peer.host : 'unknown';
utils.debug(
'Fork at height %d: expected=%s received=%s checkpoint=%s peer=%s',
@ -79,9 +97,11 @@ Chain.prototype._init = function _init() {
data.checkpoint,
host
);
if (data.checkpoint)
utils.debug('WARNING: Block failed a checkpoint.');
});
this.on('invalid', function(data, peer) {
this.on('invalid', function(block, data, peer) {
var host = peer ? peer.host : 'unknown';
utils.debug(
'Invalid block at height %d: hash=%s peer=%s',
@ -98,19 +118,19 @@ Chain.prototype._init = function _init() {
}
});
this.on('exists', function(data, peer) {
this.on('exists', function(block, data, peer) {
var host = peer ? peer.host : 'unknown';
utils.debug('Already have block %s (%s)',
data.height, host);
});
this.on('orphan', function(data, peer) {
this.on('orphan', function(block, data, peer) {
var host = peer ? peer.host : 'unknown';
utils.debug('Handled orphan %s (%s)', utils.revHex(data.hash), host);
});
this.on('purge', function(count, size) {
utils.debug('Warning: %dmb of orphans cleared!', utils.mb(size));
this.on('purge', function(count, size, peer) {
utils.debug('Warning: %d (%dmb) orphans cleared!', coin, utils.mb(size));
});
this.loading = true;
@ -392,10 +412,8 @@ Chain.prototype._removeBlock = function _removeBlock(tip, callback) {
if (self.mempool)
self.mempool.removeBlock(block);
self.emit('reorg block', block.hash('hex'));
block.txs.forEach(function(tx) {
self.emit('reorg tx', tx.hash('hex'));
self.emit('remove', tx.hash('hex'), block.hash('hex'));
});
return callback();
@ -841,7 +859,7 @@ Chain.prototype.add = function add(initial, peer, callback) {
// Do not revalidate known invalid blocks.
if (self.invalid[hash] || self.invalid[prevHash]) {
self.emit('invalid', {
self.emit('invalid', block, {
height: -1,
hash: hash,
seen: true,
@ -859,7 +877,7 @@ Chain.prototype.add = function add(initial, peer, callback) {
// orphans.
if (block === initial && !block.verify()) {
self.invalid[hash] = true;
self.emit('invalid', {
self.emit('invalid', block, {
height: prevHeight + 1,
hash: hash,
seen: false,
@ -875,12 +893,12 @@ Chain.prototype.add = function add(initial, peer, callback) {
// If the orphan chain forked, simply
// reset the orphans and find a new peer.
if (orphan.hash('hex') !== hash) {
self.emit('purge', self.orphan.count, self.orphan.size);
self.emit('purge', self.orphan.count, self.orphan.size, peer);
self.orphan.map = {};
self.orphan.bmap = {};
self.orphan.count = 0;
self.orphan.size = 0;
self.emit('fork', {
self.emit('fork', block, {
height: -1,
expected: orphan.hash('hex'),
received: hash,
@ -888,7 +906,7 @@ Chain.prototype.add = function add(initial, peer, callback) {
}, peer);
return done();
}
self.emit('orphan', {
self.emit('orphan', block, {
height: -1,
hash: hash,
seen: true
@ -903,7 +921,7 @@ Chain.prototype.add = function add(initial, peer, callback) {
self.orphan.size += block.getSize();
self.orphan.map[prevHash] = block;
self.orphan.bmap[hash] = block;
self.emit('orphan', {
self.emit('orphan', block, {
height: -1,
hash: hash,
seen: false
@ -932,18 +950,18 @@ Chain.prototype.add = function add(initial, peer, callback) {
// who isn't trying to fool us.
checkpoint = network.checkpoints[entry.height];
if (checkpoint) {
self.emit('checkpoint', {
self.emit('checkpoint', block, {
height: entry.height,
hash: entry.hash,
checkpoint: checkpoint
});
}, peer);
if (hash !== checkpoint) {
// Resetting to the last checkpoint _really_ isn't
// necessary (even bitcoind doesn't do it), but it
// could be used if you want to be on the overly
// safe (see: paranoid) side.
// this.resetLastCheckpoint(entry.height);
self.emit('fork', {
self.emit('fork', block, {
height: entry.height,
expected: network.checkpoints[entry.height],
received: entry.hash,
@ -964,7 +982,7 @@ Chain.prototype.add = function add(initial, peer, callback) {
// a stack overflow if there are a lot of
// existing blocks.
if (existing.hash === hash) {
self.emit('exists', {
self.emit('exists', block, {
height: entry.height,
hash: entry.hash
}, peer);
@ -993,7 +1011,7 @@ Chain.prototype.add = function add(initial, peer, callback) {
self.resetHeight(existing.height - 1);
self.emit('fork', {
self.emit('fork', block, {
height: existing.height,
expected: existing.hash,
received: entry.hash,
@ -1020,7 +1038,7 @@ Chain.prototype.add = function add(initial, peer, callback) {
if (!verified) {
self.invalid[entry.hash] = true;
self.emit('invalid', {
self.emit('invalid', block, {
height: entry.height,
hash: entry.hash,
seen: false,
@ -1051,10 +1069,9 @@ Chain.prototype.add = function add(initial, peer, callback) {
// Emit our block (and potentially resolved
// orphan) so the programmer can save it.
self.emit('block', block, peer);
self.emit('entry', entry);
self.emit('block', block, entry, peer);
if (block !== initial)
self.emit('resolved', block, peer);
self.emit('resolved', block, entry, peer);
// Fullfill request
self.request.fullfill(hash, block);
@ -1084,7 +1101,7 @@ Chain.prototype.add = function add(initial, peer, callback) {
// Failsafe for large orphan chains. Do not
// allow more than 20mb stored in memory.
if (self.orphan.size > self.orphanLimit) {
self.emit('purge', self.orphan.count, self.orphan.size);
self.emit('purge', self.orphan.count, self.orphan.size, peer);
Object.keys(self.orphan.bmap).forEach(function(hash) {
self.emit('unresolved', self.orphan.bmap[hash], peer);
});

View File

@ -113,7 +113,9 @@ ChainDB.prototype.closeAsync = function closeAsync(callback) {
};
ChainDB.prototype._malloc = function _malloc(size) {
return new Buffer(size);
if (!this.options.usePool)
return new Buffer(size);
if (!this._bufferPool[size])
this._bufferPool[size] = new Buffer(size);
@ -126,7 +128,9 @@ ChainDB.prototype._malloc = function _malloc(size) {
};
ChainDB.prototype._free = function _free(buf) {
return;
if (!this.options.usePool)
return;
if (this._bufferPool.used[buf.length] === buf) {
assert(this._bufferPool[buf.length] === buf);
delete this._bufferPool.used[buf.length];

View File

@ -176,7 +176,7 @@ Pool.prototype._init = function _init() {
this._addPeer();
}
this.chain.on('block', function(block, peer) {
this.chain.on('block', function(block, entry, peer) {
self.emit('block', block, peer);
// Emit merkle txs after the fact
if (block.subtype === 'merkleblock') {
@ -186,7 +186,7 @@ Pool.prototype._init = function _init() {
}
});
this.chain.on('fork', function(data, peer) {
this.chain.on('fork', function(block, data, peer) {
self.emit('fork', data, peer);
if (!peer)
@ -202,21 +202,21 @@ Pool.prototype._init = function _init() {
peer.destroy();
});
this.chain.on('invalid', function(data, peer) {
this.chain.on('invalid', function(block, data, peer) {
if (!peer)
return;
self.setMisbehavior(peer, 100);
});
this.chain.on('exists', function(data, peer) {
this.chain.on('exists', function(block, data, peer) {
if (!peer)
return;
self.setMisbehavior(peer, 1);
});
this.chain.on('orphan', function(data, peer) {
this.chain.on('orphan', function(block, data, peer) {
var host = peer ? peer.host : 'unknown';
if (!peer)