Skip to content

Commit 5b2e54a

Browse files
committed
refactor: introduce util::DbWrapperParams and helper to pull out args
Helps us reduce our gArgs usage as bitcoin-chainstate won't be having ArgsManager with it
1 parent a7bec54 commit 5b2e54a

File tree

19 files changed

+92
-44
lines changed

19 files changed

+92
-44
lines changed

src/dbwrapper.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,4 +709,19 @@ class CDBTransaction {
709709
}
710710
};
711711

712+
namespace util {
713+
struct DbWrapperParams
714+
{
715+
const fs::path path{""};
716+
const bool memory{false};
717+
const bool wipe{false};
718+
const size_t cache_size{1 << 20};
719+
};
720+
721+
static inline std::unique_ptr<CDBWrapper> MakeDbWrapper(const DbWrapperParams& params)
722+
{
723+
return std::make_unique<CDBWrapper>(params.path, params.cache_size, params.memory, params.wipe);
724+
}
725+
} // namespace util
726+
712727
#endif // BITCOIN_DBWRAPPER_H

src/evo/evodb.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <evo/evodb.h>
6-
#include <util/system.h>
76

87
#include <uint256.h>
98

@@ -32,8 +31,8 @@ void CEvoDBScopedCommitter::Rollback()
3231
evoDB.RollbackCurTransaction();
3332
}
3433

35-
CEvoDB::CEvoDB(bool fMemory, bool fWipe) :
36-
db{std::make_unique<CDBWrapper>(fMemory ? "" : (gArgs.GetDataDirNet() / "evodb"), 64 << 20, fMemory, fWipe)},
34+
CEvoDB::CEvoDB(const util::DbWrapperParams& db_params) :
35+
db{util::MakeDbWrapper({db_params.path / "evodb", db_params.memory, db_params.wipe, /*cache_size=*/64 << 20})},
3736
rootBatch{*db},
3837
rootDBTransaction{*db, rootBatch},
3938
curDBTransaction{rootDBTransaction, rootDBTransaction}

src/evo/evodb.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <sync.h>
1010

1111
class uint256;
12+
namespace util {
13+
struct DbWrapperParams;
14+
} // namespace util
15+
1216
// "b_b" was used in the initial version of deterministic MN storage
1317
// "b_b2" was used after compact diffs were introduced
1418
// "b_b3" was used after masternode type introduction in evoDB
@@ -49,7 +53,7 @@ class CEvoDB
4953
CEvoDB() = delete;
5054
CEvoDB(const CEvoDB&) = delete;
5155
CEvoDB& operator=(const CEvoDB&) = delete;
52-
explicit CEvoDB(bool fMemory, bool fWipe);
56+
explicit CEvoDB(const util::DbWrapperParams& db_params);
5357
~CEvoDB();
5458

5559
std::unique_ptr<CEvoDBScopedCommitter> BeginTransaction() EXCLUSIVE_LOCKS_REQUIRED(!cs)

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
19811981
node.mnhf_manager,
19821982
node.llmq_ctx,
19831983
Assert(node.mempool.get()),
1984+
args.GetDataDirNet(),
19841985
fPruneMode,
19851986
args.GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX),
19861987
is_governance_enabled,

src/instantsend/db.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,22 @@ static std::tuple<std::string, uint32_t, uint256> BuildInversedISLockKey(std::st
2626
}
2727
} // anonymous namespace
2828

29-
CInstantSendDb::CInstantSendDb(bool unitTests, bool fWipe) :
30-
db(std::make_unique<CDBWrapper>(unitTests ? "" : (gArgs.GetDataDirNet() / "llmq/isdb"), 32 << 20, unitTests, fWipe))
29+
CInstantSendDb::CInstantSendDb(const util::DbWrapperParams& db_params) :
30+
db{util::MakeDbWrapper({db_params.path / "llmq" / "isdb", db_params.memory, db_params.wipe, /*cache_size=*/32 << 20})}
3131
{
32-
Upgrade(unitTests);
32+
Upgrade({db_params.path / "llmq" / "isdb", db_params.memory, /*wipe=*/true, /*cache_size=*/32 << 20});
3333
}
3434

3535
CInstantSendDb::~CInstantSendDb() = default;
3636

