Skip to content

Commit

Permalink
final touches
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 11, 2023
1 parent d0fb428 commit 3195aec
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 73 deletions.
49 changes: 2 additions & 47 deletions yarn-project/archiver/src/archiver/archiver_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,51 +83,7 @@ describe('Archiver Memory Store', () => {
},
);

describe('getUnencryptedLogs errors and config', () => {
it('throws when log filter is invalid', async () => {
const txHash = new TxHash(randomBytes(TxHash.SIZE));
const fromBlock = 2;
const toBlock = 2;

// First invalid case - txHash and "fromBlock" are set
const filter1 = {
txHash,
fromBlock,
};
await expect(async () => await archiverStore.getUnencryptedLogs(filter1)).rejects.toThrow(
'Invalid filter: "txHash" is set along',
);

// Second invalid case - "txHash" and "toBlock" are set
const filter2 = {
txHash,
toBlock,
};
await expect(async () => await archiverStore.getUnencryptedLogs(filter2)).rejects.toThrow(
'Invalid filter: "txHash" is set along',
);
});

it('does not throw when both "fromBlock" and "txHash" are set with afterLog', async () => {
// this case should not throw because "afterLog" should take precedence
const txHash = new TxHash(randomBytes(TxHash.SIZE));
const fromBlock = 2;
const toBlock = 3;
const afterLog = new LogId(1, 2, 3);

// First invalid case - "txHash" and "fromBlock" are set
const filter1 = {
txHash,
fromBlock,
toBlock,
afterLog,
};
const response = await archiverStore.getUnencryptedLogs(filter1);

expect(response.logs.length).toEqual(0);
expect(response.maxLogsHit).toEqual(false);
});

describe('getUnencryptedLogs config', () => {
it('does not return more than "maxLogs" logs', async () => {
const maxLogs = 5;
archiverStore = new MemoryArchiverStore(maxLogs);
Expand Down Expand Up @@ -286,7 +242,7 @@ describe('Archiver Memory Store', () => {
expect(response.logs.length).toBeGreaterThan(1);
});

it.only('intersecting works', async () => {
it('intersecting works', async () => {
let logs = (await archiverStore.getUnencryptedLogs({ fromBlock: -10, toBlock: -5 })).logs;
expect(logs.length).toBe(0);

Expand All @@ -310,7 +266,6 @@ describe('Archiver Memory Store', () => {
// intersecting with "afterLog" works
logs = (await archiverStore.getUnencryptedLogs({ fromBlock: 2, toBlock: 5, afterLog: new LogId(4, 0, 0) })).logs;
blockNumbers = new Set(logs.map(log => log.id.blockNumber));
// Check that blockNumbers contain only 4
expect(blockNumbers).toEqual(new Set([4]));

logs = (await archiverStore.getUnencryptedLogs({ toBlock: 5, afterLog: new LogId(5, 1, 0) })).logs;
Expand Down
14 changes: 8 additions & 6 deletions yarn-project/archiver/src/archiver/archiver_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
LogType,
TxHash,
UnencryptedL2Log,
validateLogFilter,
} from '@aztec/types';

import { L1ToL2MessageStore, PendingL1ToL2MessageStore } from './l1_to_l2_message_store.js';
Expand Down Expand Up @@ -373,8 +372,6 @@ export class MemoryArchiverStore implements ArchiverDataStore {
* @throws If filter is invalid.
*/
getUnencryptedLogs(filter: LogFilter): Promise<GetsUnencryptedLogsResponse> {
validateLogFilter(filter);

let txHash: TxHash | undefined;
let fromBlockIndex = 0;
let toBlockIndex = this.unencryptedLogsPerBlock.length;
Expand All @@ -396,11 +393,16 @@ export class MemoryArchiverStore implements ArchiverDataStore {
if (filter.fromBlock !== undefined) {
fromBlockIndex = filter.fromBlock - INITIAL_L2_BLOCK_NUM;
}
if (filter.toBlock !== undefined) {
toBlockIndex = filter.toBlock - INITIAL_L2_BLOCK_NUM;
}
}

if (filter.toBlock !== undefined) {
toBlockIndex = filter.toBlock - INITIAL_L2_BLOCK_NUM;
}

// Ensure the indices are within block array bounds
fromBlockIndex = Math.max(fromBlockIndex, 0);
toBlockIndex = Math.min(toBlockIndex, this.unencryptedLogsPerBlock.length);

if (fromBlockIndex > this.unencryptedLogsPerBlock.length || toBlockIndex < fromBlockIndex || toBlockIndex <= 0) {
return Promise.resolve({
logs: [],
Expand Down
4 changes: 1 addition & 3 deletions yarn-project/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { DebugLogger, LogFn } from '@aztec/foundation/log';
import { sleep } from '@aztec/foundation/sleep';
import { fileURLToPath } from '@aztec/foundation/url';
import { compileContract, generateNoirInterface, generateTypescriptInterface } from '@aztec/noir-compiler/cli';
import { CompleteAddress, ContractData, LogFilter, validateLogFilter } from '@aztec/types';
import { CompleteAddress, ContractData, LogFilter } from '@aztec/types';

import { createSecp256k1PeerId } from '@libp2p/peer-id-factory';
import { Command, Option } from 'commander';
Expand Down Expand Up @@ -306,8 +306,6 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {

const filter: LogFilter = { txHash, fromBlock, toBlock, afterLog, contractAddress, selector };

validateLogFilter(filter);

const fetchLogs = async () => {
const response = await pxe.getUnencryptedLogs(filter);
const logs = response.logs;
Expand Down
18 changes: 1 addition & 17 deletions yarn-project/types/src/logs/log_filter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AztecAddress, FunctionSelector } from '@aztec/circuits.js';

import { INITIAL_L2_BLOCK_NUM, LogId, TxHash } from '../index.js';
import { LogId, TxHash } from '../index.js';

/**
* Log filter used to fetch L2 logs.
Expand Down Expand Up @@ -31,19 +31,3 @@ export type LogFilter = {
*/
selector?: FunctionSelector;
};

/**
* Validates a log filter.
* @param filter - Log filter to validate.
* @throws If the filter is invalid.
*/
export function validateLogFilter(filter: LogFilter) {
if (filter.afterLog) {
// afterLog is a continuation parameter and when it is set, "txHash" is ignored and "fromBlock" and "toBlock" are
// intersected with.
return;
}
if (filter.txHash && (filter.fromBlock || filter.toBlock)) {
throw new Error('Invalid filter: "txHash" is set along with "fromBlock" and or "toBlock".');
}
}

0 comments on commit 3195aec

Please sign in to comment.