Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Fix getblock.transactions #7151

Merged
merged 4 commits into from
Jul 16, 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
6 changes: 5 additions & 1 deletion packages/web3-eth/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,8 @@ Documentation:

- Fixed geth issue when running a new instance, transactions will index when there are no blocks created (#7098)

## [Unreleased]
## [Unreleased]

### Fixed

- Adds transaction property to be an empty list rather than undefined when no transactions are included in the block (#7151)
12 changes: 11 additions & 1 deletion packages/web3-eth/src/rpc_method_wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,21 @@ export async function getBlock<ReturnFormat extends DataFormat>(
hydrated,
);
}
return format(
const res = format(
blockSchema,
response as unknown as Block,
returnFormat ?? web3Context.defaultReturnFormat,
);

if (!isNullish(res)) {
const result = {
...res,
transactions: res.transactions ?? [],
}
return result;
}

return res;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export const mockRpcResponseHydrated: Block = {
...mockRpcResponse,
transactions: [hydratedTransaction, hydratedTransaction, hydratedTransaction],
};
export const noTransactionBlock: Block = {
...mockRpcResponse,
transactions: [],
}

/**
* Array consists of:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { isBytes, isNullish } from 'web3-validator';
import { ethRpcMethods } from 'web3-rpc-methods';

import { getBlock } from '../../../src/rpc_method_wrappers';
import { mockRpcResponse, mockRpcResponseHydrated, testData } from './fixtures/get_block';
import { mockRpcResponse, mockRpcResponseHydrated, testData, noTransactionBlock } from './fixtures/get_block';
import { blockSchema } from '../../../src/schemas';

jest.mock('web3-rpc-methods');
Expand Down Expand Up @@ -84,4 +84,31 @@ describe('getBlock', () => {
expect(result).toStrictEqual(expectedFormattedResult);
},
);

it.each(testData)(
`should format the block to include transactions as an empty array if no transactions are present\nTitle: %s\nInput parameters: %s\n`,
async (_, inputParameters) => {
const [inputBlock] = inputParameters;
const expectedReturnFormat = { number: FMT_NUMBER.STR, bytes: FMT_BYTES.UINT8ARRAY };
const expectedMockRpcResponse = noTransactionBlock;
// TODO: Fix format to default have a default in oneOf if no schema is matched
const formattedResult = format(
blockSchema,
expectedMockRpcResponse,
expectedReturnFormat,
);
const expectedFormattedResult = {...formattedResult,
transactions: []
};
const inputBlockIsBytes = isBytes(inputBlock as Bytes);
(
(inputBlockIsBytes
? ethRpcMethods.getBlockByHash
: ethRpcMethods.getBlockByNumber) as jest.Mock
).mockResolvedValueOnce(expectedMockRpcResponse);

const result = await getBlock(web3Context, ...inputParameters, expectedReturnFormat);
expect(result).toStrictEqual(expectedFormattedResult);
},
);
});
Loading