Skip to content

Commit 557a386

Browse files
committed
Build 1.0.0
1 parent 7a47c34 commit 557a386

File tree

102 files changed

+5277
-2364
lines changed

Some content is hidden

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

102 files changed

+5277
-2364
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Changelog
22

3-
## [Unreleased]
3+
## [1.0.0] - 2019-08-30
44

55
## Removed
66

dist/api/Api.js

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,74 @@
1-
'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _DataCollector = require('./lib/DataCollector');
2-
var _config = require('../lib/config');var _config2 = _interopRequireDefault(_config);
3-
var _types = require('../lib/types');
4-
var _Block = require('./modules/Block');
5-
var _Tx = require('./modules/Tx');
6-
var _Address = require('./modules/Address');
7-
var _Event = require('./modules/Event');
8-
var _TokenAccount = require('./modules/TokenAccount');
9-
var _TxPending = require('./modules/TxPending');
10-
var _Stats = require('./modules/Stats');
11-
var _getCirculatingSupply = require('./lib/getCirculatingSupply');var _getCirculatingSupply2 = _interopRequireDefault(_getCirculatingSupply);
12-
var _apiTools = require('./lib/apiTools');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
13-
14-
15-
16-
17-
18-
const lastLimit = _config2.default.api.lastBlocks || 10;
19-
const collections = _config2.default.blocks.collections;
1+
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = void 0;var _DataCollector = require("./lib/DataCollector");
2+
var _modules = require("./modules");
3+
var _types = require("../lib/types");
4+
var _getCirculatingSupply = _interopRequireDefault(require("./lib/getCirculatingSupply"));
5+
var _blocksCollections = require("../lib/blocksCollections");
6+
var _apiTools = require("./lib/apiTools");
7+
var _config = _interopRequireDefault(require("../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
208

219
class Api extends _DataCollector.DataCollector {
22-
constructor(db) {
23-
let collectionName = collections.Blocks;
10+
constructor({ db, initConfig, nativeContracts }, { modules, collectionsNames, lastBlocks } = {}) {
11+
const collectionName = collectionsNames.Blocks;
2412
super(db, { collectionName });
25-
this.lastLimit = lastLimit;
13+
this.collectionsNames = collectionsNames;
14+
this.collections = (0, _blocksCollections.getDbBlocksCollections)(db);
15+
this.lastLimit = lastBlocks || 10;
2616
this.latest = 0;
2717
this.lastBlocks = [];
2818
this.lastTransactions = [];
2919
this.circulatingSupply = null;
3020
this.stats = { timestamp: 0 };
31-
this.addItem(collections.Blocks, 'Block', _Block.Block);
32-
this.addItem(collections.PendingTxs, 'TxPending', _TxPending.TxPending);
33-
this.addItem(collections.Txs, 'Tx', _Tx.Tx);
34-
this.addItem(collections.Addrs, 'Address', _Address.Address);
35-
this.addItem(collections.Events, 'Event', _Event.Event);
36-
this.addItem(collections.TokensAddrs, 'Token', _TokenAccount.TokenAccount);
37-
this.addItem(collections.Stats, 'Stats', _Stats.Stats);
21+
this.loadModules((0, _modules.getEnabledApiModules)(modules));
22+
this.initConfig = initConfig;
23+
const { isNativeContract } = nativeContracts;
24+
this.isNativeContract = isNativeContract;
3825
}
3926
tick() {
4027
this.setLastBlocks();
4128
this.setCirculatingSupply();
4229
}
4330

31+
loadModules(modules) {
32+
Object.keys(modules).forEach(name => {
33+
const module = new modules[name](this.collections, name);
34+
this.log.info(`Loading module ${name}`);
35+
this.addModule(module, name);
36+
});
37+
}
38+
4439
async run(payload) {
4540
try {
4641
if (Object.keys(payload).length < 1) throw new Error('invalid request');
47-
const action = payload.action;
42+
let { module, action, params } = payload;
4843
if (!action) throw new Error('Missing action');
49-
const params = (0, _apiTools.filterParams)(payload.params);
50-
const module = (0, _apiTools.getModule)(payload.module);
51-
if (!module) throw new Error('Unknown module');
52-
const delayed = (0, _apiTools.getDelayedFields)(module, action);
44+
const moduleName = _apiTools.MODULES[module];
45+
if (!moduleName) throw new Error('Unknown module');
46+
const delayed = (0, _apiTools.getDelayedFields)(moduleName, action);
5347
const time = Date.now();
54-
const result = await this.itemPublicAction(module, action, params);
55-
48+
params = (0, _apiTools.filterParams)(payload.params);
49+
const result = await this.getModule(moduleName).run(action, params);
5650
const queryTime = Date.now() - time;
5751
const logCmd = queryTime > 1000 ? 'warn' : 'trace';
5852
this.log[logCmd](`${module}.${action}(${JSON.stringify(params)}) ${queryTime} ms`);
59-
60-
return { module, action, params, result, delayed };
53+
const res = { module, action, params, result, delayed };
54+
return res;
6155
} catch (err) {
56+
this.log.debug(err);
6257
return Promise.reject(err);
6358
}
6459
}
6560

61+
info() {
62+
let info = Object.assign({}, this.initConfig);
63+
info.txTypes = Object.assign({}, _types.txTypes);
64+
info.modules = _config.default.api.modules;
65+
return info;
66+
}
67+
6668
async setLastBlocks() {
6769
try {
68-
let { collection, lastLimit, Tx } = this;
70+
let { collection, lastLimit } = this;
71+
const Tx = this.getModule('Tx');
6972
let blocks = await collection.find().sort({ number: -1 }).limit(lastLimit).toArray();
7073
let txs = await Tx.db.find({ txType: { $in: [_types.txTypes.default, _types.txTypes.contract] } }).
7174
sort({ blockNumber: -1, transactionIndex: -1 }).
@@ -80,8 +83,8 @@ class Api extends _DataCollector.DataCollector {
8083

8184
async setCirculatingSupply() {
8285
try {
83-
const collection = this.db.collection(collections.Addrs);
84-
let circulating = await (0, _getCirculatingSupply2.default)(collection);
86+
const collection = this.collections.Addrs;
87+
let circulating = await (0, _getCirculatingSupply.default)(collection, this.initConfig.nativeContracts);
8588
this.circulatingSupply = Object.assign({}, circulating);
8689
} catch (err) {
8790
this.log.debug(err);
@@ -114,7 +117,7 @@ class Api extends _DataCollector.DataCollector {
114117
}
115118

116119
async getAddress(address) {
117-
return this.Address.run('getAddress', { address });
120+
return this.getModule('Address').run('getAddress', { address });
118121
}
119122

120123
async addAddressData(address, data, key = '_addressData') {
@@ -123,15 +126,27 @@ class Api extends _DataCollector.DataCollector {
123126
return data || account;
124127
}
125128

129+
getPendingTransaction(params) {
130+
return this.getModule('TxPending').run('getPendingTransaction', params);
131+
}
132+
126133
async updateStats() {
127134
const oldStats = this.stats;
128-
const stats = await this.Stats.run('getLatest');
135+
const stats = await this.getModule('Stats').run('getLatest');
129136
if (!stats) return;
137+
138+
const ExtendedStats = this.getModule('ExtendedStats');
139+
if (ExtendedStats) {
140+
const blockNumber = parseInt(stats.blockNumber);
141+
const extendedStats = await ExtendedStats.getExtendedStats(blockNumber);
142+
Object.assign(stats, extendedStats);
143+
}
144+
130145
this.stats = Object.assign({}, stats);
131146
if (stats.timestamp !== oldStats.timestamp) {
132147
this.events.emit('newStats', this.stats);
133148
}
134-
}}exports.default =
149+
}}var _default =
135150

136151

137-
Api;
152+
Api;exports.default = _default;

dist/api/HttpServer.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.HttpServer = undefined;var _http = require('http');var _http2 = _interopRequireDefault(_http);
2-
var _express = require('express');var _express2 = _interopRequireDefault(_express);
3-
var _api = require('./routes/api');var _api2 = _interopRequireDefault(_api);
4-
var _doc = require('./routes/doc');var _doc2 = _interopRequireDefault(_doc);
5-
var _config = require('../lib/config');var _config2 = _interopRequireDefault(_config);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
1+
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.HttpServer = void 0;var _http = _interopRequireDefault(require("http"));
2+
var _express = _interopRequireDefault(require("express"));
3+
var _api = _interopRequireDefault(require("./routes/api"));
4+
var _doc = _interopRequireDefault(require("./routes/doc"));
5+
var _config = _interopRequireDefault(require("../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
66

7-
const HttpServer = exports.HttpServer = ({ api, status, log }) => {
8-
const app = (0, _express2.default)();
9-
const httpServer = _http2.default.Server(app);
7+
const HttpServer = ({ api, status, log }) => {
8+
const app = (0, _express.default)();
9+
const httpServer = _http.default.Server(app);
1010
app.set('etag', false);
1111
app.set('x-powered-by', false);
1212

@@ -20,15 +20,15 @@ const HttpServer = exports.HttpServer = ({ api, status, log }) => {
2020
res.send(data);
2121
});
2222

23-
app.use('/api', (0, _api2.default)({ log, api }));
23+
app.use('/api', (0, _api.default)({ log, api }));
2424

25-
if (_config2.default.api.exposeDoc) {
26-
app.use('/doc', (0, _doc2.default)({ log, app }));
25+
if (_config.default.api.exposeDoc) {
26+
app.use('/doc', (0, _doc.default)({ log, app }));
2727
}
2828

2929
// 404
3030
app.use((req, res, next) => res.status(404).send());
3131
return httpServer;
32-
};exports.default =
32+
};exports.HttpServer = HttpServer;var _default =
3333

34-
HttpServer;
34+
HttpServer;exports.default = _default;

dist/api/Status.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.Status = undefined;var _DataCollector = require('./lib/DataCollector');
2-
var _config = require('../lib/config');var _config2 = _interopRequireDefault(_config);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
3-
4-
const statusCollection = _config2.default.blocks.collections.Status;
5-
const blocksCollection = _config2.default.blocks.collections.Blocks;
1+
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.Status = void 0;var _DataCollector = require("./lib/DataCollector/");
2+
var _config = _interopRequireDefault(require("../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
3+
const { collectionsNames } = _config.default;
64

75
class Status extends _DataCollector.DataCollector {
86
constructor(db) {
9-
super(db, { statusCollection });
7+
const collectionName = collectionsNames.Status;
8+
super(db, { collectionName });
109
this.tickDelay = 5000;
1110
this.state = {};
12-
this.addItem(statusCollection, 'Status', null, true);
13-
this.addItem(blocksCollection, 'Blocks', null, true);
11+
this.addModule(new _DataCollector.DataCollectorItem(db.collection(collectionsNames.Status), 'Status'));
12+
this.addModule(new _DataCollector.DataCollectorItem(db.collection(collectionsNames.Blocks), 'Blocks'));
1413
}
1514
tick() {
1615
this.updateState().then(newState => {
@@ -23,7 +22,8 @@ class Status extends _DataCollector.DataCollector {
2322
return this.formatData(this.state);
2423
}
2524
getBlocksServiceStatus() {
26-
return this.Status.find({}, { timestamp: -1 }, 1).
25+
const Status = this.getModule('Status');
26+
return Status.find({}, { timestamp: -1 }, 1).
2727
then(res => {
2828
res = res.data[0];
2929
delete res._id;
@@ -72,14 +72,14 @@ class Status extends _DataCollector.DataCollector {
7272
}
7373

7474
getHighestBlock() {
75-
return this.Blocks.db.findOne({}, { sort: { number: -1 }, limit: 1 });
75+
return this.getModule('Blocks').db.findOne({}, { sort: { number: -1 }, limit: 1 });
7676
}
7777
getLastblockReceived() {
78-
return this.Blocks.db.findOne({}, { sort: { _received: -1 }, limit: 1 });
78+
return this.getModule('Blocks').db.findOne({}, { sort: { _received: -1 }, limit: 1 });
7979
}
8080
getTotalBlocks() {
81-
return this.Blocks.db.countDocuments({});
82-
}}exports.Status = Status;exports.default =
81+
return this.getModule('Blocks').db.countDocuments({});
82+
}}exports.Status = Status;var _default =
8383

8484

85-
Status;
85+
Status;exports.default = _default;

dist/api/TxPool.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.TxPool = undefined;var _DataCollector = require('./lib/DataCollector');
2-
var _config = require('../lib/config');var _config2 = _interopRequireDefault(_config);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
1+
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.TxPool = void 0;var _DataCollector = require("./lib/DataCollector");
2+
var _config = _interopRequireDefault(require("../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
33

4-
const collectionName = _config2.default.blocks.collections.TxPool;
4+
const collectionName = _config.default.collectionsNames.TxPool;
55

66
class TxPool extends _DataCollector.DataCollector {
77
constructor(db) {
@@ -59,7 +59,7 @@ class TxPool extends _DataCollector.DataCollector {
5959
let state = Object.assign({}, this.state);
6060
delete state._id;
6161
return this.formatData(state);
62-
}}exports.TxPool = TxPool;exports.default =
62+
}}exports.TxPool = TxPool;var _default =
6363

6464

65-
TxPool;
65+
TxPool;exports.default = _default;

dist/api/UserEventsApi.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.UserEventsApi = undefined;var _path = require('path');var _path2 = _interopRequireDefault(_path);
2-
var _child_process = require('child_process');
3-
var _config = require('../lib/config');var _config2 = _interopRequireDefault(_config);
4-
var _apiTools = require('./lib/apiTools');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
1+
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.UserEventsApi = void 0;var _path = _interopRequireDefault(require("path"));
2+
var _child_process = require("child_process");
3+
var _config = _interopRequireDefault(require("../lib/config"));
4+
var _apiTools = require("./lib/apiTools");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
55

66
function UserEventsSocket() {
7-
return (0, _child_process.fork)(_path2.default.resolve(__dirname, '../services/userEvents/userEventsService.js'));
7+
return (0, _child_process.fork)(_path.default.resolve(__dirname, '../services/userEvents/userEventsService.js'));
88
}
99

10-
const UserEventsApi = exports.UserEventsApi = (io, api, { log }) => {
11-
if (!_config2.default.api.allowUserEvents) return;
10+
const UserEventsApi = (io, api, { log }) => {
11+
if (!_config.default.api.allowUserEvents) return;
1212
log = log || console;
1313
const userEvents = UserEventsSocket();
1414

@@ -21,14 +21,15 @@ const UserEventsApi = exports.UserEventsApi = (io, api, { log }) => {
2121
let req = payload;
2222
let error = res.error;
2323
const socket = io.sockets.connected[msg.socketId];
24+
log.trace(`Sending message to client ${module}.${action} error:${JSON.stringify(error)}`);
2425
if (socket) socket.emit('data', (0, _apiTools.formatRes)({ module, action, result, req, error }));
2526
} catch (err) {
2627
log.error(err);
2728
return Promise.reject(err);
2829
}
2930
});
3031
return Object.freeze(userEvents);
31-
};
32+
};exports.UserEventsApi = UserEventsApi;
3233

3334
async function processMsg(msg, api) {
3435
let data, error;
@@ -44,6 +45,6 @@ async function processMsg(msg, api) {
4445
data = msg.result;
4546
}
4647
return { data, error };
47-
}exports.default =
48+
}var _default =
4849

49-
UserEventsApi;
50+
UserEventsApi;exports.default = _default;

dist/api/channels.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.createChannels = exports.CHANNELS = undefined;
2-
var _Channel = require('./lib/Channel');var _Channel2 = _interopRequireDefault(_Channel);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
1+
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.createChannels = exports.CHANNELS = void 0;
2+
var _Channel = _interopRequireDefault(require("./lib/Channel"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
33

4-
const CHANNELS = exports.CHANNELS = {
4+
const CHANNELS = {
55
blocksChannel: 'blocks',
66
statusChannel: 'status',
77
txPoolChannel: 'txpool',
8-
statsChannel: 'stats' };
8+
statsChannel: 'stats' };exports.CHANNELS = CHANNELS;
99

1010

11-
const createChannels = exports.createChannels = io => {
11+
const createChannels = io => {
1212
const channels = {};
1313
Object.keys(CHANNELS).forEach(channel => {
1414
const name = CHANNELS[channel];
15-
channels[channel] = (0, _Channel2.default)(name, io);
15+
channels[channel] = (0, _Channel.default)(name, io);
1616
});
1717

1818
const getChannel = name => {
@@ -33,6 +33,6 @@ const createChannels = exports.createChannels = io => {
3333
};
3434

3535
return Object.freeze({ channels, subscribe, unsubscribe });
36-
};exports.default =
36+
};exports.createChannels = createChannels;var _default =
3737

38-
createChannels;
38+
createChannels;exports.default = _default;

0 commit comments

Comments
 (0)