Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
Moved DatabasePaths namespace to class
Browse files Browse the repository at this point in the history
  • Loading branch information
halfalicious committed Dec 3, 2019
1 parent 428c447 commit 99a1213
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 96 deletions.
1 change: 1 addition & 0 deletions libethcore/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ DEV_SIMPLE_EXCEPTION(DatabaseAlreadyOpen);
DEV_SIMPLE_EXCEPTION(DatabaseCorruption);
DEV_SIMPLE_EXCEPTION(DatabaseExists);
DEV_SIMPLE_EXCEPTION(DatabaseRebuildFailed);
DEV_SIMPLE_EXCEPTION(DatabasePathsNotSet);

DEV_SIMPLE_EXCEPTION(DAGCreationFailure);
DEV_SIMPLE_EXCEPTION(DAGComputeFailure);
Expand Down
69 changes: 37 additions & 32 deletions libethereum/BlockChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ using namespace std;
using namespace dev;
using namespace dev::eth;
namespace fs = boost::filesystem;
namespace db_paths = dev::eth::database_paths;

#define ETH_TIMED_IMPORTS 1

Expand Down Expand Up @@ -200,17 +199,21 @@ bool BlockChain::open(fs::path const& _path, WithExisting _we)

if (db::isDiskDatabase())
{
db_paths::setDatabasePaths(_path, m_genesisHash);
if (!_path.empty())
m_dbPaths.setPaths(_path, m_genesisHash);
else if (!m_dbPaths.pathsSet())
BOOST_THROW_EXCEPTION(DatabasePathsNotSet());

if (_we == WithExisting::Kill)
{
LOG(m_loggerInfo)
<< "Deleting chain databases. This will require a resync from genesis.";
fs::remove_all(db_paths::blocksDatabasePath());
fs::remove_all(db_paths::extrasDatabasePath());
fs::remove_all(db_paths::extrasDatabaseTemporaryPath());
fs::remove_all(m_dbPaths.blocksPath());
fs::remove_all(m_dbPaths.extrasPath());
fs::remove_all(m_dbPaths.extrasTemporaryPath());
}

bytes const minorVersionBytes = contents(db_paths::extrasDatabaseMinorVersionPath());
bytes const minorVersionBytes = contents(m_dbPaths.extrasMinorVersionPath());
if (!minorVersionBytes.empty())
{
DEV_IGNORE_EXCEPTIONS(lastMinor = (unsigned)RLP(minorVersionBytes));
Expand All @@ -219,44 +222,44 @@ bool BlockChain::open(fs::path const& _path, WithExisting _we)
rebuildNeeded = true;
LOG(m_loggerInfo) << "Database minor version change detected, the extras and state "
"databases will be rebuilt.";
LOG(m_loggerInfo) << "Version from " << db_paths::extrasDatabaseMinorVersionPath()
LOG(m_loggerInfo) << "Version from " << m_dbPaths.extrasMinorVersionPath()
<< " (" << lastMinor << ") != Aleth's version ("
<< c_databaseMinorVersion << ")";
}
}
else if (fs::exists(db_paths::extrasDatabasePath()))
else if (fs::exists(m_dbPaths.extrasPath()))
{
LOG(m_loggerInfo) << "Database minor version file not found ("
<< db_paths::extrasDatabaseMinorVersionPath()
<< ") but extras database exists (" << db_paths::extrasDatabasePath()
<< m_dbPaths.extrasMinorVersionPath()
<< ") but extras database exists (" << m_dbPaths.extrasPath()
<< "), assuming extras database needs to be upgraded.";
rebuildNeeded = true;
}
else
{
// First launch with new database
LOG(m_loggerDetail) << "Creating database minor version file: "
<< db_paths::extrasDatabaseMinorVersionPath()
<< m_dbPaths.extrasMinorVersionPath()
<< " (minor version: " << c_databaseMinorVersion << ")";
writeFile(db_paths::extrasDatabaseMinorVersionPath(), rlp(c_databaseMinorVersion));
writeFile(m_dbPaths.extrasMinorVersionPath(), rlp(c_databaseMinorVersion));
}
}

