Skip to content

Commit

Permalink
fix: LRUcache for block requests (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul committed Aug 21, 2021
1 parent 1f5f6b7 commit 9f7a29f
Show file tree
Hide file tree
Showing 25 changed files with 177 additions and 17 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"express": "^4.17.1",
"express-winston": "^4.1.0",
"http-errors": "^1.8.0",
"lru-cache": "^6.0.0",
"winston": "^3.3.3"
},
"devDependencies": {
Expand All @@ -67,6 +68,7 @@
"@types/express": "^4.17.12",
"@types/express-serve-static-core": "^4.17.21",
"@types/http-errors": "^1.8.0",
"@types/lru-cache": "^5.1.1",
"@types/morgan": "^1.9.2",
"@types/triple-beam": "^1.3.2",
"standard-version": "^9.3.0",
Expand Down
11 changes: 11 additions & 0 deletions src/chains-config/cache/lruCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import LRU from 'lru-cache';

import { IBlock } from '../../types/responses';

export const initLRUCache = (): LRU<string, IBlock> => {
const config = { max: 2 };
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const cache = new LRU(config) as LRU<string, IBlock>;

return cache;
};
2 changes: 2 additions & 0 deletions src/chains-config/defaultControllers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerConfig } from '../types/chains-config';
import { initLRUCache } from './cache/lruCache';

