From c2282b7b0fa23b2cc8c33e56c4c760a6edd2c86f Mon Sep 17 00:00:00 2001 From: Spencer T Brody Date: Mon, 16 Sep 2024 09:18:46 -0400 Subject: [PATCH] fix: Don't remove entries from AnchorRequestStore if applying the anchor commit fails (#3277) --- .../__tests__/repository.test.ts | 20 +++++++++++++++++++ .../core/src/state-management/repository.ts | 2 ++ 2 files changed, 22 insertions(+) diff --git a/packages/core/src/state-management/__tests__/repository.test.ts b/packages/core/src/state-management/__tests__/repository.test.ts index 12eddb3ccc..ee787cee0b 100644 --- a/packages/core/src/state-management/__tests__/repository.test.ts +++ b/packages/core/src/state-management/__tests__/repository.test.ts @@ -776,6 +776,26 @@ describeIfV3('handleAnchorEvent', () => { expect(shouldRemove).toBeTruthy() expect(nextSpy).not.toBeCalled() }) + + test('Throw error if unable to handle anchor commit APPLY_ANCHOR_COMMIT_ATTEMPTS times', async () => { + const tile = await TileDocument.create(ceramic, { text: 1 }, undefined, { anchor: true }) + const state$ = await repository.load(tile.id) + const witnessCar = new CARFactory().build() + witnessCar.put('root', { isRoot: true }) + const event: AnchorEvent = { + status: AnchorRequestStatusName.COMPLETED, + message: 'Last COMPLETED', + streamId: tile.id, + cid: tile.tip, + witnessCar, + } + const dispatcherImportCarSpy = jest.spyOn(ceramic.dispatcher as any, 'importCAR') + dispatcherImportCarSpy.mockImplementation(async () => { + throw new Error('Fake error') + }) + await expect(repository.handleAnchorEvent(state$, event)).rejects.toThrow('Fake error') + expect(dispatcherImportCarSpy).toBeCalledTimes(3) + }) }) }) diff --git a/packages/core/src/state-management/repository.ts b/packages/core/src/state-management/repository.ts index 87deb94c4c..9374c9b8eb 100644 --- a/packages/core/src/state-management/repository.ts +++ b/packages/core/src/state-management/repository.ts @@ -815,6 +815,8 @@ export class Repository { if (tip.equals(state$.tip)) { state$.next({ ...state$.value, anchorStatus: AnchorStatus.FAILED }) } + + throw error } } }