From f26e8f7b2c1214cb3287e97ab10b9fbf17540a4a Mon Sep 17 00:00:00 2001 From: Louis-Amas Date: Tue, 10 Oct 2023 15:32:43 +0200 Subject: [PATCH] fix(semibook): initialization (#1539) --- src/semibook.ts | 62 ++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/src/semibook.ts b/src/semibook.ts index 54d64ac0b..394222df7 100644 --- a/src/semibook.ts +++ b/src/semibook.ts @@ -703,48 +703,52 @@ class Semibook public async stateInitialize( block: BlockManager.BlockWithoutParentHash ): Promise> { - const localConfig = await this.getConfig(block.number); // TODO: make this reorg resistant too, but it's HIGHLY unlikely that we encounter an issue here - this.#offer_gasbase = localConfig.offer_gasbase; + try { + const localConfig = await this.getConfig(block.number); // TODO: make this reorg resistant too, but it's HIGHLY unlikely that we encounter an issue here + this.#offer_gasbase = localConfig.offer_gasbase; + + /** + * To ensure consistency in this cache, everything is initially fetched from a specific block, + * we expect $fetchOfferListPrefix to return error if reorg is detected + */ + const result = await this.#fetchOfferListPrefix(block); + + if (result.error) { + return { error: result.error, ok: undefined }; + } - /** - * To ensure consistency in this cache, everything is initially fetched from a specific block, - * we expect $fetchOfferListPrefix to return error if reorg is detected - */ - const result = await this.#fetchOfferListPrefix(block); + const offers = result.ok; - if (result.error) { - return { error: result.error, ok: undefined }; - } + if (offers.length > 0) { + const state: Semibook.State = { + bestInCache: offers[0].id, + worstInCache: offers[offers.length - 1].id, + offerCache: new Map(), + }; - const offers = result.ok; + for (const offer of offers) { + this.#insertOffer(state, offer); + } + + return { + error: undefined, + ok: state, + }; + } - if (offers.length > 0) { const state: Semibook.State = { - bestInCache: offers[0].id, - worstInCache: offers[offers.length - 1].id, + bestInCache: undefined, + worstInCache: undefined, offerCache: new Map(), }; - for (const offer of offers) { - this.#insertOffer(state, offer); - } - return { error: undefined, ok: state, }; + } catch (e) { + return { error: "FailedInitialize", ok: undefined }; } - - const state: Semibook.State = { - bestInCache: undefined, - worstInCache: undefined, - offerCache: new Map(), - }; - - return { - error: undefined, - ok: state, - }; } public stateHandleLog(