Skip to content

Commit

Permalink
Remove gArgs.GetArg calls added by bitcoin#25623
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanofsky committed Jul 20, 2022
1 parent 961c86a commit f103f4d
Show file tree
Hide file tree
Showing 27 changed files with 211 additions and 147 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Expand Up @@ -200,6 +200,7 @@ BITCOIN_CORE_H = \
node/chainstate.h \
node/coin.h \
node/context.h \
node/db_args.h \
node/mempool_persist_args.h \
node/miner.h \
node/minisketchwrapper.h \
Expand Down Expand Up @@ -382,6 +383,7 @@ libbitcoin_node_a_SOURCES = \
node/coin.cpp \
node/connection_types.cpp \
node/context.cpp \
node/db_args.cpp \
node/eviction.cpp \
node/interfaces.cpp \
node/mempool_persist_args.cpp \
Expand Down
5 changes: 3 additions & 2 deletions src/bitcoin-chainstate.cpp
Expand Up @@ -80,8 +80,9 @@ int main(int argc, char* argv[])
const ChainstateManager::Options chainman_opts{
.chainparams = chainparams,
.adjusted_time_callback = static_cast<int64_t (*)()>(GetTime),
.block_tree_db_opts = {
.db_path = gArgs.GetDataDirNet() / "blocks" / "index",
.datadir = abs_datadir,
.block_tree_db_params = {
.db_path = abs_datadir / "blocks" / "index",
.cache_size = 2 << 20,
},
};
Expand Down
32 changes: 16 additions & 16 deletions src/dbwrapper.cpp
Expand Up @@ -127,48 +127,48 @@ static leveldb::Options GetOptions(size_t nCacheSize)
return options;
}

CDBWrapper::CDBWrapper(const Options& opts)
: m_name{fs::PathToString(opts.db_path.stem())}
CDBWrapper::CDBWrapper(const Params& params)
: m_name{fs::PathToString(params.db_path.stem())}
{
penv = nullptr;
readoptions.verify_checksums = true;
iteroptions.verify_checksums = true;
iteroptions.fill_cache = false;
syncoptions.sync = true;
options = GetOptions(opts.cache_size);
options = GetOptions(params.cache_size);
options.create_if_missing = true;
if (opts.in_memory) {
if (params.in_memory) {
penv = leveldb::NewMemEnv(leveldb::Env::Default());
options.env = penv;
} else {
if (opts.wipe_existing) {
LogPrintf("Wiping LevelDB in %s\n", fs::PathToString(opts.db_path));
leveldb::Status result = leveldb::DestroyDB(fs::PathToString(opts.db_path), options);
if (params.wipe_existing) {
LogPrintf("Wiping LevelDB in %s\n", fs::PathToString(params.db_path));
leveldb::Status result = leveldb::DestroyDB(fs::PathToString(params.db_path), options);
dbwrapper_private::HandleError(result);
}
TryCreateDirectories(opts.db_path);
LogPrintf("Opening LevelDB in %s\n", fs::PathToString(opts.db_path));
TryCreateDirectories(params.db_path);
LogPrintf("Opening LevelDB in %s\n", fs::PathToString(params.db_path));
}
// PathToString() return value is safe to pass to leveldb open function,
// because on POSIX leveldb passes the byte string directly to ::open(), and
// on Windows it converts from UTF-8 to UTF-16 before calling ::CreateFileW
// (see env_posix.cc and env_windows.cc).
leveldb::Status status = leveldb::DB::Open(options, fs::PathToString(opts.db_path), &pdb);
leveldb::Status status = leveldb::DB::Open(options, fs::PathToString(params.db_path), &pdb);
dbwrapper_private::HandleError(status);
LogPrintf("Opened LevelDB successfully\n");

if (opts.do_compact) {
LogPrintf("Starting database compaction of %s\n", fs::PathToString(opts.db_path));
if (params.options.do_compact) {
LogPrintf("Starting database compaction of %s\n", fs::PathToString(params.db_path));
pdb->CompactRange(nullptr, nullptr);
LogPrintf("Finished database compaction of %s\n", fs::PathToString(opts.db_path));
LogPrintf("Finished database compaction of %s\n", fs::PathToString(params.db_path));
}

// The base-case obfuscation key, which is a noop.
obfuscate_key = std::vector<unsigned char>(OBFUSCATE_KEY_NUM_BYTES, '\000');

bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);

if (!key_exists && opts.obfuscate_data && IsEmpty()) {
if (!key_exists && params.obfuscate_data && IsEmpty()) {
// Initialize non-degenerate obfuscation if it won't upset
// existing, non-obfuscated data.
std::vector<unsigned char> new_key = CreateObfuscateKey();
Expand All @@ -177,10 +177,10 @@ CDBWrapper::CDBWrapper(const Options& opts)
Write(OBFUSCATE_KEY_KEY, new_key);
obfuscate_key = new_key;

LogPrintf("Wrote new obfuscate key for %s: %s\n", fs::PathToString(opts.db_path), HexStr(obfuscate_key));
LogPrintf("Wrote new obfuscate key for %s: %s\n", fs::PathToString(params.db_path), HexStr(obfuscate_key));
}

LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(opts.db_path), HexStr(obfuscate_key));
LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(params.db_path), HexStr(obfuscate_key));
}

CDBWrapper::~CDBWrapper()
Expand Down
12 changes: 9 additions & 3 deletions src/dbwrapper.h
Expand Up @@ -180,6 +180,11 @@ class CDBIterator
}
};