/**
* Controllers that Sidecar will always default to. This will always be
Expand Down Expand Up @@ -32,5 +33,6 @@ export const defaultControllers: ControllerConfig = {
finalizes: true,
minCalcFeeRuntime: null,
blockWeightStore: {},
blockStore: initLRUCache(),
},
};
2 changes: 2 additions & 0 deletions src/chains-config/dockMainnetControllers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerConfig } from '../types/chains-config';
import { initLRUCache } from './cache/lruCache';
import { getBlockWeight } from './metadata-consts';

/**
Expand All @@ -25,5 +26,6 @@ export const dockMainnetControllers: ControllerConfig = {
finalizes: true,
minCalcFeeRuntime: 1,
blockWeightStore: getBlockWeight('dock-main-runtime'),
blockStore: initLRUCache(),
},
};
2 changes: 2 additions & 0 deletions src/chains-config/dockPoSMainnetControllers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerConfig } from '../types/chains-config';
import { initLRUCache } from './cache/lruCache';
import { getBlockWeight } from './metadata-consts';

/**
Expand Down Expand Up @@ -28,5 +29,6 @@ export const dockPoSMainnetControllers: ControllerConfig = {
finalizes: true,
minCalcFeeRuntime: 29,
blockWeightStore: getBlockWeight('dock-pos-main-runtime'),
blockStore: initLRUCache(),
},
};
2 changes: 2 additions & 0 deletions src/chains-config/dockPoSTestnetControllers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerConfig } from '../types/chains-config';
import { initLRUCache } from './cache/lruCache';
import { getBlockWeight } from './metadata-consts';

/**
Expand Down Expand Up @@ -28,5 +29,6 @@ export const dockTestnetControllers: ControllerConfig = {
finalizes: true,
minCalcFeeRuntime: 26,
blockWeightStore: getBlockWeight('dock-pos-test-runtime'),
blockStore: initLRUCache(),
},
};
2 changes: 2 additions & 0 deletions src/chains-config/kiltControllers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerConfig } from '../types/chains-config';
import { initLRUCache } from './cache/lruCache';

/**
* Controllers for KILT's mashnet.
Expand All @@ -25,5 +26,6 @@ export const kiltControllers: ControllerConfig = {
finalizes: true,
minCalcFeeRuntime: null,
blockWeightStore: {},
blockStore: initLRUCache(),
},
};
2 changes: 2 additions & 0 deletions src/chains-config/kulupuControllers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerConfig } from '../types/chains-config';
import { initLRUCache } from './cache/lruCache';

export const kulupuControllers: ControllerConfig = {
controllers: [
Expand All @@ -21,5 +22,6 @@ export const kulupuControllers: ControllerConfig = {
finalizes: false,
minCalcFeeRuntime: null,
blockWeightStore: {},
blockStore: initLRUCache(),
},
};
2 changes: 2 additions & 0 deletions src/chains-config/kusamaControllers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerConfig } from '../types/chains-config';
import { initLRUCache } from './cache/lruCache';
import { getBlockWeight } from './metadata-consts';

/**
Expand Down Expand Up @@ -31,5 +32,6 @@ export const kusamaControllers: ControllerConfig = {
finalizes: true,
minCalcFeeRuntime: 1062,
blockWeightStore: getBlockWeight('kusama'),
blockStore: initLRUCache(),
},
};
2 changes: 2 additions & 0 deletions src/chains-config/mandalaControllers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerConfig } from '../types/chains-config';
import { initLRUCache } from './cache/lruCache';

/**
* Controllers for mandala, acala's test network.
Expand Down Expand Up @@ -29,5 +30,6 @@ export const mandalaControllers: ControllerConfig = {
finalizes: true,
minCalcFeeRuntime: null,
blockWeightStore: {},
blockStore: initLRUCache(),
},
};
2 changes: 2 additions & 0 deletions src/chains-config/polkadotControllers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerConfig } from '../types/chains-config';
import { initLRUCache } from './cache/lruCache';
import { getBlockWeight } from './metadata-consts';

/**
Expand Down Expand Up @@ -30,5 +31,6 @@ export const polkadotControllers: ControllerConfig = {
finalizes: true,
minCalcFeeRuntime: 0,
blockWeightStore: getBlockWeight('polkadot'),
blockStore: initLRUCache(),
},
};
2 changes: 2 additions & 0 deletions src/chains-config/polymeshControllers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerConfig } from '../types/chains-config';
import { initLRUCache } from './cache/lruCache';
import { getBlockWeight } from './metadata-consts';

/**
Expand Down Expand Up @@ -27,5 +28,6 @@ export const polymeshControllers: ControllerConfig = {
finalizes: true,
minCalcFeeRuntime: 0,
blockWeightStore: getBlockWeight('polymesh'),
blockStore: initLRUCache(),
},
};
2 changes: 2 additions & 0 deletions src/chains-config/soraControllers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerConfig } from '../types/chains-config';
import { initLRUCache } from './cache/lruCache';

/**
* SORA configuration for sidecar.
Expand Down Expand Up @@ -29,5 +30,6 @@ export const soraControllers: ControllerConfig = {
finalizes: true,
minCalcFeeRuntime: null,
blockWeightStore: {},
blockStore: initLRUCache(),
},
};
2 changes: 2 additions & 0 deletions src/chains-config/statemineControllers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerConfig } from '../types/chains-config';
import { initLRUCache } from './cache/lruCache';

/**
* Statemine configuration for Sidecar.
Expand All @@ -24,5 +25,6 @@ export const statemineControllers: ControllerConfig = {
finalizes: true,
minCalcFeeRuntime: 1,
blockWeightStore: {},
blockStore: initLRUCache(),
},
};
2 changes: 2 additions & 0 deletions src/chains-config/statemintControllers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerConfig } from '../types/chains-config';
import { initLRUCache } from './cache/lruCache';

/**
* Statemint configuration for Sidecar.
Expand All @@ -24,5 +25,6 @@ export const statemintControllers: ControllerConfig = {
finalizes: true,
minCalcFeeRuntime: 0,
blockWeightStore: {},
blockStore: initLRUCache(),
},
};
2 changes: 2 additions & 0 deletions src/chains-config/westendControllers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ControllerConfig } from '../types/chains-config';
import { initLRUCache } from './cache/lruCache';
import { getBlockWeight } from './metadata-consts';

/**
Expand Down Expand Up @@ -30,5 +31,6 @@ export const westendControllers: ControllerConfig = {
finalizes: true,
minCalcFeeRuntime: 6,
blockWeightStore: getBlockWeight('westend'),
blockStore: initLRUCache(),
},
};
4 changes: 4 additions & 0 deletions src/controllers/blocks/BlocksController.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { ApiPromise } from '@polkadot/api';
import { isHex } from '@polkadot/util';
import { RequestHandler } from 'express';
import LRU from 'lru-cache';

import { BlocksService } from '../../services';
import { INumberParam } from '../../types/requests';
import { IBlock } from '../../types/responses';
import AbstractController from '../AbstractController';

interface ControllerOptions {
finalizes: boolean;
minCalcFeeRuntime: null | number;
blockStore: LRU<string, IBlock>;
blockWeightStore: {};
}

Expand Down Expand Up @@ -79,6 +82,7 @@ export default class BlocksController extends AbstractController<BlocksService>
new BlocksService(
api,
options.minCalcFeeRuntime,
options.blockStore,
options.blockWeightStore
)
);
Expand Down
4 changes: 4 additions & 0 deletions src/controllers/blocks/BlocksExtrinsicsController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ApiPromise } from '@polkadot/api';
import { RequestHandler } from 'express';
import LRU from 'lru-cache';
import { BlockWeightStore } from 'src/types/chains-config';
import { IBlock } from 'src/types/responses';

import { BlocksService } from '../../services';
import { INumberParam } from '../../types/requests';
Expand All @@ -9,6 +11,7 @@ import AbstractController from '../AbstractController';
interface ControllerOptions {
blockWeightStore: BlockWeightStore;
minCalcFeeRuntime: null | number;
blockStore: LRU<string, IBlock>;
}

export default class BlocksExtrinsicsController extends AbstractController<BlocksService> {
Expand All @@ -19,6 +22,7 @@ export default class BlocksExtrinsicsController extends AbstractController<Block
new BlocksService(
api,
options.minCalcFeeRuntime,
options.blockStore,
options.blockWeightStore
)
);
Expand Down
Loading

0 comments on commit 9f7a29f

Please sign in to comment.