Skip to content

Commit

Permalink
Update txdb.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
ohcee authored Jun 9, 2024
1 parent d1396fa commit c891fe2
Showing 1 changed file with 7 additions and 42 deletions.
49 changes: 7 additions & 42 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ std::vector<uint256> CCoinsViewDB::GetHeadBlocks() const {
}

bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
LogPrint(BCLog::COINDB, "Starting BatchWrite with %u coins\n", (unsigned int)mapCoins.size());

CDBBatch batch(db);
size_t count = 0;
size_t changed = 0;
Expand All @@ -120,39 +118,22 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
batch.Erase(DB_BEST_BLOCK);
batch.Write(DB_HEAD_BLOCKS, std::vector<uint256>{hashBlock, old_tip});

const size_t FLUSH_SIZE = 1; // Set flush size to 1
std::vector<std::pair<CoinEntry, Coin>> cache;

for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
CoinEntry entry(&it->first);
if (it->second.coin.IsSpent()) {
cache.emplace_back(entry, Coin());
} else {
cache.emplace_back(entry, it->second.coin);
}
if (it->second.coin.IsSpent())
batch.Erase(entry);
else
batch.Write(entry, it->second.coin);
changed++;
}
count++;
CCoinsMap::iterator itOld = it++;
mapCoins.erase(itOld);

// Write to batch when cache reaches FLUSH_SIZE
if (cache.size() >= FLUSH_SIZE) {
LogPrint(BCLog::COINDB, "Flushing %u coin entries to batch\n", (unsigned int)cache.size());
for (const auto& item : cache) {
if (item.second.IsSpent()) {
batch.Erase(item.first);
} else {
batch.Write(item.first, item.second);
}
}
if (!db.WriteBatch(batch)) {
return false; // If writing the batch fails, return false
}
if (batch.SizeEstimate() > batch_size) {
LogPrint(BCLog::COINDB, "Writing partial batch of %.2f MiB\n", batch.SizeEstimate() * (1.0 / 1048576.0));
db.WriteBatch(batch);
batch.Clear();
cache.clear();

if (crash_simulate) {
static FastRandomContext rng;
if (rng.randrange(crash_simulate) == 0) {
Expand All @@ -163,21 +144,6 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
}
}

// Write remaining items in cache
if (!cache.empty()) {
LogPrint(BCLog::COINDB, "Flushing remaining %u coin entries to batch\n", (unsigned int)cache.size());
for (const auto& item : cache) {
if (item.second.IsSpent()) {
batch.Erase(item.first);
} else {
batch.Write(item.first, item.second);
}
}
if (!db.WriteBatch(batch)) {
return false; // If writing the batch fails, return false
}
}

// In the last batch, mark the database as consistent with hashBlock again.
batch.Erase(DB_HEAD_BLOCKS);
batch.Write(DB_BEST_BLOCK, hashBlock);
Expand All @@ -188,7 +154,6 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
return ret;
}


size_t CCoinsViewDB::EstimateSize() const
{
return db.EstimateSize(DB_COIN, (char)(DB_COIN+1));
Expand Down

0 comments on commit c891fe2

Please sign in to comment.