Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ega-forever committed Dec 22, 2017
1 parent 2e75edb commit be68d2b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
9 changes: 8 additions & 1 deletion config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ require('dotenv').config();

const config = {
mongo: {
uri: process.env.MONGO_URI || 'mongodb://localhost:27017/data'
accounts: {
uri: process.env.MONGO_ACCOUNTS_URI || process.env.MONGO_URI || 'mongodb://localhost:27017/data',
collectionPrefix: process.env.MONGO_COLLECTION_PREFIX || 'eth'
},
data: {
uri: process.env.MONGO_DATA_URI || process.env.MONGO_URI || 'mongodb://localhost:27017/data',
collectionPrefix: process.env.MONGO_DATA_COLLECTION_PREFIX || process.env.MONGO_COLLECTION_PREFIX || 'eth'
}
},
rabbit: {
url: process.env.RABBIT_URI || 'amqp://localhost:5672',
Expand Down
22 changes: 13 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@

const mongoose = require('mongoose'),
config = require('./config'),
blockModel = require('./models/blockModel'),
Promise = require('bluebird');

mongoose.Promise = Promise;
mongoose.connect(config.mongo.data.uri, {useMongoClient: true});
mongoose.accounts = mongoose.createConnection(config.mongo.accounts.uri, {useMongoClient: true});

const blockModel = require('./models/blockModel'),
_ = require('lodash'),
bunyan = require('bunyan'),
Web3 = require('web3'),
web3Errors = require('web3/lib/web3/errors'),
net = require('net'),
amqp = require('amqplib'),
Promise = require('bluebird'),
log = bunyan.createLogger({name: 'app'}),
filterTxsByAccountService = require('./services/filterTxsByAccountService'),
blockProcessService = require('./services/blockProcessService');

mongoose.Promise = Promise;
mongoose.connect(config.mongo.uri, {useMongoClient: true});

mongoose.connection.on('disconnected', function () {
log.error('mongo disconnected!');
process.exit(0);
});
[mongoose.accounts, mongoose.connection].forEach(connection =>
connection.on('disconnected', function () {
log.error('mongo disconnected!');
process.exit(0);
})
);

const init = async () => {

Expand Down
7 changes: 4 additions & 3 deletions models/accountModel.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/**
/**
* Mongoose model. Accounts
* @module models/accountModel
* @returns {Object} Mongoose model
* @requires factory/addressMessageFactory
* @requires factories/addressMessageFactory
*/

const mongoose = require('mongoose'),
config = require('../config'),
messages = require('../factories/messages/addressMessageFactory');

require('mongoose-long')(mongoose);
Expand All @@ -22,4 +23,4 @@ const Account = new mongoose.Schema({
erc20token : {type: mongoose.Schema.Types.Mixed, default: {}}
});

module.exports = mongoose.model('EthAccount', Account);
module.exports = mongoose.accounts.model(`${config.mongo.accounts.collectionPrefix}Account`, Account);
7 changes: 4 additions & 3 deletions models/blockModel.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/**
/**
* Mongoose model. Represents a block in eth
* @module models/blockModel
* @returns {Object} Mongoose model
*/

const mongoose = require('mongoose');
const mongoose = require('mongoose'),
config = require('../config');

const Block = new mongoose.Schema({
block: {type: Number},
network: {type: String},
created: {type: Date, required: true, default: Date.now}
});

module.exports = mongoose.model('EthBlock', Block);
module.exports = mongoose.model(`${config.mongo.data.collectionPrefix}Block`, Block);
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"eslint-plugin-chronobank": "github:chronobank/eslint-plugin-chronobank",
"ethereumjs-testrpc": "^6.0.3",
"mocha": "^3.5.0",
"sockjs-client": "^1.1.4",
"webstomp-client": "^1.0.6"
"webstomp-client": "^1.0.6",
"ws": "^3.3.3"
}
}
21 changes: 11 additions & 10 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
require('dotenv/config');

const config = require('../config'),
awaitLastBlock = require('./helpers/awaitLastBlock'),
Promise = require('bluebird'),
mongoose = require('mongoose');

mongoose.Promise = Promise;
mongoose.accounts = mongoose.createConnection(config.mongo.accounts.uri);
mongoose.connect(config.mongo.data.uri, {useMongoClient: true});

const awaitLastBlock = require('./helpers/awaitLastBlock'),
net = require('net'),
WebSocket = require('ws'),
path = require('path'),
Web3 = require('web3'),
web3 = new Web3(),
mongoose = require('mongoose'),
expect = require('chai').expect,
SockJS = require('sockjs-client'),
Promise = require('bluebird'),
accountModel = require('../models/accountModel'),
amqp = require('amqplib'),
Stomp = require('webstomp-client'),
Expand All @@ -20,8 +25,6 @@ describe('core/block processor', function () {
before(async () => {
let provider = new Web3.providers.IpcProvider(config.web3.uri, net);
web3.setProvider(provider);
mongoose.Promise = Promise;
mongoose.connect(config.mongo.uri, {useMongoClient: true});

return await awaitLastBlock(web3);
});
Expand All @@ -35,9 +38,7 @@ describe('core/block processor', function () {
let accounts = await Promise.promisify(web3.eth.getAccounts)();
try {
await new accountModel({address: accounts[0]}).save();
} catch (e) {
console.log(e);
}
} catch (e) {}
});

it('send some eth from 0 account to account 1', async () => {
Expand Down Expand Up @@ -85,7 +86,7 @@ describe('core/block processor', function () {
})
})(),
(async () => {
let ws = new SockJS('http://localhost:15674/stomp');
let ws = new WebSocket('ws://localhost:15674/ws');
let client = Stomp.over(ws, {heartbeat: false, debug: false});
return await new Promise(res =>
client.connect('guest', 'guest', () => {
Expand Down

0 comments on commit be68d2b

Please sign in to comment.