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

API-160: tx pool endpoint tests + new cs image #1408

Merged
merged 5 commits into from
Dec 5, 2024
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
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
2 changes: 1 addition & 1 deletion src/test/chain-simulator/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ services:

chainsimulator:
container_name: chainsimulator
image: multiversx/chainsimulator:v1.8.4-barnard-test
image: multiversx/chainsimulator:v1.8.4-barnard-test2
command: ["--node-override-config", "./overridable-config.toml"]
volumes:
- ./overridable-config.toml:/multiversx/overridable-config.toml
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
}
}
Loading