Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Closes #119. Adding snapshot functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Beddows committed Aug 1, 2016
1 parent 1196a9a commit 7b1c46e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ program
.option("-b, --blockchain <path>", "blockchain db path")
.option("-x, --peers [peers...]", "peers list")
.option("-l, --log <level>", "log level")
.option("-s, --snapshot <round>", "verify snapshot")
.parse(process.argv);

if (program.config) {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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);
});
Expand Down
3 changes: 2 additions & 1 deletion modules/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions modules/round.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -140,6 +141,10 @@ function RoundPromiser (scope, t) {
return t;
});
}

this.truncate = function () {
return t.none(sql.truncate, { height: scope.block.height });
}
}

// Public methods
Expand Down Expand Up @@ -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();
});
}
});
}
});
Expand Down
2 changes: 2 additions & 0 deletions sql/round.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down

0 comments on commit 7b1c46e

Please sign in to comment.