Skip to content

Commit

Permalink
wallet: deprecate getLast and getRange. Update hsw-cli history and
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
nodech committed Mar 30, 2024
1 parent 9575013 commit 24b94a5
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 277 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 18 additions & 4 deletions bin/hsw-cli
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
58 changes: 0 additions & 58 deletions lib/client/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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).
Expand Down
37 changes: 0 additions & 37 deletions lib/wallet/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
137 changes: 14 additions & 123 deletions lib/wallet/txdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<Hash[]>}
*/

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<Hash[]>}
*/

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<TX[]>}
*/

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<TX[]>}
*/

getLast(acct, limit) {
return this.getRange(acct, {
start: 0,
end: 0xffffffff,
reverse: true,
limit: limit || 10
});
}

/**
* Get all transactions.
* @param {Number} acct
Expand Down Expand Up @@ -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
};

Expand Down
Loading

0 comments on commit 24b94a5

Please sign in to comment.