Merge bitcoin/bitcoin#33042: refactor: inline constant return values from dbwrapper write methods

743abbcbde refactor: inline constant return value of `BlockTreeDB::WriteBatchSync` and `BlockManager::WriteBlockIndexDB` and `BlockTreeDB::WriteFlag` (Lőrinc)
e030240e90 refactor: inline constant return value of `CDBWrapper::Erase` and `BlockTreeDB::WriteReindexing` (Lőrinc)
cdab9480e9 refactor: inline constant return value of `CDBWrapper::Write` (Lőrinc)
d1847cf5b5 refactor: inline constant return value of `TxIndex::DB::WriteTxs` (Lőrinc)
50b63a5698 refactor: inline constant return value of `CDBWrapper::WriteBatch` (Lőrinc)

Pull request description:

  Related to https://github.com/bitcoin/bitcoin/pull/31144#discussion_r2223587480

  ### Summary
  `WriteBatch` always returns `true` - the errors are handled by throwing `dbwrapper_error` instead.

  ### Context
  This boolean return value of the `Write` methods is confusing because it's inconsistent with `CDBWrapper::Read`, which catches exceptions and returns a boolean to indicate success/failure. It's bad that `Read` returns and `Write` throws - but it's a lot worse that `Write` advertises a return value when it actually communicates errors through exceptions.

  ### Solution
  This PR removes the constant return values from write methods and inlines `true` at their call sites. Many upstream methods had boolean return values only because they were propagating these constants - those have been cleaned up as well.

  Methods that returned a constant `true` value that now return `void`:
  - `CDBWrapper::WriteBatch`, `CDBWrapper::Write`, `CDBWrapper::Erase`
  - `TxIndex::DB::WriteTxs`
  - `BlockTreeDB::WriteReindexing`, `BlockTreeDB::WriteBatchSync`, `BlockTreeDB::WriteFlag`
  - `BlockManager::WriteBlockIndexDB`

  ### Note
  `CCoinsView::BatchWrite` (and transitively `CCoinsViewCache::Flush` & `CCoinsViewCache::Sync`) were intentionally not changed here. While all implementations return `true`, the base `CCoinsView::BatchWrite` returns `false`. Changing this would cause `coins_view` tests to fail with:
  > terminating due to uncaught exception of type std::logic_error: Not all unspent flagged entries were cleared

  We can fix that in a follow-up PR.

ACKs for top commit:
  achow101:
    ACK 743abbcbde
  janb84:
    ACK 743abbcbde
  TheCharlatan:
    ACK 743abbcbde
  sipa:
    ACK 743abbcbde

Tree-SHA512: b2a550bff066216f1958d2dd9a7ef6a9949de518cc636f8ab9c670e0b7a330c1eb8c838e458a8629acb8ac980cea6616955cd84436a7b8ab9096f6d648073b1e
This commit is contained in:
Ava Chow
2025-11-10 09:15:24 -08:00
12 changed files with 53 additions and 59 deletions

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

View File

@@ -89,7 +89,7 @@ FUZZ_TARGET(block_index, .init = init_block_index)
}
// Store these files and blocks in the block index. It should not fail.
assert(block_index.WriteBatchSync(files_info, files_count - 1, blocks_info));
block_index.WriteBatchSync(files_info, files_count - 1, blocks_info);
// We should be able to read every block file info we stored. Its value should correspond to
// what we stored above.