Skip to content

Commit

Permalink
Fixes unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantina Blazhukova <[email protected]>
  • Loading branch information
konstantinabl committed Sep 9, 2024
1 parent 2bcdac1 commit 2b4d451
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 152 deletions.
3 changes: 0 additions & 3 deletions packages/relay/src/lib/clients/mirrorNodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ export class MirrorNodeClient {
?.split(MirrorNodeClient.REQUEST_PREFIX_SEPARATOR)[1]
.replace(MirrorNodeClient.REQUEST_PREFIX_TRAILING_BRACKET, MirrorNodeClient.EMPTY_STRING) ||
MirrorNodeClient.EMPTY_STRING;
console.log('Request id', requestId);
const controller = new AbortController();
try {
const axiosRequestConfig: AxiosRequestConfig = {
Expand Down Expand Up @@ -374,8 +373,6 @@ export class MirrorNodeClient {
}

async get<T = any>(path: string, pathLabel: string, requestIdPrefix: string, retries?: number): Promise<T | null> {
console.log('Mirron node getting data');
console.log(requestIdPrefix);
return this.request<T>(path, pathLabel, 'GET', requestIdPrefix, null, retries);
}

Expand Down
1 change: 0 additions & 1 deletion packages/relay/src/lib/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,6 @@ export class EthImpl implements Eth {
): Promise<IContractCallResponse | null> {
await this.contractCallFormat(transaction, requestDetails);
const callData = { ...transaction, estimate: true };
console.log('Before post contract call');
return this.mirrorNodeClient.postContractCall(callData, requestDetails.requestIdPrefix);
}

Expand Down
16 changes: 12 additions & 4 deletions packages/relay/tests/lib/eth/eth_call.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,16 @@ describe('@ethCall Eth Call spec', async function () {
...defaultCallData,
gas: 25_000_000,
};
await mockContractCall({ ...callData, gas: constants.MAX_GAS_PER_SEC, block: 'latest' }, false, 200, {
result: '0x00',
});
const res = await ethImpl.call(callData, 'latest');
await mockContractCall(
{ ...callData, gas: constants.MAX_GAS_PER_SEC, block: 'latest' },
false,
200,
{
result: '0x00',
},
requestDetails,
);
const res = await ethImpl.call(callData, 'latest', requestDetails);
expect(res).to.equal('0x00');
});

Expand Down Expand Up @@ -965,6 +971,7 @@ describe('@ethCall Eth Call spec', async function () {
data: REDIRECTED_SELECTOR,
},
'latest',
requestDetails,
);

assert(callConsensusNodeSpy.calledOnce);
Expand All @@ -978,6 +985,7 @@ describe('@ethCall Eth Call spec', async function () {
data: NON_REDIRECTED_SELECTOR,
},
'latest',
requestDetails,
);

assert(callConsensusNodeSpy.notCalled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ let getSdkClientStub;
describe('@ethGetBlockTransactionCountByHash using MirrorNode', async function () {
this.timeout(10000);
let { restMock, hapiServiceInstance, ethImpl, cacheService } = generateEthTestEnv();
const requestIdPrefix = `[Request ID: testId]`;

this.beforeEach(() => {
// reset cache and restMock
Expand All @@ -63,15 +64,15 @@ describe('@ethGetBlockTransactionCountByHash using MirrorNode', async function (
// mirror node request mocks
restMock.onGet(`blocks/${BLOCK_HASH}`).reply(200, DEFAULT_BLOCK);

const result = await ethImpl.getBlockTransactionCountByHash(BLOCK_HASH);
const result = await ethImpl.getBlockTransactionCountByHash(BLOCK_HASH, requestIdPrefix);
expect(result).equal(numberTo0x(BLOCK_TRANSACTION_COUNT));
});

it('eth_getBlockTransactionCountByHash with match should hit cache', async function () {
restMock.onGet(`blocks/${BLOCK_HASH}`).replyOnce(200, DEFAULT_BLOCK);

for (let i = 0; i < 3; i++) {
const result = await ethImpl.getBlockTransactionCountByHash(BLOCK_HASH);
const result = await ethImpl.getBlockTransactionCountByHash(BLOCK_HASH, requestIdPrefix);
expect(result).equal(numberTo0x(BLOCK_TRANSACTION_COUNT));
}
});
Expand All @@ -80,7 +81,7 @@ describe('@ethGetBlockTransactionCountByHash using MirrorNode', async function (
// mirror node request mocks
restMock.onGet(`blocks/${BLOCK_HASH}`).reply(404, NO_SUCH_BLOCK_EXISTS_RES);

const result = await ethImpl.getBlockTransactionCountByHash(BLOCK_HASH);
const result = await ethImpl.getBlockTransactionCountByHash(BLOCK_HASH, requestIdPrefix);
expect(result).to.equal(null);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ let getSdkClientStub;
describe('@ethGetBlockTransactionCountByNumber using MirrorNode', async function () {
this.timeout(10000);
let { restMock, hapiServiceInstance, ethImpl, cacheService } = generateEthTestEnv();
const requestIdPrefix = `[Request ID: testId]`;

this.beforeEach(() => {
// reset cache and restMock
Expand All @@ -65,15 +66,15 @@ describe('@ethGetBlockTransactionCountByNumber using MirrorNode', async function
// mirror node request mocks
restMock.onGet(`blocks/${BLOCK_NUMBER}`).reply(200, DEFAULT_BLOCK);

const result = await ethImpl.getBlockTransactionCountByNumber(BLOCK_NUMBER.toString());
const result = await ethImpl.getBlockTransactionCountByNumber(BLOCK_NUMBER.toString(), requestIdPrefix);
expect(result).equal(numberTo0x(BLOCK_TRANSACTION_COUNT));
});

it('eth_getBlockTransactionCountByNumber with match should hit cache', async function () {
restMock.onGet(`blocks/${BLOCK_NUMBER}`).replyOnce(200, DEFAULT_BLOCK);

for (let i = 0; i < 3; i++) {
const result = await ethImpl.getBlockTransactionCountByNumber(BLOCK_NUMBER.toString());
const result = await ethImpl.getBlockTransactionCountByNumber(BLOCK_NUMBER.toString(), requestIdPrefix);
expect(result).equal(numberTo0x(BLOCK_TRANSACTION_COUNT));
}
});
Expand All @@ -82,7 +83,7 @@ describe('@ethGetBlockTransactionCountByNumber using MirrorNode', async function
cacheService.clear();
restMock.onGet(`blocks/${BLOCK_NUMBER}`).reply(404, NO_SUCH_BLOCK_EXISTS_RES);

const result = await ethImpl.getBlockTransactionCountByNumber(BLOCK_NUMBER.toString());
const result = await ethImpl.getBlockTransactionCountByNumber(BLOCK_NUMBER.toString(), requestIdPrefix);
expect(result).to.equal(null);
});

Expand All @@ -91,7 +92,7 @@ describe('@ethGetBlockTransactionCountByNumber using MirrorNode', async function
restMock.onGet(BLOCKS_LIMIT_ORDER_URL).reply(200, DEFAULT_BLOCKS_RES);
restMock.onGet(`blocks/${BLOCK_NUMBER}`).reply(200, DEFAULT_BLOCK);

const result = await ethImpl.getBlockTransactionCountByNumber('latest');
const result = await ethImpl.getBlockTransactionCountByNumber('latest', requestIdPrefix);
expect(result).equal(numberTo0x(BLOCK_TRANSACTION_COUNT));
});

Expand All @@ -100,7 +101,7 @@ describe('@ethGetBlockTransactionCountByNumber using MirrorNode', async function
restMock.onGet(BLOCKS_LIMIT_ORDER_URL).reply(200, DEFAULT_BLOCKS_RES);
restMock.onGet(`blocks/${BLOCK_NUMBER}`).reply(200, DEFAULT_BLOCK);

const result = await ethImpl.getBlockTransactionCountByNumber('finalized');
const result = await ethImpl.getBlockTransactionCountByNumber('finalized', requestIdPrefix);
expect(result).equal(numberTo0x(BLOCK_TRANSACTION_COUNT));
});

Expand All @@ -109,7 +110,7 @@ describe('@ethGetBlockTransactionCountByNumber using MirrorNode', async function
restMock.onGet(BLOCKS_LIMIT_ORDER_URL).reply(200, DEFAULT_BLOCKS_RES);
restMock.onGet(`blocks/${BLOCK_NUMBER}`).reply(200, DEFAULT_BLOCK);

const result = await ethImpl.getBlockTransactionCountByNumber('safe');
const result = await ethImpl.getBlockTransactionCountByNumber('safe', requestIdPrefix);
expect(result).equal(numberTo0x(BLOCK_TRANSACTION_COUNT));
});

Expand All @@ -118,23 +119,23 @@ describe('@ethGetBlockTransactionCountByNumber using MirrorNode', async function
restMock.onGet(BLOCKS_LIMIT_ORDER_URL).reply(200, DEFAULT_BLOCKS_RES);
restMock.onGet(`blocks/${BLOCK_NUMBER}`).reply(200, DEFAULT_BLOCK);

const result = await ethImpl.getBlockTransactionCountByNumber('pending');
const result = await ethImpl.getBlockTransactionCountByNumber('pending', requestIdPrefix);
expect(result).equal(numberTo0x(BLOCK_TRANSACTION_COUNT));
});

it('eth_getBlockTransactionCountByNumber with earliest tag', async function () {
// mirror node request mocks
restMock.onGet(`blocks/0`).reply(200, DEFAULT_BLOCK);

const result = await ethImpl.getBlockTransactionCountByNumber('earliest');
const result = await ethImpl.getBlockTransactionCountByNumber('earliest', requestIdPrefix);
expect(result).equal(numberTo0x(BLOCK_TRANSACTION_COUNT));
});

it('eth_getBlockTransactionCountByNumber with hex number', async function () {
// mirror node request mocks
restMock.onGet(`blocks/3735929054`).reply(200, DEFAULT_BLOCK);

const result = await ethImpl.getBlockTransactionCountByNumber('0xdeadc0de');
const result = await ethImpl.getBlockTransactionCountByNumber('0xdeadc0de', requestIdPrefix);
expect(result).equal(numberTo0x(BLOCK_TRANSACTION_COUNT));
});
});
20 changes: 11 additions & 9 deletions packages/relay/tests/lib/eth/eth_getCode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ describe('@ethGetCode using MirrorNode', async function () {
let { restMock, hapiServiceInstance, ethImpl, cacheService } = generateEthTestEnv();
let validBlockParam = [null, 'earliest', 'latest', 'pending', 'finalized', 'safe', '0x0', '0x369ABF'];
let invalidBlockParam = ['hedera', 'ethereum', '0xhbar', '0x369ABF369ABF369ABF369ABF'];
const requestIdPrefix = `[Request ID: testId]`;
const requestDetails = { requestIdPrefix: `${requestIdPrefix}`, requestIp: '0.0.0.0' };

this.beforeEach(() => {
// reset cache and restMock
Expand Down Expand Up @@ -86,20 +88,20 @@ describe('@ethGetCode using MirrorNode', async function () {
}),
);

const resNoCache = await ethImpl.getCode(CONTRACT_ADDRESS_1, null);
const resCached = await ethImpl.getCode(CONTRACT_ADDRESS_1, null);
const resNoCache = await ethImpl.getCode(CONTRACT_ADDRESS_1, null, requestDetails);
const resCached = await ethImpl.getCode(CONTRACT_ADDRESS_1, null, requestDetails);
expect(resNoCache).to.equal(EthImpl.emptyHex);
expect(resCached).to.equal(EthImpl.emptyHex);
});

it('should return the runtime_bytecode from the mirror node', async () => {
const res = await ethImpl.getCode(CONTRACT_ADDRESS_1, null);
const res = await ethImpl.getCode(CONTRACT_ADDRESS_1, null, requestDetails);
expect(res).to.equal(MIRROR_NODE_DEPLOYED_BYTECODE);
});

it('should return the bytecode from SDK if Mirror Node returns 404', async () => {
restMock.onGet(`contracts/${CONTRACT_ADDRESS_1}`).reply(404, DEFAULT_CONTRACT);
const res = await ethImpl.getCode(CONTRACT_ADDRESS_1, null);
const res = await ethImpl.getCode(CONTRACT_ADDRESS_1, null, requestDetails);
expect(res).to.equal(DEPLOYED_BYTECODE);
});

Expand All @@ -108,7 +110,7 @@ describe('@ethGetCode using MirrorNode', async function () {
...DEFAULT_CONTRACT,
runtime_bytecode: EthImpl.emptyHex,
});
const res = await ethImpl.getCode(CONTRACT_ADDRESS_1, null);
const res = await ethImpl.getCode(CONTRACT_ADDRESS_1, null, requestDetails);
expect(res).to.equal(DEPLOYED_BYTECODE);
});

Expand All @@ -119,29 +121,29 @@ describe('@ethGetCode using MirrorNode', async function () {
const redirectBytecode = `6080604052348015600f57600080fd5b506000610167905077618dc65e${HTS_TOKEN_ADDRESS.slice(
2,
)}600052366000602037600080366018016008845af43d806000803e8160008114605857816000f35b816000fdfea2646970667358221220d8378feed472ba49a0005514ef7087017f707b45fb9bf56bb81bb93ff19a238b64736f6c634300080b0033`;
const res = await ethImpl.getCode(HTS_TOKEN_ADDRESS, null);
const res = await ethImpl.getCode(HTS_TOKEN_ADDRESS, null, requestDetails);
expect(res).to.equal(redirectBytecode);
});

it('should return the static bytecode for address(0x167) call', async () => {
restMock.onGet(`contracts/${EthImpl.iHTSAddress}`).reply(200, DEFAULT_CONTRACT);
restMock.onGet(`accounts/${EthImpl.iHTSAddress}${NO_TRANSACTIONS}`).reply(404, null);

const res = await ethImpl.getCode(EthImpl.iHTSAddress, null);
const res = await ethImpl.getCode(EthImpl.iHTSAddress, null, requestDetails);
expect(res).to.equal(EthImpl.invalidEVMInstruction);
});

validBlockParam.forEach((blockParam) => {
it(`should pass the validate param check with blockParam=${blockParam} and return the bytecode`, async () => {
const res = await ethImpl.getCode(CONTRACT_ADDRESS_1, blockParam);
const res = await ethImpl.getCode(CONTRACT_ADDRESS_1, blockParam, requestDetails);
expect(res).to.equal(MIRROR_NODE_DEPLOYED_BYTECODE);
});
});

invalidBlockParam.forEach((blockParam) => {
it(`should throw INVALID_PARAMETER JsonRpcError with invalid blockParam=${blockParam}`, async () => {
try {
await ethImpl.getCode(EthImpl.iHTSAddress, blockParam);
await ethImpl.getCode(EthImpl.iHTSAddress, blockParam, requestDetails);
expect(true).to.eq(false);
} catch (error: any) {
const expectedError = predefined.UNKNOWN_BLOCK(
Expand Down
Loading

0 comments on commit 2b4d451

Please sign in to comment.