diff --git a/packages/core/src/blockchain/storage-layer.ts b/packages/core/src/blockchain/storage-layer.ts index 1df32859..5c75b553 100644 --- a/packages/core/src/blockchain/storage-layer.ts +++ b/packages/core/src/blockchain/storage-layer.ts @@ -107,7 +107,7 @@ export class RemoteStorageLayer implements StorageLayerProvider { } export class StorageLayer implements StorageLayerProvider { - readonly #store: Record> = {} + readonly #store: Map> = new Map() readonly #keys: string[] = [] readonly #deletedPrefix: string[] = [] #parent?: StorageLayerProvider @@ -134,8 +134,8 @@ export class StorageLayer implements StorageLayerProvider { } async get(key: string, cache: boolean): Promise { - if (key in this.#store) { - return this.#store[key] + if (this.#store.has(key)) { + return this.#store.get(key) } if (this.#deletedPrefix.some((dp) => key.startsWith(dp))) { @@ -145,7 +145,7 @@ export class StorageLayer implements StorageLayerProvider { if (this.#parent) { const val = this.#parent.get(key, false) if (cache) { - this.#store[key] = val + this.#store.set(key, val) } return val } @@ -156,24 +156,24 @@ export class StorageLayer implements StorageLayerProvider { set(key: string, value: StorageValue): void { switch (value) { case StorageValueKind.Deleted: - this.#store[key] = StorageValueKind.Deleted + this.#store.set(key, StorageValueKind.Deleted) this.#removeKey(key) break case StorageValueKind.DeletedPrefix: this.#deletedPrefix.push(key) for (const k of this.#keys) { if (k.startsWith(key)) { - this.#store[k] = StorageValueKind.Deleted + this.#store.set(k, StorageValueKind.Deleted) this.#removeKey(k) } } break case undefined: - delete this.#store[key] + this.#store.delete(key) this.#removeKey(key) break default: - this.#store[key] = value + this.#store.set(key, value) this.#addKey(key) break } @@ -195,7 +195,7 @@ export class StorageLayer implements StorageLayerProvider { into.set(deletedPrefix, StorageValueKind.DeletedPrefix) } - for (const [key, value] of Object.entries(this.#store)) { + for (const [key, value] of this.#store) { into.set(key, await value) } @@ -212,10 +212,10 @@ export class StorageLayer implements StorageLayerProvider { if (!this.#deletedPrefix.some((dp) => startKey.startsWith(dp))) { const remote = (await this.#parent?.getKeysPaged(prefix, pageSize, startKey)) ?? [] for (const key of remote) { - if (this.#deletedPrefix.some((dp) => key.startsWith(dp))) { + if (this.#store.get(key) === StorageValueKind.Deleted) { continue } - if (this.#store[key] === StorageValueKind.Deleted) { + if (this.#deletedPrefix.some((dp) => key.startsWith(dp))) { continue } this.#addKey(key) @@ -242,7 +242,7 @@ export class StorageLayer implements StorageLayerProvider { * Merge the storage layer into the given object, can be used to get sotrage diff. */ async mergeInto(into: Record) { - for (const [key, maybeValue] of Object.entries(this.#store)) { + for (const [key, maybeValue] of this.#store) { const value = await maybeValue if (value === StorageValueKind.Deleted) { into[key] = null