This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
113 lines (93 loc) · 4.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* LiskHQ/lisk-service
* Copyright © 2022 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/
const requireAll = require('require-all');
const { Logger } = require('lisk-service-framework');
const { requestConnector } = require('../../utils/request');
const { getDirectoryNamesInPath } = require('../../utils/file');
const logger = Logger();
// Is a map of maps, where the first level keys are moduleIDs, value are maps
// The keys for the second-level map are commandIDs, values are custom 'applyTransaction' methods
const moduleProcessorMap = new Map();
const getAvailableModuleProcessors = async () => {
const IGNORE_DIRS = ['0_moduleName'];
const processors = await getDirectoryNamesInPath(__dirname);
return processors.filter(e => !IGNORE_DIRS.includes(e));
};
const getCommandProcessors = async MODULE_NAME =>
requireAll({
dirname: `${__dirname}/${MODULE_NAME}`,
filter: /(.+)\.js$/,
excludeDirs: /^\.(git|svn)$/,
recursive: false,
});
const buildModuleCommandProcessorMap = async () => {
const systemMetadata = await requestConnector('getSystemMetadata');
const registeredModules = systemMetadata.modules.map(m => m.name);
const availableModuleProcessors = await getAvailableModuleProcessors();
const promises = availableModuleProcessors.map(async moduleNameVal => {
const { index, ...availableCommandProcessors } = await getCommandProcessors(moduleNameVal);
const { MODULE_NAME } = index;
if (registeredModules.includes(MODULE_NAME)) {
if (!moduleProcessorMap.has(MODULE_NAME)) moduleProcessorMap.set(MODULE_NAME, new Map());
const moduleCommandProcessorMap = moduleProcessorMap.get(MODULE_NAME);
Object.values(availableCommandProcessors).forEach(e => {
moduleCommandProcessorMap.set(`apply_${e.COMMAND_NAME}`, e.applyTransaction);
moduleCommandProcessorMap.set(`revert_${e.COMMAND_NAME}`, e.revertTransaction);
});
}
});
return Promise.all(promises);
};
const applyTransaction = async (blockHeader, tx, events, dbTrx) => {
if (moduleProcessorMap.size === 0) await buildModuleCommandProcessorMap();
if (!moduleProcessorMap.has(tx.module)) {
logger.warn(
`No processors implemented for transactions from module: ${tx.module}. Continuing with generic transaction indexing.`,
);
return Promise.resolve();
}
const moduleCommandProcessorMap = moduleProcessorMap.get(tx.module);
if (!moduleCommandProcessorMap.has(`apply_${tx.command}`)) {
logger.warn(
`No applyTransaction hook implemented for transaction with moduleCommand: ${tx.module}:${tx.command}. Continuing with generic transaction indexing.`,
);
return Promise.resolve();
}
const transactionProcessor = moduleCommandProcessorMap.get(`apply_${tx.command}`);
return transactionProcessor(blockHeader, tx, events, dbTrx);
};
const revertTransaction = async (blockHeader, tx, events, dbTrx) => {
if (moduleProcessorMap.size === 0) await buildModuleCommandProcessorMap();
if (!moduleProcessorMap.has(tx.module)) {
logger.warn(
`No processors implemented for transactions from module: ${tx.module}. Continuing with removal of the transaction from the index.`,
);
return Promise.resolve();
}
const moduleCommandProcessorMap = moduleProcessorMap.get(tx.module);
if (!moduleCommandProcessorMap.has(`revert_${tx.command}`)) {
logger.warn(
`No revertTransaction hook implemented for transactions with moduleCommand: ${tx.module}:${tx.command}. Continuing with removal of the transaction from the index.`,
);
return Promise.resolve();
}
const transactionProcessor = moduleCommandProcessorMap.get(`revert_${tx.command}`);
return transactionProcessor(blockHeader, tx, events, dbTrx);
};
module.exports = {
applyTransaction,
revertTransaction,
};