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

Rewrite peers module - Closes #409 #452

Merged
merged 40 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
901a28d
Retrieve git commit info only once on app start
4miners Feb 5, 2017
b2efb03
Improved documentation
4miners Feb 5, 2017
4c6b835
Fix unhandled promise rejection
4miners Feb 6, 2017
7468622
Move peers management to memory instead of database
4miners Feb 6, 2017
c1373db
Save peers to db on node shutdown and load them on node start
4miners Feb 9, 2017
2f028a5
Move peers management to memory instead of database
4miners Feb 6, 2017
46c86c9
Save peers to db on node shutdown and load them on node start
4miners Feb 9, 2017
dd4725b
Merge branch '409_rewrite-peers-module' of https://github.com/4miners…
4miners Feb 9, 2017
d2a98e6
Start sync when peers ready, improve logging
4miners Feb 7, 2017
41d1ca1
Move peers management to memory instead of database
4miners Feb 6, 2017
31edbb4
Save peers to db on node shutdown and load them on node start
4miners Feb 9, 2017
22c6baa
Merge remote-tracking branch 'origin/409_rewrite-peers-module' into 4…
MaciejBaj Feb 9, 2017
854aeca
unit tests of peers module
MaciejBaj Feb 9, 2017
ccd36c9
add extra peers unit tests, resolve transaction collision tests deter…
MaciejBaj Feb 13, 2017
fcf2a60
load all modules for unit tests, sync peers loader unit tests
MaciejBaj Feb 15, 2017
1ec011b
load all modules for unit tests, sync peers loader unit tests
MaciejBaj Feb 15, 2017
277bf6e
Improved peers logic
4miners Feb 17, 2017
f41df57
Merge with development
4miners Feb 17, 2017
ff68191
Merge branch 'development' of https://github.com/LiskHQ/lisk into 409…
MaciejBaj Feb 17, 2017
27f79cb
Merge branch '409_rewrite-peers-module' of https://github.com/4miners…
MaciejBaj Feb 17, 2017
f9cca3b
peers modules tests rewrite
MaciejBaj Feb 20, 2017
64d69fb
peer logic unit tests
MaciejBaj Feb 20, 2017
a0def7a
unit tests for logic/peers
MaciejBaj Feb 20, 2017
f80b540
unit tests for logic/peers
MaciejBaj Feb 20, 2017
04eb2db
Merge remote-tracking branch 'origin/409_rewrite-peers-module' into 4…
MaciejBaj Feb 21, 2017
0bb62c4
ban unban remove tests for peers logic
MaciejBaj Feb 21, 2017
a3c6175
ban unban remove tests for peers logic
MaciejBaj Feb 21, 2017
a8142ba
add extra tests for peer and peers logic
MaciejBaj Feb 23, 2017
2e825c7
Merge branch '4miners/409_rewrite-peers-module'
Feb 27, 2017
9e20493
Removing --coverage option
Feb 27, 2017
8b4abed
Removing jshint overrides
Feb 27, 2017
541efa9
Standardising code
Feb 27, 2017
6683425
Reviewing example descriptions
Feb 27, 2017
fc04439
Sorting requirements
Feb 27, 2017
c10792f
Trimming space
Feb 27, 2017
16893c3
Removing mocha options
Feb 27, 2017
ffe05b0
Ignoring test/.coverage-unit
Feb 27, 2017
3e8eb41
remove node from unit tests, remove unused modules stubs file
MaciejBaj Feb 28, 2017
56759e9
Returning setImmediate
Feb 28, 2017
7f92df6
Removing empty lines
Feb 28, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ npm-debug.log
release
ssl/
stacktrace*
test/.coverage-unit
tmp
sftp-config.json
7 changes: 6 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ d.run(function () {
var Transaction = require('./logic/transaction.js');
var Block = require('./logic/block.js');
var Account = require('./logic/account.js');
var Peers = require('./logic/peers.js');

async.auto({
bus: function (cb) {
Expand Down Expand Up @@ -427,7 +428,10 @@ d.run(function () {
}],
block: ['db', 'bus', 'ed', 'schema', 'genesisblock', 'account', 'transaction', function (scope, cb) {
new Block(scope, cb);
}]
}],
peers: function (cb) {
new Peers(scope, cb);
}
}, cb);
}],