try
{
m_blocksDB = db::DBFactory::create(db_paths::blocksDatabasePath());
m_extrasDB = db::DBFactory::create(db_paths::extrasDatabasePath());
m_blocksDB = db::DBFactory::create(m_dbPaths.blocksPath());
m_extrasDB = db::DBFactory::create(m_dbPaths.extrasPath());
}
catch (db::DatabaseError const& ex)
{
// Determine which database open call failed
auto const dbPath =
!m_blocksDB.get() ? db_paths::blocksDatabasePath() : db_paths::extrasDatabasePath();
!m_blocksDB.get() ? m_dbPaths.blocksPath() : m_dbPaths.extrasPath();
if (db::isDiskDatabase())
{
LOG(m_loggerError) << "Error occurred when opening database: " << dbPath;
db::DatabaseStatus const dbStatus = *boost::get_error_info<db::errinfo_dbStatusCode>(ex);
if (fs::space(db_paths::rootPath()).available < 1024)
if (fs::space(m_dbPaths.rootPath()).available < 1024)
{
LOG(m_loggerError)
<< "Not enough available space found on hard drive. Please free some up and "
Expand Down Expand Up @@ -312,14 +315,14 @@ bool BlockChain::open(fs::path const& _path, WithExisting _we)
void BlockChain::open(fs::path const& _path, WithExisting _we, ProgressCallback const& _pc)
{
if (open(_path, _we) || _we == WithExisting::Verify)
rebuild(_path, _pc);
rebuild(fs::path{}, _pc);
}

void BlockChain::reopen(ChainParams const& _p, WithExisting _we, ProgressCallback const& _pc)
{
close();
init(_p);
open(db_paths::rootPath(), _we, _pc);
open(fs::path{}, _we, _pc);
}

void BlockChain::close()
Expand Down Expand Up @@ -355,7 +358,10 @@ void BlockChain::rebuild(
return;
}

db_paths::setDatabasePaths(_path, m_genesisHash);
if (!_path.empty())
m_dbPaths.setPaths(_path, m_genesisHash);
else if (!m_dbPaths.pathsSet())
BOOST_THROW_EXCEPTION(DatabasePathsNotSet());

unsigned const originalNumber = m_lastBlockNumber;

Expand All @@ -366,25 +372,25 @@ void BlockChain::rebuild(

// Keep extras DB around, but under a temp name
m_extrasDB.reset();
LOG(m_loggerInfo) << "Renaming extras path " << db_paths::extrasDatabasePath() << " to "
<< db_paths::extrasDatabaseTemporaryPath();
if (fs::exists(db_paths::extrasDatabaseTemporaryPath()))
LOG(m_loggerInfo) << "Renaming extras path " << m_dbPaths.extrasPath() << " to "
<< m_dbPaths.extrasTemporaryPath();
if (fs::exists(m_dbPaths.extrasTemporaryPath()))
{
LOG(m_loggerError)
<< "Temporary extras path " << db_paths::extrasDatabaseTemporaryPath()
<< "Temporary extras path " << m_dbPaths.extrasTemporaryPath()
<< " already exists (this usually happens because an in-progress rebuild was "
"prematurely terminated).";
LOG(m_loggerError) << "Please re-run Aleth the --kill option to delete all databases. This "
"will remove all chain data and require you to resync from genesis.";
BOOST_THROW_EXCEPTION(DatabaseExists());
}
fs::rename(db_paths::extrasDatabasePath(), db_paths::extrasDatabaseTemporaryPath());
std::unique_ptr<db::DatabaseFace> oldExtrasDB(
db::DBFactory::create(db_paths::extrasDatabaseTemporaryPath()));
m_extrasDB = db::DBFactory::create(db_paths::extrasDatabasePath());
fs::rename(m_dbPaths.extrasPath(), m_dbPaths.extrasTemporaryPath());
std::unique_ptr<db::DatabaseFace> oldExtrasDB{
db::DBFactory::create(m_dbPaths.extrasTemporaryPath())};
m_extrasDB = db::DBFactory::create(m_dbPaths.extrasPath());

// Open a fresh state DB
Block s = genesisBlock(State::openDB(db_paths::rootPath(), m_genesisHash, WithExisting::Kill));
Block s = genesisBlock(State::openDB(m_dbPaths.rootPath(), m_genesisHash, WithExisting::Kill));

// Clear all memos ready for replay.
m_details.clear();
Expand Down Expand Up @@ -453,14 +459,13 @@ void BlockChain::rebuild(
if (_progress)
_progress(d, originalNumber);
}
LOG(m_loggerInfo) << "Removing old extras database: "
<< db_paths::extrasDatabaseTemporaryPath();
LOG(m_loggerInfo) << "Removing old extras database: " << m_dbPaths.extrasTemporaryPath();
oldExtrasDB.reset();
fs::remove_all(db_paths::extrasDatabaseTemporaryPath());
fs::remove_all(m_dbPaths.extrasTemporaryPath());
if (!rebuildFailed)
{
LOG(m_loggerInfo) << "Rebuild complete! Reimported " << originalNumber << " blocks!";
writeFile(db_paths::extrasDatabaseMinorVersionPath(), rlp(c_databaseMinorVersion));
writeFile(m_dbPaths.extrasMinorVersionPath(), rlp(c_databaseMinorVersion));
}
else
{
Expand Down
9 changes: 6 additions & 3 deletions libethereum/BlockChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@
#include "BlockDetails.h"
#include "BlockQueue.h"
#include "ChainParams.h"
#include "DatabasePaths.h"
#include "LastBlockHashesFace.h"
#include "State.h"
#include "Transaction.h"
#include "VerifiedBlock.h"
#include <libdevcore/db.h>
#include <libdevcore/Exceptions.h>
#include <libdevcore/Log.h>
#include <libdevcore/Guards.h>
#include <libdevcore/Log.h>
#include <libdevcore/db.h>
#include <libethcore/BlockHeader.h>
#include <libethcore/Common.h>
#include <libethcore/SealEngine.h>
#include <boost/filesystem/path.hpp>
#include <chrono>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <boost/filesystem/path.hpp>

namespace std
{
Expand Down Expand Up @@ -408,6 +409,8 @@ class BlockChain
mutable bytes m_genesisHeaderBytes; // mutable because they're effectively memos.
mutable h256 m_genesisHash; // mutable because they're effectively memos.

DatabasePaths m_dbPaths; // Paths for various databases (e.g. blocks, extras)

std::function<void(Exception&)> m_onBad; ///< Called if we have a block that doesn't verify.
std::function<void(BlockHeader const&)> m_onBlockImport; ///< Called if we have imported a new block into the db

Expand Down
68 changes: 30 additions & 38 deletions libethereum/DatabasePaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,71 @@

#include "DatabasePaths.h"
#include "libdevcore/CommonIO.h"
#include "libdevcore/DBFactory.h"
#include "libethcore/Common.h"

namespace dev
{
namespace eth
{
namespace database_paths
{
namespace fs = boost::filesystem;

fs::path g_rootPath;
fs::path g_chainPath;
fs::path g_blocksDbPath;
fs::path g_stateDbPath;
fs::path g_extrasDbPath;
fs::path g_extrasDbTempPath;
fs::path g_extrasDbMinorVersionPath;

bool databasePathsSet() noexcept
DatabasePaths::DatabasePaths(fs::path const& _rootPath, h256 const& _genesisHash) noexcept
{
return !g_rootPath.empty();
setPaths(_rootPath, _genesisHash);
}

void setDatabasePaths(fs::path const& _rootPath, h256 const& _genesisHash) noexcept
void DatabasePaths::setPaths(fs::path const& _rootPath, h256 const& _genesisHash) noexcept
{
// Allow empty hashes since they are required by tests
// Allow an empty root path and empty genesis hash since they are required by the tests
m_rootPath = _rootPath;
m_chainPath = m_rootPath / fs::path(toHex(_genesisHash.ref().cropped(0, 4)));
m_statePath = m_chainPath / fs::path("state");
m_blocksPath = m_statePath / fs::path("blocks");

g_rootPath = _rootPath.empty() ? db::databasePath() : _rootPath;
g_chainPath = g_rootPath / fs::path(toHex(_genesisHash.ref().cropped(0, 4)));
g_stateDbPath = g_chainPath / fs::path("state");
g_blocksDbPath = g_chainPath / fs::path("blocks");
auto const extrasRootPath = m_chainPath / fs::path(toString(c_databaseVersion));
m_extrasPath = extrasRootPath / fs::path("extras");
m_extrasTemporaryPath = extrasRootPath / fs::path("extras.old");
m_extrasMinorVersionPath = m_extrasPath / fs::path("minor");
}

auto const extrasRootPath = g_chainPath / fs::path(toString(c_databaseVersion));
g_extrasDbPath = extrasRootPath / fs::path("extras");
g_extrasDbTempPath = extrasRootPath / fs::path("extras.old");
g_extrasDbMinorVersionPath = g_extrasDbPath / fs::path("minor");
bool DatabasePaths::pathsSet() const noexcept
{
return !m_rootPath.empty();
}

fs::path const& rootPath() noexcept
fs::path const& DatabasePaths::rootPath() noexcept
{
return g_rootPath;
return m_rootPath;
}

fs::path const& chainPath() noexcept
fs::path const& DatabasePaths::chainPath() noexcept
{
return g_chainPath;
return m_chainPath;
}

fs::path const& stateDatabasePath() noexcept
fs::path const& DatabasePaths::statePath() noexcept
{
return g_stateDbPath;
return m_statePath;
}

fs::path const& blocksDatabasePath() noexcept
fs::path const& DatabasePaths::blocksPath() noexcept
{
return g_blocksDbPath;
return m_blocksPath;
}

fs::path const& extrasDatabasePath() noexcept
fs::path const& DatabasePaths::extrasPath() noexcept
{
return g_extrasDbPath;
return m_extrasPath;
}

fs::path const& extrasDatabaseTemporaryPath() noexcept
fs::path const& DatabasePaths::extrasTemporaryPath() noexcept
{
return g_extrasDbTempPath;
return m_extrasTemporaryPath;
}

fs::path const& extrasDatabaseMinorVersionPath() noexcept
fs::path const& DatabasePaths::extrasMinorVersionPath() noexcept
{
return g_extrasDbMinorVersionPath;
return m_extrasMinorVersionPath;
}
} // namespace database_paths
} // namespace eth
} // namespace dev
34 changes: 23 additions & 11 deletions libethereum/DatabasePaths.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,29 @@ namespace dev
{
namespace eth
{
namespace database_paths
class DatabasePaths
{
bool databasePathsSet() noexcept;
void setDatabasePaths(boost::filesystem::path const& _rootPath, h256 const& _genesisHash) noexcept;
boost::filesystem::path const& rootPath() noexcept;
boost::filesystem::path const& chainPath() noexcept;
boost::filesystem::path const& blocksDatabasePath() noexcept;
boost::filesystem::path const& stateDatabasePath() noexcept;
boost::filesystem::path const& extrasDatabasePath() noexcept;
boost::filesystem::path const& extrasDatabaseTemporaryPath() noexcept;
boost::filesystem::path const& extrasDatabaseMinorVersionPath() noexcept;
} // namespace database_paths
public:
DatabasePaths(boost::filesystem::path const& _rootPath, h256 const& _genesisHash) noexcept;
DatabasePaths() = default;
void setPaths(boost::filesystem::path const& _rootPath, h256 const& _genesisHash) noexcept;
bool pathsSet() const noexcept;
boost::filesystem::path const& rootPath() noexcept;
boost::filesystem::path const& chainPath() noexcept;
boost::filesystem::path const& blocksPath() noexcept;
boost::filesystem::path const& statePath() noexcept;
boost::filesystem::path const& extrasPath() noexcept;
boost::filesystem::path const& extrasTemporaryPath() noexcept;
boost::filesystem::path const& extrasMinorVersionPath() noexcept;

private:
boost::filesystem::path m_rootPath;
boost::filesystem::path m_chainPath;
boost::filesystem::path m_blocksPath;
boost::filesystem::path m_statePath;
boost::filesystem::path m_extrasPath;
boost::filesystem::path m_extrasTemporaryPath;
boost::filesystem::path m_extrasMinorVersionPath;
};
} // namespace eth
} // namespace dev
Loading

0 comments on commit 99a1213

Please sign in to comment.