Skip to content

Commit

Permalink
fix(api): Improve block range validation in POI endpoint (#2645)
Browse files Browse the repository at this point in the history
* Update blockRange.ts

* Update admin.controller.ts

* Improved block range validation in POI endpoint with custom class-validator decorator
  • Loading branch information
VolodymyrBg authored Jan 20, 2025
1 parent 30fe310 commit 742ef27
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
9 changes: 6 additions & 3 deletions packages/node-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed the inconsistency between the `monitor-file-size` flag and the expected behavior.(#2644)


### 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 @@ -50,7 +53,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 @@ -149,7 +152,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 @@ -614,7 +617,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;
}

0 comments on commit 742ef27

Please sign in to comment.