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 #701 - separate interface for pending l1 -> l2 message store #702

Merged
merged 2 commits into from
May 26, 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
4 changes: 2 additions & 2 deletions yarn-project/archiver/src/archiver/archiver_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@aztec/types';
import { Fr, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/circuits.js';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { L1ToL2MessageStore } from './l1_to_l2_message_store.js';
import { L1ToL2MessageStore, PendingL1ToL2MessageStore } from './l1_to_l2_message_store.js';

/**
* Interface describing a data store to be used by the archiver to store all its relevant data
Expand Down Expand Up @@ -62,7 +62,7 @@ export class MemoryArchiverStore implements ArchiverDataStore {
/**
* Contains all the pending L1 to L2 messages (accounts for duplication of messages)
*/
private pendingL1ToL2Messages: L1ToL2MessageStore = new L1ToL2MessageStore();
private pendingL1ToL2Messages: PendingL1ToL2MessageStore = new PendingL1ToL2MessageStore();

constructor() {}

Expand Down
18 changes: 14 additions & 4 deletions yarn-project/archiver/src/archiver/l1_to_l2_message_store.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fr } from '@aztec/foundation/fields';
import { L1ToL2MessageStore } from './l1_to_l2_message_store.js';
import { L1ToL2MessageStore, PendingL1ToL2MessageStore } from './l1_to_l2_message_store.js';
import { L1Actor, L1ToL2Message, L2Actor } from '@aztec/types';

describe('l1_to_l2_message_store', () => {
Expand All @@ -24,6 +24,19 @@ describe('l1_to_l2_message_store', () => {
store.addMessage(entryKey, msg);
expect(store.getMessageAndCount(entryKey)).toEqual({ message: msg, count: 2 });
});
});

describe('pending_l1_to_l2_message_store', () => {
let store: PendingL1ToL2MessageStore;
let entryKey: Fr;
let msg: L1ToL2Message;

beforeEach(() => {
// already adds a message to the store
store = new PendingL1ToL2MessageStore();
entryKey = Fr.random();
msg = L1ToL2Message.random();
});

it('removeMessage removes the message if the count is 1', () => {
store.addMessage(entryKey, msg);
Expand All @@ -44,7 +57,6 @@ describe('l1_to_l2_message_store', () => {
});

it('get messages for an empty store', () => {
const store = new L1ToL2MessageStore();
expect(store.getMessageKeys(10)).toEqual([]);
});

Expand All @@ -54,7 +66,6 @@ describe('l1_to_l2_message_store', () => {
});

it('get messages for a non-empty store when take > number of messages in store', () => {
const store = new L1ToL2MessageStore();
const entryKeys = [1, 2, 3, 4, 5].map(x => new Fr(x));
entryKeys.forEach(entryKey => {
store.addMessage(entryKey, L1ToL2Message.random());
Expand All @@ -63,7 +74,6 @@ describe('l1_to_l2_message_store', () => {
});

it('get messages returns messages sorted by fees and also includes multiple of the same message', () => {
const store = new L1ToL2MessageStore();
const entryKeys = [1, 2, 3, 3, 3, 4].map(x => new Fr(x));
entryKeys.forEach(entryKey => {
// set msg.fee to entryKey to test the sort.
Expand Down
34 changes: 20 additions & 14 deletions yarn-project/archiver/src/archiver/l1_to_l2_message_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class L1ToL2MessageStore {
* A map containing the message key to the corresponding L1 to L2
* messages (and the number of times the message has been seen).
*/
private store: Map<bigint, L1ToL2MessageAndCount> = new Map();
protected store: Map<bigint, L1ToL2MessageAndCount> = new Map();

constructor() {}

Expand All @@ -24,27 +24,20 @@ export class L1ToL2MessageStore {
}
}

removeMessage(messageKey: Fr) {
const messageKeyBigInt = messageKey.value;
const msgAndCount = this.store.get(messageKeyBigInt);
if (!msgAndCount) {
return;
}
if (msgAndCount.count > 1) {
msgAndCount.count--;
} else {
this.store.delete(messageKeyBigInt);
}
}

getMessage(messageKey: Fr): L1ToL2Message | undefined {
return this.store.get(messageKey.value)?.message;
}

getMessageAndCount(messageKey: Fr): L1ToL2MessageAndCount | undefined {
return this.store.get(messageKey.value);
}
}

/**
* Specifically for the store that will hold pending messages
* for removing messages or fetching multiple messages.
*/
export class PendingL1ToL2MessageStore extends L1ToL2MessageStore {
getMessageKeys(take: number): Fr[] {
if (take < 1) {
return [];
Expand All @@ -63,6 +56,19 @@ export class L1ToL2MessageStore {
}
return messages;
}

removeMessage(messageKey: Fr) {
const messageKeyBigInt = messageKey.value;
const msgAndCount = this.store.get(messageKeyBigInt);
if (!msgAndCount) {
return;
}
if (msgAndCount.count > 1) {
msgAndCount.count--;
} else {
this.store.delete(messageKeyBigInt);
}
}
}

/**
Expand Down