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

Transactions pool endpoint #1165

Merged
merged 22 commits into from Jan 9, 2024
Merged

Transactions pool endpoint #1165

merged 22 commits into from Jan 9, 2024

Conversation

bogdan-rosianu
Copy link
Contributor

@bogdan-rosianu bogdan-rosianu commented Dec 15, 2023

Reasoning

  • the API service didn't have an optimized endpoint that can return the pool of transactions

Proposed Changes

  • added the /pool endpoint that can return the transactions pool by also using a cache warmer (configurable via flag) to continuously fetch the new transactions
  • added the /pool/:txHash endpoint that can return transaction pool details for a given txHash
  • added the /pool/count endpoint that can return total transactions pool
  • added new filters to be able to return specific transactions from pool ( sender, receiver, type )

How to test

  • <api>:3001/pool -> should return all transactions from pool
  • <api>:3001/pool?fields=receiver,value -> should return all transactions from pool with receiver and value properties
  • <api>:3001/pool?from=0&size=15 -> should return 15 transactions from pool
  • <api>:3001/pool?fields=nonce&from=0&size=20 -> should return 20 transactions from pool with nonce property only
  • <api>:3001/pool/ac9eab8e2e6bc18fb241fce28d6ef949a8422668c9b0b8a47ae65ca80bc6bd40 -> should return transaction pool details for the given txHash
  • <api>:3001/pool/count -> should return total transactions pool
  • <api>:3001/pool?type=Reward -> should return all Reward transactions from pool

@bogdan-rosianu bogdan-rosianu self-assigned this Dec 15, 2023
Copy link

codecov bot commented Dec 15, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (e19f734) 38.60% compared to head (bcfcfdd) 38.80%.

Additional details and impacted files
@@               Coverage Diff               @@
##           development    #1165      +/-   ##
===============================================
+ Coverage        38.60%   38.80%   +0.19%     
===============================================
  Files                9        9              
  Lines              316      317       +1     
  Branches            19       19              
===============================================
+ Hits               122      123       +1     
  Misses             175      175              
  Partials            19       19              
Flag Coverage Δ
unittests 38.80% <100.00%> (+0.19%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cfaur09 cfaur09 added the feature New feature or request label Dec 18, 2023
@bogdan-rosianu bogdan-rosianu changed the title added redis cache to the tx pool endpoint Transactions pool endpoint Dec 19, 2023
ramonalobont
ramonalobont previously approved these changes Dec 22, 2023
Copy link
Contributor

Choose a reason for hiding this comment

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

we also need here the possibility of filtering by type and also the /pool/count endpoint

Copy link
Contributor Author

Choose a reason for hiding this comment

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

great idea. done

@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 })
async getTransactionPool(
@Query('from', new DefaultValuePipe(0), ParseIntPipe) from: number,
Copy link
Contributor

Choose a reason for hiding this comment

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

filter by sender / receiver should also be possible. same with the /count endpoint

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

): Promise<TransactionInPool> {
const transaction = await this.poolService.getTransactionFromPool(txHash);
if (transaction === undefined) {
throw new HttpException('Transaction not found', HttpStatus.NOT_FOUND);
Copy link
Contributor

Choose a reason for hiding this comment

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

you can throw here the dedicated NotFoundException

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

transactionPoolWarmer:
enabled: true
cronExpression: '*/5 * * * * *'
ttlInSeconds: 6
Copy link
Contributor

Choose a reason for hiding this comment

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

default ttl should be maybe 60 seconds, to make sure the value doesn't expire if the gateway experiences a slowdown

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

cfaur09
cfaur09 previously approved these changes Dec 29, 2023
ramonalobont
ramonalobont previously approved these changes Dec 29, 2023
Copy link
Contributor

Choose a reason for hiding this comment

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

even if it's a bit verbose, it's a good best practice to have file names and class names 1:1, then it's easier to search for classes in vscode also by file name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done. renamed to tx.pool.gateway.response.ts. I have multiple classes in that file, but the TxPoolGatewayResponse is the highest one


@Injectable()
export class GatewayService {
private snapshotlessRequestsSet: Set<String> | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

you can initialize this directly here. we can then make the field read-only and skip the undefined marker

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

private parseTransactions(rawPool: TxPoolGatewayResponse): TransactionInPool[] {
const transactionPool: TransactionInPool[] = [];
if (rawPool && rawPool.txPool) {
transactionPool.push(...this.processTransactionType(rawPool.txPool.regularTransactions || [], TransactionType.Transaction));
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of || [], please use the preferred ?? [] annotation which explicitly handles null or undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


private parseTransaction(tx: TxInPoolFields, type: TransactionType): TransactionInPool {
return new TransactionInPool({
txHash: tx.hash || '',
Copy link
Contributor

Choose a reason for hiding this comment

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

similar to above, use the preferred ?? operator

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


const results: TransactionInPool[] = [];

for (const transaction of pool) {
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of using the for and continue strategy, we can use the .filter function for every filter parameter. in this way, we can skip the initial check which might cause bugs when adding a new filter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

refactored by using the filter function

Copy link
Contributor

Choose a reason for hiding this comment

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

can you please split all classes here in multiple files? easier to search and match

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@cfaur09 cfaur09 merged commit 620ce22 into development Jan 9, 2024
5 of 7 checks passed
@cfaur09 cfaur09 deleted the SERVICES-2012-txs-pool-api branch January 9, 2024 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants