Skip to content

Commit 2aa6e6a

Browse files
filvecchiatoImod7
andauthored
feat: inject controllers using metadata's definition of pallets (#1592)
* adds naming to AbstractController and to controllers * wip:injected controllers * inject controllers using metadata * inject controllers using metadata * removed unused method * injects controllers using pallets in metadata * lint * update requiredPallets abstract definition * update injected flag to make injected controllers not default (no breaking changes) * Update README.md Co-authored-by: Dominique <[email protected]> * Update src/chains-config/index.ts Co-authored-by: Dominique <[email protected]> * fix default options * remove log * update readme * Update src/controllers/coretime/CoretimeChainController.ts Co-authored-by: Dominique <[email protected]> * Update src/controllers/coretime/CoretimeGenericController.ts Co-authored-by: Dominique <[email protected]> * linting * rebase and fix tests --------- Co-authored-by: Dominique <[email protected]>
1 parent e65ba2b commit 2aa6e6a

File tree

59 files changed

+326
-28
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+326
-28
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ For more information on our configuration manager visit its readme [here](https:
165165
- `SAS_EXPRESS_PORT`: port on which the server will be listening, defaults to `8080`.
166166
- `SAS_EXPRESS_KEEP_ALIVE_TIMEOUT`: Set the `keepAliveTimeout` in express.
167167
- `SAS_EXPRESS_MAX_BODY`: Set the size of request body payload, defaults to `100kb`
168+
- `SAS_EXPRESS_INJECTED_CONTROLLERS`: When set (_e.g. SAS_EXPRESS_INJECTED_CONTROLLERS=true_), automatically detects the available pallets in the chain's metadata and injects the appropriate controllers. If set to `false`, it uses the controllers defined in the chains configuration files. Defaults to `false` when the flag is not defined. Note: This flag does not replace completely the need of the chains-config files since we are still retrieving the options from that file.
168169

169170
### Substrate node
170171

src/SidecarConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export class SidecarConfig {
5555
PORT: config.Get(MODULES.EXPRESS, CONFIG.PORT) as number,
5656
KEEP_ALIVE_TIMEOUT: config.Get(MODULES.EXPRESS, CONFIG.KEEP_ALIVE_TIMEOUT) as number,
5757
MAX_BODY: config.Get(MODULES.EXPRESS, CONFIG.MAX_BODY) as string,
58+
INJECTED_CONTROLLERS: config.Get(MODULES.EXPRESS, CONFIG.INJECTED_CONTROLLERS) as boolean,
5859
},
5960
SUBSTRATE: {
6061
URL: config.Get(MODULES.SUBSTRATE, CONFIG.URL) as string,

src/Specs.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ export class Specs {
9595
type: 'string',
9696
}),
9797
);
98+
99+
this._specs.appendSpec(
100+
MODULES.EXPRESS,
101+
this._specs.getSpec(
102+
CONFIG.INJECTED_CONTROLLERS,
103+
'Use the controllers from the chain configuration or use controllers injected from pallets definitions',
104+
{
105+
default: 'false',
106+
type: 'boolean',
107+
regexp: /^true|false$/,
108+
},
109+
),
110+
);
98111
}
99112

