|
| 1 | +import { EthAddress } from '@aztec/foundation/eth-address'; |
| 2 | +import { DatabaseVersion } from '@aztec/stdlib/database-version/version'; |
| 3 | + |
| 4 | +import { expect } from 'chai'; |
| 5 | + |
| 6 | +import { mockLogger } from '../interfaces/utils.js'; |
| 7 | +import { initStoreForRollupAndSchemaVersion } from '../utils.js'; |
| 8 | +import { AztecIndexedDBStore } from './store.js'; |
| 9 | + |
| 10 | +describe('IndexedDB Version Management', () => { |
| 11 | + let store: AztecIndexedDBStore; |
| 12 | + const schemaVersion = 42; |
| 13 | + let rollupAddress: EthAddress; |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + rollupAddress = EthAddress.random(); |
| 17 | + // Create a fresh ephemeral store for each test |
| 18 | + store = await AztecIndexedDBStore.open(mockLogger, undefined, true); |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(async () => { |
| 22 | + await store.delete(); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('initStoreForRollup', () => { |
| 26 | + describe('fresh store (no existing data)', () => { |
| 27 | + it('stores version on first run', async () => { |
| 28 | + await initStoreForRollupAndSchemaVersion(store, schemaVersion, rollupAddress, mockLogger); |
| 29 | + |
| 30 | + const versionSingleton = store.openSingleton<string>('dbVersion'); |
| 31 | + const stored = await versionSingleton.getAsync(); |
| 32 | + |
| 33 | + const storedVersion = DatabaseVersion.fromBuffer(Buffer.from(stored!, 'utf-8')); |
| 34 | + expect(storedVersion.schemaVersion).to.equal(schemaVersion); |
| 35 | + expect(storedVersion.rollupAddress.toString()).to.equal(rollupAddress.toString()); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('store with matching version', () => { |
| 40 | + it('does not clear store when version matches', async () => { |
| 41 | + await initStoreForRollupAndSchemaVersion(store, schemaVersion, rollupAddress, mockLogger); |
| 42 | + |
| 43 | + const testMap = store.openMap<string, string>('test'); |
| 44 | + await testMap.set('key', 'value'); |
| 45 | + |
| 46 | + await initStoreForRollupAndSchemaVersion(store, schemaVersion, rollupAddress, mockLogger); |
| 47 | + |
| 48 | + // Data should still exist |
| 49 | + expect(await testMap.getAsync('key')).to.equal('value'); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('store with different rollup address', () => { |
| 54 | + it('clears store when rollup address changes', async () => { |
| 55 | + await initStoreForRollupAndSchemaVersion(store, schemaVersion, rollupAddress, mockLogger); |
| 56 | + |
| 57 | + const testMap = store.openMap<string, string>('test'); |
| 58 | + await testMap.set('key', 'value'); |
| 59 | + |
| 60 | + const newRollupAddress = EthAddress.random(); |
| 61 | + await initStoreForRollupAndSchemaVersion(store, schemaVersion, newRollupAddress, mockLogger); |
| 62 | + |
| 63 | + expect(await testMap.getAsync('key')).to.be.undefined; |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('store with different schema version', () => { |
| 68 | + it('clears store when schema version increases', async () => { |
| 69 | + await initStoreForRollupAndSchemaVersion(store, schemaVersion, rollupAddress, mockLogger); |
| 70 | + |
| 71 | + const testMap = store.openMap<string, string>('test'); |
| 72 | + await testMap.set('key', 'value'); |
| 73 | + |
| 74 | + await initStoreForRollupAndSchemaVersion(store, schemaVersion + 1, rollupAddress, mockLogger); |
| 75 | + |
| 76 | + expect(await testMap.getAsync('key')).to.be.undefined; |
| 77 | + }); |
| 78 | + |
| 79 | + it('clears store when schema version decreases', async () => { |
| 80 | + await initStoreForRollupAndSchemaVersion(store, schemaVersion, rollupAddress, mockLogger); |
| 81 | + |
| 82 | + const testMap = store.openMap<string, string>('test'); |
| 83 | + await testMap.set('key', 'value'); |
| 84 | + |
| 85 | + await initStoreForRollupAndSchemaVersion(store, schemaVersion - 1, rollupAddress, mockLogger); |
| 86 | + |
| 87 | + expect(await testMap.getAsync('key')).to.be.undefined; |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('store with malformed version data', () => { |
| 92 | + it('clears store when version is malformed JSON', async () => { |
| 93 | + const versionSingleton = store.openSingleton<string>('dbVersion'); |
| 94 | + await versionSingleton.set('not-valid-json'); |
| 95 | + |
| 96 | + const testMap = store.openMap<string, string>('test'); |
| 97 | + await testMap.set('key', 'value'); |
| 98 | + |
| 99 | + await initStoreForRollupAndSchemaVersion(store, schemaVersion, rollupAddress, mockLogger); |
| 100 | + |
| 101 | + expect(await testMap.getAsync('key')).to.be.undefined; |
| 102 | + }); |
| 103 | + |
| 104 | + it('clears store when version has wrong structure', async () => { |
| 105 | + const versionSingleton = store.openSingleton<string>('dbVersion'); |
| 106 | + await versionSingleton.set(JSON.stringify({ foo: 'bar' })); |
| 107 | + |
| 108 | + const testMap = store.openMap<string, string>('test'); |
| 109 | + await testMap.set('key', 'value'); |
| 110 | + |
| 111 | + await initStoreForRollupAndSchemaVersion(store, schemaVersion, rollupAddress, mockLogger); |
| 112 | + |
| 113 | + expect(await testMap.getAsync('key')).to.be.undefined; |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('migration from old store format (pre-version management)', () => { |
| 118 | + it('clears store that has old rollupAddress format but no dbVersion', async () => { |
| 119 | + // Simulate old store format: has rollupAddress singleton but no dbVersion |
| 120 | + const oldRollupSingleton = store.openSingleton<string>('rollupAddress'); |
| 121 | + await oldRollupSingleton.set(rollupAddress.toString()); |
| 122 | + |
| 123 | + const testMap = store.openMap<string, string>('test'); |
| 124 | + await testMap.set('key', 'value'); |
| 125 | + |
| 126 | + // Init with new version management should clear the old data |
| 127 | + await initStoreForRollupAndSchemaVersion(store, schemaVersion, rollupAddress, mockLogger); |
| 128 | + |
| 129 | + expect(await testMap.getAsync('key')).to.be.undefined; |
| 130 | + |
| 131 | + const versionSingleton = store.openSingleton<string>('dbVersion'); |
| 132 | + const stored = await versionSingleton.getAsync(); |
| 133 | + const storedVersion = DatabaseVersion.fromBuffer(Buffer.from(stored!, 'utf-8')); |
| 134 | + expect(storedVersion.schemaVersion).to.equal(schemaVersion); |
| 135 | + expect(storedVersion.rollupAddress.toString()).to.equal(rollupAddress.toString()); |
| 136 | + }); |
| 137 | + |
| 138 | + it('clears store with old format even if rollup address matches', async () => { |
| 139 | + // Old format with same rollup address |
| 140 | + const oldRollupSingleton = store.openSingleton<string>('rollupAddress'); |
| 141 | + await oldRollupSingleton.set(rollupAddress.toString()); |
| 142 | + |
| 143 | + const testMap = store.openMap<string, string>('test'); |
| 144 | + await testMap.set('key', 'value'); |
| 145 | + |
| 146 | + await initStoreForRollupAndSchemaVersion(store, schemaVersion, rollupAddress, mockLogger); |
| 147 | + |
| 148 | + expect(await testMap.getAsync('key')).to.be.undefined; |
| 149 | + }); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments