From f2fccdd14f7094a1253d30f7496bbcea40cc70d4 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Thu, 19 Mar 2015 13:18:31 -0300 Subject: [PATCH] sync first attempt --- lib/services/block.js | 26 +++++++++++++++++++++----- package.json | 3 ++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/services/block.js b/lib/services/block.js index 712e6f37..d95edd7c 100644 --- a/lib/services/block.js +++ b/lib/services/block.js @@ -206,9 +206,27 @@ BlockService.prototype.getLatest = function() { return self.getBlock(blockHash); - }).catch(blockNotFound); + }).catch(function(err) { + if (err instanceof LevelUp.errors.NotFoundError) { + return null; + } + throw err; + }); }; + +/** + * Save a new block + * + * @param {bitcore.Block} block + * @return {Promise} a promise of the same block, for chaining + */ +BlockService.prototype.save = function(block) { + // TODO: unconfirm previous tip, confirm new tip. + return this._confirmBlock(block); +}; + + /** * Set a block as the current tip of the blockchain * @@ -250,11 +268,9 @@ BlockService.prototype._confirmBlock = function(block) { }).then(function() { - return self.database.batchAsync(ops) + return self.database.batchAsync(ops); - }).then(function() { - return self.unlock(); - }); + }).then(this.unlock.bind(this)); }; BlockService.prototype._setNextBlock = function(ops, prevBlockHash, block) { diff --git a/package.json b/package.json index ecce833d..cb4592e0 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,8 @@ "glob": "*", "js-yaml": "^3.2.7", "level-lock": "^1.0.1", - "levelup": "~0.19.0", + "leveldown": "^1.0.0", + "levelup": "^0.19.0", "moment": "~2.5.0", "morgan": "^1.5.1", "request": "^2.48.0",