refactor: inline constant return value of `CDBWrapper::Write`
What changed, and why it matters
This is a code cleanup change. A helper function that always returned 'true' is changed to return nothing, and callers are updated to stop checking a result that could never fail. There is no security-relevant behavior change.
No security action needed. Treat as ordinary refactoring review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
CDBWrapper::Write previously returned bool and always returned true. The commit refactors it to return void, inlines the constant success assumption at call sites, and updates tests to stop asserting the always-true return value. The underlying LevelDB write batch logic and error handling are unchanged; failures would still be surfaced via exceptions/LogError in WriteBatch, not via the return value.
Changed components
src/dbwrapper.hsrc/index/blockfilterindex.cppsrc/index/coinstatsindex.cppsrc/node/blockstorage.cppsrc/test/dbwrapper_tests.cppInspect captured patch +26 / −26
diff --git a/src/dbwrapper.h b/src/dbwrapper.h
index 94e452db..50be26fb 100644
--- a/src/dbwrapper.h
+++ b/src/dbwrapper.h
@@ -230,12 +230,11 @@ public:
}
template <typename K, typename V>
- bool Write(const K& key, const V& value, bool fSync = false)
+ void Write(const K& key, const V& value, bool fSync = false)
{
CDBBatch batch(*this);
batch.Write(key, value);
WriteBatch(batch, fSync);
- return true;
}
//! @returns filesystem path to the on-disk data.
diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp
index c56d7879..77bf931d 100644
--- a/src/index/blockfilterindex.cpp
+++ b/src/index/blockfilterindex.cpp
@@ -291,9 +291,7 @@ bool BlockFilterIndex::Write(const BlockFilter& filter, uint32_t block_height, c
value.second.header = filter_header;
value.second.pos = m_next_filter_pos;
- if (!m_db->Write(DBHeightKey(block_height), value)) {
- return false;
- }
+ m_db->Write(DBHeightKey(block_height), value);
m_next_filter_pos.nPos += bytes_written;
return true;
diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp
index 8f172d70..8072167e 100644
--- a/src/index/coinstatsindex.cpp
+++ b/src/index/coinstatsindex.cpp
@@ -226,7 +226,8 @@ bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
// Intentionally do not update DB_MUHASH here so it stays in sync with
// DB_BEST_BLOCK, and the index is not corrupted if there is an unclean shutdown.
- return m_db->Write(DBHeightKey(block.height), value);
+ m_db->Write(DBHeightKey(block.height), value);
+ return true;
}
[[nodiscard]] static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch,
diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp
index f1d4be6b..7371111c 100644
--- a/src/node/blockstorage.cpp
+++ b/src/node/blockstorage.cpp
@@ -62,7 +62,8 @@ bool BlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo& info)
bool BlockTreeDB::WriteReindexing(bool fReindexing)
{
if (fReindexing) {
- return Write(DB_REINDEX_FLAG, uint8_t{'1'});
+ Write(DB_REINDEX_FLAG, uint8_t{'1'});
+ return true;
} else {
return Erase(DB_REINDEX_FLAG);
}
@@ -94,7 +95,8 @@ bool BlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFi
bool BlockTreeDB::WriteFlag(const std::string& name, bool fValue)
{
- return Write(std::make_pair(DB_FLAG, name), fValue ? uint8_t{'1'} : uint8_t{'0'});
+ Write(std::make_pair(DB_FLAG, name), fValue ? uint8_t{'1'} : uint8_t{'0'});
+ return true;
}
bool BlockTreeDB::ReadFlag(const std::string& name, bool& fValue)
diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp
index 70666657..804830d4 100644
--- a/src/test/dbwrapper_tests.cpp
+++ b/src/test/dbwrapper_tests.cpp
@@ -39,7 +39,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
for (uint8_t k{0}; k < 10; ++k) {
uint8_t key{k};
uint256 value{m_rng.rand256()};
- BOOST_CHECK(dbw.Write(key, value));
+ dbw.Write(key, value);
key_values.emplace_back(key, value);
}
}
@@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
std::string key_block = "b" + m_rng.rand256().ToString();
uint256 in_block = m_rng.rand256();
- BOOST_CHECK(dbw.Write(key_block, in_block));
+ dbw.Write(key_block, in_block);
BOOST_CHECK(dbw.Read(key_block, res));
BOOST_CHECK_EQUAL(res.ToString(), in_block.ToString());
@@ -94,7 +94,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
std::string key_file = strprintf("f%04x", m_rng.rand32());
uint256 in_file_info = m_rng.rand256();
- BOOST_CHECK(dbw.Write(key_file, in_file_info));
+ dbw.Write(key_file, in_file_info);
BOOST_CHECK(dbw.Read(key_file, res));
BOOST_CHECK_EQUAL(res.ToString(), in_file_info.ToString());
@@ -102,7 +102,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
std::string key_transaction = "t" + m_rng.rand256().ToString();
uint256 in_transaction = m_rng.rand256();
- BOOST_CHECK(dbw.Write(key_transaction, in_transaction));
+ dbw.Write(key_transaction, in_transaction);
BOOST_CHECK(dbw.Read(key_transaction, res));
BOOST_CHECK_EQUAL(res.ToString(), in_transaction.ToString());
@@ -110,28 +110,28 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
std::string key_utxo = "c" + m_rng.rand256().ToString();
uint256 in_utxo = m_rng.rand256();
- BOOST_CHECK(dbw.Write(key_utxo, in_utxo));
+ dbw.Write(key_utxo, in_utxo);
BOOST_CHECK(dbw.Read(key_utxo, res));
BOOST_CHECK_EQUAL(res.ToString(), in_utxo.ToString());
//Simulate last block file number - "l"
uint8_t key_last_blockfile_number{'l'};
uint32_t lastblockfilenumber = m_rng.rand32();
- BOOST_CHECK(dbw.Write(key_last_blockfile_number, lastblockfilenumber));
+ dbw.Write(key_last_blockfile_number, lastblockfilenumber);
BOOST_CHECK(dbw.Read(key_last_blockfile_number, res_uint_32));
BOOST_CHECK_EQUAL(lastblockfilenumber, res_uint_32);
//Simulate Is Reindexing - "R"
uint8_t key_IsReindexing{'R'};
bool isInReindexing = m_rng.randbool();
- BOOST_CHECK(dbw.Write(key_IsReindexing, isInReindexing));
+ dbw.Write(key_IsReindexing, isInReindexing);
BOOST_CHECK(dbw.Read(key_IsReindexing, res_bool));
BOOST_CHECK_EQUAL(isInReindexing, res_bool);
//Simulate last block hash up to which UXTO covers - 'B'
uint8_t key_lastblockhash_uxto{'B'};
uint256 lastblock_hash = m_rng.rand256();
- BOOST_CHECK(dbw.Write(key_lastblockhash_uxto, lastblock_hash));
+ dbw.Write(key_lastblockhash_uxto, lastblock_hash);
BOOST_CHECK(dbw.Read(key_lastblockhash_uxto, res));
BOOST_CHECK_EQUAL(lastblock_hash, res);
@@ -142,7 +142,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
std::string key_file_option = strprintf("%s%01x%s", file_option_tag, filename_length, filename);
bool in_file_bool = m_rng.randbool();
- BOOST_CHECK(dbw.Write(key_file_option, in_file_bool));
+ dbw.Write(key_file_option, in_file_bool);
BOOST_CHECK(dbw.Read(key_file_option, res_bool));
BOOST_CHECK_EQUAL(res_bool, in_file_bool);
}
@@ -195,10 +195,10 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
// The two keys are intentionally chosen for ordering
uint8_t key{'j'};
uint256 in = m_rng.rand256();
- BOOST_CHECK(dbw.Write(key, in));
+ dbw.Write(key, in);
uint8_t key2{'k'};
uint256 in2 = m_rng.rand256();
- BOOST_CHECK(dbw.Write(key2, in2));
+ dbw.Write(key2, in2);
std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
@@ -238,7 +238,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
uint256 in = m_rng.rand256();
uint256 res;
- BOOST_CHECK(dbw->Write(key, in));
+ dbw->Write(key, in);
BOOST_CHECK(dbw->Read(key, res));
BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
@@ -261,7 +261,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
uint256 res3;
// Check that we can write successfully
- BOOST_CHECK(odbw.Write(key, in2));
+ odbw.Write(key, in2);
BOOST_CHECK(odbw.Read(key, res3));
BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString());
}
@@ -279,7 +279,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
uint256 in = m_rng.rand256();
uint256 res;
- BOOST_CHECK(dbw->Write(key, in));
+ dbw->Write(key, in);
BOOST_CHECK(dbw->Read(key, res));
BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
@@ -298,7 +298,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
uint256 res3;
// Check that we can write successfully
- BOOST_CHECK(odbw.Write(key, in2));
+ odbw.Write(key, in2);
BOOST_CHECK(odbw.Read(key, res3));
BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString());
}
@@ -310,7 +310,7 @@ BOOST_AUTO_TEST_CASE(iterator_ordering)
for (int x=0x00; x<256; ++x) {
uint8_t key = x;
uint32_t value = x*x;
- if (!(x & 1)) BOOST_CHECK(dbw.Write(key, value));
+ if (!(x & 1)) dbw.Write(key, value);
}
// Check that creating an iterator creates a snapshot
@@ -319,7 +319,7 @@ BOOST_AUTO_TEST_CASE(iterator_ordering)
for (unsigned int x=0x00; x<256; ++x) {
uint8_t key = x;
uint32_t value = x*x;
- if (x & 1) BOOST_CHECK(dbw.Write(key, value));
+ if (x & 1) dbw.Write(key, value);
}
for (const int seek_start : {0x00, 0x80}) {
@@ -381,7 +381,7 @@ BOOST_AUTO_TEST_CASE(iterator_string_ordering)
for (int z = 0; z < y; ++z)
key += key;
uint32_t value = x*x;
- BOOST_CHECK(dbw.Write(StringContentsSerializer{key}, value));
+ dbw.Write(StringContentsSerializer{key}, value);
}
}
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.