Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge branch '0.9.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
isabello committed Jul 5, 2017
2 parents 007ee1a + f68be1d commit f4cf8fd
Show file tree
Hide file tree
Showing 25 changed files with 2,262 additions and 275 deletions.
6 changes: 3 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ module.exports = function (grunt) {
},

coverage: {
command: 'node_modules/.bin/istanbul cover --dir test/.coverage-unit ./node_modules/.bin/_mocha',
command: 'export NODE_ENV=TEST && node_modules/.bin/istanbul cover --dir test/.coverage-unit ./node_modules/.bin/_mocha',
maxBuffer: maxBufferSize
},

coverageSingle: {
command: 'node_modules/.bin/istanbul cover --dir test/.coverage-unit ./node_modules/.bin/_mocha $TEST',
command: 'export NODE_ENV=TEST && node_modules/.bin/istanbul cover --dir test/.coverage-unit ./node_modules/.bin/_mocha $TEST',
maxBuffer: maxBufferSize
},

Expand Down Expand Up @@ -130,7 +130,7 @@ module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-compress');
grunt.loadNpmTasks('grunt-eslint');

grunt.registerTask('default', ['release']);
grunt.registerTask('default', ['release']);
grunt.registerTask('release', ['exec:folder', 'obfuscator', 'exec:package', 'exec:build', 'compress']);
grunt.registerTask('jenkins', ['exec:coverageSingle']);
grunt.registerTask('eslint-nofix', ['eslint']);
Expand Down
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ d.run(function () {

ready: ['modules', 'bus', 'logic', function (scope, cb) {
scope.bus.message('bind', scope.modules);
scope.logic.transaction.bindModules(scope.modules.rounds);
scope.logic.peers.bind(scope);
scope.logic.transaction.bindModules(scope.modules);
scope.logic.peers.bindModules(scope.modules);
cb();
}],

Expand Down
5 changes: 3 additions & 2 deletions helpers/jobsQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ var jobsQueue = {
}

var nextJob = function () {
setImmediate(job);
jobsQueue.jobs[name] = setTimeout(nextJob, time);
return job(function () {
jobsQueue.jobs[name] = setTimeout(nextJob, time);
});
};

nextJob();
Expand Down
3 changes: 2 additions & 1 deletion logic/broadcaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ function Broadcaster (broadcasts, force, peers, transaction, logger) {
}];

// Broadcaster timer
function nextRelease () {
function nextRelease (cb) {
__private.releaseQueue(function (err) {
if (err) {
library.logger.log('Broadcaster timer', err);
}
return setImmediate(cb);
});
}

Expand Down
15 changes: 9 additions & 6 deletions logic/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ Peer.prototype.nullable = [
'updated'
];

Peer.STATE = {
BANNED: 0,
DISCONNECTED: 1,
CONNECTED: 2
};

// Public methods
/**
* Checks peer properties and adjusts according rules.
Expand Down Expand Up @@ -118,10 +124,7 @@ Peer.prototype.normalize = function (peer) {
}

peer.port = this.parseInt(peer.port, 0);

if (!/^[0-2]{1}$/.test(peer.state)) {
peer.state = 1;
}
peer.state = this.parseInt(peer.state, Peer.STATE.DISCONNECTED);

return peer;
};
Expand Down Expand Up @@ -161,8 +164,8 @@ Peer.prototype.update = function (peer) {

// Accept only supported properties
_.each(this.properties, function (key) {
// Change value only when is defined, also prevent release ban when banned peer connect to our node
if (peer[key] !== null && peer[key] !== undefined && !(key === 'state' && this.state === 0 && peer.state === 2) && !_.includes(this.immutable, key)) {
// Change value only when is defined
if (peer[key] !== null && peer[key] !== undefined && !_.includes(this.immutable, key)) {
this[key] = peer[key];
}
}.bind(this));
Expand Down
59 changes: 14 additions & 45 deletions logic/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var schema = require('../schema/peers.js');
var __private = {};
var self;
var library;

var modules;
/**
* Initializes library.
* @memberof module:peers
Expand All @@ -22,7 +22,7 @@ var library;
// Constructor
function Peers (logger, cb) {
library = {
logger: logger,
logger: logger
};
self = this;
__private.peers = {};
Expand Down Expand Up @@ -75,11 +75,13 @@ Peers.prototype.get = function (peer) {
Peers.prototype.upsert = function (peer, insertOnly) {
// Insert new peer
var insert = function (peer) {
peer.updated = Date.now();
__private.peers[peer.string] = peer;

library.logger.debug('Inserted new peer', peer.string);
library.logger.trace('Inserted new peer', {peer: peer});
if (!_.isEmpty(modules.peers.acceptable([peer]))) {
peer.updated = Date.now();
__private.peers[peer.string] = peer;
library.logger.debug('Inserted new peer', peer.string);
} else {
library.logger.debug('Rejecting unacceptable peer', peer.string);
}
};

// Update existing peer
Expand Down Expand Up @@ -145,42 +147,6 @@ Peers.prototype.upsert = function (peer, insertOnly) {
return true;
};

/**
* Upserts peer with banned state `0` and clock with current time + seconds.
* @param {string} pip - Peer ip
* @param {number} port
* @param {number} seconds
* @return {function} Calls upsert
*/
Peers.prototype.ban = function (ip, port, seconds) {
return self.upsert({
ip: ip,
port: port,
// State 0 for banned peer
state: 0,
clock: Date.now() + (seconds || 1) * 1000
});
};

/**
* Upserts peer with unbanned state `1` and deletes clock.
* @param {string} pip - Peer ip
* @param {number} port
* @param {number} seconds
* @return {peer}
*/
Peers.prototype.unban = function (peer) {
peer = self.get(peer);
if (peer) {
delete peer.clock;
peer.state = 1;
library.logger.debug('Released ban for peer', peer.string);
} else {
library.logger.debug('Failed to release ban for peer', {err: 'INVALID', peer: peer});
}
return peer;
};

/**
* Deletes peer from peers list.
* @param {peer} peer
Expand Down Expand Up @@ -217,9 +183,12 @@ Peers.prototype.list = function (normalize) {
// Public methods
/**
* Modules are not required in this file.
* @param {modules} scope - Loaded modules.
* @param {Object} __modules - Peers module.
*/
Peers.prototype.bind = function (scope) {
Peers.prototype.bindModules = function (__modules) {
modules = {
peers: __modules.peers
};
};

// Export
Expand Down
8 changes: 4 additions & 4 deletions logic/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ Transaction.prototype.undo = function (trs, block, sender, cb) {
__private.types[trs.type].undo.call(this, trs, block, sender, function (err) {
if (err) {
this.scope.account.merge(sender.address, {
balance: amount,
balance: -amount,
blockId: block.id,
round: modules.rounds.calc(block.height)
}, function (err) {
Expand Down Expand Up @@ -1133,12 +1133,12 @@ Transaction.prototype.dbRead = function (raw) {
// Events
/**
* Binds input parameters to private variables modules.
* @param {Rounds} rounds
* @param {Object} __modules
*/
Transaction.prototype.bindModules = function (rounds) {
Transaction.prototype.bindModules = function (__modules) {
this.scope.logger.trace('Logic/Transaction->bindModules');
modules = {
rounds: rounds,
rounds: __modules.rounds
};
};

Expand Down
6 changes: 4 additions & 2 deletions logic/transactionPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,24 @@ function TransactionPool (broadcastInterval, releaseLimit, transaction, bus, log
self.processed = 0;

// Bundled transaction timer
function nextBundle () {
function nextBundle (cb) {
self.processBundled(function (err) {
if (err) {
library.logger.log('Bundled transaction timer', err);
}
return setImmediate(cb);
});
}

jobsQueue.register('transactionPoolNextBundle', nextBundle, self.bundledInterval);

// Transaction expiry timer
function nextExpiry () {
function nextExpiry (cb) {
self.expireTransactions(function (err) {
if (err) {
library.logger.log('Transaction expiry timer', err);
}
return setImmediate(cb);
});
}

Expand Down
6 changes: 4 additions & 2 deletions modules/delegates.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,15 +548,17 @@ Delegates.prototype.onBlockchainReady = function () {

__private.loadDelegates(function (err) {

function nextForge () {
function nextForge (cb) {
if (err) {
library.logger.error('Failed to load delegates', err);
}

async.series([
__private.forge,
modules.transactions.fillPool
]);
], function () {
return setImmediate(cb);
});
}

jobsQueue.register('delegatesNextForge', nextForge, 1000);
Expand Down
19 changes: 12 additions & 7 deletions modules/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,21 @@ __private.syncTrigger = function (turnOn) {
__private.syncTimer = function () {
library.logger.trace('Setting sync timer');

function nextSync () {
function nextSync (cb) {
library.logger.trace('Sync timer trigger', {loaded: __private.loaded, syncing: self.syncing(), last_receipt: modules.blocks.lastReceipt.get()});

if (__private.loaded && !self.syncing() && modules.blocks.lastReceipt.isStale()) {
library.sequence.add(function (cb) {
async.retry(__private.retries, __private.sync, cb);
library.sequence.add(function (sequenceCb) {
async.retry(__private.retries, __private.sync, sequenceCb);
}, function (err) {
if (err) {
library.logger.error('Sync timer', err);
__private.initialize();
}
return setImmediate(cb);
});
} else {
return setImmediate(cb);
}
}

Expand Down Expand Up @@ -198,15 +201,15 @@ __private.loadSignatures = function (cb) {

/**
* Gets a random peer and loads transactions calling the api.
* Validates each transaction from peer and bans peer if invalid.
* Validates each transaction from peer and remove peer if invalid.
* Calls processUnconfirmedTransaction for each transaction.
* @private
* @implements {Loader.getNetwork}
* @implements {modules.transport.getFromPeer}
* @implements {library.schema.validate}
* @implements {async.eachSeries}
* @implements {library.logic.transaction.objectNormalize}
* @implements {modules.peers.ban}
* @implements {modules.peers.remove}
* @implements {library.balancesSequence.add}
* @implements {modules.transactions.processUnconfirmedTransaction}
* @param {function} cb
Expand Down Expand Up @@ -254,8 +257,8 @@ __private.loadTransactions = function (cb) {
} catch (e) {
library.logger.debug('Transaction normalization failed', {id: id, err: e.toString(), module: 'loader', tx: transaction});

library.logger.warn(['Transaction', id, 'is not valid, ban 10 min'].join(' '), peer.string);
modules.peers.ban(peer.ip, peer.port, 600);
library.logger.warn(['Transaction', id, 'is not valid, peer removed'].join(' '), peer.string);
modules.peers.remove(peer.ip, peer.port);

return setImmediate(eachSeriesCb, e);
}
Expand Down Expand Up @@ -423,6 +426,8 @@ __private.loadBlockChain = function () {
if ((count === 1) || (count % constants.activeDelegates > 0)) {
library.config.loading.snapshot = (round > 1) ? (round - 1) : 1;
}

modules.rounds.setSnapshotRounds(library.config.loading.snapshot);
}

library.logger.info('Snapshotting to end of round: ' + library.config.loading.snapshot);
Expand Down
Loading

0 comments on commit f4cf8fd

Please sign in to comment.