diff --git a/yarn-project/archiver/src/archiver/archiver_store.ts b/yarn-project/archiver/src/archiver/archiver_store.ts index 8d5dec6a6df..5b2b9388434 100644 --- a/yarn-project/archiver/src/archiver/archiver_store.ts +++ b/yarn-project/archiver/src/archiver/archiver_store.ts @@ -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 @@ -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() {} diff --git a/yarn-project/archiver/src/archiver/l1_to_l2_message_store.test.ts b/yarn-project/archiver/src/archiver/l1_to_l2_message_store.test.ts index fbf197a7342..60201d595fc 100644 --- a/yarn-project/archiver/src/archiver/l1_to_l2_message_store.test.ts +++ b/yarn-project/archiver/src/archiver/l1_to_l2_message_store.test.ts @@ -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', () => { @@ -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); @@ -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([]); }); @@ -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()); @@ -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. diff --git a/yarn-project/archiver/src/archiver/l1_to_l2_message_store.ts b/yarn-project/archiver/src/archiver/l1_to_l2_message_store.ts index a936b22641f..a87f828d4b6 100644 --- a/yarn-project/archiver/src/archiver/l1_to_l2_message_store.ts +++ b/yarn-project/archiver/src/archiver/l1_to_l2_message_store.ts @@ -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 = new Map(); + protected store: Map = new Map(); constructor() {} @@ -24,19 +24,6 @@ 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; } @@ -44,7 +31,13 @@ export class L1ToL2MessageStore { 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 []; @@ -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); + } + } } /**