Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Mar 7, 2024
1 parent a4be66f commit f03406e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 45 deletions.
97 changes: 53 additions & 44 deletions yarn-project/archiver/src/archiver/archiver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,47 @@ import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { sleep } from '@aztec/foundation/sleep';
import { AvailabilityOracleAbi, ContractDeploymentEmitterAbi, InboxAbi, RollupAbi } from '@aztec/l1-artifacts';
import {
AvailabilityOracleAbi,
ContractDeploymentEmitterAbi,
InboxAbi,
NewInboxAbi,
RollupAbi,
} from '@aztec/l1-artifacts';

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

import { Archiver } from './archiver.js';
import { ArchiverDataStore } from './archiver_store.js';
import { MemoryArchiverStore } from './memory_archiver_store/memory_archiver_store.js';

describe('Archiver', () => {
const rollupAddress = EthAddress.ZERO.toString();
const inboxAddress = EthAddress.ZERO.toString();
const registryAddress = EthAddress.ZERO.toString();
const availabilityOracleAddress = EthAddress.ZERO.toString();
const contractDeploymentEmitterAddress = '0x0000000000000000000000000000000000000001';
const rollupAddress = EthAddress.ZERO;
const inboxAddress = EthAddress.ZERO;
// TODO(#4492): Nuke this once the old inbox is purged
const newInboxAddress = EthAddress.ZERO;
const registryAddress = EthAddress.ZERO;
const availabilityOracleAddress = EthAddress.ZERO;
const contractDeploymentEmitterAddress = EthAddress.fromString('0x0000000000000000000000000000000000000001');
const blockNumbers = [1, 2, 3];
let publicClient: MockProxy<PublicClient<HttpTransport, Chain>>;
let archiverStore: ArchiverDataStore;
let newInboxAddress!: EthAddress;

beforeEach(async () => {
beforeEach(() => {
publicClient = mock<PublicClient<HttpTransport, Chain>>();
archiverStore = new MemoryArchiverStore(1000);

// TODO(#4492): Nuke this once the old inbox is purged
{
const rollup = getContract({
address: getAddress(rollupAddress.toString()),
abi: RollupAbi,
client: publicClient,
});
newInboxAddress = EthAddress.fromString(await rollup.read.NEW_INBOX());
}
});

it('can start, sync and stop and handle l1 to l2 messages and logs', async () => {
const archiver = new Archiver(
publicClient,
EthAddress.fromString(rollupAddress),
EthAddress.fromString(availabilityOracleAddress),
EthAddress.fromString(inboxAddress),
rollupAddress,
availabilityOracleAddress,
inboxAddress,
newInboxAddress,
EthAddress.fromString(registryAddress),
EthAddress.fromString(contractDeploymentEmitterAddress),
registryAddress,
contractDeploymentEmitterAddress,
archiverStore,
1000,
);
Expand Down Expand Up @@ -104,11 +91,13 @@ describe('Archiver', () => {
publicClient.getLogs
.mockResolvedValueOnce(l1ToL2MessageAddedEvents.slice(0, 2).flat())
.mockResolvedValueOnce([]) // no messages to cancel
.mockResolvedValueOnce([makeLeafInsertedEvent(98n, 1n, 0n), makeLeafInsertedEvent(99n, 1n, 1n)])
.mockResolvedValueOnce([makeTxsPublishedEvent(101n, blocks[0].body.getTxsEffectsHash())])
.mockResolvedValueOnce([makeL2BlockProcessedEvent(101n, 1n)])
.mockResolvedValueOnce([makeContractDeploymentEvent(103n, blocks[0])]) // the first loop of the archiver ends here at block 2500
.mockResolvedValueOnce(l1ToL2MessageAddedEvents.slice(2, 4).flat())
.mockResolvedValueOnce(makeL1ToL2MessageCancelledEvents(2503n, l1ToL2MessagesToCancel))
.mockResolvedValueOnce([makeLeafInsertedEvent(2504n, 2n, 0n), makeLeafInsertedEvent(2504n, 2n, 1n)])
.mockResolvedValueOnce([
makeTxsPublishedEvent(2510n, blocks[1].body.getTxsEffectsHash()),
makeTxsPublishedEvent(2520n, blocks[2].body.getTxsEffectsHash()),
Expand Down Expand Up @@ -167,12 +156,12 @@ describe('Archiver', () => {
const numL2BlocksInTest = 2;
const archiver = new Archiver(
publicClient,
EthAddress.fromString(rollupAddress),
EthAddress.fromString(availabilityOracleAddress),
EthAddress.fromString(inboxAddress),
rollupAddress,
availabilityOracleAddress,
inboxAddress,
newInboxAddress,
EthAddress.fromString(registryAddress),
EthAddress.fromString(contractDeploymentEmitterAddress),
registryAddress,
contractDeploymentEmitterAddress,
archiverStore,
1000,
);
Expand Down Expand Up @@ -222,6 +211,7 @@ describe('Archiver', () => {
);
})
.mockResolvedValueOnce([])
.mockResolvedValueOnce([makeLeafInsertedEvent(66n, 1n, 0n), makeLeafInsertedEvent(68n, 1n, 1n)])
.mockResolvedValueOnce([
makeTxsPublishedEvent(70n, blocks[0].body.getTxsEffectsHash()),
makeTxsPublishedEvent(80n, blocks[1].body.getTxsEffectsHash()),
Expand Down Expand Up @@ -252,12 +242,12 @@ describe('Archiver', () => {
it('pads L1 to L2 messages', async () => {
const archiver = new Archiver(
publicClient,
EthAddress.fromString(rollupAddress),
EthAddress.fromString(availabilityOracleAddress),
EthAddress.fromString(inboxAddress),
rollupAddress,
availabilityOracleAddress,
inboxAddress,
newInboxAddress,
EthAddress.fromString(registryAddress),
EthAddress.fromString(contractDeploymentEmitterAddress),
registryAddress,
contractDeploymentEmitterAddress,
archiverStore,
1000,
);
Expand All @@ -279,6 +269,7 @@ describe('Archiver', () => {
),
)
.mockResolvedValueOnce([])
.mockResolvedValueOnce([])
.mockResolvedValueOnce([makeTxsPublishedEvent(101n, block.body.getTxsEffectsHash())])
.mockResolvedValueOnce([makeL2BlockProcessedEvent(101n, 1n)])
.mockResolvedValue([]);
Expand Down Expand Up @@ -402,6 +393,24 @@ function makeL1ToL2MessageCancelledEvents(l1BlockNum: bigint, entryKeys: string[
});
}

/**
* Makes fake L1ToL2 LeafInserted events for testing purposes.
* @param l1BlockNum - L1 block number.
* @param l2BlockNumber - The L2 block number of the leaf inserted.
* @returns LeafInserted event logs.
*/
function makeLeafInsertedEvent(l1BlockNum: bigint, l2BlockNumber: bigint, index: bigint) {
return {
blockNumber: l1BlockNum,
args: {
blockNumber: l2BlockNumber,
index,
value: Fr.random().toString(),
},
transactionHash: `0x${l1BlockNum}`,
} as Log<bigint, number, false, undefined, true, typeof NewInboxAbi, 'LeafInserted'>;
}

/**
* Makes a fake rollup tx for testing purposes.
* @param block - The L2Block.
Expand Down
14 changes: 14 additions & 0 deletions yarn-project/archiver/src/archiver/archiver_store_test_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
L2BlockContext,
LogId,
LogType,
NewInboxLeaf,
TxHash,
UnencryptedL2Log,
} from '@aztec/circuit-types';
Expand Down Expand Up @@ -96,6 +97,7 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch
addedBlock: 0n,
addedMessages: 0n,
cancelledMessages: 0n,
newMessages: 0n,
});
});

Expand All @@ -105,6 +107,7 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch
addedBlock: blocks.at(-1)!.getL1BlockNumber(),
addedMessages: 0n,
cancelledMessages: 0n,
newMessages: 0n,
});
});

Expand All @@ -114,6 +117,16 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch
addedBlock: 0n,
addedMessages: 1n,
cancelledMessages: 0n,
newMessages: 0n,
});
});
it('returns the L1 block number that most recently added messages from new inbox', async () => {
await store.addNewL1ToL2Messages([new NewInboxLeaf(0n, 0n, Buffer.alloc(32))], 1n);
await expect(store.getL1BlockNumber()).resolves.toEqual({
addedBlock: 0n,
addedMessages: 0n,
cancelledMessages: 0n,
newMessages: 1n,
});
});
it('returns the L1 block number that most recently cancelled pending messages', async () => {
Expand All @@ -124,6 +137,7 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch
addedBlock: 0n,
addedMessages: 1n,
cancelledMessages: 2n,
newMessages: 0n,
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class MemoryArchiverStore implements ArchiverDataStore {
return Promise.resolve(false);
}

this.lastL1BlockAddedMessages = lastMessageL1BlockNumber;
this.lastL1BlockNewMessages = lastMessageL1BlockNumber;
for (const message of messages) {
this.newL1ToL2Messages.addMessage(message);
}
Expand Down

0 comments on commit f03406e

Please sign in to comment.