-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathinitModule.js
281 lines (262 loc) · 8.35 KB
/
initModule.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
'use strict';
var express = require('express');
var path = require('path');
var randomString = require('randomstring');
var _ = require('lodash');
var async = require('async');
var dirname = path.join(__dirname, '..', '..');
var config = require(path.join(dirname, 'test/config.json')); // use Testnet config
var packageJson = require(path.join(dirname, '/package.json'));
var database = require(path.join(dirname, '/helpers', 'database.js'));
var genesisblock = require(path.join(dirname, 'test/genesisBlock.json')); // use Testnet genesisBlock
var Logger = require(dirname + '/logger.js');
var z_schema = require('../../helpers/z_schema.js');
var cacheHelper = require('../../helpers/cache.js');
var Cache = require('../../modules/cache.js');
var ed = require('../../helpers/ed');
var Transaction = require('../../logic/transaction.js');
var Account = require('../../logic/account.js');
var accounts = require('../../helpers/accounts');
var Sequence = require('../../helpers/sequence.js');
const { removeQueuedJobs } = require('../common/globalAfter.js');
const Consensus = require('../../logic/consensus/consensus.js');
var modulesLoader = new function () {
const consensus = new Consensus();
consensus.bindModules({
loader: {
getHeight: () => 2_000_000,
},
});
this.db = null;
this.logger = new Logger({ echo: null, errorLevel: config.fileLogLevel, filename: config.logFileName });
this.scope = {
config: config,
packageJson: packageJson,
genesisblock: { block: genesisblock },
logger: this.logger,
balancesSequence: new Sequence(),
consensus,
network: {
app: express()
},
public: '../../public',
schema: new z_schema(),
ed: ed,
accounts: accounts,
bus: {
message: function () {}
},
nonce: randomString.generate(16)
};
/**
* Initializes Logic class with params
*
* @param {Function} Logic
* @param {Object} scope
* @param {Function} cb
*/
this.initLogic = function (Logic, scope, cb) {
switch (Logic.name) {
case 'Account':
new Logic(scope.db, scope.schema, scope.logger, cb);
break;
case 'Transaction':
async.series({
account: function (cb) {
new Account(scope.db, scope.schema, scope.logger, cb);
}
}, function (err, result) {
// null is for clientWs
new Logic(scope.db, scope.ed, scope.schema, scope.genesisblock, result.account, scope.logger, null, scope.consensus, cb);
});
break;
case 'Block':
async.waterfall([
function (waterCb) {
return new Account(scope.db, scope.schema, scope.logger, waterCb);
},
function (account, waterCb) {
// null is for clientWs
return new Transaction(scope.db, scope.ed, scope.schema, scope.genesisblock, account, scope.logger, null, scope.consensus, waterCb);
}
], function (err, transaction) {
new Logic(scope.ed, scope.schema, transaction, cb);
});
break;
case 'Peers':
new Logic(scope.logger, cb);
break;
case 'State':
async.series({
account: function (cb) {
new Account(scope.db, scope.schema, scope.logger, cb);
}
}, function (err, result) {
new Logic(scope.db, scope.ed, scope.schema, result.account, scope.logger, cb);
});
break;
default:
console.log('no Logic case initLogic');
}
};
/**
* Initializes Module class with params
*
* @param {Function} Module
* @param {Object} scope
* @param {Function} cb
*/
this.initModule = function (Module, scope, cb) {
return new Module(cb, scope);
};
/**
* Initializes multiple Modules
*
* @param {Array<{name: Module}>} modules
* @param {Array<{name: Logic}>} logic
* @param {Object>} scope
* @param {Function} cb
*/
this.initModules = function (modules, logic, scope, cb) {
async.waterfall([
function (waterCb) {
this.getDbConnection(waterCb);
}.bind(this),
function (db, waterCb) {
scope = _.merge(this.scope, { db: db }, scope);
async.reduce(logic, {}, function (memo, logicObj, mapCb) {
var name = _.keys(logicObj)[0];
return this.initLogic(logicObj[name], scope, function (err, initializedLogic) {
memo[name] = initializedLogic;
return mapCb(err, memo);
});
}.bind(this), waterCb);
}.bind(this),
function (logic, waterCb) {
scope = _.merge(this.scope, { logic: { ...logic, ...scope.logic } }, scope);
async.reduce(modules, {}, function (memo, moduleObj, mapCb) {
var name = _.keys(moduleObj)[0];
return this.initModule(moduleObj[name], scope, function (err, module) {
memo[name] = module;
return mapCb(err, memo);
});
}.bind(this), waterCb);
}.bind(this),
function (modules, waterCb) {
_.each(scope.logic, function (logic) {
if (typeof logic.bind === 'function') {
logic.bind({ modules: modules });
}
if (typeof logic.bindModules === 'function') {
logic.bindModules(modules);
}
});
waterCb(null, modules);
}
], cb);
};
/**
* Initializes all created Modules in directory
*
* @param {Function} cb
* @param {object} [scope={}] scope
*/
this.initAllModules = function (cb, scope) {
this.initModules([
{ accounts: require('../../modules/accounts') },
{ blocks: require('../../modules/blocks') },
{ crypto: require('../../modules/crypto') },
{ delegates: require('../../modules/delegates') },
{ loader: require('../../modules/loader') },
{ multisignatures: require('../../modules/multisignatures') },
{ peers: require('../../modules/peers') },
{ rounds: require('../../modules/rounds') },
{ server: require('../../modules/server') },
{ signatures: require('../../modules/signatures') },
{ sql: require('../../modules/sql') },
{ system: require('../../modules/system') },
{ transactions: require('../../modules/transactions') },
{ transport: require('../../modules/transport') },
], [
{ 'transaction': require('../../logic/transaction') },
{ 'account': require('../../logic/account') },
{ 'block': require('../../logic/block') },
{ 'peers': require('../../logic/peers.js') }
], scope || {}, cb);
};
/**
* Initializes Module class with basic conf
*
* @param {Function} Module
* @param {Function} cb
* @param {Object=} scope
*/
this.initModuleWithDb = function (Module, cb, scope) {
this.initWithDb(Module, this.initModule, cb, scope);
};
/**
* Initializes Logic class with basic conf
*
* @param {Function} Logic
* @param {Function} cb
* @param {Object=} scope
*/
this.initLogicWithDb = function (Logic, cb, scope) {
this.initWithDb(Logic, this.initLogic, cb, scope);
};
/**
* Accepts Class to invoke (Logic or Module) and fills the scope with basic conf
*
* @param {Function} Klass
* @param {Function} moduleConstructor
* @param {Function} cb
* @param {Object=} scope
*/
this.initWithDb = function (Klass, moduleConstructor, cb, scope) {
this.getDbConnection(function (err, db) {
if (err) {
return cb(err);
}
moduleConstructor(Klass, _.merge(this.scope, { db: db }, scope), cb);
}.bind(this));
};
/**
* Starts and returns db connection
*
* @param {Function} cb
*/
this.getDbConnection = function (cb) {
if (this.db) {
return cb(null, this.db);
}
database.connect(config.db, this.logger, function (err, db) {
if (err) {
return cb(err);
}
this.db = db;
cb(null, this.db);
}.bind(this));
};
/**
* Initializes Cache module
* @param {Function} cb
*/
this.initCache = function (cb) {
var cacheEnabled, cacheConfig;
cacheEnabled = this.scope.config.cacheEnabled;
cacheConfig = this.scope.config.redis;
cacheHelper.connect(cacheEnabled, cacheConfig, this.logger, function (err, __cache) {
if (err) {
cb(err, __cache);
} else {
this.initModule(Cache, _.merge(this.scope, { cache: __cache }), cb);
}
}.bind(this));
};
};
afterEach(() =>
removeQueuedJobs()
)
module.exports = {
modulesLoader: modulesLoader
};