From 9e6250aacbe2d47aa71dee9fa5e43c66eec73e75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Fri, 26 Jan 2024 19:29:45 -0300 Subject: [PATCH] chore: delete MemoryDB (#4241) This module is not being used anywhere and was just being kept around as a reference. However this results in a maintenance burden as this shares tests with the KV DB, which means we have to keep feature parity (see e.g. https://github.com/AztecProtocol/aztec-packages/pull/4238). The KV DB is in a good place already, and we can always look at the repo history if we wish to bring this back, so the simplest path forward is to remove this. --- .../end-to-end/src/e2e_persistence.test.ts | 2 +- yarn-project/pxe/src/database/index.ts | 1 - .../pxe/src/database/memory_db.test.ts | 67 ----- yarn-project/pxe/src/database/memory_db.ts | 248 ------------------ 4 files changed, 1 insertion(+), 317 deletions(-) delete mode 100644 yarn-project/pxe/src/database/memory_db.test.ts delete mode 100644 yarn-project/pxe/src/database/memory_db.ts diff --git a/yarn-project/end-to-end/src/e2e_persistence.test.ts b/yarn-project/end-to-end/src/e2e_persistence.test.ts index 98726df23a7..7750291f270 100644 --- a/yarn-project/end-to-end/src/e2e_persistence.test.ts +++ b/yarn-project/end-to-end/src/e2e_persistence.test.ts @@ -153,7 +153,7 @@ describe('Aztec persistence', () => { expect(ownerBalance).toEqual(initialOwnerBalance - 500n); expect(targetBalance).toEqual(500n); - }); + }, 30_000); }); describe.each([ diff --git a/yarn-project/pxe/src/database/index.ts b/yarn-project/pxe/src/database/index.ts index 35d4e000a20..4685cc2f7a8 100644 --- a/yarn-project/pxe/src/database/index.ts +++ b/yarn-project/pxe/src/database/index.ts @@ -1,2 +1 @@ export * from './pxe_database.js'; -export * from './memory_db.js'; diff --git a/yarn-project/pxe/src/database/memory_db.test.ts b/yarn-project/pxe/src/database/memory_db.test.ts deleted file mode 100644 index f505efa4a79..00000000000 --- a/yarn-project/pxe/src/database/memory_db.test.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { AztecAddress, Fr } from '@aztec/circuits.js'; - -import { MemoryDB } from './memory_db.js'; -import { randomNoteDao } from './note_dao.test.js'; -import { describePxeDatabase } from './pxe_database_test_suite.js'; - -describe('Memory DB', () => { - let db: MemoryDB; - - beforeEach(() => { - db = new MemoryDB(); - }); - - describePxeDatabase(() => db); - - describe('NoteDao', () => { - const contractAddress = AztecAddress.random(); - const storageSlot = Fr.random(); - - const createNotes = (numberOfNotes: number, sameStorage = true) => - Array(numberOfNotes) - .fill(0) - .map(() => - randomNoteDao({ - contractAddress: sameStorage ? contractAddress : AztecAddress.random(), - storageSlot: sameStorage ? storageSlot : Fr.random(), - }), - ); - - it('should add and get notes', async () => { - const notes = createNotes(3, false); - for (let i = 0; i < notes.length; ++i) { - await db.addNote(notes[i]); - } - - for (let i = 0; i < notes.length; ++i) { - const result = await db.getNotes({ - contractAddress: notes[i].contractAddress, - storageSlot: notes[i].storageSlot, - }); - expect(result).toEqual([notes[i]]); - } - }); - - it('should batch add notes', async () => { - const notes = createNotes(3, false); - await db.addNotes(notes); - - for (let i = 0; i < notes.length; ++i) { - const result = await db.getNotes({ - contractAddress: notes[i].contractAddress, - storageSlot: notes[i].storageSlot, - }); - expect(result).toEqual([notes[i]]); - } - }); - - it('should get all notes with the same contract storage slot', async () => { - const notes = createNotes(3); - await db.addNotes(notes); - - const result = await db.getNotes({ contractAddress, storageSlot }); - expect(result.length).toBe(notes.length); - expect(result).toEqual(expect.objectContaining(notes)); - }); - }); -}); diff --git a/yarn-project/pxe/src/database/memory_db.ts b/yarn-project/pxe/src/database/memory_db.ts deleted file mode 100644 index 5390c654b94..00000000000 --- a/yarn-project/pxe/src/database/memory_db.ts +++ /dev/null @@ -1,248 +0,0 @@ -import { MerkleTreeId, NoteFilter } from '@aztec/circuit-types'; -import { BlockHeader, CompleteAddress, PublicKey } from '@aztec/circuits.js'; -import { ContractArtifact } from '@aztec/foundation/abi'; -import { AztecAddress } from '@aztec/foundation/aztec-address'; -import { Fr, Point } from '@aztec/foundation/fields'; -import { createDebugLogger } from '@aztec/foundation/log'; -import { ContractInstanceWithAddress } from '@aztec/types/contracts'; - -import { MemoryContractDatabase } from '../contract_database/index.js'; -import { DeferredNoteDao } from './deferred_note_dao.js'; -import { NoteDao } from './note_dao.js'; -import { PxeDatabase } from './pxe_database.js'; - -/** - * The MemoryDB class provides an in-memory implementation of a database to manage transactions and auxiliary data. - * It extends the MemoryContractDatabase, allowing it to store contract-related data as well. - * The class offers methods to add, fetch, and remove transaction records and auxiliary data based on various filters such as transaction hash, address, and storage slot. - * As an in-memory database, the stored data will not persist beyond the life of the application instance. - */ -export class MemoryDB extends MemoryContractDatabase implements PxeDatabase { - private notesTable: NoteDao[] = []; - private deferredNotesTable: DeferredNoteDao[] = []; - private treeRoots: Record | undefined; - private globalVariablesHash: Fr | undefined; - private blockNumber: number | undefined; - private addresses: CompleteAddress[] = []; - private authWitnesses: Record = {}; - private syncedBlockPerPublicKey = new Map(); - // A capsule is a "blob" of data that is passed to the contract through an oracle. - // We are using a stack to keep track of the capsules that are passed to the contract. - private capsuleStack: Fr[][] = []; - - private contractArtifacts = new Map(); - private contractInstances = new Map(); - - constructor(logSuffix?: string) { - super(createDebugLogger(logSuffix ? 'aztec:memory_db_' + logSuffix : 'aztec:memory_db')); - } - - public addContractArtifact(id: Fr, contract: ContractArtifact): Promise { - this.contractArtifacts.set(id.toString(), contract); - return Promise.resolve(); - } - - public getContractArtifact(id: Fr): Promise { - const contract = this.contractArtifacts.get(id.toString()); - return Promise.resolve(contract); - } - - public addContractInstance(contract: ContractInstanceWithAddress): Promise { - this.contractInstances.set(contract.address.toString(), contract); - return Promise.resolve(); - } - - public getContractInstance(address: AztecAddress): Promise { - const contract = this.contractInstances.get(address.toString()); - return Promise.resolve(contract); - } - - /** - * Add a auth witness to the database. - * @param messageHash - The message hash. - * @param witness - An array of field elements representing the auth witness. - */ - public addAuthWitness(messageHash: Fr, witness: Fr[]): Promise { - this.authWitnesses[messageHash.toString()] = witness; - return Promise.resolve(); - } - - /** - * Fetching the auth witness for a given message hash. - * @param messageHash - The message hash. - * @returns A Promise that resolves to an array of field elements representing the auth witness. - */ - public getAuthWitness(messageHash: Fr): Promise { - return Promise.resolve(this.authWitnesses[messageHash.toString()]); - } - - public addNote(note: NoteDao): Promise { - this.notesTable.push(note); - return Promise.resolve(); - } - - public addDeferredNotes(notes: DeferredNoteDao[]): Promise { - this.deferredNotesTable.push(...notes); - return Promise.resolve(); - } - - public getDeferredNotesByContract(contractAddress: AztecAddress): Promise { - return Promise.resolve(this.deferredNotesTable.filter(note => note.contractAddress.equals(contractAddress))); - } - - public removeDeferredNotesByContract(contractAddress: AztecAddress): Promise { - const removed: DeferredNoteDao[] = []; - this.deferredNotesTable = this.deferredNotesTable.filter(note => { - if (note.contractAddress.equals(contractAddress)) { - removed.push(note); - return false; - } - return true; - }); - return Promise.resolve(removed); - } - - public addCapsule(capsule: Fr[]): Promise { - this.capsuleStack.push(capsule); - return Promise.resolve(); - } - - public popCapsule(): Promise { - return Promise.resolve(this.capsuleStack.pop()); - } - - public addNotes(notes: NoteDao[]) { - this.notesTable.push(...notes); - return Promise.resolve(); - } - - public async getNotes(filter: NoteFilter): Promise { - let ownerPublicKey: PublicKey | undefined; - if (filter.owner !== undefined) { - const ownerCompleteAddress = await this.getCompleteAddress(filter.owner); - if (ownerCompleteAddress === undefined) { - throw new Error(`Owner ${filter.owner.toString()} not found in memory database`); - } - ownerPublicKey = ownerCompleteAddress.publicKey; - } - - return this.notesTable.filter( - note => - (filter.contractAddress == undefined || note.contractAddress.equals(filter.contractAddress)) && - (filter.txHash == undefined || note.txHash.equals(filter.txHash)) && - (filter.storageSlot == undefined || note.storageSlot.equals(filter.storageSlot!)) && - (ownerPublicKey == undefined || note.publicKey.equals(ownerPublicKey!)), - ); - } - - public removeNullifiedNotes(nullifiers: Fr[], account: PublicKey) { - const nullifierSet = new Set(nullifiers.map(nullifier => nullifier.toString())); - const [remaining, removed] = this.notesTable.reduce( - (acc: [NoteDao[], NoteDao[]], note) => { - const nullifier = note.siloedNullifier.toString(); - if (note.publicKey.equals(account) && nullifierSet.has(nullifier)) { - acc[1].push(note); - } else { - acc[0].push(note); - } - return acc; - }, - [[], []], - ); - - this.notesTable = remaining; - - return Promise.resolve(removed); - } - - public getTreeRoots(): Record { - const roots = this.treeRoots; - if (!roots) { - throw new Error(`Tree roots not set in memory database`); - } - return roots; - } - - private setTreeRoots(roots: Record) { - this.treeRoots = roots; - } - - public getBlockHeader(): BlockHeader { - const roots = this.getTreeRoots(); - if (!this.globalVariablesHash) { - throw new Error(`Global variables hash not set in memory database`); - } - return new BlockHeader( - roots[MerkleTreeId.NOTE_HASH_TREE], - roots[MerkleTreeId.NULLIFIER_TREE], - roots[MerkleTreeId.CONTRACT_TREE], - roots[MerkleTreeId.L1_TO_L2_MESSAGE_TREE], - roots[MerkleTreeId.ARCHIVE], - Fr.ZERO, // todo: private kernel vk tree root - roots[MerkleTreeId.PUBLIC_DATA_TREE], - this.globalVariablesHash, - ); - } - - public setBlockData(blockNumber: number, blockHeader: BlockHeader): Promise { - this.globalVariablesHash = blockHeader.globalVariablesHash; - this.blockNumber = blockNumber; - this.setTreeRoots({ - [MerkleTreeId.NOTE_HASH_TREE]: blockHeader.noteHashTreeRoot, - [MerkleTreeId.NULLIFIER_TREE]: blockHeader.nullifierTreeRoot, - [MerkleTreeId.CONTRACT_TREE]: blockHeader.contractTreeRoot, - [MerkleTreeId.L1_TO_L2_MESSAGE_TREE]: blockHeader.l1ToL2MessageTreeRoot, - [MerkleTreeId.ARCHIVE]: blockHeader.archiveRoot, - [MerkleTreeId.PUBLIC_DATA_TREE]: blockHeader.publicDataTreeRoot, - }); - - return Promise.resolve(); - } - - public getBlockNumber(): number | undefined { - return this.blockNumber; - } - - public addCompleteAddress(completeAddress: CompleteAddress): Promise { - const accountIndex = this.addresses.findIndex(r => r.address.equals(completeAddress.address)); - if (accountIndex !== -1) { - if (this.addresses[accountIndex].equals(completeAddress)) { - return Promise.resolve(false); - } - - return Promise.reject( - new Error( - `Complete address with aztec address ${completeAddress.address.toString()} but different public key or partial key already exists in memory database`, - ), - ); - } - this.addresses.push(completeAddress); - return Promise.resolve(true); - } - - public getCompleteAddress(address: AztecAddress): Promise { - const recipient = this.addresses.find(r => r.address.equals(address)); - return Promise.resolve(recipient); - } - - public getCompleteAddresses(): Promise { - return Promise.resolve(this.addresses); - } - - getSynchedBlockNumberForPublicKey(publicKey: Point): number | undefined { - return this.syncedBlockPerPublicKey.get(publicKey.toString()); - } - - setSynchedBlockNumberForPublicKey(publicKey: Point, blockNumber: number): Promise { - this.syncedBlockPerPublicKey.set(publicKey.toString(), blockNumber); - return Promise.resolve(true); - } - - public estimateSize() { - const notesSize = this.notesTable.reduce((sum, note) => sum + note.getSize(), 0); - const treeRootsSize = this.treeRoots ? Object.entries(this.treeRoots).length * Fr.SIZE_IN_BYTES : 0; - const authWits = Object.entries(this.authWitnesses); - const authWitsSize = authWits.reduce((sum, [key, value]) => sum + key.length + value.length * Fr.SIZE_IN_BYTES, 0); - return notesSize + treeRootsSize + authWitsSize + this.addresses.length * CompleteAddress.SIZE_IN_BYTES; - } -}