2018-09-08 11:40:29 +02:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
2021-02-28 22:16:05 +01:00
|
|
|
* Copyright (C) 2018-2021 Tom Zander <tom@flowee.org>
|
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>
|
|
|
|
|
|
2018-09-08 11:40:29 +02:00
|
|
|
PruneCommand::PruneCommand()
|
2021-02-28 22:16:05 +01:00
|
|
|
: m_force(QStringList() << "force", "Force pruning"),
|
|
|
|
|
m_backup(QStringList() << "keep" << "k", "Keep a backup file")
|
2018-09-08 11:40:29 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2021-02-28 22:16:05 +01:00
|
|
|
parser.addOption(m_backup);
|
2018-09-08 11:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-28 22:16:05 +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;
|
|
|
|
|
}
|
2021-02-28 22:16:05 +01:00
|
|
|
QFileInfo dbInfo(infoFile.databaseFiles().at(0).filepath());
|
2018-09-08 11:40:29 +02:00
|
|
|
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;
|
2018-09-20 22:59:52 +02:00
|
|
|
// 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());
|
2021-02-28 22:16:05 +01:00
|
|
|
for (int i = 0; i < 20; ++i)
|
2018-09-08 22:50:32 +02:00
|
|
|
boost::filesystem::remove(cache.filenameFor(i));
|
|
|
|
|
|
2018-09-16 20:02:16 +02:00
|
|
|
pruner.commit();
|
2018-09-09 13:03:43 +02:00
|
|
|
fflush(nullptr);
|
2021-02-28 22:16:05 +01:00
|
|
|
|
|
|
|
|
if (!commandLineParser().isSet(m_backup)) {
|
|
|
|
|
QFile::remove(dbInfo.absoluteFilePath() + "~");
|
|
|
|
|
QFile::remove(info.absoluteFilePath() + "~");
|
|
|
|
|
}
|
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
|
|
|
}
|
2018-09-09 13:03:43 +02:00
|
|
|
}
|