From 5069ec1bfbeeb907165083ee576374ed352ec0aa Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 4 Nov 2016 19:10:42 -0700 Subject: [PATCH] walletdb: list blocks. --- bin/cli | 12 ++++++++++-- lib/http/client.js | 11 +++++++++++ lib/http/server.js | 6 ++++++ lib/http/wallet.js | 8 ++++++++ lib/wallet/browser.js | 3 ++- lib/wallet/txdb.js | 16 +++++++++++++++- lib/wallet/wallet.js | 9 +++++++++ lib/wallet/walletdb.js | 3 +++ 8 files changed, 64 insertions(+), 4 deletions(-) diff --git a/bin/cli b/bin/cli index 94a7c6f8..0eacbc6f 100755 --- a/bin/cli +++ b/bin/cli @@ -320,10 +320,15 @@ CLI.prototype.getDetails = co(function* getDetails() { this.log(details); }); +CLI.prototype.getWalletBlocks = co(function* getWalletBlocks() { + var blocks = yield this.wallet.getBlocks(); + this.log(blocks); +}); + CLI.prototype.getWalletBlock = co(function* getWalletBlock() { var height = this.argv[0] | 0; - var details = yield this.wallet.getBlock(height); - this.log(details); + var block = yield this.wallet.getBlock(height); + this.log(block); }); CLI.prototype.retoken = co(function* retoken() { @@ -486,6 +491,8 @@ CLI.prototype.handleWallet = co(function* handleWallet() { return yield this.zapWallet(); case 'tx': return yield this.getDetails(); + case 'blocks': + return yield this.getWalletBlocks(); case 'block': return yield this.getWalletBlock(); case 'view': @@ -526,6 +533,7 @@ CLI.prototype.handleWallet = co(function* handleWallet() { this.log(' $ sign [tx-hex]: Sign transaction.'); this.log(' $ zap [age?]: Zap pending wallet TXs.'); this.log(' $ tx [hash]: View transaction details.'); + this.log(' $ blocks: List wallet blocks.'); this.log(' $ block [height]: View wallet block.'); this.log(' $ view [tx-hex]: Parse and view transaction.'); this.log(' $ import [wif|hex]: Import private or public key.'); diff --git a/lib/http/client.js b/lib/http/client.js index 486c5b40..5ed9ecf7 100644 --- a/lib/http/client.js +++ b/lib/http/client.js @@ -541,6 +541,17 @@ HTTPClient.prototype.getWalletTX = function getWalletTX(id, hash) { return this._get('/wallet/' + id + '/tx/' + hash); }; +/** + * Get wallet blocks. + * @param {WalletID} id + * @param {Number} height + * @returns {Promise} + */ + +HTTPClient.prototype.getWalletBlocks = function getWalletBlocks(id) { + return this._get('/wallet/' + id + '/block'); +}; + /** * Get wallet block. * @param {WalletID} id diff --git a/lib/http/server.js b/lib/http/server.js index f4aa0695..9768a874 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -917,6 +917,12 @@ HTTPServer.prototype._init = function _init() { send(200, { success: true }); })); + // List blocks + this.get('/wallet/:id/block', con(function* (req, res, send, next) { + var heights = yield req.wallet.getBlocks(); + send(200, heights); + })); + // Get Block Record this.get('/wallet/:id/block/:height', con(function* (req, res, send, next) { var height = req.options.height; diff --git a/lib/http/wallet.js b/lib/http/wallet.js index b7d6f805..fb49fc5a 100644 --- a/lib/http/wallet.js +++ b/lib/http/wallet.js @@ -198,6 +198,14 @@ HTTPWallet.prototype.getTX = function getTX(hash) { return this.client.getWalletTX(this.id, hash); }; +/** + * @see Wallet#getBlocks + */ + +HTTPWallet.prototype.getBlocks = function getBlocks() { + return this.client.getWalletBlocks(this.id); +}; + /** * @see Wallet#getBlock */ diff --git a/lib/wallet/browser.js b/lib/wallet/browser.js index 360fbcd0..12515125 100644 --- a/lib/wallet/browser.js +++ b/lib/wallet/browser.js @@ -199,7 +199,8 @@ layout.txdb = { return 'b' + pad32(height); }, bb: function bb(key) { - return +key.slice(1); + key = key.slice(12); + return +key.slice(0); } }; diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index d7548f9f..c0917832 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -198,7 +198,8 @@ var layout = { return key; }, bb: function bb(key) { - return key.readUInt32BE(1, true); + key = key.slice(6); + return key.readUInt32BE(0, true); } }; @@ -974,6 +975,19 @@ TXDB.prototype.removeBlockMap = co(function* removeBlockMap(tx, height) { this.walletdb.writeBlockMap(this.wallet, height, block); }); +/** + * List block records. + * @returns {Promise} + */ + +TXDB.prototype.getBlocks = function getBlocks() { + return this.keys({ + gte: layout.b(0), + lte: layout.b(0xffffffff), + parse: layout.bb + }); +}; + /** * Get block record. * @param {Number} height diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index b18383da..f5d9b538 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -1984,6 +1984,15 @@ Wallet.prototype.getTX = function getTX(hash) { return this.txdb.getTX(hash); }; +/** + * List blocks for the wallet. + * @returns {Promise} - Returns {@link BlockRecord}. + */ + +Wallet.prototype.getBlocks = function getBlocks() { + return this.txdb.getBlocks(); +}; + /** * Get a block from the wallet. * @param {Number} height diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 6cadf85a..9570b5cd 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -846,8 +846,11 @@ WalletDB.prototype._rename = co(function* _rename(wallet, id) { WalletDB.prototype.renameAccount = function renameAccount(account, name) { var wallet = account.wallet; var batch = this.batch(wallet); + batch.del(layout.i(account.wid, account.name)); + account.name = name; + this.saveAccount(account); };