Skip to content

Commit cdbb494

Browse files
committed
fix: Handle latest block tag in getBlock
Fixes #17068
1 parent c8819e6 commit cdbb494

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

yarn-project/aztec-node/src/aztec-node/server.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { protocolContractTreeRoot } from '@aztec/protocol-contracts';
1111
import { computeFeePayerBalanceLeafSlot } from '@aztec/protocol-contracts/fee-juice';
1212
import type { GlobalVariableBuilder } from '@aztec/sequencer-client';
1313
import { AztecAddress } from '@aztec/stdlib/aztec-address';
14-
import type { L2BlockSource } from '@aztec/stdlib/block';
14+
import { L2Block, type L2BlockSource } from '@aztec/stdlib/block';
1515
import type { ContractDataSource } from '@aztec/stdlib/contract';
1616
import { EmptyL1RollupConstants } from '@aztec/stdlib/epoch-helpers';
1717
import { GasFees } from '@aztec/stdlib/gas';
@@ -321,6 +321,36 @@ describe('aztec node', () => {
321321
expect(await node.getBlockHeader(3)).toEqual(undefined);
322322
});
323323
});
324+
325+
describe('getBlock', () => {
326+
let block1: L2Block;
327+
let block2: L2Block;
328+
329+
beforeEach(() => {
330+
block1 = L2Block.empty();
331+
block2 = L2Block.empty();
332+
333+
l2BlockSource.getBlockNumber.mockResolvedValue(2);
334+
});
335+
336+
it('returns requested block number', async () => {
337+
l2BlockSource.getBlock.mockResolvedValue(block1);
338+
expect(await node.getBlock(1)).toEqual(block1);
339+
expect(l2BlockSource.getBlock).toHaveBeenCalledWith(1);
340+
});
341+
342+
it('returns latest block', async () => {
343+
l2BlockSource.getBlock.mockResolvedValue(block2);
344+
expect(await node.getBlock('latest')).toEqual(block2);
345+
expect(l2BlockSource.getBlock).toHaveBeenCalledWith(2);
346+
});
347+
348+
it('returns undefined for non-existent block', async () => {
349+
l2BlockSource.getBlock.mockResolvedValue(undefined);
350+
expect(await node.getBlock(3)).toEqual(undefined);
351+
expect(l2BlockSource.getBlock).toHaveBeenCalledWith(3);
352+
});
353+
});
324354
});
325355

326356
describe('simulatePublicCalls', () => {

yarn-project/aztec-node/src/aztec-node/server.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,9 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
510510
* @param number - The block number being requested.
511511
* @returns The requested block.
512512
*/
513-
public async getBlock(number: number): Promise<L2Block | undefined> {
514-
return await this.blockSource.getBlock(number);
513+
public async getBlock(number: L2BlockNumber): Promise<L2Block | undefined> {
514+
const blockNumber = number === 'latest' ? await this.getBlockNumber() : number;
515+
return await this.blockSource.getBlock(blockNumber);
515516
}
516517

517518
/**

0 commit comments

Comments
 (0)