Skip to content

Commit

Permalink
fix(semibook): initialization (#1539)
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis-Amas authored Oct 10, 2023
1 parent 9ab3d7a commit f26e8f7
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions src/semibook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,48 +703,52 @@ class Semibook
public async stateInitialize(
block: BlockManager.BlockWithoutParentHash
): Promise<LogSubscriber.ErrorOrState<Semibook.State>> {
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(
Expand Down

0 comments on commit f26e8f7

Please sign in to comment.