Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit cab0b96

Browse files
committed
Remove support for legacy crypto stack in StorageManager
We're not going to use the legacy stack any more.
1 parent 38f1098 commit cab0b96

File tree

2 files changed

+53
-121
lines changed

2 files changed

+53
-121
lines changed

src/utils/StorageManager.ts

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { LocalStorageCryptoStore, IndexedDBStore, IndexedDBCryptoStore } from "matrix-js-sdk/src/matrix";
17+
import { IndexedDBStore, IndexedDBCryptoStore } from "matrix-js-sdk/src/matrix";
1818
import { logger } from "matrix-js-sdk/src/logger";
1919

20-
import SettingsStore from "../settings/SettingsStore";
21-
import { Features } from "../settings/Settings";
2220
import { getIDBFactory } from "./StorageAccess";
2321

2422
const localStorage = window.localStorage;
@@ -141,55 +139,34 @@ async function checkSyncStore(): Promise<StoreCheck> {
141139
}
142140

143141
async function checkCryptoStore(): Promise<StoreCheck> {
144-
if (await SettingsStore.getValue(Features.RustCrypto)) {
145-
// check first if there is a rust crypto store
146-
try {
147-
const rustDbExists = await IndexedDBCryptoStore.exists(getIDBFactory()!, RUST_CRYPTO_STORE_NAME);
148-
log(`Rust Crypto store using IndexedDB contains data? ${rustDbExists}`);
149-
150-
if (rustDbExists) {
151-
// There was an existing rust database, so consider it healthy.
152-
return { exists: true, healthy: true };
153-
} else {
154-
// No rust store, so let's check if there is a legacy store not yet migrated.
155-
try {
156-
const legacyIdbExists = await IndexedDBCryptoStore.existsAndIsNotMigrated(
157-
getIDBFactory()!,
158-
LEGACY_CRYPTO_STORE_NAME,
159-
);
160-
log(`Legacy Crypto store using IndexedDB contains non migrated data? ${legacyIdbExists}`);
161-
return { exists: legacyIdbExists, healthy: true };
162-
} catch (e) {
163-
error("Legacy crypto store using IndexedDB inaccessible", e);
164-
}
165-
166-
// No need to check local storage or memory as rust stack doesn't support them.
167-
// Given that rust stack requires indexeddb, set healthy to false.
168-
return { exists: false, healthy: false };
142+
// check first if there is a rust crypto store
143+
try {
144+
const rustDbExists = await IndexedDBCryptoStore.exists(getIDBFactory()!, RUST_CRYPTO_STORE_NAME);
145+
log(`Rust Crypto store using IndexedDB contains data? ${rustDbExists}`);
146+
147+
if (rustDbExists) {
148+
// There was an existing rust database, so consider it healthy.
149+
return { exists: true, healthy: true };
150+
} else {
151+
// No rust store, so let's check if there is a legacy store not yet migrated.
152+
try {
153+
const legacyIdbExists = await IndexedDBCryptoStore.existsAndIsNotMigrated(
154+
getIDBFactory()!,
155+
LEGACY_CRYPTO_STORE_NAME,
156+
);
157+
log(`Legacy Crypto store using IndexedDB contains non migrated data? ${legacyIdbExists}`);
158+
return { exists: legacyIdbExists, healthy: true };
159+
} catch (e) {
160+
error("Legacy crypto store using IndexedDB inaccessible", e);
169161
}
170-
} catch (e) {
171-
error("Rust crypto store using IndexedDB inaccessible", e);
162+
163+
// No need to check local storage or memory as rust stack doesn't support them.
164+
// Given that rust stack requires indexeddb, set healthy to false.
172165
return { exists: false, healthy: false };
173166
}
174-
} else {
175-
let exists = false;
176-
// legacy checks
177-
try {
178-
exists = await IndexedDBCryptoStore.exists(getIDBFactory()!, LEGACY_CRYPTO_STORE_NAME);
179-
log(`Crypto store using IndexedDB contains data? ${exists}`);
180-
return { exists, healthy: true };
181-
} catch (e) {
182-
error("Crypto store using IndexedDB inaccessible", e);
183-
}
184-
try {
185-
exists = LocalStorageCryptoStore.exists(localStorage);
186-
log(`Crypto store using local storage contains data? ${exists}`);
187-
return { exists, healthy: true };
188-
} catch (e) {
189-
error("Crypto store using local storage inaccessible", e);
190-
}
191-
log("Crypto store using memory only");
192-
return { exists, healthy: false };
167+
} catch (e) {
168+
error("Rust crypto store using IndexedDB inaccessible", e);
169+
return { exists: false, healthy: false };
193170
}
194171
}
195172

test/utils/StorageManager-test.ts

Lines changed: 27 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { IDBFactory } from "fake-indexeddb";
2020
import { IndexedDBCryptoStore } from "matrix-js-sdk/src/matrix";
2121

2222
import * as StorageManager from "../../src/utils/StorageManager";
23-
import SettingsStore from "../../src/settings/SettingsStore";
2423

2524
const LEGACY_CRYPTO_STORE_NAME = "matrix-js-sdk:crypto";
2625
const RUST_CRYPTO_STORE_NAME = "matrix-js-sdk::matrix-sdk-crypto";
@@ -77,98 +76,54 @@ describe("StorageManager", () => {
7776
indexedDB = new IDBFactory();
7877
});
7978

80-
describe("with `feature_rust_crypto` enabled", () => {
81-
beforeEach(() => {
82-
jest.spyOn(SettingsStore, "getValue").mockImplementation(async (key) => {
83-
if (key === "feature_rust_crypto") {
84-
return true;
85-
}
86-
throw new Error(`Unknown key ${key}`);
87-
});
88-
});
79+
it("should not be ok if sync store but no crypto store", async () => {
80+
const result = await StorageManager.checkConsistency();
81+
expect(result.healthy).toBe(true);
82+
expect(result.dataInCryptoStore).toBe(false);
83+
});
8984

90-
it("should not be ok if sync store but no crypto store", async () => {
91-
const result = await StorageManager.checkConsistency();
92-
expect(result.healthy).toBe(true);
93-
expect(result.dataInCryptoStore).toBe(false);
94-
});
85+
it("should be ok if sync store and a rust crypto store", async () => {
86+
await createDB(RUST_CRYPTO_STORE_NAME);
9587

96-
it("should be ok if sync store and a rust crypto store", async () => {
97-
await createDB(RUST_CRYPTO_STORE_NAME);
88+
const result = await StorageManager.checkConsistency();
89+
expect(result.healthy).toBe(true);
90+
expect(result.dataInCryptoStore).toBe(true);
91+
});
92+
93+
describe("without rust store", () => {
94+
it("should be ok if there is non migrated legacy crypto store", async () => {
95+
await populateLegacyStore(undefined);
9896

9997
const result = await StorageManager.checkConsistency();
10098
expect(result.healthy).toBe(true);
10199
expect(result.dataInCryptoStore).toBe(true);
102100
});
103101

104-
describe("without rust store", () => {
105-
it("should be ok if there is non migrated legacy crypto store", async () => {
106-
await populateLegacyStore(undefined);
107-
108-
const result = await StorageManager.checkConsistency();
109-
expect(result.healthy).toBe(true);
110-
expect(result.dataInCryptoStore).toBe(true);
111-
});
112-
113-
it("should be ok if legacy store in MigrationState `NOT_STARTED`", async () => {
114-
await populateLegacyStore(0 /* MigrationState.NOT_STARTED*/);
115-
116-
const result = await StorageManager.checkConsistency();
117-
expect(result.healthy).toBe(true);
118-
expect(result.dataInCryptoStore).toBe(true);
119-
});
120-
121-
it("should not be ok if MigrationState greater than `NOT_STARTED`", async () => {
122-
await populateLegacyStore(1 /*INITIAL_DATA_MIGRATED*/);
123-
124-
const result = await StorageManager.checkConsistency();
125-
expect(result.healthy).toBe(true);
126-
expect(result.dataInCryptoStore).toBe(false);
127-
});
102+
it("should be ok if legacy store in MigrationState `NOT_STARTED`", async () => {
103+
await populateLegacyStore(0 /* MigrationState.NOT_STARTED*/);
128104

129-
it("should not be healthy if no indexeddb", async () => {
130-
// eslint-disable-next-line no-global-assign
131-
indexedDB = {} as IDBFactory;
132-
133-
const result = await StorageManager.checkConsistency();
134-
expect(result.healthy).toBe(false);
135-
136-
// eslint-disable-next-line no-global-assign
137-
indexedDB = new IDBFactory();
138-
});
139-
});
140-
});
141-
142-
describe("with `feature_rust_crypto` disabled", () => {
143-
beforeEach(() => {
144-
jest.spyOn(SettingsStore, "getValue").mockImplementation(async (key) => {
145-
if (key === "feature_rust_crypto") {
146-
return false;
147-
}
148-
throw new Error(`Unknown key ${key}`);
149-
});
150-
});
151-
152-
it("should not be ok if sync store but no crypto store", async () => {
153105
const result = await StorageManager.checkConsistency();
154106
expect(result.healthy).toBe(true);
155-
expect(result.dataInCryptoStore).toBe(false);
107+
expect(result.dataInCryptoStore).toBe(true);
156108
});
157109

158-
it("should not be ok if sync store but no crypto store and a rust store", async () => {
159-
await createDB(RUST_CRYPTO_STORE_NAME);
110+
it("should not be ok if MigrationState greater than `NOT_STARTED`", async () => {
111+
await populateLegacyStore(1 /*INITIAL_DATA_MIGRATED*/);
160112

161113
const result = await StorageManager.checkConsistency();
162114
expect(result.healthy).toBe(true);
163115
expect(result.dataInCryptoStore).toBe(false);
164116
});
165117

166-
it("should be healthy if sync store and a legacy crypto store", async () => {
167-
await createDB(LEGACY_CRYPTO_STORE_NAME);
118+
it("should not be healthy if no indexeddb", async () => {
119+
// eslint-disable-next-line no-global-assign
120+
indexedDB = {} as IDBFactory;
168121

169122
const result = await StorageManager.checkConsistency();
170-
expect(result.healthy).toBe(true);
171-
expect(result.dataInCryptoStore).toBe(true);
123+
expect(result.healthy).toBe(false);
124+
125+
// eslint-disable-next-line no-global-assign
126+
indexedDB = new IDBFactory();
172127
});
173128
});
174129
});

0 commit comments

Comments
 (0)