Skip to content

Commit

Permalink
feat(core-api): filter locks by expiration status (ArkEcosystem#3227)
Browse files Browse the repository at this point in the history
  • Loading branch information
dated authored and faustbrian committed Nov 8, 2019
1 parent b5ea607 commit 91e815e
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 12 deletions.
22 changes: 19 additions & 3 deletions __tests__/integration/core-api/handlers/locks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("API 2.0 - Locks", () => {
secretHash: transaction.id,
expiration: {
type: j % 2 === 0 ? 1 : 2,
value: 100 * (j + 1),
value: !j ? 0 : (100 * (j + 1)),
},
timestamp: (i + 1) * 100000,
};
Expand All @@ -71,7 +71,7 @@ describe("API 2.0 - Locks", () => {
const response = await utils.request("GET", "locks", { orderBy: "expirationValue:asc" });
expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeArray();
expect(response.data.data[0].expirationValue).toBe(100);
expect(response.data.data[0].expirationValue).toBe(0);
});

it("should GET all the locks by epoch expiration", async () => {
Expand All @@ -90,6 +90,22 @@ describe("API 2.0 - Locks", () => {
expect(response.data.data.every(lock => lock.expirationType === 2)).toBeTrue();
});

it("should GET all the locks that are expired", async () => {
const response = await utils.request("GET", "locks", { isExpired: true });
expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeArray();
expect(response.data.data).not.toBeEmpty();
expect(response.data.data.every(lock => lock.isExpired)).toBeTrue();
});

it("should GET all the locks that are not expired", async () => {
const response = await utils.request("GET", "locks", { isExpired: false });
expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeArray();
expect(response.data.data).not.toBeEmpty();
expect(response.data.data.every(lock => !lock.isExpired)).toBeTrue();
});

describe("orderBy", () => {
it("should be ordered by amount:desc", async () => {
const response = await utils.request("GET", "locks", { orderBy: "amount:desc", expirationType: 2 });
Expand All @@ -104,7 +120,7 @@ describe("API 2.0 - Locks", () => {
}
});

it("should be ordered by amount:ascs", async () => {
it("should be ordered by amount:asc", async () => {
const response = await utils.request("GET", "locks", { orderBy: "amount:asc", expirationType: 2 });
expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { WalletsBusinessRepository } from "../../../../packages/core-database/sr
import { DatabaseService } from "../../../../packages/core-database/src/database-service";
import { Wallets } from "../../../../packages/core-state/src";
import { Address } from "../../../../packages/crypto/src/identities";
import { stateStorageStub } from "../__fixtures__/state-storage-stub";

let genesisSenders;
let repository: Database.IWalletsBusinessRepository;
Expand Down Expand Up @@ -266,6 +267,8 @@ describe("Wallet Repository", () => {
const wallets = generateHtlcLocks();
walletManager.index(wallets);

jest.spyOn(stateStorageStub, "getLastBlock").mockReturnValue(genesisBlock);

const locks = repository.search(Database.SearchScope.Locks, {});
expect(locks.rows).toHaveLength(genesisBlock.transactions.length);
});
Expand Down
2 changes: 2 additions & 0 deletions packages/core-api/src/handlers/locks/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const index: object = {
.integer()
.min(0),
expirationType: Joi.number().only(1, 2),
isExpired: Joi.bool(),
},
},
};
Expand Down Expand Up @@ -85,6 +86,7 @@ export const search: object = {
.integer()
.min(0),
}),
isExpired: Joi.bool(),
},
};

Expand Down
8 changes: 2 additions & 6 deletions packages/core-api/src/handlers/locks/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { formatTimestamp } from "@arkecosystem/core-utils";
import { expirationCalculator } from "@arkecosystem/core-utils";
import { Interfaces } from "@arkecosystem/crypto";

export const transformLock = lock => {
export const transformLock = (lock: Interfaces.IHtlcLock) => {
return {
...lock,
amount: lock.amount.toFixed(),
timestamp: formatTimestamp(lock.timestamp),
isExpired: expirationCalculator.calculateLockExpirationStatus({
type: lock.expirationType,
value: lock.expirationValue,
}),
};
};
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/wallets/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export const locks: object = {
query: {
...pagination,
...{
isExpired: Joi.bool(),
orderBy: Joi.string(),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export = <T = any>(rows: ReadonlyArray<T>, params: Database.IParameters, filters
return rows.filter(item => {
if (filters.hasOwnProperty("exact")) {
for (const elem of filters.exact) {
if (params[elem] && getProperty(item, elem) !== params[elem]) {
if (params[elem] !== undefined && getProperty(item, elem) !== params[elem]) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Database, State } from "@arkecosystem/core-interfaces";
import { delegateCalculator, hasSomeProperty } from "@arkecosystem/core-utils";
import { delegateCalculator, expirationCalculator, hasSomeProperty } from "@arkecosystem/core-utils";
import { Interfaces, Utils } from "@arkecosystem/crypto";
import { searchEntries } from "./utils/search-entries";

Expand All @@ -18,6 +18,7 @@ interface IUnwrappedHtlcLock {
timestamp: number;
expirationType: number;
expirationValue: number;
isExpired: boolean;
vendorField: string;
}

Expand Down Expand Up @@ -179,7 +180,15 @@ export class WalletsBusinessRepository implements Database.IWalletsBusinessRepos

private searchLocks(params: Database.IParameters = {}): ISearchContext<IUnwrappedHtlcLock> {
const query: Record<string, string[]> = {
exact: ["senderPublicKey", "lockId", "recipientId", "secretHash", "expirationType", "vendorField"],
exact: [
"expirationType",
"isExpired",
"lockId",
"recipientId",
"secretHash",
"senderPublicKey",
"vendorField",
],
between: ["expirationValue", "amount", "timestamp"],
};

Expand All @@ -203,6 +212,9 @@ export class WalletsBusinessRepository implements Database.IWalletsBusinessRepos
timestamp: lock.timestamp,
expirationType: lock.expiration.type,
expirationValue: lock.expiration.value,
isExpired: expirationCalculator.calculateLockExpirationStatus(
lock.expiration,
),
vendorField: lock.vendorField,
});
}
Expand Down

0 comments on commit 91e815e

Please sign in to comment.