Skip to content

Commit

Permalink
Fix #255 blockchain should be exportable
Browse files Browse the repository at this point in the history
  • Loading branch information
c-geek committed Dec 14, 2015
1 parent f8ac77b commit cf21ce1
Showing 1 changed file with 46 additions and 19 deletions.
65 changes: 46 additions & 19 deletions bin/ucoind
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ function keys (val) {
return val.split(',');
}

var INTERACTIVE_SYNC = true;
var ERASE_IF_ALREADY_RECORDED = true;
const INTERACTIVE_SYNC = true;
const ERASE_IF_ALREADY_RECORDED = true;
const NO_LOGS = true;

program
.version(pjson.version)
Expand Down Expand Up @@ -357,26 +358,44 @@ program
.command('export-bc [upto]')
.description('Exports the whole blockchain as JSON array, up to [upto] block number (excluded).')
.action(service(function (upto, server) {
async.waterfall([
function (next){
server.dal.getBlocksUntil(upto).then(_.partial(next, null)).catch(next);
},
function (blocks, next){
var jsoned = [];
return co(function *() {
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 });
}
// A last chunk
if (lastNumber > chunks * CHUNK_SIZE) {
chunks.push({ start: chunksCount * CHUNK_SIZE, to: lastNumber });
}
for (let j = 0, len = chunks.length; j < len; j++) {
let chunk = chunks[j];
let blocks = yield server.dal.getBlocksBetween(chunk.start, chunk.to);
blocks.forEach(function (block) {
jsoned.push(_(block.json()).omit('raw'));
jsoned.push(_(new Block(block).json()).omit('raw'));
});
console.log(JSON.stringify(jsoned, null, " "));
next();
}
], function (err) {
if (err) {
logger.error(err);
}
server.disconnect();
console.log(JSON.stringify(jsoned, null, " "));
yield server.disconnect();
process.exit();
});
}));
})
.catch(function(err) {
logger.warn(err.message || err);
server.disconnect()
.catch(() => null)
.then(function(){
process.exit();
});
});
}, NO_LOGS));

program
.command('check-config')
Expand Down Expand Up @@ -769,7 +788,15 @@ function connect(callback, forConf, useDefaultConf) {
};
}

function service(callback) {
function service(callback, nologs) {

if (nologs) {
// Disable logs
require('log4js').configure({
"appenders": [
]
});
}
return function () {
var cbArgs = arguments;
var dbName = program.mdb || "ucoin_default";
Expand Down

0 comments on commit cf21ce1

Please sign in to comment.