From 67e739ef3956bb594da5e7c9588dc156d558a6f2 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 5 Mar 2016 08:43:52 -0800 Subject: [PATCH] fixes. --- lib/bcoin/blockdb.js | 6 ++--- lib/bcoin/chain.js | 5 ++++ lib/bcoin/chainblock.js | 4 ++- lib/bcoin/chaindb.js | 55 ++++++++++++++++++++++++++++++++++++----- lib/bcoin/pool.js | 4 +-- lib/bcoin/tx.js | 2 +- 6 files changed, 63 insertions(+), 13 deletions(-) diff --git a/lib/bcoin/blockdb.js b/lib/bcoin/blockdb.js index 7a7e72ae..130334aa 100644 --- a/lib/bcoin/blockdb.js +++ b/lib/bcoin/blockdb.js @@ -35,15 +35,15 @@ function BlockDB(node, options) { bcoin.ensurePrefix(); if (!this.file) - this.file = bcoin.prefix + '/index-' + network.type + '.db'; + this.file = bcoin.prefix + '/block-' + network.type + '.db'; this.options = options; this.node = node; this.cache = { - unspent: new bcoin.lru(32 * 1024 * 1024), - tx: new bcoin.lru(32 * 1024 * 1024) + unspent: new bcoin.lru(32 * 1024 * 1024, function() { return 80; }), + tx: new bcoin.lru(32 * 1024 * 1024, function(tx) { return tx.getSize(); }) }; if (+process.env.BCOIN_FRESH === 1 && bcoin.cp) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 1e23e6f6..19c2129d 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -275,6 +275,9 @@ Chain.prototype._preload = function _preload(callback) { if (!this.options.preload) return callback(); + if (!this.options.spv) + return callback(); + if (network.type !== 'main') return callback(new Error('Electrum.org only offers `main` headers.')); @@ -484,6 +487,8 @@ Chain.prototype._verify = function _verify(block, prev, callback) { if (block.bits !== self.getTarget(prev, block)) { utils.debug('Block is using wrong target: %s', block.rhash); + utils.debug('Has %d, expected: %d', + block.bits, self.getTarget(prev, block)); return done(null, false); } diff --git a/lib/bcoin/chainblock.js b/lib/bcoin/chainblock.js index 3a08b018..4259f9cb 100644 --- a/lib/bcoin/chainblock.js +++ b/lib/bcoin/chainblock.js @@ -178,8 +178,10 @@ ChainBlock.prototype._alloc = function _alloc(max, callback) { if (this.previous.length >= max) return callback(); - if (!this.chain.db.hasCache(entry.prevBlock)) + if (!this.chain.db.hasCache(entry.prevBlock)) { + this.previous.pop(); break; + } entry = this.chain.db.getCache(entry.prevBlock); } diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index 8919218d..77791d49 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -56,7 +56,7 @@ function ChainDB(node, chain, options) { // http://hur.st/bloomfilter?n=800000&p=0.01 (m=936kb, k=7) // 10% false positive rate for 800k blocks // http://hur.st/bloomfilter?n=800000&p=0.10 (m=468kb, k=3) - this.bloom = new bcoin.bloom(937 * 1024, 7, 0xdeadbeef); + // this.bloom = new bcoin.bloom(937 * 1024, 7, 0xdeadbeef); // Need to cache up to the retarget interval // if we're going to be checking the damn @@ -68,6 +68,8 @@ function ChainDB(node, chain, options) { this.cacheHash = new DumbCache(this._cacheWindow * 200); // (not hashcash) this.cacheHeight = new DumbCache(this._cacheWindow * 200); + // this.cacheHash = new bcoin.lru(this._cacheWindow, function() { return 1; }); // (not hashcash) + // this.cacheHeight = new bcoin.lru(this._cacheWindow, function() { return 1; }); this._init(); } @@ -130,8 +132,10 @@ ChainDB.prototype.load = function load(callback) { callback(); } - this.has(genesis.hash, function(err, exists) { - assert(!err); + this.db.get('c/h/' + genesis.hash, function(err, exists) { + if (err && err.type !== 'NotFoundError') + throw err; + if (!exists) self.save(genesis, finish); else @@ -183,8 +187,8 @@ ChainDB.prototype.getHeight = function getHeight(hash, callback) { if (this.cacheHash.has(hash)) return callback(null, this.cacheHash.get(hash).height); - if (!this.bloom.test(hash, 'hex')) - return callback(null, -1); + // if (!this.bloom.test(hash, 'hex')) + // return callback(null, -1); this.db.get('c/h/' + hash, function(err, height) { if (err && err.type !== 'NotFoundError') @@ -218,6 +222,45 @@ ChainDB.prototype.getHash = function getHash(height, callback) { }); }; +ChainDB.prototype.dump = function dump(callback) { + var self = this; + var records = {}; + + var iter = this.db.db.iterator({ + gte: 'c', + lte: 'c~', + keys: true, + values: true, + fillCache: false, + keyAsBuffer: false, + valueAsBuffer: true + }); + + callback = utils.ensure(callback); + + (function next() { + iter.next(function(err, key, value) { + if (err) { + return iter.end(function() { + callback(err); + }); + } + + if (key === undefined) { + return iter.end(function(err) { + if (err) + return callback(err); + return callback(null, records); + }); + } + + records[key] = value; + + next(); + }); + })(); +}; + ChainDB.prototype.getChainHeight = function getChainHeight(callback) { return this.getTip(function(err, entry) { if (err) @@ -327,7 +370,7 @@ ChainDB.prototype.save = function save(entry, callback) { // (necessary for isSuperMajority) this.addCache(entry); - this.bloom.add(entry.hash, 'hex'); + // this.bloom.add(entry.hash, 'hex'); batch = this.db.batch(); height = new Buffer(4); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 16e5bcca..ec7c01ef 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -204,7 +204,7 @@ Pool.prototype._init = function _init() { if (!peer) return; - self.setMisbehavior(peer, 1); + // self.setMisbehavior(peer, 1); }); this.chain.on('orphan', function(block, data, peer) { @@ -215,7 +215,7 @@ Pool.prototype._init = function _init() { // Increase banscore by 10 if we're using getheaders. if (self.options.headers) { - self.setMisbehavior(peer, 10); + // self.setMisbehavior(peer, 10); return; } diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index c6ecb6fc..434ac4d0 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -29,7 +29,7 @@ function TX(data, block, index) { this.locktime = data.locktime || 0; this.ts = data.ts || 0; this.block = data.block || null; - this.index = data.index || -1; + this.index = data.index != null ? data.index : -1; this.ps = this.ts === 0 ? utils.now() : 0; this._hash = null;