Skip to content

Commit 1e42a39

Browse files
committed
fix default options
1 parent b14015a commit 1e42a39

File tree

3 files changed

+100
-12
lines changed

3 files changed

+100
-12
lines changed

src/chains-config/index.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

1717
import { ApiPromise } from '@polkadot/api';
18+
import { ISidecarConfig } from 'src/types/sidecar-config';
1819

1920
import { controllers } from '../controllers';
2021
import AbstractController from '../controllers/AbstractController';
@@ -125,7 +126,8 @@ function getControllersFromConfig(api: ApiPromise, config: ControllerConfig) {
125126

126127
export const getControllersByPallets = (pallets: string[], api: ApiPromise, specName: string) => {
127128
const controllersSet: AbstractController<AbstractService>[] = [];
128-
const config = specToControllerMap?.[specName]?.options || specToControllerMap?.defaultControllers?.options;
129+
const config = specToControllerMap?.[specName]?.options || defaultControllers?.options;
130+
129131
Object.values(controllers).forEach((controller) => {
130132
if (controller.canInjectByPallets(pallets)) {
131133
controllersSet.push(new controller(api, config));
@@ -134,3 +136,20 @@ export const getControllersByPallets = (pallets: string[], api: ApiPromise, spec
134136

135137
return controllersSet;
136138
};
139+
140+
export const getControllers = (
141+
api: ApiPromise,
142+
config: ISidecarConfig,
143+
specName: string,
144+
): AbstractController<AbstractService>[] => {
145+
if (config.EXPRESS.INJECTED_CONTROLLERS) {
146+
console.log('here');
147+
return getControllersByPallets(
148+
(api.registry.metadata.toJSON().pallets as unknown as Record<string, unknown>[]).map((p) => p.name as string),
149+
api,
150+
specName,
151+
);
152+
} else {
153+
return getControllersForSpec(api, specName);
154+
}
155+
};

src/controllers/controllerInjection.spec.ts

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// write tests that get the metadata from an rpc and then inject the metadata into the controller
22

33
import { ApiPromise, WsProvider } from '@polkadot/api';
4+
import { ISidecarConfig } from 'src/types/sidecar-config';
45

5-
import { specToControllerMap } from '../chains-config';
6+
import { getControllers, specToControllerMap } from '../chains-config';
67
import { defaultControllers } from '../chains-config/defaultControllers';
78
import { controllers } from '.';
89

@@ -13,6 +14,42 @@ import { controllers } from '.';
1314
* @param implName
1415
*/
1516

17+
const mockSidecarConfig: ISidecarConfig = {
18+
EXPRESS: {
19+
HOST: '',
20+
PORT: 3000,
21+
KEEP_ALIVE_TIMEOUT: 5000,
22+
MAX_BODY: '100kb',
23+
INJECTED_CONTROLLERS: false,
24+
},
25+
SUBSTRATE: {
26+
URL: '',
27+
TYPES_BUNDLE: '',
28+
TYPES_CHAIN: '',
29+
TYPES_SPEC: '',
30+
TYPES: '',
31+
CACHE_CAPACITY: 1000,
32+
},
33+
LOG: {
34+
LEVEL: 'info',
35+
JSON: false,
36+
FILTER_RPC: false,
37+
STRIP_ANSI: false,
38+
WRITE: false,
39+
WRITE_PATH: '',
40+
WRITE_MAX_FILE_SIZE: 0,
41+
WRITE_MAX_FILES: 0,
42+
},
43+
METRICS: {
44+
ENABLED: false,
45+
PROM_HOST: '',
46+
PROM_PORT: 0,
47+
LOKI_HOST: '',
48+
LOKI_PORT: 0,
49+
INCLUDE_QUERYPARAMS: false,
50+
},
51+
};
52+
1653
const chainsToNode: Record<string, string> = {
1754
'asset-hub-kusama': 'wss://asset-hub-kusama-rpc.dwellir.com',
1855
kusama: 'wss://kusama-rpc.dwellir.com',
@@ -58,4 +95,44 @@ describe('controllerInjection', () => {
5895
expect(filtered).toHaveLength(0);
5996
});
6097
}
98+
99+
it('should inject default controllers when pallets are not checked (injected-controllers: false) and a custom config is not available', async () => {
100+
const wsProvider = new WsProvider('wss://kusama-rpc.dwellir.com');
101+
const api = await ApiPromise.create({ provider: wsProvider });
102+
try {
103+
await api.isReady;
104+
} finally {
105+
await api?.disconnect(); // Close WebSocket connection
106+
}
107+
108+
const controllers = getControllers(
109+
api,
110+
{
111+
...mockSidecarConfig,
112+
EXPRESS: {
113+
...mockSidecarConfig.EXPRESS,
114+
INJECTED_CONTROLLERS: true,
115+
},
116+
},
117+
'kusama_go_default',
118+
);
119+
120+
expect(controllers).toBeDefined();
121+
expect(controllers).not.toHaveLength(0);
122+
123+
const controllersDefault = getControllers(
124+
api,
125+
{
126+
...mockSidecarConfig,
127+
EXPRESS: {
128+
...mockSidecarConfig.EXPRESS,
129+
INJECTED_CONTROLLERS: false,
130+
},
131+
},
132+
'kusama_go_default',
133+
);
134+
135+
expect(controllersDefault).toBeDefined();
136+
expect(controllersDefault).toHaveLength(defaultControllers.controllers.length);
137+
});
61138
});

src/main.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { json } from 'express';
2626

2727
import packageJSON from '../package.json';
2828
import App from './App';
29-
import { getControllersByPallets, getControllersForSpec } from './chains-config';
29+
import { getControllers } from './chains-config';
3030
import { consoleOverride } from './logging/consoleOverride';
3131
import { Log } from './logging/Log';
3232
import { MetricsApp } from './metrics/index';
@@ -80,17 +80,9 @@ async function main() {
8080
metricsApp.listen();
8181
}
8282

83-
const controllers = !config.EXPRESS.INJECTED_CONTROLLERS
84-
? getControllersForSpec(api, specName.toString())
85-
: getControllersByPallets(
86-
(api.registry.metadata.toJSON().pallets as unknown as Record<string, unknown>[]).map((p) => p.name as string),
87-
api,
88-
specName.toString(),
89-
);
90-
9183
const app = new App({
9284
preMiddleware: preMiddlewares,
93-
controllers: controllers,
85+
controllers: getControllers(api, config, specName.toString()),
9486
postMiddleware: [
9587
middleware.txError,
9688
middleware.httpError,

0 commit comments

Comments
 (0)