Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fix committed Mar 9, 2017
1 parent 5e01863 commit 63c57c3
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 42 deletions.
22 changes: 10 additions & 12 deletions logic/peer.js
Expand Up @@ -9,7 +9,6 @@ _.extend(__schemas, require('../schema/api.public.js'));
// Private fields
var modules, library;


var __headers;

//
Expand Down Expand Up @@ -116,24 +115,23 @@ Peer.prototype.fetchHeight = function(cb){

Peer.prototype.fetchStatus = function(cb){
var that = this;
this.get('/peer/status', function(err, res){
this.request('/peer/status', {method:'GET', timeout: 2000}, function(err, res){
if(!err){
that.height = res.body.height;
that.blockheader = res.body.header;
that.forgingAllowed = res.body.forgingAllowed;
that.currentSlot = res.body.currentSlot;
var verification = {
verified: false
};
var check = {verified: false};
try {
verification = modules.blocks.verifyBlockHeader(res.body.header);
check = modules.blocks.verifyBlockHeader(res.body.header);
} catch (e) {
verification.errors = [e];
check.errors = [e];
}
if(!verification.verified){
if(!check.verified){
that.status="FORK";
library.logger.trace(that + " sent header", res.body.header);
return cb('Received invalid block header from peer!', res);
library.logger.debug(that + " header errors", check.errors);
return cb && cb('Received invalid block header from peer '+that, res);
}
else {
that.status = "OK";
Expand All @@ -148,16 +146,16 @@ Peer.prototype.fetchPeers = function(cb){
}

Peer.prototype.postTransactions = function(transactions, cb){
this.post('/peer/transactions',{transactions: transactions}, cb);
this.post('/peer/transactions', {transactions: transactions}, cb);
}

Peer.prototype.getTransactionFromIds = function(transactionIds, cb){
this.get('/peer/transactionsFromIds?ids='+transactionIds.join(","), cb);s
this.get('/peer/transactionsFromIds?ids='+transactionIds.join(","), cb);
}

Peer.prototype.accept = function(){
this.lastchecked=new Date().getTime();
return true;
return this;
};

Peer.prototype.get = function(api, cb){
Expand Down
3 changes: 1 addition & 2 deletions modules/blocks.js
Expand Up @@ -1413,8 +1413,7 @@ Blocks.prototype.simpleDeleteAfterBlock = function (blockId, cb) {
Blocks.prototype.loadBlocksFromPeer = function (peer, cb) {
var lastValidBlock = modules.blockchain.getLastBlock();

peer = modules.peers.inspect(peer);
library.logger.info('Loading blocks from: ' + peer.string);
library.logger.info('Loading blocks from: ' + peer);

// we increase timeout as it can be a big payload
modules.transport.requestFromPeer(peer, {
Expand Down
2 changes: 1 addition & 1 deletion modules/delegates.js
Expand Up @@ -313,7 +313,7 @@ __private.forge = function (cb) {

return cb();
}
// PBFT: most nodes are on same branch, no other block have been forged
// PBFT: most nodes are on same branch, no other block have been forged and we are on forgeable currentSlot
if(quorum/(quorum+noquorum) > 0.66){
letsforge = true;
}
Expand Down
2 changes: 0 additions & 2 deletions modules/loader.js
Expand Up @@ -123,8 +123,6 @@ __private.loadUnconfirmedTransactions = function (cb) {
return cb("Transactions list is not conform");
}

var peer = modules.peers.inspect(res.peer);

var transactions = res.body.transactions;

library.bus.message("transactionsReceived", transactions, "network", cb);
Expand Down
30 changes: 7 additions & 23 deletions modules/transport.js
Expand Up @@ -46,7 +46,7 @@ __private.attachApi = function () {
var router = new Router();

router.use(function (req, res, next) {
if (modules) { return next(); }
if(modules) { return next(); }
res.status(500).send({success: false, error: 'Blockchain is loading'});
});

Expand All @@ -59,47 +59,31 @@ __private.attachApi = function () {
}
);
} catch (e) {
// Remove peer
__private.removePeer({peer: req.peer, code: 'EHEADERS', req: req});

library.logger.debug(e.toString());
return res.status(406).send({success: false, error: 'Invalid request headers'});
return res.status(500).send({success: false, error: 'Invalid request headers'});
}

var headers = req.headers;
headers.ip = req.peer.ip;
headers.port = req.peer.port;

req.sanitize(headers, schema.headers, function (err, report) {
if (err) { return next(err); }
if (!report.isValid) {
// Remove peer
__private.removePeer({peer: req.peer, code: 'EHEADERS', req: req});

if(err) { return next(err); }
if(!report.isValid) {
return res.status(500).send({status: false, error: report.issues});
}

if (headers.nethash !== library.config.nethash) {
// Remove peer
if(headers.nethash !== library.config.nethash) {
__private.removePeer({peer: req.peer, code: 'ENETHASH', req: req});

return res.status(200).send({success: false, message: 'Request is made on the wrong network', expected: library.config.nethash, received: headers.nethash});
return res.status(500).send({success: false, message: 'Request is made on the wrong network', expected: library.config.nethash, received: headers.nethash});
}

req.peer.os = headers.os;
req.peer.version = headers.version;

// if ((req.peer.version === library.config.version) && (headers.nethash === library.config.nethash)) {
// if (!modules.blocks.lastReceipt()) {
// modules.delegates.enableForging();
// }
// }

modules.peers.accept(req.peer);

return next();
});

});

router.get('/list', function (req, res) {
Expand Down Expand Up @@ -344,7 +328,7 @@ __private.attachApi = function () {
router.get('/status', function (req, res) {
res.set(__private.headers);
var block = modules.blockchain.getLastBlock();
var blockheader={
var blockheader = {
id: block.id,
height: block.height,
version: block.version,
Expand Down
12 changes: 11 additions & 1 deletion schema/api.peer.js
Expand Up @@ -48,7 +48,17 @@ module.exports = {
},
'GET:/peer/transactions':{
id: 'GET:/peer/transactions',
type: 'object'
type: 'object',
properties: {
success: {
type: 'boolean'
},
transactions: {
type: 'array',
uniqueItems: true
}
},
required: ['transactions']
},
'GET:/peer/transactionsFromIds':{
id: 'POST:/peer/transactionsFromIds',
Expand Down
2 changes: 1 addition & 1 deletion views/example.pug
Expand Up @@ -62,7 +62,7 @@ html(lang='en')
h3 Congratulations you just installed ark-node sucessfully
ul
li nethash
b #{nethash}
pre #{nethash}
.row
.col-md-6
.well(style='opacity:0.8;')
Expand Down

0 comments on commit 63c57c3

Please sign in to comment.