From b616d75128b3dbe252e313da78c0a1aaa30fa174 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 20 Sep 2016 22:26:47 -0700 Subject: [PATCH] refactor: db. --- lib/db/lowlevelup.js | 25 +++++-------------------- lib/net/pool.js | 5 +++-- lib/node/fullnode.js | 1 + lib/utils/spawn.js | 33 +++++++++++++++------------------ 4 files changed, 24 insertions(+), 40 deletions(-) diff --git a/lib/db/lowlevelup.js b/lib/db/lowlevelup.js index 447b678d..d439d170 100644 --- a/lib/db/lowlevelup.js +++ b/lib/db/lowlevelup.js @@ -153,11 +153,8 @@ LowlevelUp.prototype.get = function get(key, options) { assert(this.loaded, 'Cannot use database before it is loaded.'); - if (!options) - options = {}; - return new Promise(function(resolve, reject) { - self.binding.get(key, options, function(err, result) { + self.binding.get(key, options || {}, function(err, result) { if (err) { if (isNotFound(err)) return resolve(); @@ -216,7 +213,7 @@ LowlevelUp.prototype.batch = function batch(ops, options) { return new Batch(this); return new Promise(function(resolve, reject) { - self.binding.batch(ops, options, P(resolve, reject)); + self.binding.batch(ops, options || {}, P(resolve, reject)); }); }; @@ -451,11 +448,7 @@ Batch.prototype.del = function del(key) { Batch.prototype.write = function write() { var self = this; return new Promise(function(resolve, reject) { - self.batch.write(function(err) { - if (err) - return reject(err); - resolve(); - }); + self.batch.write(P(resolve, reject)); }); }; @@ -481,11 +474,7 @@ Iterator.prototype.next = function() { } if (key === undefined && value === undefined) { - self.iter.end(function(err) { - if (err) - return reject(err); - resolve(); - }); + self.iter.end(P(resolve, reject)); return; } @@ -501,11 +490,7 @@ Iterator.prototype.seek = function seek(key) { Iterator.prototype.end = function end() { var self = this; return new Promise(function(resolve, reject) { - self.iter.end(function(err) { - if (err) - return reject(err); - resolve(); - }); + self.iter.end(P(resolve, reject)); }); }; diff --git a/lib/net/pool.js b/lib/net/pool.js index 14b25b07..09ca274d 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -1519,6 +1519,7 @@ Pool.prototype.watchAddress = function watchAddress(address) { Pool.prototype.getData = function getData(peer, type, hash) { return spawn(function *() { + var self = this; var item, exists; if (!this.loaded) @@ -1534,10 +1535,10 @@ Pool.prototype.getData = function getData(peer, type, hash) { if (type === this.txType) { if (peer.queueTX.length === 0) { utils.nextTick(function() { - this.logger.debug( + self.logger.debug( 'Requesting %d/%d txs from peer with getdata (%s).', peer.queueTX.length, - this.activeTX, + self.activeTX, peer.hostname); peer.getData(peer.queueTX); diff --git a/lib/node/fullnode.js b/lib/node/fullnode.js index ffb678c0..2fe82ea5 100644 --- a/lib/node/fullnode.js +++ b/lib/node/fullnode.js @@ -194,6 +194,7 @@ Fullnode.prototype._init = function _init() { }); this.chain.on('connect', function(entry, block) { + return; self.walletdb.addBlock(entry, block.txs).catch(onError); if (self.chain.synced) diff --git a/lib/utils/spawn.js b/lib/utils/spawn.js index 9de45e70..5c4121bd 100644 --- a/lib/utils/spawn.js +++ b/lib/utils/spawn.js @@ -2,42 +2,39 @@ // See: https://github.com/yoursnetwork/asink -function spawn(genF, self) { +function spawn(generator, self) { return new Promise(function(resolve, reject) { - var gen = genF.call(self); + var gen = generator.call(self); - function step(nextF) { + function step(value, rejection) { var next; try { - next = nextF(); + if (rejection) + next = gen.throw(value); + else + next = gen.next(value); } catch (e) { - // finished with failure, reject the promise reject(e); return; } if (next.done) { - // finished with success, resolve the promise resolve(next.value); return; } - // not finished, chain off the yielded promise and `step` again - Promise.resolve(next.value).then(function(v) { - step(function() { - return gen.next(v); - }); - }, function (e) { - step(function() { - return gen.throw(e); - }); + if (!(next.value instanceof Promise)) { + step(next.value); + return; + } + + next.value.then(step, function(e) { + step(e, true); }); } - step(function() { - return gen.next(undefined); - }); + step(undefined); }); }