//! Optional settings to use loading the database.
struct DBWrapperOptions {
bool do_compact = false;
};

class CDBWrapper
{
friend const std::vector<unsigned char>& dbwrapper_private::GetObfuscateKey(const CDBWrapper &w);
Expand Down Expand Up @@ -221,16 +226,17 @@ class CDBWrapper

public:

struct Options {
//! Required parameters to use loading the database.
struct Params {
fs::path db_path;
size_t cache_size;
bool in_memory = false;
bool wipe_existing = false;
bool obfuscate_data = false;
bool do_compact = false;
DBWrapperOptions options;
};

CDBWrapper(const Options& opts);
CDBWrapper(const Params& params);
~CDBWrapper();

CDBWrapper(const CDBWrapper&) = delete;
Expand Down
10 changes: 0 additions & 10 deletions src/index/base.cpp
Expand Up @@ -44,16 +44,6 @@ CBlockLocator GetLocator(interfaces::Chain& chain, const uint256& block_hash)
return locator;
}

BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool f_memory, bool f_wipe, bool f_obfuscate)
: CDBWrapper{{
.db_path = path,
.cache_size = n_cache_size,
.in_memory = f_memory,
.wipe_existing = f_wipe,
.obfuscate_data = f_obfuscate,
.do_compact = gArgs.GetBoolArg("-forcecompactdb", false),
}} {}

bool BaseIndex::DB::ReadBestBlock(CBlockLocator& locator) const
{
bool success = Read(DB_BEST_BLOCK, locator);
Expand Down
24 changes: 22 additions & 2 deletions src/index/base.h
Expand Up @@ -17,6 +17,16 @@ namespace interfaces {
class Chain;
} // namespace interfaces

//! Parameters to use loading the database.
struct IndexParams {
std::unique_ptr<interfaces::Chain> chain;
fs::path base_path;
size_t cache_size = 0;
bool in_memory = false;
bool wipe_existing = false;
DBWrapperOptions db_options;
};

struct IndexSummary {
std::string name;
bool synced{false};
Expand All @@ -41,8 +51,7 @@ class BaseIndex : public CValidationInterface
class DB : public CDBWrapper
{
public:
DB(const fs::path& path, size_t n_cache_size,
bool f_memory = false, bool f_wipe = false, bool f_obfuscate = false);
using CDBWrapper::CDBWrapper;

/// Read block locator of the chain that the index is in sync with.
bool ReadBestBlock(CBlockLocator& locator) const;
Expand All @@ -51,6 +60,17 @@ class BaseIndex : public CValidationInterface
void WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator);
};

CDBWrapper::Params DBParams(const IndexParams& params, fs::path path)
{
return {
.db_path = std::move(path),
.cache_size = params.cache_size,
.in_memory = params.in_memory,
.wipe_existing = params.wipe_existing,
.options = params.db_options,
};
}

private:
/// Whether the index is in sync with the main chain. The flag is flipped
/// from false to true once, after which point this starts processing
Expand Down
15 changes: 6 additions & 9 deletions src/index/blockfilterindex.cpp
Expand Up @@ -95,18 +95,17 @@ struct DBHashKey {

static std::map<BlockFilterType, BlockFilterIndex> g_filter_indexes;

BlockFilterIndex::BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain, BlockFilterType filter_type,
size_t n_cache_size, bool f_memory, bool f_wipe)
: BaseIndex(std::move(chain)), m_filter_type(filter_type)
BlockFilterIndex::BlockFilterIndex(IndexParams&& params, BlockFilterType filter_type)
: BaseIndex(std::move(params.chain)), m_filter_type(filter_type)
{
const std::string& filter_name = BlockFilterTypeName(filter_type);
if (filter_name.empty()) throw std::invalid_argument("unknown filter_type");

fs::path path = gArgs.GetDataDirNet() / "indexes" / "blockfilter" / fs::u8path(filter_name);
fs::path path = std::move(params.base_path) / "blockfilter" / fs::u8path(filter_name);
fs::create_directories(path);

m_name = filter_name + " block filter index";
m_db = std::make_unique<BaseIndex::DB>(path / "db", n_cache_size, f_memory, f_wipe);
m_db = std::make_unique<BaseIndex::DB>(DBParams(params, path / "db"));
m_filter_fileseq = std::make_unique<FlatFileSeq>(std::move(path), "fltr", FLTR_FILE_CHUNK_SIZE);
}

Expand Down Expand Up @@ -469,13 +468,11 @@ void ForEachBlockFilterIndex(std::function<void (BlockFilterIndex&)> fn)
for (auto& entry : g_filter_indexes) fn(entry.second);
}

