From 24b94a5359e7de2c10bc42551b04869438588c53 Mon Sep 17 00:00:00 2001 From: Nodari Chkuaselidze Date: Sat, 30 Mar 2024 16:57:38 +0400 Subject: [PATCH] wallet: deprecate getLast and getRange. Update hsw-cli history and pending. hsw-cli: - hsw-cli: `history` now accepts new args on top of `--account`: `--reverse`, `--limit`, `--after`, `--after`. - hsw-cli: `pending` now accepts new args, same as above. wallet-http: - Deprecate and remove: `GET /wallet/:id/tx/range` - Deprecate and remove: `GET /wallet/:id/tx/last` --- CHANGELOG.md | 12 ++- bin/hsw-cli | 22 +++++- lib/client/wallet.js | 58 -------------- lib/wallet/http.js | 37 --------- lib/wallet/txdb.js | 137 ++++------------------------------ lib/wallet/wallet.js | 26 ------- test/wallet-auction-test.js | 14 +++- test/wallet-deepclean-test.js | 17 +++-- test/wallet-test.js | 76 +++++++++++++------ 9 files changed, 122 insertions(+), 277 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c46c00b84..74cba90d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -92,7 +92,17 @@ process and allows parallel rescans. - `GET /wallet/:id/tx/history` - The params are now `time`, `after`, `limit`, and `reverse`. - `GET /wallet/:id/tx/unconfirmed` - The params are are same as above. - These endpoints have been deprecated: + +These endpoints have been deprecated: + - `GET /wallet/:id/tx/range` - Instead use the `time` param for the history and + unconfirmed endpoints. + - `GET /wallet/:id/tx/last` - Instead use `reverse` param for the history and + unconfirmed endpoints. + +##### Wallet CLI (hsw-cli) + - `history` now accepts new args on top of `--account`: `--reverse`, + `--limit`, `--after`, `--after`. + - `pending` now accepts new args, same as above. ##### Examples diff --git a/bin/hsw-cli b/bin/hsw-cli index 7727f2d3a..8f9f1c2c0 100755 --- a/bin/hsw-cli +++ b/bin/hsw-cli @@ -271,15 +271,29 @@ class CLI { } async getWalletHistory() { - const account = this.config.str('account'); - const txs = await this.wallet.getHistory(account); + const options = { + account: this.config.str('account'), + limit: this.config.uint('limit'), + reverse: this.config.bool('reverse'), + after: this.config.str('after'), + time: this.config.uint('time') + }; + + const txs = await this.wallet.getHistory(options); this.log(txs); } async getWalletPending() { - const account = this.config.str('account'); - const txs = await this.wallet.getPending(account); + const options = { + account: this.config.str('account'), + limit: this.config.uint('limit'), + reverse: this.config.bool('reverse'), + after: this.config.str('after'), + time: this.config.uint('time') + }; + + const txs = await this.wallet.getPending(options); this.log(txs); } diff --git a/lib/client/wallet.js b/lib/client/wallet.js index 2da798b6d..5180aec33 100644 --- a/lib/client/wallet.js +++ b/lib/client/wallet.js @@ -259,38 +259,6 @@ class WalletClient extends Client { return this.get(`/wallet/${id}/balance`, { account }); } - /** - * Get last N wallet transactions. - * @param {String} account - * @param {Number} limit - Max number of transactions. - * @returns {Promise} - */ - - getLast(id, account, limit) { - return this.get(`/wallet/${id}/tx/last`, { account, limit }); - } - - /** - * Get wallet transactions by timestamp range. - * @param {String} account - * @param {Object} options - * @param {Number} options.start - Start time. - * @param {Number} options.end - End time. - * @param {Number?} options.limit - Max number of records. - * @param {Boolean?} options.reverse - Reverse order. - * @returns {Promise} - */ - - getRange(id, account, options) { - return this.get(`/wallet/${id}/tx/range`, { - account: account, - start: options.start, - end: options.end, - limit: options.limit, - reverse: options.reverse - }); - } - /** * Get transaction (only possible if the transaction * is available in the wallet history). @@ -988,32 +956,6 @@ class Wallet extends EventEmitter { return this.client.getBalance(this.id, account); } - /** - * Get last N wallet transactions. - * @param {String} account - * @param {Number} limit - Max number of transactions. - * @returns {Promise} - */ - - getLast(account, limit) { - return this.client.getLast(this.id, account, limit); - } - - /** - * Get wallet transactions by timestamp range. - * @param {String} account - * @param {Object} options - * @param {Number} options.start - Start time. - * @param {Number} options.end - End time. - * @param {Number?} options.limit - Max number of records. - * @param {Boolean?} options.reverse - Reverse order. - * @returns {Promise} - */ - - getRange(account, options) { - return this.client.getRange(this.id, account, options); - } - /** * Get transaction (only possible if the transaction * is available in the wallet history). diff --git a/lib/wallet/http.js b/lib/wallet/http.js index 50f3ad505..f3f4659de 100644 --- a/lib/wallet/http.js +++ b/lib/wallet/http.js @@ -801,43 +801,6 @@ class HTTP extends Server { res.json(200, result); }); - // Wallet TXs within time range - this.get('/wallet/:id/tx/range', async (req, res) => { - const valid = Validator.fromRequest(req); - const acct = valid.str('account'); - - const options = { - start: valid.u32('start'), - end: valid.u32('end'), - limit: valid.u32('limit'), - reverse: valid.bool('reverse') - }; - - const txs = await req.wallet.getRange(acct, options); - const details = await req.wallet.toDetails(txs); - const result = []; - - for (const item of details) - result.push(item.getJSON(this.network, this.wdb.height)); - - res.json(200, result); - }); - - // Last Wallet TXs - this.get('/wallet/:id/tx/last', async (req, res) => { - const valid = Validator.fromRequest(req); - const acct = valid.str('account'); - const limit = valid.u32('limit'); - const txs = await req.wallet.getLast(acct, limit); - const details = await req.wallet.toDetails(txs); - const result = []; - - for (const item of details) - result.push(item.getJSON(this.network, this.wdb.height)); - - res.json(200, result); - }); - // Wallet TX this.get('/wallet/:id/tx/:hash', async (req, res) => { const valid = Validator.fromRequest(req); diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 3bf489315..6d84f817c 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -61,7 +61,7 @@ class TXDB { this.db = wdb.db; this.logger = wdb.logger; - this.maxTxs = wdb.options.maxHistoryTXs || 100; + this.maxTXs = wdb.options.maxHistoryTXs || 100; this.nowFn = util.now; this.wid = wid || 0; @@ -1093,7 +1093,6 @@ class TXDB { // Save and index the transaction record. b.put(layout.t.encode(hash), wtx.encode()); - b.put(layout.m.encode(wtx.mtime, hash), null); if (!block) b.put(layout.p.encode(hash), null); @@ -1107,7 +1106,6 @@ class TXDB { await this.updateAccountBalance(b, acct, delta); b.put(layout.T.encode(acct, hash), null); - b.put(layout.M.encode(acct, wtx.mtime, hash), null); if (!block) b.put(layout.P.encode(acct, hash), null); @@ -1468,7 +1466,6 @@ class TXDB { // Remove the transaction data // itself as well as unindex. b.del(layout.t.encode(hash)); - b.del(layout.m.encode(wtx.mtime, hash)); if (!block) b.del(layout.p.encode(hash)); @@ -1480,7 +1477,6 @@ class TXDB { await this.updateAccountBalance(b, acct, delta); b.del(layout.T.encode(acct, hash)); - b.del(layout.M.encode(acct, wtx.mtime, hash)); if (!block) b.del(layout.P.encode(acct, hash)); @@ -2141,8 +2137,8 @@ class TXDB { assert(typeof options.limit === 'number'); assert(typeof options.reverse === 'boolean'); - if (options.limit > this.maxTxs) - throw new Error(`Limit exceeds max of ${this.maxTxs}.`); + if (options.limit > this.maxTXs) + throw new Error(`Limit exceeds max of ${this.maxTXs}.`); let hashes = []; @@ -2186,8 +2182,8 @@ class TXDB { assert(typeof options.limit === 'number'); assert(typeof options.reverse === 'boolean'); - if (options.limit > this.maxTxs) - throw new Error(`Limit exceeds max of ${this.maxTxs}.`); + if (options.limit > this.maxTXs) + throw new Error(`Limit exceeds max of ${this.maxTXs}.`); let max = null; let min = null; @@ -2307,8 +2303,8 @@ class TXDB { assert(typeof options.reverse === 'boolean'); assert(typeof options.inclusive === 'boolean'); - if (options.limit > this.maxTxs) - throw new Error(`Limit exceeds max of ${this.maxTxs}.`); + if (options.limit > this.maxTXs) + throw new Error(`Limit exceeds max of ${this.maxTXs}.`); const count = await this.getCountForTX(options.hash); @@ -2365,8 +2361,8 @@ class TXDB { assert(typeof options.limit === 'number'); assert(typeof options.reverse === 'boolean'); - if (options.limit > this.maxTxs) - throw new Error(`Limit exceeds max of ${this.maxTxs}.`); + if (options.limit > this.maxTXs) + throw new Error(`Limit exceeds max of ${this.maxTXs}.`); const height = UNCONFIRMED_HEIGHT; @@ -2411,8 +2407,8 @@ class TXDB { assert(typeof options.limit === 'number'); assert(typeof options.reverse === 'boolean'); - if (options.limit > this.maxTxs) - throw new Error(`Limit exceeds max of ${this.maxTxs}.`); + if (options.limit > this.maxTXs) + throw new Error(`Limit exceeds max of ${this.maxTXs}.`); let max = null; let min = null; @@ -2535,8 +2531,8 @@ class TXDB { assert(typeof options.reverse === 'boolean'); assert(typeof options.inclusive === 'boolean'); - if (options.limit > this.maxTxs) - throw new Error(`Limit exceeds max of ${this.maxTxs}.`); + if (options.limit > this.maxTXs) + throw new Error(`Limit exceeds max of ${this.maxTXs}.`); const count = await this.getCountForTX(options.hash); @@ -3523,111 +3519,6 @@ class TXDB { return this.getHeightRangeHashes({ start: height, end: height }); } - /** - * Get TX hashes by timestamp range. - * @deprecated - * @param {Number} acct - * @param {Object} options - * @param {Number} options.start - Start height. - * @param {Number} options.end - End height. - * @param {Number?} options.limit - Max number of records. - * @param {Boolean?} options.reverse - Reverse order. - * @returns {Promise} - */ - - getAccountRangeHashes(acct, options) { - assert(typeof acct === 'number'); - - const start = options.start || 0; - const end = options.end || 0xffffffff; - - return this.bucket.keys({ - gte: layout.M.min(acct, start), - lte: layout.M.max(acct, end), - limit: options.limit, - reverse: options.reverse, - parse: (key) => { - const [,, hash] = layout.M.decode(key); - return hash; - } - }); - } - - /** - * Get TX hashes by timestamp range. - * @deprecated - * @param {Number} acct - * @param {Object} options - * @param {Number} options.start - Start height. - * @param {Number} options.end - End height. - * @param {Number?} options.limit - Max number of records. - * @param {Boolean?} options.reverse - Reverse order. - * @returns {Promise} - */ - - getRangeHashes(acct, options) { - assert(typeof acct === 'number'); - - if (acct !== -1) - return this.getAccountRangeHashes(acct, options); - - const start = options.start || 0; - const end = options.end || 0xffffffff; - - return this.bucket.keys({ - gte: layout.m.min(start), - lte: layout.m.max(end), - limit: options.limit, - reverse: options.reverse, - parse: (key) => { - const [, hash] = layout.m.decode(key); - return hash; - } - }); - } - - /** - * Get transactions by timestamp range. - * @deprecated - * @param {Number} acct - * @param {Object} options - * @param {Number} options.start - Start time. - * @param {Number} options.end - End time. - * @param {Number?} options.limit - Max number of records. - * @param {Boolean?} options.reverse - Reverse order. - * @returns {Promise} - */ - - async getRange(acct, options) { - const hashes = await this.getRangeHashes(acct, options); - const txs = []; - - for (const hash of hashes) { - const tx = await this.getTX(hash); - assert(tx); - txs.push(tx); - } - - return txs; - } - - /** - * Get last N transactions. - * @deprecated - * @param {Number} acct - * @param {Number} limit - Max number of transactions. - * @returns {Promise} - */ - - getLast(acct, limit) { - return this.getRange(acct, { - start: 0, - end: 0xffffffff, - reverse: true, - limit: limit || 10 - }); - } - /** * Get all transactions. * @param {Number} acct @@ -4138,7 +4029,7 @@ class TXDB { const now = this.nowFn(); const options = { time: now - age, - limit: Math.min(100, this.maxTxs), + limit: Math.min(100, this.maxTXs), reverse: true }; diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index f9141dc9d..1f47c993a 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -5019,32 +5019,6 @@ class Wallet extends EventEmitter { return this.txdb.getBalance(account); } - /** - * Get a range of transactions between two timestamps. - * @param {(String|Number)?} acct - * @param {Object} options - * @param {Number} options.start - * @param {Number} options.end - * @returns {Promise} - Returns {@link TX}[]. - */ - - async getRange(acct, options) { - const account = await this.ensureIndex(acct); - return this.txdb.getRange(account, options); - } - - /** - * Get the last N transactions. - * @param {(String|Number)?} acct - * @param {Number} limit - * @returns {Promise} - Returns {@link TX}[]. - */ - - async getLast(acct, limit) { - const account = await this.ensureIndex(acct); - return this.txdb.getLast(account, limit); - } - /** * @param {(String|Number)} [acct] * @param {Object} options diff --git a/test/wallet-auction-test.js b/test/wallet-auction-test.js index 267e111dd..0820aee02 100644 --- a/test/wallet-auction-test.js +++ b/test/wallet-auction-test.js @@ -834,7 +834,19 @@ describe('Wallet Auction', function() { const {receiveDepth} = acct; const addrIndexes = Array(receiveDepth - 1).fill(0); - const txs = await wallet.getHistory(); + const historyOptions = { + limit: 100, + reverse: false + }; + + const txs = []; + txs.push(...await wallet.listHistory(-1, historyOptions)); + txs.push(...await wallet.listHistoryAfter(-1, { + hash: txs[txs.length - 1].hash, + ...historyOptions + })); + + console.log(txs.length); const wtxs = await wallet.toDetails(txs); for (const wtx of wtxs) { for (const output of wtx.outputs) diff --git a/test/wallet-deepclean-test.js b/test/wallet-deepclean-test.js index 1675e4551..a9b6c0961 100644 --- a/test/wallet-deepclean-test.js +++ b/test/wallet-deepclean-test.js @@ -34,6 +34,11 @@ let bobAcct0Info, bobNames, bobBalance, bobHistory; const aliceBlinds = []; const bobBlinds = []; +const historyOptions = { + limit: 100, + reverse: false +}; + async function mineBlocks(n, addr) { addr = addr ? addr : new Address().toString('regtest'); for (let i = 0; i < n; i++) { @@ -141,12 +146,12 @@ describe('Wallet Deep Clean', function() { it('should save wallet data', async () => { aliceBalance = await alice.getBalance(); aliceNames = await alice.getNames(); - aliceHistory = await alice.getHistory(); + aliceHistory = await alice.listHistory(-1, historyOptions); aliceAcct0Info = await alice.getAccount(0); bobBalance = await bob.getBalance(); bobNames = await bob.getNames(); - bobHistory = await bob.getHistory(); + bobHistory = await bob.listHistory(-1, historyOptions); bobAcct0Info = await bob.getAccount(0); }); @@ -157,12 +162,12 @@ describe('Wallet Deep Clean', function() { it('should have erased wallet data', async () => { const aliceBalance2 = await alice.getBalance(); const aliceNames2 = await alice.getNames(); - const aliceHistory2 = await alice.getHistory(); + const aliceHistory2 = await alice.listHistory(-1, historyOptions); const aliceAcct0Info2 = await alice.getAccount(0); const bobBalance2 = await bob.getBalance(); const bobNames2 = await bob.getNames(); - const bobHistory2 = await bob.getHistory(); + const bobHistory2 = await bob.listHistory(-1, historyOptions); const bobAcct0Info2 = await bob.getAccount(0); // Account metadata is fine @@ -193,12 +198,12 @@ describe('Wallet Deep Clean', function() { it('should have recovered wallet data', async () => { const aliceBalance2 = await alice.getBalance(); const aliceNames2 = await alice.getNames(); - const aliceHistory2 = await alice.getHistory(); + const aliceHistory2 = await alice.listHistory(-1, historyOptions); const aliceAcct0Info2 = await alice.getAccount(0); const bobBalance2 = await bob.getBalance(); const bobNames2 = await bob.getNames(); - const bobHistory2 = await bob.getHistory(); + const bobHistory2 = await bob.listHistory(-1, historyOptions); const bobAcct0Info2 = await bob.getAccount(0); assert.deepStrictEqual(aliceBalance, aliceBalance2); diff --git a/test/wallet-test.js b/test/wallet-test.js index a0cbc6a47..13e1c0b81 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -242,7 +242,10 @@ describe('Wallet', function() { const balance = await alice.getBalance(); assert.strictEqual(balance.unconfirmed, 11000); - const txs = await alice.getHistory(); + const txs = await alice.listHistory(-1, { + reverse: false, + limit: 100 + }); assert(txs.some((wtx) => { return wtx.hash.equals(f1.hash()); })); @@ -252,7 +255,10 @@ describe('Wallet', function() { const balance = await bob.getBalance(); assert.strictEqual(balance.unconfirmed, 10000); - const txs = await bob.getHistory(); + const txs = await bob.listHistory(-1, { + reverse: false, + limit: 100 + }); assert(txs.some((wtx) => { return wtx.tx.hash().equals(f1.hash()); })); @@ -272,7 +278,10 @@ describe('Wallet', function() { assert.strictEqual(balance.unconfirmed, 11000); assert.strictEqual(balance.confirmed, 11000); - const txs = await alice.getHistory(); + const txs = await alice.listHistory(-1, { + limit: 100, + reverse: false + }); assert(txs.some((wtx) => { return wtx.hash.equals(f1.hash()); })); @@ -283,7 +292,10 @@ describe('Wallet', function() { assert.strictEqual(balance.unconfirmed, 10000); assert.strictEqual(balance.confirmed, 10000); - const txs = await bob.getHistory(); + const txs = await bob.listHistory(-1, { + limit: 100, + reverse: false + }); assert(txs.some((wtx) => { return wtx.tx.hash().equals(f1.hash()); })); @@ -297,7 +309,10 @@ describe('Wallet', function() { await wdb.removeBlock(curBlock(wdb)); { - const txs = await wallet.getHistory(); + const txs = await wallet.listHistory(-1, { + limit: 100, + reverse: false + }); assert.strictEqual(txs.length, 5); const total = txs.reduce((t, wtx) => { @@ -326,7 +341,10 @@ describe('Wallet', function() { } { - const txs = await wallet.getHistory(); + const txs = await wallet.listHistory(-1, { + limit: 100, + reverse: false + }); assert.strictEqual(txs.length, 2); const total = txs.reduce((t, wtx) => { @@ -627,7 +645,10 @@ describe('Wallet', function() { const balance = await alice.getBalance(); assert.strictEqual(balance.unconfirmed, 11000); - const txs = await alice.getHistory(); + const txs = await alice.listHistory(-1, { + limit: 100, + reverse: false + }); assert(txs.some((wtx) => { return wtx.tx.hash().equals(f1.hash()); })); @@ -637,7 +658,11 @@ describe('Wallet', function() { const balance = await bob.getBalance(); assert.strictEqual(balance.unconfirmed, 10000); - const txs = await bob.getHistory(); + const txs = await bob.listHistory(-1, { + limit: 100, + reverse: false + }); + assert(txs.some((wtx) => { return wtx.tx.hash().equals(f1.hash()); })); @@ -1321,18 +1346,20 @@ describe('Wallet', function() { assert(t3.verify()); }); - it('should get range of txs', async () => { + it('should get pending range of txs', async () => { const wallet = currentWallet; - const txs = await wallet.getRange(null, { - start: util.now() - 1000 + const txs = await wallet.listUnconfirmed(null, { + limit: 100, + reverse: false }); assert.strictEqual(txs.length, 2); }); - it('should get range of txs from account', async () => { + it('should get penidng range of txs from account', async () => { const wallet = currentWallet; - const txs = await wallet.getRange('foo', { - start: util.now() - 1000 + const txs = await wallet.listUnconfirmed('foo', { + limit: 100, + reverse: false }); assert.strictEqual(txs.length, 2); }); @@ -1342,8 +1369,9 @@ describe('Wallet', function() { let txs, err; try { - txs = await wallet.getRange('bad', { - start: 0xdeadbeef - 1000 + txs = await wallet.listUnconfirmed('bad', { + limit: 100, + reverse: false }); } catch (e) { err = e; @@ -1456,8 +1484,9 @@ describe('Wallet', function() { it('should get details', async () => { const wallet = currentWallet; - const txs = await wallet.getRange('foo', { - start: util.now() - 1000 + const txs = await wallet.listUnconfirmed('foo', { + limit: 100, + reverse: false }); const details = await wallet.toDetails(txs); @@ -1472,8 +1501,9 @@ describe('Wallet', function() { await wallet.rename('test'); - const txs = await wallet.getRange('foo', { - start: util.now() - 1000 + const txs = await wallet.listUnconfirmed('foo', { + limit: 100, + reverse: false }); const details = await wallet.toDetails(txs); @@ -1891,7 +1921,11 @@ describe('Wallet', function() { assert.strictEqual(pending.length, 0); // Check history for TX - const history = await wallet.getHistory(); + const history = await wallet.listHistory(-1, { + limit: 100, + reverse: false + }); + const wtxs = await wallet.toDetails(history); assert.strictEqual(wtxs.length, 1); assert.bufferEqual(wtxs[0].hash, hash);