37-
void CInstantSendDb::Upgrade(bool unitTests)
37+
void CInstantSendDb::Upgrade(const util::DbWrapperParams& db_params)
3838
{
3939
LOCK(cs_db);
4040
int v{0};
4141
if (!db->Read(DB_VERSION, v) || v < CInstantSendDb::CURRENT_VERSION) {
4242
// Wipe db
4343
db.reset();
44-
db = std::make_unique<CDBWrapper>(unitTests ? "" : (gArgs.GetDataDirNet() / "llmq/isdb"), 32 << 20, unitTests,
45-
/*fWipe=*/true);
44+
db = util::MakeDbWrapper(db_params);
4645
CDBBatch batch(*db);
4746
batch.Write(DB_VERSION, CInstantSendDb::CURRENT_VERSION);
4847
// Sync DB changes to disk

src/instantsend/db.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class CBlockIndex;
2424
class CDBBatch;
2525
class CDBWrapper;
2626
class COutPoint;
27+
namespace util {
28+
struct DbWrapperParams;
29+
} // namespace util
2730

2831
namespace instantsend {
2932
class CInstantSendDb
@@ -78,10 +81,10 @@ class CInstantSendDb
7881
uint256 GetInstantSendLockHashByTxidInternal(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_db);
7982

8083

81-
void Upgrade(bool unitTests) EXCLUSIVE_LOCKS_REQUIRED(!cs_db);
84+
void Upgrade(const util::DbWrapperParams& db_params) EXCLUSIVE_LOCKS_REQUIRED(!cs_db);
8285

8386
public:
84-
explicit CInstantSendDb(bool unitTests, bool fWipe);
87+
explicit CInstantSendDb(const util::DbWrapperParams& db_params);
8588
~CInstantSendDb();
8689

8790
/**

src/instantsend/instantsend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ Uint256HashSet GetIdsFromLockable(const std::vector<T>& vec)
5858

5959
CInstantSendManager::CInstantSendManager(CChainLocksHandler& _clhandler, CChainState& chainstate, CQuorumManager& _qman,
6060
CSigningManager& _sigman, CSporkManager& sporkman, CTxMemPool& _mempool,
61-
const CMasternodeSync& mn_sync, bool unitTests, bool fWipe) :
62-
db{unitTests, fWipe},
61+
const CMasternodeSync& mn_sync, const util::DbWrapperParams& db_params) :
62+
db{db_params},
6363
clhandler{_clhandler},
6464
m_chainstate{chainstate},
6565
qman{_qman},

src/instantsend/instantsend.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class PeerManager;
3131
namespace Consensus {
3232
struct LLMQParams;
3333
} // namespace Consensus
34+
namespace util {
35+
struct DbWrapperParams;
36+
} // namespace util
3437

3538
namespace instantsend {
3639
class InstantSendSigner;
@@ -94,7 +97,7 @@ class CInstantSendManager final : public instantsend::InstantSendSignerParent
9497
CInstantSendManager& operator=(const CInstantSendManager&) = delete;
9598
explicit CInstantSendManager(CChainLocksHandler& _clhandler, CChainState& chainstate, CQuorumManager& _qman,
9699
CSigningManager& _sigman, CSporkManager& sporkman, CTxMemPool& _mempool,
97-
const CMasternodeSync& mn_sync, bool unitTests, bool fWipe);
100+
const CMasternodeSync& mn_sync, const util::DbWrapperParams& db_params);
98101
~CInstantSendManager();
99102

100103
void ConnectSigner(gsl::not_null<instantsend::InstantSendSigner*> signer)

src/llmq/context.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@
1818
LLMQContext::LLMQContext(ChainstateManager& chainman, CDeterministicMNManager& dmnman, CEvoDB& evo_db,
1919
CMasternodeMetaMan& mn_metaman, CMNHFManager& mnhfman, CSporkManager& sporkman,
2020
CTxMemPool& mempool, const CActiveMasternodeManager* const mn_activeman,
21-
const CMasternodeSync& mn_sync, bool unit_tests, bool wipe) :
21+
const CMasternodeSync& mn_sync, const util::DbWrapperParams& db_params) :
2222
bls_worker{std::make_shared<CBLSWorker>()},
2323
dkg_debugman{std::make_unique<llmq::CDKGDebugManager>()},
2424
qsnapman{std::make_unique<llmq::CQuorumSnapshotManager>(evo_db)},
2525
quorum_block_processor{
2626
std::make_unique<llmq::CQuorumBlockProcessor>(chainman.ActiveChainstate(), dmnman, evo_db, *qsnapman)},
2727
qdkgsman{std::make_unique<llmq::CDKGSessionManager>(*bls_worker, chainman.ActiveChainstate(), dmnman, *dkg_debugman,
2828
mn_metaman, *quorum_block_processor, *qsnapman, mn_activeman,
29-
sporkman, unit_tests, wipe)},
29+
sporkman, db_params)},
3030
qman{std::make_unique<llmq::CQuorumManager>(*bls_worker, chainman.ActiveChainstate(), dmnman, *qdkgsman, evo_db,
3131
*quorum_block_processor, *qsnapman, mn_activeman, mn_sync, sporkman,
32-
unit_tests, wipe)},
33-
sigman{std::make_unique<llmq::CSigningManager>(chainman.ActiveChainstate(), *qman, unit_tests, wipe)},
32+
db_params)},
33+
sigman{std::make_unique<llmq::CSigningManager>(chainman.ActiveChainstate(), *qman, db_params)},
3434
clhandler{std::make_unique<llmq::CChainLocksHandler>(chainman.ActiveChainstate(), *qman, sporkman, mempool, mn_sync)},
3535
isman{std::make_unique<llmq::CInstantSendManager>(*clhandler, chainman.ActiveChainstate(), *qman, *sigman, sporkman,
36-
mempool, mn_sync, unit_tests, wipe)}
36+
mempool, mn_sync, db_params)}
3737
{
3838
// Have to start it early to let VerifyDB check ChainLock signatures in coinbase
3939
bls_worker->Start();

src/llmq/context.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ class CQuorumBlockProcessor;
2828
class CQuorumManager;
2929
class CQuorumSnapshotManager;
3030
class CSigningManager;
31-
}
31+
} // namespace llmq
32+
namespace util {
33+
struct DbWrapperParams;
34+
} // namespace util
3235

3336
struct LLMQContext {
3437
public:
@@ -38,7 +41,7 @@ struct LLMQContext {
3841
explicit LLMQContext(ChainstateManager& chainman, CDeterministicMNManager& dmnman, CEvoDB& evo_db,
3942
CMasternodeMetaMan& mn_metaman, CMNHFManager& mnhfman, CSporkManager& sporkman,
4043
CTxMemPool& mempool, const CActiveMasternodeManager* const mn_activeman,
41-
const CMasternodeSync& mn_sync, bool unit_tests, bool wipe);
44+
const CMasternodeSync& mn_sync, const util::DbWrapperParams& db_params);
4245
~LLMQContext();
4346

4447
void Interrupt();

0 commit comments

Comments
 (0)