refactor: inline constant return value of CDBWrapper::Write

This commit is contained in:
Lőrinc
2025-07-22 12:28:44 -07:00
parent d1847cf5b5
commit cdab9480e9
5 changed files with 26 additions and 26 deletions

View File

@@ -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.

View File

@@ -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;

View File

@@ -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,

View File

@@ -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)

View File

@@ -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);
}
}