refactor: inline constant return value of `CDBWrapper::WriteBatch`
What changed, and why it matters
This is a straightforward code cleanup change. A database helper function that always returned true is changed to return nothing, and callers are updated to stop checking a value that could never be false. No security bug is introduced or fixed.
No security action required. Treat as normal code-quality refactor during review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors CDBWrapper::WriteBatch from bool to void because the implementation always returned true; failures are signaled by throwing dbwrapper_error. Callers in indexes, txdb, blockstorage, and tests are updated to call WriteBatch without checking a return value, and wrapper functions that previously returned WriteBatch’s result now explicitly return true. This is a non-functional refactor with no change to error handling, control flow, or behavior on failure paths.
Changed components
src/dbwrapper.cppsrc/dbwrapper.hsrc/index/base.cppsrc/index/blockfilterindex.cppsrc/index/coinstatsindex.cppsrc/index/txindex.cppsrc/node/blockstorage.cppsrc/test/dbwrapper_tests.cppsrc/txdb.cppInspect captured patch +16 / −13
diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp
index fe5f9cb0..cb9abee5 100644
--- a/src/dbwrapper.cpp
+++ b/src/dbwrapper.cpp
@@ -274,7 +274,7 @@ CDBWrapper::~CDBWrapper()
DBContext().options.env = nullptr;
}
-bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
+void CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
{
const bool log_memory = LogAcceptCategory(BCLog::LEVELDB, BCLog::Level::Debug);
double mem_before = 0;
@@ -288,7 +288,6 @@ bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
LogDebug(BCLog::LEVELDB, "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
m_name, mem_before, mem_after);
}
- return true;
}
size_t CDBWrapper::DynamicMemoryUsage() const
diff --git a/src/dbwrapper.h b/src/dbwrapper.h
index b9b98bd9..94e452db 100644
--- a/src/dbwrapper.h
+++ b/src/dbwrapper.h
@@ -234,7 +234,8 @@ public:
{
CDBBatch batch(*this);
batch.Write(key, value);
- return WriteBatch(batch, fSync);
+ WriteBatch(batch, fSync);
+ return true;
}
//! @returns filesystem path to the on-disk data.
@@ -259,10 +260,11 @@ public:
{
CDBBatch batch(*this);
batch.Erase(key);
- return WriteBatch(batch, fSync);
+ WriteBatch(batch, fSync);
+ return true;
}
- bool WriteBatch(CDBBatch& batch, bool fSync = false);
+ void WriteBatch(CDBBatch& batch, bool fSync = false);
// Get an estimate of LevelDB memory usage (in bytes).
size_t DynamicMemoryUsage() const;
diff --git a/src/index/base.cpp b/src/index/base.cpp
index fdd0e0d8..0fdb5af3 100644
--- a/src/index/base.cpp
+++ b/src/index/base.cpp
@@ -262,7 +262,7 @@ bool BaseIndex::Commit()
ok = CustomCommit(batch);
if (ok) {
GetDB().WriteBestBlock(batch, GetLocator(*m_chain, m_best_block_index.load()->GetBlockHash()));
- ok = GetDB().WriteBatch(batch);
+ GetDB().WriteBatch(batch);
}
}
if (!ok) {
diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp
index 2ccae3a2..c56d7879 100644
--- a/src/index/blockfilterindex.cpp
+++ b/src/index/blockfilterindex.cpp
@@ -338,7 +338,7 @@ bool BlockFilterIndex::CustomRemove(const interfaces::BlockInfo& block)
// But since this creates new references to the filter, the position should get updated here
// atomically as well in case Commit fails.
batch.Write(DB_FILTER_POS, m_next_filter_pos);
- if (!m_db->WriteBatch(batch)) return false;
+ m_db->WriteBatch(batch);
// Update cached header to the previous block hash
m_last_header = *Assert(ReadFilterHeader(block.height - 1, *Assert(block.prev_hash)));
diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp
index 96693f7f..8f172d70 100644
--- a/src/index/coinstatsindex.cpp
+++ b/src/index/coinstatsindex.cpp
@@ -263,7 +263,7 @@ bool CoinStatsIndex::CustomRemove(const interfaces::BlockInfo& block)
return false;
}
- if (!m_db->WriteBatch(batch)) return false;
+ m_db->WriteBatch(batch);
if (!ReverseBlock(block)) {
return false; // failure cause logged internally
diff --git a/src/index/txindex.cpp b/src/index/txindex.cpp
index 11dd856e..16038a3a 100644
--- a/src/index/txindex.cpp
+++ b/src/index/txindex.cpp
@@ -46,7 +46,8 @@ bool TxIndex::DB::WriteTxs(const std::vector<std::pair<Txid, CDiskTxPos>>& v_pos
for (const auto& [txid, pos] : v_pos) {
batch.Write(std::make_pair(DB_TXINDEX, txid.ToUint256()), pos);
}
- return WriteBatch(batch);
+ WriteBatch(batch);
+ return true;
}
TxIndex::TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp
index 3c01ff2e..f1d4be6b 100644
--- a/src/node/blockstorage.cpp
+++ b/src/node/blockstorage.cpp
@@ -88,7 +88,8 @@ bool BlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFi
for (const CBlockIndex* bi : blockinfo) {
batch.Write(std::make_pair(DB_BLOCK_INDEX, bi->GetBlockHash()), CDiskBlockIndex{bi});
}
- return WriteBatch(batch, true);
+ WriteBatch(batch, true);
+ return true;
}
bool BlockTreeDB::WriteFlag(const std::string& name, bool fValue)
diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp
index cd0f347b..70666657 100644
--- a/src/test/dbwrapper_tests.cpp
+++ b/src/test/dbwrapper_tests.cpp
@@ -173,7 +173,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
// Remove key3 before it's even been written
batch.Erase(key3);
- BOOST_CHECK(dbw.WriteBatch(batch));
+ dbw.WriteBatch(batch);
BOOST_CHECK(dbw.Read(key, res));
BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
diff --git a/src/txdb.cpp b/src/txdb.cpp
index bb6ee2eb..23824f39 100644
--- a/src/txdb.cpp
+++ b/src/txdb.cpp
@@ -149,9 +149,9 @@ bool CCoinsViewDB::BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &hashB
batch.Write(DB_BEST_BLOCK, hashBlock);
LogDebug(BCLog::COINDB, "Writing final batch of %.2f MiB\n", batch.ApproximateSize() * (1.0 / 1048576.0));
- bool ret = m_db->WriteBatch(batch);
+ m_db->WriteBatch(batch);
LogDebug(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count);
- return ret;
+ return true;
}
size_t CCoinsViewDB::EstimateSize() const
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.