From 5248540e7c48e6c98d77da1fc39f3484a8510d04 Mon Sep 17 00:00:00 2001 From: Valentine Date: Tue, 9 Apr 2024 16:39:58 +0000 Subject: [PATCH] fix epoch saving (#909) * fix epoch saving * Update packages/storage/src/indexed-db/indexed-db.test.ts Co-authored-by: Jesse Pinho * move sctParams below * bump idb version --------- Co-authored-by: Jesse Pinho --- apps/extension/.env | 2 +- packages/query/src/block-processor.ts | 3 +-- packages/storage/src/indexed-db/index.ts | 3 +++ packages/storage/src/indexed-db/indexed-db.test.ts | 7 +++++++ 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/apps/extension/.env b/apps/extension/.env index b72914eb8c..6d0f4d731e 100644 --- a/apps/extension/.env +++ b/apps/extension/.env @@ -1,6 +1,6 @@ PRAX=lkpmkhpnhknhmibgnmmhdhgdilepfghe -IDB_VERSION=33 +IDB_VERSION=34 USDC_ASSET_ID="reum7wQmk/owgvGMWMZn/6RFPV24zIKq3W6In/WwZgg=" MINIFRONT_URL=https://app.testnet.penumbra.zone/ PENUMBRA_NODE_PD_URL=https://grpc.testnet.penumbra.zone/ diff --git a/packages/query/src/block-processor.ts b/packages/query/src/block-processor.ts index e678b003aa..2191da080f 100644 --- a/packages/query/src/block-processor.ts +++ b/packages/query/src/block-processor.ts @@ -423,11 +423,10 @@ export class BlockProcessor implements BlockProcessorInterface { endHeightOfPreviousEpoch: bigint, latestKnownBlockHeight: bigint, ): Promise { - const { sctParams } = await this.querier.app.appParams(); const nextEpochStartHeight = endHeightOfPreviousEpoch + 1n; - await this.indexedDb.addEpoch(nextEpochStartHeight); + const { sctParams } = (await this.indexedDb.getAppParams()) ?? {}; const nextEpochIsLatestKnownEpoch = sctParams && latestKnownBlockHeight - nextEpochStartHeight < sctParams.epochDuration; diff --git a/packages/storage/src/indexed-db/index.ts b/packages/storage/src/indexed-db/index.ts index 30b8a8d374..d769fcea1c 100644 --- a/packages/storage/src/indexed-db/index.ts +++ b/packages/storage/src/indexed-db/index.ts @@ -477,6 +477,9 @@ export class IndexedDb implements IndexedDbInterface { const previousEpoch = cursor?.value ? Epoch.fromJson(cursor.value) : undefined; const index = previousEpoch?.index !== undefined ? previousEpoch.index + 1n : 0n; + // avoid saving the same epoch twice + if (previousEpoch?.startHeight === startHeight) return; + const newEpoch = { startHeight: startHeight.toString(), index: index.toString(), diff --git a/packages/storage/src/indexed-db/indexed-db.test.ts b/packages/storage/src/indexed-db/indexed-db.test.ts index 299b23428e..194550bec6 100644 --- a/packages/storage/src/indexed-db/indexed-db.test.ts +++ b/packages/storage/src/indexed-db/indexed-db.test.ts @@ -545,6 +545,13 @@ describe('IndexedDb', () => { expect(result2?.index).toBe(2n); expect(result3?.index).toBe(3n); }); + + it('should not save the epoch with the same startHeight twice', async () => { + await db.addEpoch(epoch3.startHeight); + + const result = await db.getEpochByHeight(350n); + expect(result?.index).toBe(3n); + }); }); describe('getEpochByHeight', () => {