Skip to content

Commit

Permalink
Fix getblock.transactions (#7151)
Browse files Browse the repository at this point in the history
* add default array to block.transactions

* update

* update
  • Loading branch information
luu-alex authored Jul 16, 2024
1 parent 6b80cf0 commit 9afaa61
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
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);
},
);
});

1 comment on commit 9afaa61

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 9afaa61 Previous: 6b80cf0 Ratio
processingTx 9405 ops/sec (±3.55%) 9439 ops/sec (±3.74%) 1.00
processingContractDeploy 40485 ops/sec (±7.27%) 40790 ops/sec (±6.86%) 1.01
processingContractMethodSend 15979 ops/sec (±8.96%) 16075 ops/sec (±6.85%) 1.01
processingContractMethodCall 28320 ops/sec (±6.00%) 26970 ops/sec (±8.10%) 0.95
abiEncode 44820 ops/sec (±7.48%) 43844 ops/sec (±7.10%) 0.98
abiDecode 30901 ops/sec (±8.03%) 30447 ops/sec (±7.63%) 0.99
sign 1528 ops/sec (±3.66%) 1584 ops/sec (±0.51%) 1.04
verify 377 ops/sec (±0.59%) 358 ops/sec (±2.93%) 0.95

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.