Skip to content

Commit

Permalink
API-160: tx pool endpoint tests + new cs image (#1408)
Browse files Browse the repository at this point in the history
* API-160: tx pool endpoint tests + new cs image

* push staged file

* fix test

* remove self sended txs

* fixes after review
  • Loading branch information
bogdan-rosianu authored Dec 5, 2024
1 parent 428c2d6 commit 3660954
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
4 changes: 2 additions & 2 deletions config/config.e2e.mainnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ features:
hitsThreshold: 100
ttl: 12
transactionPool:
enabled: false
enabled: true
transactionPoolWarmer:
enabled: false
enabled: true
cronExpression: '*/5 * * * * *'
ttlInSeconds: 60
updateCollectionExtraDetails:
Expand Down
75 changes: 75 additions & 0 deletions src/test/chain-simulator/pool.cs-e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import axios from 'axios';
import { config } from './config/env.config';
import { ChainSimulatorUtils } from './utils/test.utils';
import { fundAddress } from './utils/chain.simulator.operations';

describe('Pool e2e tests with chain simulator', () => {
beforeAll(async () => {
await ChainSimulatorUtils.waitForEpoch(2);
await fundAddress(config.chainSimulatorUrl, config.aliceAddress);
await new Promise((resolve) => setTimeout(resolve, 20000));
});

beforeEach(() => {
jest.clearAllMocks();
});

describe('GET /pool', () => {
it('should return status code 200', async () => {
const response = await axios.get(`${config.apiServiceUrl}/pool`);
const txsPool = response.data;

expect(response.status).toBe(200);
expect(txsPool).toBeInstanceOf(Array);
});

it('should return the transaction pool', async () => {
const response = await axios.get(
`${config.apiServiceUrl}/pool`,
);
const pool = response.data;
const expectedProperties = [
'txHash',
'nonce',
'receiver',
'gasPrice',
'gasLimit',
];

for (const tx of pool) {
for (const property of expectedProperties) {
expect(tx).toHaveProperty(property);
}
}
});
});

describe('GET /pool/:txhash', () => {
it('should return status code 200 and the transaction', async () => {
const poolResponse = await axios.get(
`${config.apiServiceUrl}/pool?size=1`,
);
const response = await axios.get(
`${config.apiServiceUrl}/pool/${poolResponse.data[0].txHash}`,
);
const tx = response.data;

expect(response.status).toBe(200);
expect(tx).toHaveProperty(
'txHash',
poolResponse.data[0].txHash,
);
});

it('should return status code 404 for non-existent tx hash', async () => {
const nonExistentTxHash = '0000000000000000000000000000000000000000000000000000000000000000';
try {
await axios.get(
`${config.apiServiceUrl}/pool/${nonExistentTxHash}`,
);
} catch (error: any) {
expect(error.response.status).toBe(404);
}
});
});
});
15 changes: 13 additions & 2 deletions src/test/chain-simulator/utils/chain.simulator.operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ export async function sendTransaction(
const tx = {
sender: args.sender,
receiver: args.receiver,
nonce: nonce,
nonce: nonce + (args.nonceOffset ?? 0),
value: args.value,
gasPrice: 1000000000,
gasLimit: args.gasLimit,
gasLimit: args.gasLimit ?? (50_000 + 1_500 * args.dataField.length),
data: Buffer.from(args.dataField).toString('base64'),
signature: 'a'.repeat(128),
chainID: 'chain',
Expand All @@ -133,6 +133,16 @@ export async function sendTransaction(
tx,
);
const txHash = txHashResponse.data.data.txHash;
if (args.nonceOffset) {
// when a nonce offset is present, it means that the transaction won't be executed in real time, so we should early exit
console.log(`Broadcasted tx hash ${txHash} of sender ${args.sender} with nonce ${tx.nonce}`);
console.log(JSON.stringify(tx));
await axios.post(
`${args.chainSimulatorUrl}/simulator/generate-blocks/1`,
);
return txHash;
}

await axios.post(
`${args.chainSimulatorUrl}/simulator/generate-blocks-until-transaction-processed/${txHash}`,
);
Expand Down Expand Up @@ -172,6 +182,7 @@ export class SendTransactionArgs {
dataField: string = '';
value?: string = '0';
gasLimit?: number = 100_000_000;
nonceOffset?: number = 0; // useful for scenarios where a higher nonce is desired

constructor(options: Partial<SendTransactionArgs> = {}) {
Object.assign(this, options);
Expand Down
2 changes: 1 addition & 1 deletion src/test/jest-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
"../.."
],
"testTimeout": 180000
}
}

0 comments on commit 3660954

Please sign in to comment.