Files
thehub/unspentdb/PruneCommand.cpp
T

116 lines
4.1 KiB
C++
Raw Permalink Normal View History

2018-09-08 11:40:29 +02:00
/*
* This file is part of the Flowee project
2020-03-01 23:13:48 +01:00
* Copyright (C) 2018-2020 Tom Zander <tomz@freedommail.ch>
2018-09-08 11:40:29 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "PruneCommand.h"
2018-09-16 20:02:16 +02:00
#include <utxo/Pruner_p.h>
2018-09-08 11:40:29 +02:00
#include <utxo/UnspentOutputDatabase_p.h>
#include <QFileInfo>
2018-09-08 22:50:32 +02:00
#include <QDir>
2018-09-08 18:57:41 +02:00
#include <boost/filesystem.hpp>
static void nothing(const char *){}
2018-09-08 11:40:29 +02:00
PruneCommand::PruneCommand()
: m_force(QStringList() << "force", "Force pruning")
{
}
QString PruneCommand::commandDescription() const
{
return "Prune\nTakes the selected database file and prunes already spent outputs";
}
void PruneCommand::addArguments(QCommandLineParser &parser)
{
parser.addOption(m_force);
}
Flowee::ReturnCodes PruneCommand::run()
{
2020-03-01 23:13:48 +01:00
DatabaseFile infoFile = dbDataFiles().first();
if (dbDataFiles().size() > 1) {
err << "Wholesale pruning is not yet possible" << endl;
return Flowee::InvalidOptions;
}
else if (infoFile.filetype() == InfoFile) {
2018-09-08 11:40:29 +02:00
if (!commandLineParser().isSet(m_force)) {
err << "You selected a specific info file instead of a database\n"
"this risks you might not use the latest version.\n\n"
2020-03-01 23:13:48 +01:00
"Select db file instead or pass --force if you don't mind losing data" << endl;
2018-09-08 11:40:29 +02:00
return Flowee::NeedForce;
}
}
2020-03-01 23:13:48 +01:00
else if (infoFile.filetype() == Datadir) {
2018-09-08 11:40:29 +02:00
err << "Whole datadir pruning is not yet possible" << endl;
return Flowee::InvalidOptions;
}
else {
// we need to find the info file with the highest blockheight;
// the cache takes a filename like the database, but without extensions
2020-03-01 23:13:48 +01:00
QString path = infoFile.filepath();
2018-09-08 11:40:29 +02:00
if (path.endsWith(".db"))
path = path.mid(0, path.length() - 3);
DataFileCache cache(path.toStdString());
int highest = 0, index = -1;
for (DataFileCache::InfoFile info : cache.m_validInfoFiles) {
if (info.lastBlockHeight > highest) {
highest = info.lastBlockHeight;
index = info.index;
}
}
2020-03-01 23:13:48 +01:00
for (auto info : infoFile.infoFiles()) {
2018-09-08 11:40:29 +02:00
if (info.index() == index) {
infoFile = info;
break;
}
}
}
QFileInfo info(infoFile.filepath());
if (!info.exists()) {
err << "Failed to find an appropriate info file" << endl;
return Flowee::InvalidOptions;
}
QFileInfo dbInfo(infoFile.databaseFiles().first().filepath());
out << "Operating on " << dbInfo.fileName() << " and snapshot file " << info.fileName() << endl;
2018-09-16 20:02:16 +02:00
try {
Pruner pruner(dbInfo.absoluteFilePath().toStdString(), info.absoluteFilePath().toStdString());
pruner.prune();
2018-09-08 22:50:32 +02:00
out << "Finishing up" << endl;
// backup original files.
QFile::rename(dbInfo.absoluteFilePath(), dbInfo.absoluteFilePath() + "~");
QFile::rename(info.absoluteFilePath(), info.absoluteFilePath() + "~");
2018-09-08 22:50:32 +02:00
// remove all old info files (they can no longer work) and rename the db file
// and the info file over the original ones
DataFileCache cache(dbInfo.dir().filePath(dbInfo.baseName()).toStdString());
for (int i = 0; i < 10; ++i)
boost::filesystem::remove(cache.filenameFor(i));
2018-09-16 20:02:16 +02:00
pruner.commit();
fflush(nullptr);
2018-09-08 22:50:32 +02:00
out << "Done" << endl;
return Flowee::Ok;
2018-09-16 20:02:16 +02:00
} catch (const std::runtime_error &ex) {
2019-06-19 16:53:12 +02:00
err << ex.what() << endl;
2018-09-16 20:02:16 +02:00
return Flowee::CommandFailed;
2018-09-08 22:50:32 +02:00
}
}