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

Add from/to timestamp filter to get blocks endpoint - Closes #2391 #2472

Merged
merged 8 commits into from
Oct 18, 2018
2 changes: 2 additions & 0 deletions api/controllers/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ BlocksController.getBlocks = function(context, next) {
id: params.blockId.value,
height: params.height.value,
generatorPublicKey: params.generatorPublicKey.value,
fromTimestamp: params.fromTimestamp.value,
toTimestamp: params.toTimestamp.value,
sort: params.sort.value,
limit: params.limit.value,
offset: params.offset.value,
Expand Down
10 changes: 10 additions & 0 deletions modules/blocks/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ __private.list = function(filter, cb) {
params.height = filter.height;
}

if (filter.fromTimestamp >= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In swagger we say 0 is the valid value. Should not we simplify it on API level to disallow zero as param value?

https://github.com/LiskHQ/lisk/blob/5acfc7bdd902965124263d448be124edf481b6ae/schema/swagger.yml#L1169-L1174

Copy link
Author

@diego-G diego-G Oct 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to keep 0 as an allowed value. However, there was a mistake in the control of toTimestamp that I've caught thanks to this comment. @nazarhussain could you check it again? I hope now it's easier to understand.

where.push('"b_timestamp" >= ${fromTimestamp}');
params.fromTimestamp = filter.fromTimestamp;
}

if (filter.toTimestamp >= 1) {
where.push('"b_timestamp" <= ${toTimestamp}');
params.toTimestamp = filter.toTimestamp;
}

// FIXME: Useless condition
if (filter.totalAmount >= 0) {
where.push('"b_totalAmount" = ${totalAmount}');
Expand Down
2 changes: 2 additions & 0 deletions schema/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,8 @@ paths:
parameters:
- $ref: '#/parameters/blockId'
- $ref: '#/parameters/height'
- $ref: '#/parameters/fromTimestamp'
- $ref: '#/parameters/toTimestamp'
- $ref: '#/parameters/limit'
- $ref: '#/parameters/offset'
- in: query
Expand Down
67 changes: 67 additions & 0 deletions test/functional/http/get/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require('../../functional.js');
var waitFor = require('../../../common/utils/wait_for');
var swaggerEndpoint = require('../../../common/swagger_spec');
var apiHelpers = require('../../../common/helpers/api');
var slots = require('../../../../helpers/slots');

var expectSwaggerParamError = apiHelpers.expectSwaggerParamError;

Expand Down Expand Up @@ -112,6 +113,72 @@ describe('GET /blocks', () => {
});
});

describe('fromTimestamp', () => {
it('using invalid fromTimestamp should fail', () => {
return blocksEndpoint
.makeRequest(
{
fromTimestamp: -1,
},
400
)
.then(res => {
expectSwaggerParamError(res, 'fromTimestamp');
});
});

it('using valid fromTimestamp should return transactions', () => {
// Last hour lisk time
var queryTime = slots.getTime() - 60 * 60;

return blocksEndpoint
.makeRequest(
{
fromTimestamp: queryTime,
},
200
)
.then(res => {
res.body.data.forEach(transaction => {
expect(transaction.timestamp).to.be.at.least(queryTime);
});
});
});
});

describe('toTimestamp', () => {
it('using invalid toTimestamp should fail', () => {
return blocksEndpoint
.makeRequest(
{
toTimestamp: 0,
},
400
)
.then(res => {
expectSwaggerParamError(res, 'toTimestamp');
});
});

it('using valid toTimestamp should return transactions', () => {
// Current lisk time
var queryTime = slots.getTime();

return blocksEndpoint
.makeRequest(
{
toTimestamp: queryTime,
},
200
)
.then(res => {
res.body.data.forEach(transaction => {
expect(transaction.timestamp).to.be.at.most(queryTime);
});
});
});
});

describe('generatorPublicKey', () => {
it('using invalid generatorPublicKey = "InvalidKey" format should fail with error', () => {
return blocksEndpoint
Expand Down
4 changes: 2 additions & 2 deletions test/functional/http/get/delegates.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ describe('GET /delegates', () => {

describe('?', () => {
describe('fromTimestamp', () => {
it('using too small fromTimestamp should fail', () => {
it('using invalid fromTimestamp should fail', () => {
return forgedEndpoint
.makeRequest(
{ address: validDelegate.address, fromTimestamp: -1 },
Expand Down Expand Up @@ -690,7 +690,7 @@ describe('GET /delegates', () => {
});

describe('toTimestamp', () => {
it('using too small toTimestamp should fail', () => {
it('using invalid toTimestamp should fail', () => {
return forgedEndpoint
.makeRequest(
{ address: validDelegate.address, toTimestamp: 0 },
Expand Down
4 changes: 2 additions & 2 deletions test/functional/http/get/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ describe('GET /api/transactions', () => {
});

describe('fromTimestamp', () => {
it('using too small fromTimestamp should fail', () => {
it('using invalid fromTimestamp should fail', () => {
return transactionsEndpoint
.makeRequest({ fromTimestamp: -1 }, 400)
.then(res => {
Expand All @@ -625,7 +625,7 @@ describe('GET /api/transactions', () => {
});

describe('toTimestamp', () => {
it('using too small toTimestamp should fail', () => {
it('using invalid toTimestamp should fail', () => {
return transactionsEndpoint
.makeRequest({ toTimestamp: 0 }, 400)
.then(res => {
Expand Down