Commit f1ab528f authored by Cédric Moreau's avatar Cédric Moreau

Huge refactoring: ucoin node as a stream (beginning)

parent dacbd5e1
var async = require('async');
var mongoose = require('mongoose');
var _ = require('underscore');
var Amendment = mongoose.model('Amendment');
var Merkle = mongoose.model('Merkle');
var service = require('../service');
var logger = require('../lib/logger')();
// Services
var ParametersService = service.Parameters;
var MerkleService = service.Merkle;
var VoteService = service.Vote;
var StrategyService = service.Strategy;
var PeeringService = service.Peering;
var SyncService = service.Sync;
var ContractService = service.Contract;
module.exports = function (hdcServer) {
return new AmendmentBinding(hdcServer);
};
module.exports = function (pgp, currency, conf) {
function AmendmentBinding (hdcServer) {
// Services
var ParametersService = hdcServer.ParametersService;
var MerkleService = hdcServer.MerkleService;
var VoteService = hdcServer.VoteService;
var StrategyService = hdcServer.StrategyService;
var PeeringService = hdcServer.PeeringService;
var SyncService = hdcServer.SyncService;
var ContractService = hdcServer.ContractService;
// Models
var Amendment = hdcServer.conn.model('Amendment');
var Merkle = hdcServer.conn.model('Merkle');
this.promoted = function (req, res) {
showAmendment(res, ContractService.current());
......@@ -133,8 +137,6 @@ module.exports = function (pgp, currency, conf) {
});
}
}
return this;
}
function amendmentMerkle (req, res, merkleSource, merkleMap) {
......
var jpgp = require('../lib/jpgp');
var async = require('async');
var mongoose = require('mongoose');
var _ = require('underscore');
var Coin = mongoose.model('Coin');
var Transaction = mongoose.model('Transaction');
var service = require('../service');
var logger = require('../lib/logger')();
// Services
var ParametersService = service.Parameters;
module.exports = function (hdcServer) {
return new CoinBinding(hdcServer);
};
module.exports = function (pgp, currency, conf) {
function CoinBinding(hdcServer) {
// Services
var ParametersService = hdcServer.ParametersService;
// Models
var Coin = hdcServer.conn.model('Coin');
var Transaction = hdcServer.conn.model('Transaction');
this.list = function (req, res) {
......
var jpgp = require('../lib/jpgp');
var async = require('async');
var mongoose = require('mongoose');
var _ = require('underscore');
var Amendment = mongoose.model('Amendment');
var PublicKey = mongoose.model('PublicKey');
var Merkle = mongoose.model('Merkle');
module.exports = function (pgp, currency, conf) {
module.exports = function (hdcServer) {
return new HDCBinding(hdcServer);
}
function HDCBinding (hdcServer) {
this.amendments = require('./amendments')(pgp, currency, conf);
this.transactions = require('./transactions')(pgp, currency, conf);
this.coins = require('./coins')(pgp, currency, conf);
return this;
this.amendments = require('./amendments')(hdcServer);
this.transactions = require('./transactions')(hdcServer);
this.coins = require('./coins')(hdcServer);
}
var jpgp = require('../lib/jpgp');
var async = require('async');
var vucoin = require('vucoin');
var mongoose = require('mongoose');
var Peer = mongoose.model('Peer');
var Forward = mongoose.model('Forward');
var Amendment = mongoose.model('Amendment');
var PublicKey = mongoose.model('PublicKey');
var Merkle = mongoose.model('Merkle');
var Wallet = mongoose.model('Wallet');
var Key = mongoose.model('Key');
var _ = require('underscore');
var openpgp = require('openpgp');
var logger = require('../lib/logger');
......@@ -16,119 +8,49 @@ var plogger = logger('peering');
var flogger = logger('forward');
var slogger = logger('status');
var tlogger = logger('wallet');
var service = require('../service');
// Services
var http = service.HTTP;
var MerkleService = service.Merkle;
var ParametersService = service.Parameters;
var WalletService = service.Wallet;
var PeeringService = service.Peering;
module.exports = function (peerServer, conf) {
return new NetworkBinding(peerServer, conf);
};
module.exports = function (pgp, currency, conf) {
function NetworkBinding (peerServer, conf) {
var privateKey = openpgp.key.readArmored(conf.pgpkey).keys[0];
this.ascciiPubkey = privateKey ? privateKey.toPublic().armor() : "";
this.cert = this.ascciiPubkey ? jpgp().certificate(this.ascciiPubkey) : { fingerprint: '' };
// Services
var http = peerServer.HTTPService;
var MerkleService = peerServer.MerkleService;
var ParametersService = peerServer.ParametersService;
var WalletService = peerServer.WalletService;
var PeeringService = peerServer.PeeringService;
// Models
var Peer = peerServer.conn.model('Peer');
var Forward = peerServer.conn.model('Forward');
var Amendment = peerServer.conn.model('Amendment');
var PublicKey = peerServer.conn.model('PublicKey');
var Merkle = peerServer.conn.model('Merkle');
var Wallet = peerServer.conn.model('Wallet');
var Key = peerServer.conn.model('Key');
this.cert = PeeringService.cert;
var that = this;
this.pubkey = function (req, res) {
res.send(200, this.ascciiPubkey);
res.send(200, PeeringService.ascciiPubkey);
},
this.forward = function (req, res) {
var errCode = 400;
async.waterfall([
// Parameters
function(callback){
if(!(req.body && req.body.forward && req.body.signature)){
callback('Requires a peering forward + signature');
return;
}
callback(null, req.body.forward, req.body.signature);
},
// Check signature's key ID
function(pr, sig, callback){
PublicKey.getFromSignature(sig, function (err, pubkey) {
callback(null, new Forward(), pr + sig, pubkey);
});
function(next){
ParametersService.getForward(req, next);
},
// Verify signature
function(fwd, signedPR, pubkey, callback){
async.waterfall([
function (next){
fwd.parse(signedPR, next);
},
function (fwd, next){
fwd.verify(currency, next);
},
function(valid, next){
if(!valid){
next('Not a valid peering request');
return;
}
flogger.debug('⬇ %s type %s', fwd.from, fwd.forward);
next();
},
function (next) {
Peer.find({ fingerprint: fwd.from }, next);
},
function (peers, next) {
if(peers.length == 0){
errCode = 404;
next('Peer ' + fwd.from + ' not found, POST at ucg/peering/peers first');
return;
}
next();
},
function (next){
if(!pubkey){
next('Public key not found, POST at ucg/peering/peers to make the node retrieve it');
return;
}
next();
},
function (next){
if(!fwd.to.match(new RegExp("^" + cert.fingerprint + "$", "g"))){
next('Node\'s fingerprint ('+cert.fingerprint+') is not concerned by this forwarding (' + fwd.to + ')');
return;
}
if(!fwd.from.match(new RegExp("^" + pubkey.fingerprint + "$", "g"))){
next('Forwarder\'s fingerprint ('+fwd.from+') does not match signatory (' + pubkey.fingerprint + ')');
return;
}
fwd.verifySignature(pubkey.raw, next);
},
function (verified, next){
if(!verified){
next('Signature does not match');
return;
}
next();
},
function (next){
Forward.find({ from: fwd.from, to: this.cert.fingerprint }, next);
},
function (fwds, next){
var fwdEntity = fwd;
if(fwds.length > 0){
// Already existing fwd
fwdEntity = fwds[0];
fwd.copyValues(fwdEntity);
}
fwdEntity.save(function (err) {
next(err, fwdEntity);
});
}
], callback);
function(fwd, next){
flogger.debug('⬇ %s type %s', fwd.from, fwd.forward);
PeeringService.submitForward(fwd, next);
}
], function (err, recordedFWD) {
http.answer(res, errCode, err, function () {
http.answer(res, 400, err, function () {
flogger.debug('✔ %s type %s', recordedFWD.from, recordedFWD.forward);
res.end(JSON.stringify(recordedFWD.json(), null, " "));
});
......@@ -355,8 +277,8 @@ module.exports = function (pgp, currency, conf) {
function (callback) {
ParametersService.getStatus(req, callback);
},
function(signedStatus, callback){
PeeringService.submitStatus(signedStatus, callback);
function(statusObj, callback){
PeeringService.submitStatus(statusObj, callback);
}
], function (err, status, peer, wasStatus) {
http.answer(res, 400, err, function () {
......@@ -365,6 +287,4 @@ module.exports = function (pgp, currency, conf) {
})
});
}
return this;
}
var fs = require('fs');
var util = require('util');
var async = require('async');
var mongoose = require('mongoose');
var PublicKey = mongoose.model('PublicKey');
var Merkle = mongoose.model('Merkle');
var _ = require('underscore');
var stream = require('stream');
var logger = require('../lib/logger')();
var service = require('../service');
// Services
var MerkleService = service.Merkle;
var ParametersService = service.Parameters;
var PeeringService = service.Peering;
var PublicKeyService = service.PublicKey;
var http = service.HTTP;
module.exports = function (pksServer) {
return new PKSBinding(pksServer);
}
module.exports = function (pgp, currency, conf) {
function PKSBinding (pksServer) {
var conn = pksServer.conn;
var MerkleService = pksServer.MerkleService;
var ParametersService = pksServer.ParametersService;
var PeeringService = pksServer.PeeringService;
var PublicKeyService = pksServer.PublicKeyService;
var http = pksServer.HTTPService;
var PublicKey = conn.model('PublicKey');
var Merkle = conn.model('Merkle');
this.getAll = function (req, res) {
async.waterfall([
......@@ -84,6 +88,4 @@ module.exports = function (pgp, currency, conf) {
});
});
};
return this;
};
var jpgp = require('../lib/jpgp');
var async = require('async');
var vucoin = require('vucoin');
var mongoose = require('mongoose');
var Peer = mongoose.model('Peer');
var Forward = mongoose.model('Forward');
var Amendment = mongoose.model('Amendment');
var Membership = mongoose.model('Membership');
var Voting = mongoose.model('Voting');
var PublicKey = mongoose.model('PublicKey');
var Merkle = mongoose.model('Merkle');
var Key = mongoose.model('Key');
var _ = require('underscore');
var logger = require('../lib/logger')();
var mlogger = require('../lib/logger')('membership');
var vlogger = require('../lib/logger')('voting');
var service = require('../service');
// Services
var http = service.HTTP;
var MerkleService = service.Merkle;
var ParametersService = service.Parameters;
var PeeringService = service.Peering;
var SyncService = service.Sync;
var ContractService = service.Contract;
module.exports = function (registryServer, conf) {
return new RegistryBinding(registryServer, conf);
};
module.exports = function (pgp, currency, conf) {
function RegistryBinding (registryServer, conf) {
var that = this;
// Services
var http = registryServer.HTTPService;
var MerkleService = registryServer.MerkleService;
var ParametersService = registryServer.ParametersService;
var PeeringService = registryServer.PeeringService;
var SyncService = registryServer.SyncService;
var ContractService = registryServer.ContractService;
// Models
var Peer = registryServer.conn.model('Peer');
var Forward = registryServer.conn.model('Forward');
var Amendment = registryServer.conn.model('Amendment');
var Membership = registryServer.conn.model('Membership');
var Voting = registryServer.conn.model('Voting');
var PublicKey = registryServer.conn.model('PublicKey');
var Merkle = registryServer.conn.model('Merkle');
var Key = registryServer.conn.model('Key');
this.parameters = function (req, res) {
res.end(JSON.stringify({
......@@ -42,7 +48,7 @@ module.exports = function (pgp, currency, conf) {
this.amendmentCurrent = function (req, res) {
var am = ContractService.proposed();
req.params.am_number = ((am && am.number) || 0).toString();
this.amendmentNext(req, res);
that.amendmentNext(req, res);
};
this.amendmentNext = function (req, res) {
......@@ -288,6 +294,4 @@ module.exports = function (pgp, currency, conf) {
});
});
};
return this;
}
var jpgp = require('../lib/jpgp');
var async = require('async');
var mongoose = require('mongoose');
var _ = require('underscore');
var Amendment = mongoose.model('Amendment');
var PublicKey = mongoose.model('PublicKey');
var Merkle = mongoose.model('Merkle');
var Key = mongoose.model('Key');
var Transaction = mongoose.model('Transaction');
var service = require('../service');
var logger = require('../lib/logger')('transaction');
// Services
var MerkleService = service.Merkle;
var ParametersService = service.Parameters;
var TransactionService = service.Transactions;
var PeeringService = service.Peering;
module.exports = function (hdcServer) {
return new TransactionBinding(hdcServer);
};
module.exports = function (pgp, currency, conf) {
function TransactionBinding(hdcServer) {
// Services
var MerkleService = hdcServer.MerkleService;
var ParametersService = hdcServer.ParametersService;
var TransactionService = hdcServer.TransactionsService;
var PeeringService = hdcServer.PeeringService;
// Models
var Amendment = hdcServer.conn.model('Amendment');
var PublicKey = hdcServer.conn.model('PublicKey');
var Merkle = hdcServer.conn.model('Merkle');
var Key = hdcServer.conn.model('Key');
var Transaction = hdcServer.conn.model('Transaction');
this.viewtx = function (req, res) {
async.waterfall([
......
......@@ -9,19 +9,16 @@ var Amendment = mongoose.model('Amendment');
var Key = mongoose.model('Key');
var connectPgp = require('connect-pgp');
var _ = require('underscore');
var service = require('../service');
var jpgp = require('./jpgp');
var sha1 = require('sha1');
var vucoin = require('vucoin');
var logger = require('./logger')('daemon');
module.exports = new Daemon();
module.exports = function (PeeringService, ContractService) {
return new Daemon(PeeringService, ContractService);
};
function Daemon () {
// Services
var PeeringService = service.Peering;
var ContractService = service.Contract;
function Daemon (PeeringService, ContractService) {
// self reference, private scope
var daemon = this;
......
......@@ -53,10 +53,6 @@ function JPGP() {
});
}
this.signsDetached = function (message, privateKey, done) {
require('./server').sign(message, done);
}
this.issuer = function() {
var issuer = "";
try{
......
This diff is collapsed.
var async = require('async');
var mongoose = require('mongoose');
var _ = require('underscore');
var sha1 = require('sha1');
var merkle = require('merkle');
var Amendment = mongoose.model('Amendment');
var PublicKey = mongoose.model('PublicKey');
var Merkle = mongoose.model('Merkle');
var Key = mongoose.model('Key');
var Membership = mongoose.model('Membership');
var Voting = mongoose.model('Voting');
var Transaction = mongoose.model('Transaction');
var Wallet = mongoose.model('Wallet');
var Peer = mongoose.model('Peer');
var vucoin = require('vucoin');
var jpgp = require('./jpgp');
var logger = require('./logger')('sync');
var service = require('../service');
var CONST_FORCE_TX_PROCESSING = false;
// Services
var KeyService = null;
var VoteService = null;
var TransactionService = null;
var WalletService = null;
var PeeringService = null;
var StrategyService = null;
var ParametersService = null;
var SyncService = null;
module.exports = function Synchroniser (server, host, port, authenticated, conf) {
var that = this;
// Services
var KeyService = null;
var VoteService = null;
var TransactionService = null;
var WalletService = null;
var PeeringService = null;
var StrategyService = null;
var ParametersService = null;
var SyncService = null;
// Models
var Amendment = server.conn.model('Amendment');
var PublicKey = server.conn.model('PublicKey');
var Merkle = server.conn.model('Merkle');
var Key = server.conn.model('Key');
var Membership = server.conn.model('Membership');
var Voting = server.conn.model('Voting');
var Transaction = server.conn.model('Transaction');
var Wallet = server.conn.model('Wallet');
var Peer = server.conn.model('Peer');
this.remoteFingerprint = null;
......@@ -87,34 +87,20 @@ module.exports = function Synchroniser (server, host, port, authenticated, conf)
peer.verify(peer.currency, next);
},
function (verified, next) {
server.services.init(peer.currency, conf, true, next);
},
function (next){
KeyService = service.Key;
VoteService = service.Vote;
TransactionService = service.Transactions;
WalletService = service.Wallet;
PeeringService = service.Peering;
StrategyService = service.Strategy;
ParametersService = service.Parameters;
SyncService = service.Sync;
Peer.find({ fingerprint: remotePeer.fingerprint, hash: sha1(signedPR).toUpperCase() }, next);
},
function (peers, next){
if(peers.length > 0){
next('Peer already saved', peers[0]);
return;
}
next();
server.initServer(next);
},
function (next){
PeeringService.submit(signedPR, keyID, next);
KeyService = server.KeyService;
VoteService = server.VoteService;
TransactionService = server.TransactionsService;
WalletService = server.WalletService;
PeeringService = server.PeeringService;
StrategyService = server.StrategyService;
ParametersService = server.ParametersService;
SyncService = server.SyncService;
PeeringService.submit(peer, keyID, next);
},
], function (err, peer) {
if(err && !peer){
next(err);
return;
}
], function (err) {
next(null, peer);
});
},
......@@ -150,7 +136,9 @@ module.exports = function Synchroniser (server, host, port, authenticated, conf)
},
function (json, cb){
hashes.push(leaf);
PublicKey.persistFromRaw(json.leaf.value.pubkey, cb);
PublicKey.persistFromRaw(json.leaf.value.pubkey, function (err) {
cb();
});
},
function (next) {
KeyService.handleKey(leaf, conf && conf.kmanagement == 'ALL', next);
......@@ -183,7 +171,7 @@ module.exports = function Synchroniser (server, host, port, authenticated, conf)
}
remoteCurrentNumber = parseInt(json.number);
amendments[remoteCurrentNumber] = json.raw;
var toGetNumbers = _.range(number, remoteCurrentNumber + 1);
var toGetNumbers = _.range(number, 22 + 1);
async.forEachSeries(toGetNumbers, function(amNumber, callback){
async.waterfall([
function (cb){
......@@ -227,9 +215,9 @@ module.exports = function Synchroniser (server, host, port, authenticated, conf)
}, next);
},
//==================
// Trust Hash Table
//==================