Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan-rosianu committed Dec 28, 2023
1 parent dcc8ca7 commit 5edd9df
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 11 deletions.
2 changes: 1 addition & 1 deletion config/config.devnet-old.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ features:
transactionPoolWarmer:
enabled: true
cronExpression: '*/5 * * * * *'
ttlInSeconds: 6
ttlInSeconds: 60
updateCollectionExtraDetails:
enabled: false
marketplace:
Expand Down
2 changes: 1 addition & 1 deletion config/config.devnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ features:
transactionPoolWarmer:
enabled: true
cronExpression: '*/5 * * * * *'
ttlInSeconds: 6
ttlInSeconds: 60
updateCollectionExtraDetails:
enabled: false
marketplace:
Expand Down
2 changes: 1 addition & 1 deletion config/config.mainnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ features:
transactionPoolWarmer:
enabled: true
cronExpression: '*/5 * * * * *'
ttlInSeconds: 6
ttlInSeconds: 60
updateCollectionExtraDetails:
enabled: false
marketplace:
Expand Down
2 changes: 1 addition & 1 deletion config/config.testnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ features:
transactionPoolWarmer:
enabled: true
cronExpression: '*/5 * * * * *'
ttlInSeconds: 6
ttlInSeconds: 60
updateCollectionExtraDetails:
enabled: false
marketplace:
Expand Down
11 changes: 11 additions & 0 deletions src/endpoints/pool/entities/pool.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TransactionType } from "src/endpoints/transactions/entities/transaction.type";

export class PoolFilter {
constructor(init?: Partial<PoolFilter>) {
Object.assign(this, init);
}

sender?: string;
receiver?: string;
type?: TransactionType;
}
35 changes: 29 additions & 6 deletions src/endpoints/pool/pool.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ParseIntPipe, ParseTransactionHashPipe } from "@multiversx/sdk-nestjs-common";
import { Controller, DefaultValuePipe, Get, HttpException, HttpStatus, Param, Query } from "@nestjs/common";
import { ApiOkResponse, ApiOperation, ApiQuery, ApiTags } from "@nestjs/swagger";
import { ParseAddressAndMetachainPipe, ParseAddressArrayPipe, ParseIntPipe, ParseTransactionHashPipe } from "@multiversx/sdk-nestjs-common";
import { Controller, DefaultValuePipe, Get, HttpException, HttpStatus, NotFoundException, Param, Query } from "@nestjs/common";
import { ApiNotFoundResponse, ApiOkResponse, ApiOperation, ApiQuery, ApiTags } from "@nestjs/swagger";
import { PoolService } from "./pool.service";
import { QueryPagination } from "src/common/entities/query.pagination";
import { TransactionInPool } from "./entities/transaction.in.pool.dto";
import { TransactionType } from "../transactions/entities/transaction.type";
import { PoolFilter } from "./entities/pool.filter";

