diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 3ec97558..4e38c1fb 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -133,48 +133,31 @@ Chain.prototype._init = function _init() { }); this.on('resolved', function(block, entry) { - bcoin.debug('Orphan %s (%d) was resolved.', - utils.revHex(entry.hash), entry.height); + bcoin.debug('Orphan %s (%d) was resolved.', block.rhash, entry.height); }); - this.on('checkpoint', function(block, data) { - bcoin.debug('Hit checkpoint block %s (%d).', - utils.revHex(data.checkpoint), data.height); + this.on('checkpoint', function(block, height) { + bcoin.debug('Hit checkpoint block %s (%d).', block.rhash, height); }); - this.on('fork', function(block, data) { + this.on('fork', function(block, height, expected) { bcoin.debug( - 'Fork at height %d: expected=%s received=%s checkpoint=%s', - data.height, - utils.revHex(data.expected), - utils.revHex(data.received), - data.checkpoint + 'Fork at height %d: expected=%s received=%s', + height, + utils.revHex(expected), + block.rhash ); - if (data.checkpoint) - bcoin.debug('WARNING: Block failed a checkpoint.'); }); - this.on('invalid', function(block, data) { - bcoin.debug( - 'Invalid block at height %d: hash=%s', - data.height, - utils.revHex(data.hash) - ); - if (data.chain) { - bcoin.debug( - 'Peer is sending an invalid continuation chain.'); - } else if (data.seen) { - bcoin.debug('Peer is sending an invalid chain.'); - } + this.on('invalid', function(block, height) { + bcoin.debug('Invalid block at height %d: hash=%s', height, block.rhash); }); - this.on('exists', function(block, data) { - bcoin.debug('Already have block %s (%d).', - utils.revHex(data.hash), data.height); - }); + this.on('exists', function(block, height) { + bcoin.debug('Already have block %s (%d).', block.rhash, height); - this.on('orphan', function(block, data) { - bcoin.debug('Handled orphan %s.', utils.revHex(data.hash)); + this.on('orphan', function(block, height) { + bcoin.debug('Handled orphan %s (%d).', block.rhash, height); }); this.on('purge', function(count, size) { @@ -1068,12 +1051,7 @@ Chain.prototype.reorganize = function reorganize(entry, block, callback) { if (err) return callback(err); - self.emit('fork', block, { - height: fork.height, - expected: tip.hash, - received: entry.hash, - checkpoint: false - }); + self.emit('fork', block, tip.height, tip.hash); return callback(); }); @@ -1141,12 +1119,7 @@ Chain.prototype.connect = function connect(entry, callback) { if (err) { if (err.type === 'VerifyError') { self.invalid[entry.hash] = true; - self.emit('invalid', block, { - height: entry.height, - hash: entry.hash, - seen: false, - chain: false - }); + self.emit('invalid', block, entry.height); } return callback(err); } @@ -1200,12 +1173,7 @@ Chain.prototype.setBestChain = function setBestChain(entry, block, prev, callbac if (err.type === 'VerifyError') { self.invalid[entry.hash] = true; - self.emit('invalid', block, { - height: entry.height, - hash: entry.hash, - seen: false, - chain: false - }); + self.emit('invalid', block, entry.height); } return callback(err); @@ -1372,22 +1340,14 @@ Chain.prototype.add = function add(block, callback, force) { // Do not revalidate known invalid blocks. if (self.invalid[hash] || self.invalid[prevHash]) { - self.emit('invalid', block, { - height: block.getCoinbaseHeight(), - hash: hash, - seen: !!self.invalid[hash], - chain: !!self.invalid[prevHash] - }); + self.emit('invalid', block, block.getCoinbaseHeight()); self.invalid[hash] = true; return done(new VerifyError(block, 'duplicate', 'duplicate', 100)); } // Do we already have this block? if (self.hasPending(hash)) { - self.emit('exists', block, { - height: block.getCoinbaseHeight(), - hash: hash - }); + self.emit('exists', block, block.getCoinbaseHeight()); return done(new VerifyError(block, 'duplicate', 'duplicate', 0)); } @@ -1395,22 +1355,14 @@ Chain.prototype.add = function add(block, callback, force) { // an orphan, ignore it. orphan = self.orphan.map[prevHash]; if (orphan) { - // If the orphan chain forked, simply - // reset the orphans. + // The orphan chain forked. if (orphan.hash('hex') !== hash) { - self.emit('fork', block, { - height: block.getCoinbaseHeight(), - expected: orphan.hash('hex'), - received: hash, - checkpoint: false - }); + self.emit('fork', block, + block.getCoinbaseHeight(), + orphan.hash('hex')); } - self.emit('orphan', block, { - height: block.getCoinbaseHeight(), - hash: hash, - seen: true - }); + self.emit('orphan', block, block.getCoinbaseHeight()); return done(new VerifyError(block, 'invalid', 'bad-prevblk', 0)); } @@ -1425,12 +1377,7 @@ Chain.prototype.add = function add(block, callback, force) { // orphans. if (initial && !block.verify(ret)) { self.invalid[hash] = true; - self.emit('invalid', block, { - height: block.getCoinbaseHeight(), - hash: hash, - seen: false, - chain: false - }); + self.emit('invalid', block, block.getCoinbaseHeight()); return done(new VerifyError(block, 'invalid', ret.reason, ret.score)); } @@ -1440,10 +1387,7 @@ Chain.prototype.add = function add(block, callback, force) { // Do we already have this block? if (existing) { - self.emit('exists', block, { - height: block.getCoinbaseHeight(), - hash: hash - }); + self.emit('exists', block, block.getCoinbaseHeight()); return done(new VerifyError(block, 'duplicate', 'duplicate', 0)); } @@ -1476,44 +1420,28 @@ Chain.prototype.add = function add(block, callback, force) { self.network.updateHeight(self.bestHeight); } - self.emit('orphan', block, { - height: block.getCoinbaseHeight(), - hash: hash, - seen: false - }); + self.emit('orphan', block, block.getCoinbaseHeight()); return done(new VerifyError(block, 'invalid', 'bad-prevblk', 0)); } // Verify the checkpoint. - checkpoint = self.network.checkpoints[height]; - if (checkpoint) { - self.emit('checkpoint', block, { - height: height, - hash: hash, - checkpoint: checkpoint - }); + if (self.options.useCheckpoints) { + checkpoint = self.network.checkpoints[height]; + if (checkpoint) { + // Someone is very likely trying to fool us. + if (hash !== checkpoint) { + self.purgeOrphans(); - // Block did not match the checkpoint. The - // chain could be reset to the last sane - // checkpoint, but it really isn't necessary, - // so we don't do it. The misbehaving peer has - // been killed and hopefully we find a peer - // who isn't trying to fool us. - if (hash !== checkpoint) { - self.purgeOrphans(); + self.emit('fork', block, height, checkpoint); - self.emit('fork', block, { - height: height, - expected: checkpoint, - received: hash, - checkpoint: true - }); + return done(new VerifyError(block, + 'checkpoint', + 'checkpoint mismatch', + 100)); + } - return done(new VerifyError(block, - 'checkpoint', - 'checkpoint mismatch', - 100)); + self.emit('checkpoint', block, height); } } diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 3d928159..9f8f0d66 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -231,24 +231,28 @@ Pool.prototype._init = function _init() { }); } - this.chain.on('block', function(block, entry, peer) { - self.emit('block', block, peer); + this.chain.on('block', function(block, entry) { + self.emit('block', block, entry); }); - this.chain.on('fork', function(block, data, peer) { - self.emit('fork', data, peer); + this.chain.on('competitor', function(block, entry) { + self.emit('competitor', block, entry); }); - this.chain.on('invalid', function(block, data, peer) { - self.emit('invalid', data, peer); + this.chain.on('fork', function(block, height, expected) { + self.emit('fork', block, height, expected); }); - this.chain.on('exists', function(block, data, peer) { - self.emit('exists', data, peer); + this.chain.on('invalid', function(block, height) { + self.emit('invalid', block, height); }); - this.chain.on('orphan', function(block, data, peer) { - self.emit('orphan', data, peer); + this.chain.on('exists', function(block, height) { + self.emit('exists', block, height); + }); + + this.chain.on('orphan', function(block, height) { + self.emit('orphan', block, height); }); this.chain.on('full', function() {