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

[enh] `export-bc` modularized

parent ff35b718
Branches
Tags
No related merge requests found
......@@ -218,47 +218,6 @@ module.exports = () => {
});
})));
program
.command('export-bc [upto]')
.description('Exports the whole blockchain as JSON array, up to [upto] block number (excluded).')
.action(subCommand(service(function (upto, server) {
return co(function *() {
try {
let CHUNK_SIZE = 500;
let jsoned = [];
let current = yield server.dal.getCurrentBlockOrNull();
let lastNumber = current ? current.number + 1 : -1;
if (upto !== undefined && upto.match(/\d+/)) {
lastNumber = Math.min(parseInt(upto), lastNumber);
}
let chunksCount = Math.floor(lastNumber / CHUNK_SIZE);
let chunks = [];
// Max-size chunks
for (let i = 0, len = chunksCount; i < len; i++) {
chunks.push({start: i * CHUNK_SIZE, to: i * CHUNK_SIZE + CHUNK_SIZE - 1});
}
// A last chunk
if (lastNumber > chunksCount * CHUNK_SIZE) {
chunks.push({start: chunksCount * CHUNK_SIZE, to: lastNumber});
}
for (const chunk of chunks) {
let blocks = yield server.dal.getBlocksBetween(chunk.start, chunk.to);
blocks.forEach(function (block) {
jsoned.push(_(new Block(block).json()).omit('raw'));
});
}
if (!program.nostdout) {
console.log(JSON.stringify(jsoned, null, " "));
}
yield server.disconnect();
return jsoned;
} catch(err) {
logger.warn(err.message || err);
yield server.disconnect();
}
});
}, NO_LOGS)));
program
.on('*', function (cmd) {
console.log("Unknown command '%s'. Try --help for a listing of commands & options.", cmd);
......
"use strict";
const co = require('co');
const _ = require('underscore');
const Block = require('../lib/entity/block');
module.exports = {
duniter: {
cli: [{
name: 'export-bc [upto]',
desc: 'Exports the whole blockchain as JSON array, up to [upto] block number (excluded).',
logs: false,
onPluggedDALExecute: (server, conf, program, params) => co(function*() {
const upto = params[0];
const logger = server.logger;
try {
let CHUNK_SIZE = 500;
let jsoned = [];
let current = yield server.dal.getCurrentBlockOrNull();
let lastNumber = current ? current.number + 1 : -1;
if (upto !== undefined && upto.match(/\d+/)) {
lastNumber = Math.min(parseInt(upto), lastNumber);
}
let chunksCount = Math.floor(lastNumber / CHUNK_SIZE);
let chunks = [];
// Max-size chunks
for (let i = 0, len = chunksCount; i < len; i++) {
chunks.push({start: i * CHUNK_SIZE, to: i * CHUNK_SIZE + CHUNK_SIZE - 1});
}
// A last chunk
if (lastNumber > chunksCount * CHUNK_SIZE) {
chunks.push({start: chunksCount * CHUNK_SIZE, to: lastNumber});
}
for (const chunk of chunks) {
let blocks = yield server.dal.getBlocksBetween(chunk.start, chunk.to);
blocks.forEach(function (block) {
jsoned.push(_(new Block(block).json()).omit('raw'));
});
}
if (!program.nostdout) {
console.log(JSON.stringify(jsoned, null, " "));
}
yield server.disconnect();
return jsoned;
} catch(err) {
logger.warn(err.message || err);
yield server.disconnect();
}
})
}]
}
}
......@@ -18,6 +18,7 @@ const genDependency = require('./app/modules/gen');
const syncDependency = require('./app/modules/synchronization');
const resetDependency = require('./app/modules/reset');
const checkConfDependency = require('./app/modules/check-config');
const exportBcDependency = require('./app/modules/export-bc');
const MINIMAL_DEPENDENCIES = [
{ name: 'duniter-config', required: configDependency }
......@@ -30,6 +31,7 @@ const DEFAULT_DEPENDENCIES = [
{ name: 'duniter-gen', required: genDependency },
{ name: 'duniter-reset', required: resetDependency },
{ name: 'duniter-chkconf', required: checkConfDependency },
{ name: 'duniter-exportbc', required: exportBcDependency },
{ name: 'duniter-keypair', required: dkeypairDependency }
];
......@@ -166,6 +168,10 @@ function Stack(dependencies) {
const dbHome = program.home;
const home = directory.getHome(dbName, dbHome);
if (command.logs === false) {
logger.mute();
}
// Add log files for this instance
logger.addHomeLogs(home);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment