Skip to content

Commit 314d509

Browse files
authored
Merge pull request #299 from bancorprotocol/reorg-reset
better work on resetting self on reorg
2 parents d131af9 + b3830b9 commit 314d509

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@bancor/carbon-sdk",
33
"type": "module",
44
"source": "src/index.ts",
5-
"version": "0.0.120-DEV",
5+
"version": "0.0.121-DEV",
66
"description": "The SDK is a READ-ONLY tool, intended to facilitate working with Carbon contracts. It's a convenient wrapper around our matching algorithm, allowing programs and users get a ready to use transaction data that will allow them to manage strategies and fulfill trades",
77
"main": "dist/index.cjs",
88
"module": "dist/index.js",

src/chain-cache/ChainCache.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,18 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
196196
logger.debug('Cache miss for pair', token0, token1, 'resolved');
197197
}
198198

199-
public clear(silent: boolean = false): void {
200-
const pairs = Object.keys(this._strategiesByPair).map(fromPairKey);
199+
public clear(): void {
201200
this._strategiesByPair = {};
202201
this._strategiesById = {};
203202
this._ordersByDirectedPair = {};
204203
this._latestBlockNumber = 0;
205204
this._latestTradesByPair = {};
206205
this._latestTradesByDirectedPair = {};
207206
this._blocksMetadata = [];
208-
if (!silent) {
209-
this.emit('onPairDataChanged', pairs);
210-
}
207+
this._blocksMetadata = [];
208+
this._tradingFeePPMByPair = {};
209+
this._isCacheInitialized = false;
210+
this.emit('onCacheCleared');
211211
}
212212

213213
//#region public getters

src/chain-cache/ChainSync.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,35 @@ export class ChainSync {
2121
private _activeTimers: Set<number> = new Set();
2222
private _isStopped: boolean = false;
2323

24+
private _reinitializeSelf(): void {
25+
this._syncCalled = false;
26+
this._slowPollPairs = false;
27+
this._uncachedPairs = [];
28+
this._lastFetch = Date.now();
29+
this._activeTimers = new Set();
30+
this._isStopped = false;
31+
}
32+
33+
private _resetSelf(): void {
34+
const shouldSyncAgain = this._syncCalled;
35+
this.stop();
36+
this._reinitializeSelf();
37+
38+
// clearing the cache will emit onCacheCleared event
39+
this._chainCache.clear();
40+
if (shouldSyncAgain) {
41+
this.startDataSync();
42+
}
43+
}
44+
2445
constructor(
2546
fetcher: Fetcher,
2647
chainCache: ChainCache,
2748
numOfPairsToBatch: number = 100,
2849
msToWaitBetweenSyncs: number = 1000,
2950
chunkSize: number = 1000
3051
) {
52+
this._reinitializeSelf();
3153
this._fetcher = fetcher;
3254
this._chainCache = chainCache;
3355
this._numOfPairsToBatch = numOfPairsToBatch;
@@ -258,10 +280,7 @@ export class ChainSync {
258280
if (currentBlock > latestBlock) {
259281
if (await this._detectReorg(currentBlock)) {
260282
logger.debug('_syncEvents detected reorg - resetting');
261-
this._chainCache.clear();
262-
this._chainCache.applyEvents([], currentBlock);
263-
this._resetPairsFetching();
264-
this._setTimeout(processEvents, 1);
283+
this._resetSelf();
265284
return;
266285
}
267286

@@ -329,11 +348,6 @@ export class ChainSync {
329348
this._setTimeout(processEvents, 1);
330349
}
331350

332-
private _resetPairsFetching() {
333-
this._uncachedPairs = [];
334-
this._slowPollPairs = false;
335-
}
336-
337351
/**
338352
* Detects blockchain reorganization by comparing stored block metadata with current blockchain state
339353
* @param currentBlock - The current block number to check against

src/chain-cache/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type CacheEvents = {
99
onPairDataChanged: (affectedPairs: TokenPair[]) => void;
1010
onPairAddedToCache: (addedPair: TokenPair) => void;
1111
onCacheInitialized: () => void;
12+
onCacheCleared: () => void;
1213
};
1314

1415
export interface TypedEventEmitter<Events extends EventMap> {

0 commit comments

Comments
 (0)