Skip to content

Commit

Permalink
test: fix l1-publisher-test
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Sep 11, 2024
1 parent 6b4140b commit e67fe5e
Showing 1 changed file with 11 additions and 52 deletions.
63 changes: 11 additions & 52 deletions yarn-project/sequencer-client/src/publisher/l1-publisher.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { L2Block, type ViemSignature } from '@aztec/circuit-types';
import { EthAddress } from '@aztec/circuits.js';
import { sleep } from '@aztec/foundation/sleep';
import { RollupAbi } from '@aztec/l1-artifacts';
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';

import { type MockProxy, mock } from 'jest-mock-extended';
Expand All @@ -13,6 +14,7 @@ interface MockPublicClient {
getTransactionReceipt: ({ hash }: { hash: '0x${string}' }) => Promise<GetTransactionReceiptReturnType>;
getBlock(): Promise<{ timestamp: bigint }>;
getTransaction: ({ hash }: { hash: '0x${string}' }) => Promise<{ input: `0x${string}`; hash: `0x${string}` }>;
estimateGas: ({ to, data }: { to: '0x${string}'; data: '0x${string}' }) => Promise<bigint>;
}

interface MockRollupContractWrite {
Expand All @@ -37,24 +39,17 @@ interface MockRollupContractRead {
}

class MockRollupContract {
constructor(
public write: MockRollupContractWrite,
public simulate: MockRollupContractWrite,
public read: MockRollupContractRead,
) {}
constructor(public write: MockRollupContractWrite, public read: MockRollupContractRead, public abi = RollupAbi) {}
}

describe('L1Publisher', () => {
let rollupContractRead: MockProxy<MockRollupContractRead>;
let rollupContractWrite: MockProxy<MockRollupContractWrite>;
let rollupContractSimulate: MockProxy<MockRollupContractWrite>;
let rollupContract: MockRollupContract;

let publicClient: MockProxy<MockPublicClient>;

let processTxHash: `0x${string}`;
let proposeTxHash: `0x${string}`;
let processTxReceipt: GetTransactionReceiptReturnType;
let proposeTxReceipt: GetTransactionReceiptReturnType;
let l2Block: L2Block;

Expand All @@ -77,24 +72,17 @@ describe('L1Publisher', () => {
blockHash = l2Block.header.hash().toBuffer();
body = l2Block.body.toBuffer();

processTxHash = `0x${Buffer.from('txHashProcess').toString('hex')}`; // random tx hash
proposeTxHash = `0x${Buffer.from('txHashPropose').toString('hex')}`; // random tx hash

processTxReceipt = {
transactionHash: processTxHash,
status: 'success',
logs: [],
} as unknown as GetTransactionReceiptReturnType;
proposeTxReceipt = {
transactionHash: proposeTxHash,
status: 'success',
logs: [],
} as unknown as GetTransactionReceiptReturnType;

rollupContractWrite = mock<MockRollupContractWrite>();
rollupContractSimulate = mock<MockRollupContractWrite>();
rollupContractRead = mock<MockRollupContractRead>();
rollupContract = new MockRollupContract(rollupContractWrite, rollupContractSimulate, rollupContractRead);
rollupContract = new MockRollupContract(rollupContractWrite, rollupContractRead);

publicClient = mock<MockPublicClient>();

Expand All @@ -117,12 +105,12 @@ describe('L1Publisher', () => {

rollupContractRead.getCurrentSlot.mockResolvedValue(l2Block.header.globalVariables.slotNumber.toBigInt());
publicClient.getBlock.mockResolvedValue({ timestamp: 12n });
publicClient.estimateGas.mockResolvedValue(GAS_GUESS);
});

it('publishes and propose l2 block to l1', async () => {
rollupContractRead.archive.mockResolvedValue(l2Block.header.lastArchive.root.toString() as `0x${string}`);
rollupContractWrite.propose.mockResolvedValueOnce(proposeTxHash);
rollupContractSimulate.propose.mockResolvedValueOnce(proposeTxHash);

publicClient.getTransactionReceipt.mockResolvedValueOnce(proposeTxReceipt);

Expand All @@ -139,40 +127,14 @@ describe('L1Publisher', () => {
] as const;
expect(rollupContractWrite.propose).toHaveBeenCalledWith(args, {
account: account,
gas: L1Publisher.PROPOSE_GAS_GUESS + GAS_GUESS * 2n,
gas: L1Publisher.PROPOSE_GAS_GUESS + GAS_GUESS,
});
expect(publicClient.getTransactionReceipt).toHaveBeenCalledWith({ hash: proposeTxHash });
});

it('publishes l2 block to l1 (already published body)', async () => {
rollupContractRead.archive.mockResolvedValue(l2Block.header.lastArchive.root.toString() as `0x${string}`);
rollupContractWrite.propose.mockResolvedValueOnce(processTxHash);
rollupContractSimulate.propose.mockResolvedValueOnce(processTxHash);
publicClient.getTransactionReceipt.mockResolvedValueOnce(processTxReceipt);

const result = await publisher.processL2Block(l2Block);

expect(result).toEqual(true);
const args = [
`0x${header.toString('hex')}`,
`0x${archive.toString('hex')}`,
`0x${blockHash.toString('hex')}`,
[],
] as const;
expect(rollupContractWrite.propose).toHaveBeenCalledWith(args, { account, gas: L1Publisher.PROPOSE_GAS_GUESS });
expect(publicClient.getTransactionReceipt).toHaveBeenCalledWith({ hash: processTxHash });
});

it('does not retry if sending a propose tx fails', async () => {
rollupContractRead.archive.mockResolvedValue(l2Block.header.lastArchive.root.toString() as `0x${string}`);
rollupContractWrite.propose
.mockRejectedValueOnce(new Error())
.mockResolvedValueOnce(processTxHash as `0x${string}`);

// Note that simulate will be valid both times
rollupContractSimulate.propose
.mockResolvedValueOnce(processTxHash as `0x${string}`)
.mockResolvedValueOnce(processTxHash as `0x${string}`);
rollupContractWrite.propose.mockRejectedValueOnce(new Error()).mockResolvedValueOnce(proposeTxHash);

const result = await publisher.processL2Block(l2Block);

Expand All @@ -192,7 +154,6 @@ describe('L1Publisher', () => {

it('does not retry if sending a publish and propose tx fails', async () => {
rollupContractRead.archive.mockResolvedValue(l2Block.header.lastArchive.root.toString() as `0x${string}`);
rollupContractSimulate.propose.mockResolvedValueOnce(proposeTxHash as `0x${string}`);
rollupContractWrite.propose.mockRejectedValueOnce(new Error());

const result = await publisher.processL2Block(l2Block);
Expand All @@ -203,9 +164,8 @@ describe('L1Publisher', () => {

it('retries if fetching the receipt fails (propose)', async () => {
rollupContractRead.archive.mockResolvedValue(l2Block.header.lastArchive.root.toString() as `0x${string}`);
rollupContractSimulate.propose.mockResolvedValueOnce(processTxHash);
rollupContractWrite.propose.mockResolvedValueOnce(processTxHash);
publicClient.getTransactionReceipt.mockRejectedValueOnce(new Error()).mockResolvedValueOnce(processTxReceipt);
rollupContractWrite.propose.mockResolvedValueOnce(proposeTxHash);
publicClient.getTransactionReceipt.mockRejectedValueOnce(new Error()).mockResolvedValueOnce(proposeTxReceipt);

const result = await publisher.processL2Block(l2Block);

Expand All @@ -215,7 +175,6 @@ describe('L1Publisher', () => {

it('retries if fetching the receipt fails (publish propose)', async () => {
rollupContractRead.archive.mockResolvedValue(l2Block.header.lastArchive.root.toString() as `0x${string}`);
rollupContractSimulate.propose.mockResolvedValueOnce(proposeTxHash as `0x${string}`);
rollupContractWrite.propose.mockResolvedValueOnce(proposeTxHash as `0x${string}`);
publicClient.getTransactionReceipt.mockRejectedValueOnce(new Error()).mockResolvedValueOnce(proposeTxReceipt);

Expand All @@ -238,7 +197,7 @@ describe('L1Publisher', () => {
it('returns false if propose tx reverts', async () => {
rollupContractRead.archive.mockResolvedValue(l2Block.header.lastArchive.root.toString() as `0x${string}`);

publicClient.getTransactionReceipt.mockResolvedValueOnce({ ...processTxReceipt, status: 'reverted' });
publicClient.getTransactionReceipt.mockResolvedValueOnce({ ...proposeTxReceipt, status: 'reverted' });

const result = await publisher.processL2Block(l2Block);

Expand All @@ -259,7 +218,7 @@ describe('L1Publisher', () => {

it('returns false if sending propose tx is interrupted', async () => {
rollupContractRead.archive.mockResolvedValue(l2Block.header.lastArchive.root.toString() as `0x${string}`);
rollupContractWrite.propose.mockImplementationOnce(() => sleep(10, processTxHash) as Promise<`0x${string}`>);
rollupContractWrite.propose.mockImplementationOnce(() => sleep(10, proposeTxHash) as Promise<`0x${string}`>);

const resultPromise = publisher.processL2Block(l2Block);
publisher.interrupt();
Expand Down

0 comments on commit e67fe5e

Please sign in to comment.