Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

Commit

Permalink
Reviewed hash locks endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
martinayora authored Jun 30, 2020
1 parent 5eb7eac commit eb30622
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 278 deletions.
1 change: 1 addition & 0 deletions catapult-sdk/src/plugins/lockHash.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const constants = { sizes };
const lockHashPlugin = {
registerSchema: builder => {
builder.addSchema('hashLockInfo', {
id: ModelType.objectId,
lock: { type: ModelType.object, schemaName: 'hashLockInfo.lock' }
});
builder.addSchema('hashLockInfo.lock', {
Expand Down
2 changes: 1 addition & 1 deletion catapult-sdk/test/plugins/lockHash_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('lock hash plugin', () => {
]);

// - hash lock infos
assertSchema(modelSchema.hashLockInfo, 1, 'lock');
assertSchema(modelSchema.hashLockInfo, 2, 'id', 'lock');
assertSchema(modelSchema['hashLockInfo.lock'], 5,
'ownerAddress', 'mosaicId', 'amount', 'endHeight', 'hash');

Expand Down
22 changes: 13 additions & 9 deletions rest/src/plugins/lockHash/LockHashDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@ class LockHashDb {
// region lock retrieval

/**
* Retrieves hash infos for given addresses.
* Retrieves hash lock infos for given accounts filtered and paginated.
* @param {array<{Uint8Array}>} addresses Account addresses.
* @param {string} id Paging id.
* @param {int} pageSize Page size.
* @param {object} options Additional options.
* @param {object} options Options for ordering and pagination. Can have an `offset`, and must contain the `sortField`, `sortDirection`,
* `pageSize` and `pageNumber`. 'sortField' must be within allowed 'sortingOptions'.
* @returns {Promise.<array>} Hash lock infos for all accounts.
*/
hashLocksByAddresses(addresses, id, pageSize, options) {
hashLocks(addresses, options) {
const sortingOptions = { id: '_id' };
const buffers = addresses.map(address => Buffer.from(address));
const conditions = { 'lock.ownerAddress': { $in: buffers } };
return this.catapultDb.queryPagedDocuments('hashLocks', conditions, id, pageSize, options)
.then(this.catapultDb.sanitizer.copyAndDeleteIds);
const conditions = [{ 'lock.ownerAddress': { $in: buffers } }];

if (options.offset)
conditions.push({ [sortingOptions[options.sortField]]: { [1 === options.sortDirection ? '$gt' : '$lt']: options.offset } });

const sortConditions = { $sort: { [sortingOptions[options.sortField]]: options.sortDirection } };
return this.catapultDb.queryPagedDocuments_2(conditions, [], sortConditions, 'hashLocks', options);
}

/**
Expand All @@ -51,7 +55,7 @@ class LockHashDb {
*/
hashLockByHash(hash) {
return this.catapultDb.queryDocument('hashLocks', { 'lock.hash': Buffer.from(hash) })
.then(this.catapultDb.sanitizer.copyAndDeleteId);
.then(this.catapultDb.sanitizer.renameId);
}

// endregion
Expand Down
8 changes: 4 additions & 4 deletions rest/src/plugins/lockHash/lockHashRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
const routeUtils = require('../../routes/routeUtils');

module.exports = {
register: (server, db) => {
register: (server, db, services) => {
server.get('/account/:address/lock/hash', (req, res, next) => {
const accountAddress = routeUtils.parseArgument(req.params, 'address', 'address');
const pagingOptions = routeUtils.parsePagingArguments(req.params);
const options = routeUtils.parsePaginationArguments(req.params, services.config.pageSize, { id: 'objectId' });

return db.hashLocksByAddresses([accountAddress], pagingOptions.id, pagingOptions.pageSize)
.then(routeUtils.createSender('hashLockInfo').sendArray('address', res, next));
return db.hashLocks([accountAddress], options)
.then(result => routeUtils.createSender('hashLockInfo').sendPage(res, next)(result));
});

server.get('/lock/hash/:hash', (req, res, next) => {
Expand Down
Loading

0 comments on commit eb30622

Please sign in to comment.