@Controller()
@ApiTags('pool')
Expand All @@ -17,22 +19,43 @@ export class PoolController {
@ApiOkResponse({ type: [TransactionInPool], isArray: true })
@ApiQuery({ name: 'from', description: 'Number of items to skip for the result set', required: false })
@ApiQuery({ name: 'size', description: 'Number of items to retrieve', required: false })
@ApiQuery({ name: 'sender', description: 'Search in transaction pool by a specific sender', required: false })
@ApiQuery({ name: 'receiver', description: 'Search in transaction pool by a specific receiver', required: false })
@ApiQuery({ name: 'type', description: 'Search in transaction pool by type', required: false })
async getTransactionPool(
@Query('from', new DefaultValuePipe(0), ParseIntPipe) from: number,
@Query('size', new DefaultValuePipe(25), ParseIntPipe) size: number,
@Query('sender', ParseAddressAndMetachainPipe) sender?: string,
@Query('receiver', ParseAddressArrayPipe) receiver?: string,
@Query('type') type?: TransactionType,
): Promise<TransactionInPool[]> {
return await this.poolService.getPool(new QueryPagination({ from, size }));
return await this.poolService.getPool(new QueryPagination({ from, size }), new PoolFilter({ sender: sender, receiver: receiver, type: type }));
}

@Get("/pool/count")
@ApiOperation({ summary: 'Transactions pool count', description: 'Returns the number of transactions that are currently in the memory pool.' })
@ApiOkResponse({ type: Number })
@ApiQuery({ name: 'sender', description: 'Returns the number of transactions with a specific sender', required: false })
@ApiQuery({ name: 'receiver', description: 'Returns the number of transactions with a specific receiver', required: false })
@ApiQuery({ name: 'type', description: 'Returns the number of transactions with a specific type', required: false })
async getTransactionPoolCount(
@Query('sender', ParseAddressAndMetachainPipe) sender?: string,
@Query('receiver', ParseAddressArrayPipe) receiver?: string,
@Query('type') type?: TransactionType,
): Promise<number> {
return await this.poolService.getPoolCount(new PoolFilter({ sender: sender, receiver: receiver, type: type }));
}

@Get("/pool/:txhash")
@ApiOperation({ summary: 'Transaction from pool', description: 'Returns a transaction from the memory pool.' })
@ApiOkResponse({ type: [TransactionInPool] })
@ApiOkResponse({ type: TransactionInPool })
@ApiNotFoundResponse({ description: 'Transaction not found' })
async getTransactionFromPool(
@Param('txhash', ParseTransactionHashPipe) txHash: string,
): Promise<TransactionInPool> {
const transaction = await this.poolService.getTransactionFromPool(txHash);
if (transaction === undefined) {
throw new HttpException('Transaction not found', HttpStatus.NOT_FOUND);
throw new NotFoundException('Transaction not found');
}

return transaction;
Expand Down
31 changes: 30 additions & 1 deletion src/endpoints/pool/pool.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CacheInfo } from "src/utils/cache.info";
import { TxInPoolFields, TxPoolGatewayResponse } from "src/common/gateway/entities/transaction.pool";
import { TransactionType } from "../transactions/entities/transaction.type";
import { TransactionInPool } from "./entities/transaction.in.pool.dto";
import { PoolFilter } from "./entities/pool.filter";

@Injectable()
export class PoolService {
Expand All @@ -23,19 +24,47 @@ export class PoolService {
return transaction;
}

async getPoolCount(filter: PoolFilter): Promise<number> {
const pool = await this.getEntirePool();
return this.applyFilters(pool, filter).length;
}

async getPool(
queryPagination: QueryPagination,
filter: PoolFilter,
): Promise<TransactionInPool[]> {
if (!this.apiConfigService.isTransactionPoolEnabled()) {
return [];
}

const { from, size } = queryPagination;

const pool = await this.getEntirePool();
const pool = this.applyFilters(await this.getEntirePool(), filter);
return pool.slice(from, from + size);
}

private applyFilters(pool: TransactionInPool[], filters: PoolFilter): TransactionInPool[] {
let results: TransactionInPool[] = [];

Check failure on line 47 in src/endpoints/pool/pool.service.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'results' is never reassigned. Use 'const' instead

for (const transaction of pool) {
if (filters.sender && transaction.sender !== filters.sender) {
continue;
}

if (filters.receiver && transaction.receiver !== filters.receiver) {
continue;
}

if (filters.type && transaction.type !== filters.type) {
continue;
}

results.push(transaction);
}

return results;
}

async getEntirePool(): Promise<TransactionInPool[]> {
return await this.cacheService.getOrSet(
CacheInfo.TransactionPool.key,
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ async function bootstrap() {
logger.log(`Staking v4 enabled: ${apiConfigService.isStakingV4Enabled()}`);
logger.log(`Events notifier enabled: ${apiConfigService.isEventsNotifierFeatureActive()}`);
logger.log(`Guest caching enabled: ${apiConfigService.isGuestCacheFeatureActive()}`);
logger.log(`Transaction pool enabled: ${apiConfigService.isTransactionPoolEnabled()}`);
logger.log(`Transaction pool cache warmer enabled: ${apiConfigService.isTransactionPoolCacheWarmerEnabled()}`);
}

async function configurePublicApp(publicApp: NestExpressApplication, apiConfigService: ApiConfigService) {
Expand Down

0 comments on commit 5edd9df

Please sign in to comment.