Skip to content

Commit

Permalink
Build 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliorizzo committed Aug 30, 2019
1 parent 7a47c34 commit 557a386
Show file tree
Hide file tree
Showing 102 changed files with 5,277 additions and 2,364 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

## [Unreleased]
## [1.0.0] - 2019-08-30

## Removed

Expand Down
105 changes: 60 additions & 45 deletions dist/api/Api.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,74 @@
'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _DataCollector = require('./lib/DataCollector');
var _config = require('../lib/config');var _config2 = _interopRequireDefault(_config);
var _types = require('../lib/types');
var _Block = require('./modules/Block');
var _Tx = require('./modules/Tx');
var _Address = require('./modules/Address');
var _Event = require('./modules/Event');
var _TokenAccount = require('./modules/TokenAccount');
var _TxPending = require('./modules/TxPending');
var _Stats = require('./modules/Stats');
var _getCirculatingSupply = require('./lib/getCirculatingSupply');var _getCirculatingSupply2 = _interopRequireDefault(_getCirculatingSupply);
var _apiTools = require('./lib/apiTools');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}





const lastLimit = _config2.default.api.lastBlocks || 10;
const collections = _config2.default.blocks.collections;
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = void 0;var _DataCollector = require("./lib/DataCollector");
var _modules = require("./modules");
var _types = require("../lib/types");
var _getCirculatingSupply = _interopRequireDefault(require("./lib/getCirculatingSupply"));
var _blocksCollections = require("../lib/blocksCollections");
var _apiTools = require("./lib/apiTools");
var _config = _interopRequireDefault(require("../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}

class Api extends _DataCollector.DataCollector {
constructor(db) {
let collectionName = collections.Blocks;
constructor({ db, initConfig, nativeContracts }, { modules, collectionsNames, lastBlocks } = {}) {
const collectionName = collectionsNames.Blocks;
super(db, { collectionName });
this.lastLimit = lastLimit;
this.collectionsNames = collectionsNames;
this.collections = (0, _blocksCollections.getDbBlocksCollections)(db);
this.lastLimit = lastBlocks || 10;
this.latest = 0;
this.lastBlocks = [];
this.lastTransactions = [];
this.circulatingSupply = null;
this.stats = { timestamp: 0 };
this.addItem(collections.Blocks, 'Block', _Block.Block);
this.addItem(collections.PendingTxs, 'TxPending', _TxPending.TxPending);
this.addItem(collections.Txs, 'Tx', _Tx.Tx);
this.addItem(collections.Addrs, 'Address', _Address.Address);
this.addItem(collections.Events, 'Event', _Event.Event);
this.addItem(collections.TokensAddrs, 'Token', _TokenAccount.TokenAccount);
this.addItem(collections.Stats, 'Stats', _Stats.Stats);
this.loadModules((0, _modules.getEnabledApiModules)(modules));
this.initConfig = initConfig;
const { isNativeContract } = nativeContracts;
this.isNativeContract = isNativeContract;
}
tick() {
this.setLastBlocks();
this.setCirculatingSupply();
}

loadModules(modules) {
Object.keys(modules).forEach(name => {
const module = new modules[name](this.collections, name);
this.log.info(`Loading module ${name}`);
this.addModule(module, name);
});
}

async run(payload) {
try {
if (Object.keys(payload).length < 1) throw new Error('invalid request');
const action = payload.action;
let { module, action, params } = payload;
if (!action) throw new Error('Missing action');
const params = (0, _apiTools.filterParams)(payload.params);
const module = (0, _apiTools.getModule)(payload.module);
if (!module) throw new Error('Unknown module');
const delayed = (0, _apiTools.getDelayedFields)(module, action);
const moduleName = _apiTools.MODULES[module];
if (!moduleName) throw new Error('Unknown module');
const delayed = (0, _apiTools.getDelayedFields)(moduleName, action);
const time = Date.now();
const result = await this.itemPublicAction(module, action, params);

params = (0, _apiTools.filterParams)(payload.params);
const result = await this.getModule(moduleName).run(action, params);
const queryTime = Date.now() - time;
const logCmd = queryTime > 1000 ? 'warn' : 'trace';
this.log[logCmd](`${module}.${action}(${JSON.stringify(params)}) ${queryTime} ms`);

return { module, action, params, result, delayed };
const res = { module, action, params, result, delayed };
return res;
} catch (err) {
this.log.debug(err);
return Promise.reject(err);
}
}

info() {
let info = Object.assign({}, this.initConfig);
info.txTypes = Object.assign({}, _types.txTypes);
info.modules = _config.default.api.modules;
return info;
}

async setLastBlocks() {
try {
let { collection, lastLimit, Tx } = this;
let { collection, lastLimit } = this;
const Tx = this.getModule('Tx');
let blocks = await collection.find().sort({ number: -1 }).limit(lastLimit).toArray();
let txs = await Tx.db.find({ txType: { $in: [_types.txTypes.default, _types.txTypes.contract] } }).
sort({ blockNumber: -1, transactionIndex: -1 }).
Expand All @@ -80,8 +83,8 @@ class Api extends _DataCollector.DataCollector {

async setCirculatingSupply() {
try {
const collection = this.db.collection(collections.Addrs);
let circulating = await (0, _getCirculatingSupply2.default)(collection);
const collection = this.collections.Addrs;
let circulating = await (0, _getCirculatingSupply.default)(collection, this.initConfig.nativeContracts);
this.circulatingSupply = Object.assign({}, circulating);
} catch (err) {
this.log.debug(err);
Expand Down Expand Up @@ -114,7 +117,7 @@ class Api extends _DataCollector.DataCollector {
}

async getAddress(address) {
return this.Address.run('getAddress', { address });
return this.getModule('Address').run('getAddress', { address });
}

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

getPendingTransaction(params) {
return this.getModule('TxPending').run('getPendingTransaction', params);
}

async updateStats() {
const oldStats = this.stats;
const stats = await this.Stats.run('getLatest');
const stats = await this.getModule('Stats').run('getLatest');
if (!stats) return;

const ExtendedStats = this.getModule('ExtendedStats');
if (ExtendedStats) {
const blockNumber = parseInt(stats.blockNumber);
const extendedStats = await ExtendedStats.getExtendedStats(blockNumber);
Object.assign(stats, extendedStats);
}

this.stats = Object.assign({}, stats);
if (stats.timestamp !== oldStats.timestamp) {
this.events.emit('newStats', this.stats);
}
}}exports.default =
}}var _default =


Api;
Api;exports.default = _default;
26 changes: 13 additions & 13 deletions dist/api/HttpServer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.HttpServer = undefined;var _http = require('http');var _http2 = _interopRequireDefault(_http);
var _express = require('express');var _express2 = _interopRequireDefault(_express);
var _api = require('./routes/api');var _api2 = _interopRequireDefault(_api);
var _doc = require('./routes/doc');var _doc2 = _interopRequireDefault(_doc);
var _config = require('../lib/config');var _config2 = _interopRequireDefault(_config);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.HttpServer = void 0;var _http = _interopRequireDefault(require("http"));
var _express = _interopRequireDefault(require("express"));
var _api = _interopRequireDefault(require("./routes/api"));
var _doc = _interopRequireDefault(require("./routes/doc"));
var _config = _interopRequireDefault(require("../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}

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

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

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

if (_config2.default.api.exposeDoc) {
app.use('/doc', (0, _doc2.default)({ log, app }));
if (_config.default.api.exposeDoc) {
app.use('/doc', (0, _doc.default)({ log, app }));
}

// 404
app.use((req, res, next) => res.status(404).send());
return httpServer;
};exports.default =
};exports.HttpServer = HttpServer;var _default =

HttpServer;
HttpServer;exports.default = _default;
28 changes: 14 additions & 14 deletions dist/api/Status.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.Status = undefined;var _DataCollector = require('./lib/DataCollector');
var _config = require('../lib/config');var _config2 = _interopRequireDefault(_config);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}

const statusCollection = _config2.default.blocks.collections.Status;
const blocksCollection = _config2.default.blocks.collections.Blocks;
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.Status = void 0;var _DataCollector = require("./lib/DataCollector/");
var _config = _interopRequireDefault(require("../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
const { collectionsNames } = _config.default;

class Status extends _DataCollector.DataCollector {
constructor(db) {
super(db, { statusCollection });
const collectionName = collectionsNames.Status;
super(db, { collectionName });
this.tickDelay = 5000;
this.state = {};
this.addItem(statusCollection, 'Status', null, true);
this.addItem(blocksCollection, 'Blocks', null, true);
this.addModule(new _DataCollector.DataCollectorItem(db.collection(collectionsNames.Status), 'Status'));
this.addModule(new _DataCollector.DataCollectorItem(db.collection(collectionsNames.Blocks), 'Blocks'));
}
tick() {
this.updateState().then(newState => {
Expand All @@ -23,7 +22,8 @@ class Status extends _DataCollector.DataCollector {
return this.formatData(this.state);
}
getBlocksServiceStatus() {
return this.Status.find({}, { timestamp: -1 }, 1).
const Status = this.getModule('Status');
return Status.find({}, { timestamp: -1 }, 1).
then(res => {
res = res.data[0];
delete res._id;
Expand Down Expand Up @@ -72,14 +72,14 @@ class Status extends _DataCollector.DataCollector {
}

getHighestBlock() {
return this.Blocks.db.findOne({}, { sort: { number: -1 }, limit: 1 });
return this.getModule('Blocks').db.findOne({}, { sort: { number: -1 }, limit: 1 });
}
getLastblockReceived() {
return this.Blocks.db.findOne({}, { sort: { _received: -1 }, limit: 1 });
return this.getModule('Blocks').db.findOne({}, { sort: { _received: -1 }, limit: 1 });
}
getTotalBlocks() {
return this.Blocks.db.countDocuments({});
}}exports.Status = Status;exports.default =
return this.getModule('Blocks').db.countDocuments({});
}}exports.Status = Status;var _default =


Status;
Status;exports.default = _default;
10 changes: 5 additions & 5 deletions dist/api/TxPool.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.TxPool = undefined;var _DataCollector = require('./lib/DataCollector');
var _config = require('../lib/config');var _config2 = _interopRequireDefault(_config);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.TxPool = void 0;var _DataCollector = require("./lib/DataCollector");
var _config = _interopRequireDefault(require("../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}

const collectionName = _config2.default.blocks.collections.TxPool;
const collectionName = _config.default.collectionsNames.TxPool;

class TxPool extends _DataCollector.DataCollector {
constructor(db) {
Expand Down Expand Up @@ -59,7 +59,7 @@ class TxPool extends _DataCollector.DataCollector {
let state = Object.assign({}, this.state);
delete state._id;
return this.formatData(state);
}}exports.TxPool = TxPool;exports.default =
}}exports.TxPool = TxPool;var _default =


TxPool;
TxPool;exports.default = _default;
21 changes: 11 additions & 10 deletions dist/api/UserEventsApi.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.UserEventsApi = undefined;var _path = require('path');var _path2 = _interopRequireDefault(_path);
var _child_process = require('child_process');
var _config = require('../lib/config');var _config2 = _interopRequireDefault(_config);
var _apiTools = require('./lib/apiTools');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.UserEventsApi = void 0;var _path = _interopRequireDefault(require("path"));
var _child_process = require("child_process");
var _config = _interopRequireDefault(require("../lib/config"));
var _apiTools = require("./lib/apiTools");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}

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

const UserEventsApi = exports.UserEventsApi = (io, api, { log }) => {
if (!_config2.default.api.allowUserEvents) return;
const UserEventsApi = (io, api, { log }) => {
if (!_config.default.api.allowUserEvents) return;
log = log || console;
const userEvents = UserEventsSocket();

Expand All @@ -21,14 +21,15 @@ const UserEventsApi = exports.UserEventsApi = (io, api, { log }) => {
let req = payload;
let error = res.error;
const socket = io.sockets.connected[msg.socketId];
log.trace(`Sending message to client ${module}.${action} error:${JSON.stringify(error)}`);
if (socket) socket.emit('data', (0, _apiTools.formatRes)({ module, action, result, req, error }));
} catch (err) {
log.error(err);
return Promise.reject(err);
}
});
return Object.freeze(userEvents);
};
};exports.UserEventsApi = UserEventsApi;

async function processMsg(msg, api) {
let data, error;
Expand All @@ -44,6 +45,6 @@ async function processMsg(msg, api) {
data = msg.result;
}
return { data, error };
}exports.default =
}var _default =

UserEventsApi;
UserEventsApi;exports.default = _default;
16 changes: 8 additions & 8 deletions dist/api/channels.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.createChannels = exports.CHANNELS = undefined;
var _Channel = require('./lib/Channel');var _Channel2 = _interopRequireDefault(_Channel);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.createChannels = exports.CHANNELS = void 0;
var _Channel = _interopRequireDefault(require("./lib/Channel"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}

const CHANNELS = exports.CHANNELS = {
const CHANNELS = {
blocksChannel: 'blocks',
statusChannel: 'status',
txPoolChannel: 'txpool',
statsChannel: 'stats' };
statsChannel: 'stats' };exports.CHANNELS = CHANNELS;


const createChannels = exports.createChannels = io => {
const createChannels = io => {
const channels = {};
Object.keys(CHANNELS).forEach(channel => {
const name = CHANNELS[channel];
channels[channel] = (0, _Channel2.default)(name, io);
channels[channel] = (0, _Channel.default)(name, io);
});

const getChannel = name => {
Expand All @@ -33,6 +33,6 @@ const createChannels = exports.createChannels = io => {
};

return Object.freeze({ channels, subscribe, unsubscribe });
};exports.default =
};exports.createChannels = createChannels;var _default =

createChannels;
createChannels;exports.default = _default;
Loading

0 comments on commit 557a386

Please sign in to comment.