Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): Improve block range validation in POI endpoint #2645

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/node-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Add `TextEncoder` in sandbox, some network package util method is depended on it

### Fixed
- Improved block range validation in POI endpoint with custom class-validator decorator

## [16.1.0] - 2024-12-11
### Changed
- If any tests fail with the `test` subcommand the exit code will now be 1 instead of 0 (#2624)
Expand Down Expand Up @@ -45,7 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add an `--enable-cache` flag, allowing you to choose between DB or cache for IO operations.

### Fixed
- When using a GET query to retrieve an entity, it will include a store field.
- When using a GET query to retrieve an entity, it will include a "store" field.
- Support for historical indexing by timestamp as well as block height (#2584)
- Subscriptions not emitting deleted mutation type with historical (#2600)

Expand Down Expand Up @@ -144,7 +147,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [10.10.2] - 2024-07-10
### Fixed
- Fix issue admin api can not get `dbSize` due to it not been set in \_metadata table
- Fix issue admin api can not get `dbSize` due to it not been set in _metadata table

## [10.10.1] - 2024-07-09
### Added
Expand Down Expand Up @@ -609,7 +612,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Base58 encoding check for POI (#1788)
- Fix project \_startHeight been blocked by poiSync (#1792)
- Fix project _startHeight been blocked by poiSync (#1792)

## [2.4.4] - 2023-06-07
### Fixed
Expand Down
10 changes: 0 additions & 10 deletions packages/node-core/src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,6 @@ export class AdminController {
@Get('poi/')
async getPoisByRange(@Query(ValidationPipe) blockRange: BlockRangeDto): Promise<ProofOfIndexHuman[]> {
const {endBlock, startBlock} = blockRange;
// TODO, class validator seems not work properly, need to complete in future
if (endBlock && Number(startBlock) > Number(endBlock)) {
throw new HttpException(
{
status: HttpStatus.BAD_REQUEST,
error: 'startBlock must be greater than endBlock',
},
HttpStatus.BAD_REQUEST
);
}
logger.info(`[POI] Getting poi history for blocks from ${startBlock} to ${endBlock}`);
return handleServiceCall(async () => {
const pois = await this.poiService.plainPoiRepo.getPoiBlocksByRange(
Expand Down
26 changes: 25 additions & 1 deletion packages/node-core/src/admin/blockRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,30 @@
// SPDX-License-Identifier: GPL-3.0

import {Type} from 'class-transformer';
import {IsInt, IsPositive, Min, IsOptional} from 'class-validator';
import {IsInt, IsPositive, Min, IsOptional, ValidateIf, registerDecorator, ValidationOptions, ValidationArguments} from 'class-validator';

export function IsGreaterThan(property: string, validationOptions?: ValidationOptions) {
return function (object: any, propertyName: string) {
registerDecorator({
name: 'isGreaterThan',
target: object.constructor,
propertyName: propertyName,
constraints: [property],
options: validationOptions,
validator: {
validate(value: any, args: ValidationArguments) {
const [relatedPropertyName] = args.constraints;
const relatedValue = (args.object as any)[relatedPropertyName];
return value === undefined || value > relatedValue;
},
defaultMessage(args: ValidationArguments) {
const [relatedPropertyName] = args.constraints;
return `${args.property} must be greater than ${relatedPropertyName}`;
},
},
});
};
}

export interface BlockRangeDtoInterface {
startBlock: number;
Expand All @@ -26,5 +49,6 @@ export class BlockRangeDto implements BlockRangeDtoInterface {
@IsPositive()
@Min(0)
@IsOptional()
@IsGreaterThan('startBlock')
endBlock: number | undefined;
}
Loading