Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Pad L1 to L2 messages upon retrieval from L1 #2879

Merged
merged 5 commits into from
Oct 17, 2023
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
1 change: 1 addition & 0 deletions yarn-project/archiver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"concurrently": "^8.0.1",
"jest": "^29.5.0",
"jest-mock-extended": "^3.0.4",
"lodash.times": "^4.3.2",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
Expand Down
56 changes: 56 additions & 0 deletions yarn-project/archiver/src/archiver/archiver.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/circuits.js';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
Expand All @@ -6,6 +7,7 @@ import { ContractDeploymentEmitterAbi, InboxAbi, RollupAbi } from '@aztec/l1-art
import { ExtendedContractData, L2Block, L2BlockL2Logs, LogType } from '@aztec/types';

import { MockProxy, mock } from 'jest-mock-extended';
import times from 'lodash.times';
import { Chain, HttpTransport, Log, PublicClient, Transaction, encodeFunctionData, toHex } from 'viem';

import { Archiver } from './archiver.js';
Expand Down Expand Up @@ -204,6 +206,60 @@ describe('Archiver', () => {

await archiver.stop();
}, 10_000);

it('pads L1 to L2 messages', async () => {
const NUM_RECEIVED_L1_MESSAGES = 2;

const archiver = new Archiver(
publicClient,
EthAddress.fromString(rollupAddress),
EthAddress.fromString(inboxAddress),
EthAddress.fromString(registryAddress),
EthAddress.fromString(contractDeploymentEmitterAddress),
0,
archiverStore,
1000,
);

let latestBlockNum = await archiver.getBlockNumber();
expect(latestBlockNum).toEqual(0);

const block = L2Block.random(1, 4, 1, 2, 4, 6);
block.newL1ToL2Messages = times(2, Fr.random);
const rollupTx = makeRollupTx(block);

publicClient.getBlockNumber.mockResolvedValueOnce(2500n);
// logs should be created in order of how archiver syncs.
publicClient.getLogs
.mockResolvedValueOnce(
makeL1ToL2MessageAddedEvents(
100n,
block.newL1ToL2Messages.map(x => x.toString(true)),
),
)
.mockResolvedValueOnce([])
.mockResolvedValueOnce([makeL2BlockProcessedEvent(101n, 1n)])
.mockResolvedValue([]);
publicClient.getTransaction.mockResolvedValueOnce(rollupTx);

await archiver.start(false);

// Wait until block 1 is processed. If this won't happen the test will fail with timeout.
while ((await archiver.getBlockNumber()) !== 1) {
await sleep(100);
}

latestBlockNum = await archiver.getBlockNumber();
expect(latestBlockNum).toEqual(1);

const expectedL1Messages = block.newL1ToL2Messages
.concat(times(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP - NUM_RECEIVED_L1_MESSAGES, () => Fr.ZERO))
.map(x => x.value);
const receivedBlock = await archiver.getBlock(1);
expect(receivedBlock?.newL1ToL2Messages.map(x => x.value)).toEqual(expectedL1Messages);

await archiver.stop();
}, 10_000);
});

/**
Expand Down
11 changes: 7 additions & 4 deletions yarn-project/archiver/src/archiver/archiver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FunctionSelector } from '@aztec/circuits.js';
import { FunctionSelector, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/circuits.js';
import { createEthereumChain } from '@aztec/ethereum';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { padArrayEnd } from '@aztec/foundation/collection';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';
Expand Down Expand Up @@ -281,9 +282,11 @@ export class Archiver implements L2BlockSource, L2LogsSource, ContractDataSource
// store retrieved L2 blocks after removing new logs information.
// remove logs to serve "lightweight" block information. Logs can be fetched separately if needed.
await this.store.addBlocks(
retrievedBlocks.retrievedData.map(block =>
L2Block.fromFields(omit(block, ['newEncryptedLogs', 'newUnencryptedLogs']), block.getBlockHash()),
),
retrievedBlocks.retrievedData.map(block => {
// Ensure we pad the L1 to L2 message array to the full size before storing.
block.newL1ToL2Messages = padArrayEnd(block.newL1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);
return L2Block.fromFields(omit(block, ['newEncryptedLogs', 'newUnencryptedLogs']), block.getBlockHash());
}),
);

// set the L1 block for the next search
Expand Down
1 change: 1 addition & 0 deletions yarn-project/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ __metadata:
jest: ^29.5.0
jest-mock-extended: ^3.0.4
lodash.omit: ^4.5.0
lodash.times: ^4.3.2
ts-jest: ^29.1.0
ts-node: ^10.9.1
tsc-watch: ^6.0.0
Expand Down