Skip to content
Snippets Groups Projects
Commit 4beae427 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Fix #230 peers were stored in files which lead to JSON parse errors

parent c1672945
No related branches found
No related tags found
No related merge requests found
......@@ -98,7 +98,7 @@ function FileDAL(profile, home, localDir, myFS, parentFileDAL, dalName, loki) {
// DALs
this.confDAL = new ConfDAL(rootPath, myFS, parentFileDAL && parentFileDAL.confDAL.coreFS, that, CFSStorage);
this.peerDAL = new PeerDAL(rootPath, myFS, parentFileDAL && parentFileDAL.peerDAL.coreFS, that, CFSStorage);
this.peerDAL = new PeerDAL(loki);
this.blockDAL = new BlockDAL(loki, blocksCFS, getLowerWindowBlock);
this.sourcesDAL = new SourcesDAL(loki);
this.txsDAL = new TxsDAL(loki);
......@@ -1193,7 +1193,12 @@ function FileDAL(profile, home, localDir, myFS, parentFileDAL, dalName, loki) {
this.resetPeers = function(done) {
var files = [];
var dirs = ['peers'];
return resetFiles(files, dirs, done);
return co(function *() {
that.peerDAL.lokiRemoveAll();
yield resetFiles(files, dirs);
})
.then(() => done && done())
.catch((err) => done && done(err));
};
this.resetTransactions = function(done) {
......
......@@ -68,6 +68,9 @@ function AbstractLoki(collection) {
return false;
};
this.lokiRemoveAll = () =>
collection.removeDataOnly();
this.lokiRemoveWhere = (conditions) =>
collection.removeWhere(conditions);
......
......@@ -2,19 +2,41 @@
* Created by cgeek on 22/08/15.
*/
var Q = require('q');
var AbstractLoki = require('./AbstractLoki');
module.exports = PeerDAL;
function PeerDAL(rootPath, qioFS, parentCore, localDAL, AbstractStorage) {
function PeerDAL(loki) {
"use strict";
AbstractStorage.call(this, rootPath, qioFS, parentCore, localDAL);
let collection = loki.getCollection('peers') || loki.addCollection('peers', { indices: ['pubkey', 'status'] });
AbstractLoki.call(this, collection);
this.idKeys = ['pubkey'];
this.propsToSave = [
'version',
'currency',
'status',
'statusTS',
'hash',
'first_down',
'last_try',
'pub',
'pubkey',
'block',
'signature',
'endpoints',
'raw'
];
this.init = () => this.coreFS.makeTree('peers/');
this.init = () => null;
this.listAll = () => this.coreFS.listJSON('peers/');
this.listAll = () => Q(collection.find());
this.getPeer = (pubkey) => this.coreFS.readJSON('peers/' + pubkey + '.json');
this.getPeer = (pubkey) => Q(collection.find({ pubkey: pubkey })[0]);
this.savePeer = (peer) => this.coreFS.writeJSON('peers/' + peer.pubkey + '.json', peer);
this.savePeer = (peer) => this.lokiSave(peer);
}
......@@ -372,7 +372,8 @@ program
program
.command('reset [config|data|peers|tx|stats|all]')
.description('Reset configuration, data, peers, transactions or everything in the database')
.action(connect(function (type, server) {
.action((type) => {
connect(function (server) {
if(!~['config', 'data', 'peers', 'tx', 'stats', 'all'].indexOf(type)){
logger.error('Bad command: usage `reset config`, `reset data`, `reset peers`, `reset tx`, `reset stats` or `reset all`');
server.disconnect();
......@@ -396,7 +397,8 @@ program
if(type == 'all'){
server.reset(resetDone(server, 'Data & Configuration successfully reseted.'));
}
}, true));
}, type != 'peers')(type);
});
function resetDone(server, msg) {
return function(err) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment