Skip to content

Commit

Permalink
fix: added null check for nullable values in formatContractResult() (#…
Browse files Browse the repository at this point in the history
…3210)

* fix: added null check for nullable values in formatContractResult()

Signed-off-by: Logan Nguyen <[email protected]>

* fix: fixed unit tests

Signed-off-by: Logan Nguyen <[email protected]>

---------

Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node authored and ebadiere committed Nov 5, 2024
1 parent 22bc051 commit b893d2c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
6 changes: 3 additions & 3 deletions packages/relay/src/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ const formatContractResult = (cr: any) => {
const commonFields = {
blockHash: toHash32(cr.block_hash),
blockNumber: nullableNumberTo0x(cr.block_number),
from: cr.from.substring(0, 42),
from: cr.from?.substring(0, 42) || null,
gas: nanOrNumberTo0x(cr.gas_used),
gasPrice,
hash: cr.hash.substring(0, 66),
hash: cr.hash?.substring(0, 66) || null,
input: cr.function_parameters,
nonce: nanOrNumberTo0x(cr.nonce),
r: cr.r === null ? '0x0' : stripLeadingZeroForSignatures(cr.r.substring(0, 66)),
s: cr.s === null ? '0x0' : stripLeadingZeroForSignatures(cr.s.substring(0, 66)),
to: cr.to?.substring(0, 42),
to: cr.to?.substring(0, 42) || null,
transactionIndex: nullableNumberTo0x(cr.transaction_index),
type: cr.type === null ? '0x0' : nanOrNumberTo0x(cr.type),
v: cr.v === null ? '0x0' : nanOrNumberTo0x(cr.v),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('@ethGetTransactionByBlockHashAndIndex using MirrorNode', async functio
verifyAggregatedInfo(result);
});

it('eth_getTransactionByBlockHashAndIndex should throw for internal error', async function () {
it('eth_getTransactionByBlockHashAndIndex should NOT throw for internal error if `from` field is null', async function () {
const randomBlock = {
hash: '0x5f827a801c579c84eca738827b65612b28ed425b7578bfdd10177e24fc3db8d4b1a7f3d56d83c39b950cc5e4d175dd64',
count: 9,
Expand All @@ -131,16 +131,9 @@ describe('@ethGetTransactionByBlockHashAndIndex using MirrorNode', async functio
.onGet(contractResultsByHashByIndexURL(randomBlock.hash, randomBlock.count))
.reply(200, defaultContractResultsWithNullableFrom);

const args = [randomBlock.hash, numberTo0x(randomBlock.count), requestDetails];
const errMessage = "Cannot read properties of null (reading 'substring')";

await RelayAssertions.assertRejection(
predefined.INTERNAL_ERROR(errMessage),
ethImpl.getTransactionByBlockHashAndIndex,
true,
ethImpl,
args,
);
const result = await ethImpl.getTransactionByBlockHashAndIndex(randomBlock.hash, randomBlock.count, requestDetails);
expect(result).to.not.be.null;
expect(result.from).to.be.null;
});

it('eth_getTransactionByBlockHashAndIndex with no contract result match', async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('@ethGetTransactionByBlockNumberAndIndex using MirrorNode', async funct
expect(result).to.equal(null);
});

it('eth_getTransactionByBlockNumberAndIndex should throw for internal error', async function () {
it('eth_getTransactionByBlockNumberAndIndex should NOT throw for internal error if `from` field is null', async function () {
const defaultContractResultsWithNullableFrom = _.cloneDeep(defaultContractResults);
defaultContractResultsWithNullableFrom.results[0].from = null;
const randomBlock = {
Expand All @@ -170,16 +170,14 @@ describe('@ethGetTransactionByBlockNumberAndIndex using MirrorNode', async funct
.onGet(contractResultsByNumberByIndexURL(randomBlock.number, randomBlock.count))
.reply(200, defaultContractResultsWithNullableFrom);

const args = [numberTo0x(randomBlock.number), numberTo0x(randomBlock.count), requestDetails];
const errMessage = "Cannot read properties of null (reading 'substring')";

await RelayAssertions.assertRejection(
predefined.INTERNAL_ERROR(errMessage),
ethImpl.getTransactionByBlockNumberAndIndex,
true,
ethImpl,
args,
const result = await ethImpl.getTransactionByBlockNumberAndIndex(
numberTo0x(randomBlock.number),
numberTo0x(randomBlock.count),
requestDetails,
);

expect(result).to.not.be.null;
expect(result.from).to.be.null;
});

it('eth_getTransactionByBlockNumberAndIndex with no contract results', async function () {
Expand Down
9 changes: 9 additions & 0 deletions packages/relay/tests/lib/formatters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,15 @@ describe('Formatters', () => {
const formattedResult = formatContractResult({ ...contractResult, type: undefined });
expect(formattedResult).to.be.null;
});

it('Should return formatted result even when `from` and `hash` fields are null', () => {
const formattedResult: any = formatContractResult({ ...contractResult, from: null, hash: null, to: null });

expect(formattedResult).to.not.be.undefined;
expect(formattedResult.from).to.be.null;
expect(formattedResult.hash).to.be.null;
expect(formattedResult.to).to.be.null;
});
});

describe('prepend0x', () => {
Expand Down

0 comments on commit b893d2c

Please sign in to comment.