Expand Down Expand Up @@ -459,6 +463,7 @@ d.run(function () {
ready: ['modules', 'bus', 'logic', function (scope, cb) {
scope.bus.message('bind', scope.modules);
scope.logic.transaction.bindModules(scope.modules);
scope.logic.peers.bind(scope);
cb();
}]
}, function (err, scope) {
Expand Down
2 changes: 1 addition & 1 deletion logic/broadcaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Broadcaster.prototype.broadcast = function (params, options, cb) {
if (params.limit === self.config.peerLimit) { peers.splice(0, self.config.broadcastLimit); }

async.eachLimit(peers, self.config.parallelLimit, function (peer, eachLimitCb) {
peer = modules.peers.accept(peer);
peer = library.logic.peers.create(peer);

modules.transport.getFromPeer(peer, options, function (err) {
if (err) {
Expand Down
99 changes: 65 additions & 34 deletions logic/peer.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
'use strict';

var extend = require('extend');
var _ = require('lodash');
var ip = require('ip');

var self;

// Constructor
function Peer (peer) {
return this.accept(peer || {});
self = this;
return self.accept(peer || {});
}

// Public properties
Peer.prototype.properties = [
'ip',
'port',
'state',
'os',
'version',
'dappid',
'broadhash',
'height',
'clock',
'updated'
];

Peer.prototype.headers = [
'os',
'version',
'dappid',
Expand All @@ -25,7 +38,9 @@ Peer.prototype.nullable = [
'version',
'dappid',
'broadhash',
'height'
'height',
'clock',
'updated'
];

// Public methods
Expand All @@ -36,25 +51,35 @@ Peer.prototype.accept = function (peer) {
this.ip = peer.ip;
}

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

if (this.ip) {
this.string = (this.ip + ':' + this.port || 'unknown');
} else {
this.string = 'unknown';
if (this.ip && this.port) {
this.string = this.ip + ':' + this.port;
}

if (peer.state != null) {
if (/^[0-2]{1}$/.test(peer.state)) {
this.state = peer.state;
} else {
this.state = 1;
}

if (peer.dappid != null) {
this.dappid = peer.dappid;
if (peer.dappid) {
if (Array.isArray(peer.dappid)) {
this.dappid = peer.dappid;
} else {
this.dappid = [];
this.dappid.push(peer.dappid);
}
}

if (peer.height) {
this.height = self.parseInt(peer.height, 1);
}

if (peer.clock) {
this.clock = peer.clock;
}

this.headers(peer);
return this;
};

Expand All @@ -65,49 +90,55 @@ Peer.prototype.parseInt = function (integer, fallback) {
return integer;
};

Peer.prototype.headers = function (headers) {
Peer.prototype.applyHeaders = function (headers) {
headers = headers || {};

headers.os = headers.os || 'unknown';
headers.version = headers.version || '0.0.0';
headers.port = this.parseInt(headers.port, 0);
if (headers.height) {
headers.height = self.parseInt(headers.height, 1);
}

if (headers.height != null) {
headers.height = this.parseInt(headers.height, 1);
if (headers.port) {
headers.port = self.parseInt(headers.port, 0);
}

this.nullable.forEach(function (property) {
if (headers[property] != null) {
this[property] = headers[property];
} else {
delete headers[property];
_.each(headers, function (value, key) {
if (_.includes(this.headers, key)) {
this[key] = value;
}
}.bind(this));

return headers;
};

Peer.prototype.extend = function (object) {
var base = this.object();
var extended = extend(this.object(), object);
Peer.prototype.update = function (object) {
_.each(object, function (value, key) {
// Change value only when is defined, also prevent release ban when banned peer connect to our node
if (value !== null && value !== undefined && !(key === 'state' && this.state === 0 && object.state === 2)) {
this[key] = value;
}
}.bind(this));

return this.headers(extended);
return this;
};

Peer.prototype.object = function () {
var object = {};
var copy = {};

this.properties.forEach(function (property) {
object[property] = this[property];
_.each(self.properties, function (key) {
copy[key] = this[key];
}.bind(this));

this.nullable.forEach(function (property) {
if (object[property] == null) {
object[property] = null;
_.each(self.nullable, function (key) {
if (!copy[key]) {
copy[key] = null;
}
});

return object;
if (!/^[0-2]{1}$/.test(this.state)) {
copy.state = 1;
}

return copy;
};

// Export
Expand Down
102 changes: 0 additions & 102 deletions logic/peerSweeper.js

This file was deleted.

Loading