From 7b1c46e0f8aa7468fe8538ee489790dce96d2b4b Mon Sep 17 00:00:00 2001 From: Oliver Beddows Date: Mon, 1 Aug 2016 11:17:35 +0200 Subject: [PATCH] Closes #119. Adding snapshot functionality. --- app.js | 16 ++++++++++++++++ modules/loader.js | 3 ++- modules/round.js | 10 ++++++++++ sql/round.js | 2 ++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 6e70fdff945..f8d64f88afb 100644 --- a/app.js +++ b/app.js @@ -31,6 +31,7 @@ program .option("-b, --blockchain ", "blockchain db path") .option("-x, --peers [peers...]", "peers list") .option("-l, --log ", "log level") + .option("-s, --snapshot ", "verify snapshot") .parse(process.argv); if (program.config) { @@ -63,6 +64,16 @@ if (program.log) { appConfig.consoleLogLevel = program.log; } +if (program.snapshot) { + appConfig.loading.snapshot = Math.abs( + Math.floor(program.snapshot) + ); +} + +if (appConfig.loading.snapshot != null) { + appConfig.loading.verifyOnLoading = true; +} + var config = { "db": appConfig.db, "modules": { @@ -113,6 +124,11 @@ d.run(function () { charset: "alphanumeric" }); + if (appConfig.loading.snapshot != null) { + appConfig.loading.verifyOnLoading = false; + delete appConfig.loading.snapshot; + } + fs.writeFile("./config.json", JSON.stringify(appConfig, null, 4), "utf8", function (err) { cb(err, appConfig); }); diff --git a/modules/loader.js b/modules/loader.js index 99757c03a72..cbfaad839b6 100644 --- a/modules/loader.js +++ b/modules/loader.js @@ -23,9 +23,10 @@ private.syncIntervalId = null; // Constructor function Loader(cb, scope) { library = scope; - private.genesisBlock = private.loadingLastBlock = library.genesisblock; self = this; self.__private = private; + self.__private.genesisBlock = self.__private.loadingLastBlock = library.genesisblock; + self.__private.snapshot = library.config.loading.snapshot; private.attachApi(); setImmediate(cb, null, self); diff --git a/modules/round.js b/modules/round.js index 2ec57077c4c..e8607d30df9 100644 --- a/modules/round.js +++ b/modules/round.js @@ -24,6 +24,7 @@ function Round(cb, scope) { library = scope; self = this; self.__private = private; + self.__private.snapshot = library.config.loading.snapshot; setImmediate(cb, null, self); } @@ -140,6 +141,10 @@ function RoundPromiser (scope, t) { return t; }); } + + this.truncate = function () { + return t.none(sql.truncate, { height: scope.block.height }); + } } // Public methods @@ -273,6 +278,11 @@ Round.prototype.tick = function (block, done) { delete private.rewardsByRound[round]; delete private.delegatesByRound[round]; library.bus.message("finishRound", round); + if (private.snapshot == round) { + promised.truncate().then(function () { + process.exit(); + }); + } }); } }); diff --git a/sql/round.js b/sql/round.js index babd2b1b205..3fe04ba72ad 100644 --- a/sql/round.js +++ b/sql/round.js @@ -1,6 +1,8 @@ const RoundSql = { flush: 'DELETE FROM mem_round WHERE "round" = (${round})::bigint;', + truncate: 'DELETE FROM blocks WHERE "height" > (${height})::bigint;', + updateMissedBlocks: 'UPDATE mem_accounts SET "missedblocks" = "missedblocks" + 1 WHERE "address" IN ($1:csv);', getVotes: 'SELECT d."delegate", d."amount" FROM (SELECT m."delegate", SUM(m."amount") AS "amount", "round" FROM mem_round m GROUP BY m."delegate", m."round") AS d WHERE "round" = (${round})::bigint',