bool InitBlockFilterIndex(std::function<std::unique_ptr<interfaces::Chain>()> make_chain, BlockFilterType filter_type,
size_t n_cache_size, bool f_memory, bool f_wipe)
bool InitBlockFilterIndex(IndexParams&& params, BlockFilterType filter_type)
{
auto result = g_filter_indexes.emplace(std::piecewise_construct,
std::forward_as_tuple(filter_type),
std::forward_as_tuple(make_chain(), filter_type,
n_cache_size, f_memory, f_wipe));
std::forward_as_tuple(std::move(params), filter_type));
return result.second;
}

Expand Down
6 changes: 2 additions & 4 deletions src/index/blockfilterindex.h
Expand Up @@ -55,8 +55,7 @@ class BlockFilterIndex final : public BaseIndex

public:
/** Constructs the index, which becomes available to be queried. */
explicit BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain, BlockFilterType filter_type,
size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
explicit BlockFilterIndex(IndexParams&& params, BlockFilterType filter_type);

BlockFilterType GetFilterType() const { return m_filter_type; }

Expand Down Expand Up @@ -88,8 +87,7 @@ void ForEachBlockFilterIndex(std::function<void (BlockFilterIndex&)> fn);
* Initialize a block filter index for the given type if one does not already exist. Returns true if
* a new index is created and false if one has already been initialized.
*/
bool InitBlockFilterIndex(std::function<std::unique_ptr<interfaces::Chain>()> make_chain, BlockFilterType filter_type,
size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
bool InitBlockFilterIndex(IndexParams&& params, BlockFilterType filter_type);

/**
* Destroy the block filter index with the given type. Returns false if no such index exists. This
Expand Down
8 changes: 4 additions & 4 deletions src/index/coinstatsindex.cpp
Expand Up @@ -103,13 +103,13 @@ struct DBHashKey {

std::unique_ptr<CoinStatsIndex> g_coin_stats_index;

CoinStatsIndex::CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
: BaseIndex(std::move(chain))
CoinStatsIndex::CoinStatsIndex(IndexParams&& params)
: BaseIndex(std::move(params.chain))
{
fs::path path{gArgs.GetDataDirNet() / "indexes" / "coinstats"};
fs::path path{std::move(params.base_path) / "coinstats"};
fs::create_directories(path);

m_db = std::make_unique<CoinStatsIndex::DB>(path / "db", n_cache_size, f_memory, f_wipe);
m_db = std::make_unique<CoinStatsIndex::DB>(DBParams(params, path / "db"));
}

bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
Expand Down
2 changes: 1 addition & 1 deletion src/index/coinstatsindex.h
Expand Up @@ -53,7 +53,7 @@ class CoinStatsIndex final : public BaseIndex

public:
// Constructs the index, which becomes available to be queried.
explicit CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
explicit CoinStatsIndex(IndexParams&& params);

// Look up stats for a specific block using CBlockIndex
std::optional<kernel::CCoinsStats> LookUpStats(const CBlockIndex* block_index) const;
Expand Down
11 changes: 4 additions & 7 deletions src/index/txindex.cpp
Expand Up @@ -20,7 +20,7 @@ std::unique_ptr<TxIndex> g_txindex;
class TxIndex::DB : public BaseIndex::DB
{
public:
explicit DB(size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
using BaseIndex::DB::DB;

/// Read the disk location of the transaction data with the given hash. Returns false if the
/// transaction hash is not indexed.
Expand All @@ -30,10 +30,6 @@ class TxIndex::DB : public BaseIndex::DB
bool WriteTxs(const std::vector<std::pair<uint256, CDiskTxPos>>& v_pos);
};

TxIndex::DB::DB(size_t n_cache_size, bool f_memory, bool f_wipe) :
BaseIndex::DB(gArgs.GetDataDirNet() / "indexes" / "txindex", n_cache_size, f_memory, f_wipe)
{}

bool TxIndex::DB::ReadTxPos(const uint256 &txid, CDiskTxPos& pos) const
{
return Read(std::make_pair(DB_TXINDEX, txid), pos);
Expand All @@ -48,8 +44,9 @@ bool TxIndex::DB::WriteTxs(const std::vector<std::pair<uint256, CDiskTxPos>>& v_
return WriteBatch(batch);
}

TxIndex::TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
: BaseIndex(std::move(chain)), m_db(std::make_unique<TxIndex::DB>(n_cache_size, f_memory, f_wipe))
TxIndex::TxIndex(IndexParams&& params)
: BaseIndex(std::move(params.chain)),
m_db(std::make_unique<TxIndex::DB>(DBParams(params, std::move(params.base_path) / "txindex")))
{}

TxIndex::~TxIndex() = default;
Expand Down
2 changes: 1 addition & 1 deletion src/index/txindex.h
Expand Up @@ -31,7 +31,7 @@ class TxIndex final : public BaseIndex

public:
/// Constructs the index, which becomes available to be queried.
explicit TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
explicit TxIndex(IndexParams&& params);

// Destructor is declared because this class contains a unique_ptr to an incomplete type.
virtual ~TxIndex() override;
Expand Down

0 comments on commit f103f4d

Please sign in to comment.