100113
/**

src/chains-config/astarControllers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export const astarControllers: ControllerConfig = {
3535
'PalletsAssets',
3636
'PalletsErrors',
3737
'PalletsStorage',
38-
'Paras',
3938
'RuntimeCode',
4039
'RuntimeMetadata',
4140
'RuntimeSpec',

src/chains-config/bifrostControllers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export const bifrostControllers: ControllerConfig = {
3131
'NodeTransactionPool',
3232
'NodeVersion',
3333
'PalletsStorage',
34-
'Paras',
3534
'RuntimeCode',
3635
'RuntimeMetadata',
3736
'RuntimeSpec',

src/chains-config/bifrostPolkadotControllers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export const bifrostPolkadotControllers: ControllerConfig = {
3131
'NodeTransactionPool',
3232
'NodeVersion',
3333
'PalletsStorage',
34-
'Paras',
3534
'RuntimeCode',
3635
'RuntimeMetadata',
3736
'RuntimeSpec',

src/chains-config/calamariControllers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export const calamariControllers: ControllerConfig = {
3535
'NodeVersion',
3636
'PalletsStakingProgress',
3737
'PalletsStorage',
38-
'Paras',
3938
'RuntimeCode',
4039
'RuntimeMetadata',
4140
'RuntimeSpec',

src/chains-config/coretimeControllers.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,11 @@ import { initLRUCache, QueryFeeDetailsCache } from './cache';
2222
*/
2323
export const coretimeControllers: ControllerConfig = {
2424
controllers: [
25-
'AccountsBalanceInfo',
26-
'AccountsConvert',
27-
'AccountsProxyInfo',
2825
'Blocks',
2926
'BlocksExtrinsics',
30-
'BlocksTrace',
3127
'BlocksRawExtrinsics',
3228
'NodeNetwork',
3329
'NodeVersion',
34-
'PalletsConsts',
35-
'PalletsErrors',
36-
'PalletsEvents',
37-
'PalletsNominationPools',
38-
'PalletsOnGoingReferenda',
39-
'PalletsStakingProgress',
40-
'PalletsStakingValidators',
41-
'PalletsStorage',
4230
'RuntimeCode',
4331
'RuntimeMetadata',
4432
'RuntimeSpec',

src/chains-config/index.ts

Lines changed: 38 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';
@@ -49,7 +50,7 @@ import { shidenControllers } from './shidenControllers';
4950
import { soraControllers } from './soraControllers';
5051
import { westendControllers } from './westendControllers';
5152

52-
const specToControllerMap: { [x: string]: ControllerConfig } = {
53+
export const specToControllerMap: { [x: string]: ControllerConfig } = {
5354
westend: westendControllers,
5455
polkadot: polkadotControllers,
5556
polymesh: polymeshControllers,
@@ -117,3 +118,39 @@ function getControllersFromConfig(api: ApiPromise, config: ControllerConfig) {
117118
return acc;
118119
}, [] as AbstractController<AbstractService>[]);
119120
}
121+
122+
/**
123+
* Return an array of instantiated controller instances based off of a `specName`.
124+
* @param pallets pallets available to define controllers
125+
* @param api ApiPromise to inject into controllers
126+
* @param specName specName of chain to get options
127+
*/
128+
129+
export const getControllersByPallets = (pallets: string[], api: ApiPromise, specName: string) => {
130+
const controllersSet: AbstractController<AbstractService>[] = [];
131+
const config = specToControllerMap?.[specName]?.options || defaultControllers?.options;
132+
133+
Object.values(controllers).forEach((controller) => {
134+
if (controller.canInjectByPallets(pallets)) {
135+
controllersSet.push(new controller(api, config));
136+
}
137+
});
138+
139+
return controllersSet;
140+
};
141+
142+
export const getControllers = (
143+
api: ApiPromise,
144+
config: ISidecarConfig,
145+
specName: string,
146+
): AbstractController<AbstractService>[] => {
147+
if (config.EXPRESS.INJECTED_CONTROLLERS) {
148+
return getControllersByPallets(
149+
(api.registry.metadata.toJSON().pallets as unknown as Record<string, unknown>[]).map((p) => p.name as string),
150+
api,
151+
specName,
152+
);
153+
} else {
154+
return getControllersForSpec(api, specName);
155+
}
156+
};

src/chains-config/karuraControllers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export const karuraControllers: ControllerConfig = {
3131
'NodeTransactionPool',
3232
'NodeVersion',
3333
'PalletsStorage',
34-
'Paras',
3534
'RuntimeCode',
3635
'RuntimeMetadata',
3736
'RuntimeSpec',

0 commit comments

Comments
 (0)