Skip to content

Commit 8c87f17

Browse files
committed
Merge #9310: Assert FRESH validity in CCoinsViewCache::BatchWrite
dd44ea3 Check FRESH validity in CCoinsViewCache::BatchWrite (Russell Yanofsky)
2 parents 25720fc + dd44ea3 commit 8c87f17

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/coins.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn
207207
entry.flags |= CCoinsCacheEntry::FRESH;
208208
}
209209
} else {
210+
// Assert that the child cache entry was not marked FRESH if the
211+
// parent cache entry has unspent outputs. If this ever happens,
212+
// it means the FRESH flag was misapplied and there is a logic
213+
// error in the calling code.
214+
if ((it->second.flags & CCoinsCacheEntry::FRESH) && !itUs->second.coins.IsPruned())
215+
throw std::logic_error("FRESH flag misapplied to cache entry for base transaction with spendable outputs");
216+
210217
// Found the entry in the parent cache
211218
if ((itUs->second.flags & CCoinsCacheEntry::FRESH) && it->second.coins.IsPruned()) {
212219
// The grandparent does not have an entry, and the child is

src/test/coins_tests.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -791,12 +791,18 @@ BOOST_AUTO_TEST_CASE(ccoins_modify_new)
791791
void CheckWriteCoins(CAmount parent_value, CAmount child_value, CAmount expected_value, char parent_flags, char child_flags, char expected_flags)
792792
{
793793
SingleEntryCacheTest test(ABSENT, parent_value, parent_flags);
794-
WriteCoinsViewEntry(test.cache, child_value, child_flags);
795-
test.cache.SelfTest();
796794

797795
CAmount result_value;
798796
char result_flags;
799-
GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
797+
try {
798+
WriteCoinsViewEntry(test.cache, child_value, child_flags);
799+
test.cache.SelfTest();
800+
GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
801+
} catch (std::logic_error& e) {
802+
result_value = FAIL;
803+
result_flags = NO_ENTRY;
804+
}
805+
800806
BOOST_CHECK_EQUAL(result_value, expected_value);
801807
BOOST_CHECK_EQUAL(result_flags, expected_flags);
802808
}
@@ -840,21 +846,21 @@ BOOST_AUTO_TEST_CASE(ccoins_write)
840846
CheckWriteCoins(VALUE1, ABSENT, VALUE1, DIRTY , NO_ENTRY , DIRTY );
841847
CheckWriteCoins(VALUE1, ABSENT, VALUE1, DIRTY|FRESH, NO_ENTRY , DIRTY|FRESH);
842848
CheckWriteCoins(VALUE1, PRUNED, PRUNED, 0 , DIRTY , DIRTY );
843-
CheckWriteCoins(VALUE1, PRUNED, PRUNED, 0 , DIRTY|FRESH, DIRTY );
849+
CheckWriteCoins(VALUE1, PRUNED, FAIL , 0 , DIRTY|FRESH, NO_ENTRY );
844850
CheckWriteCoins(VALUE1, PRUNED, ABSENT, FRESH , DIRTY , NO_ENTRY );
845-
CheckWriteCoins(VALUE1, PRUNED, ABSENT, FRESH , DIRTY|FRESH, NO_ENTRY );
851+
CheckWriteCoins(VALUE1, PRUNED, FAIL , FRESH , DIRTY|FRESH, NO_ENTRY );
846852
CheckWriteCoins(VALUE1, PRUNED, PRUNED, DIRTY , DIRTY , DIRTY );
847-
CheckWriteCoins(VALUE1, PRUNED, PRUNED, DIRTY , DIRTY|FRESH, DIRTY );
853+
CheckWriteCoins(VALUE1, PRUNED, FAIL , DIRTY , DIRTY|FRESH, NO_ENTRY );
848854
CheckWriteCoins(VALUE1, PRUNED, ABSENT, DIRTY|FRESH, DIRTY , NO_ENTRY );
849-
CheckWriteCoins(VALUE1, PRUNED, ABSENT, DIRTY|FRESH, DIRTY|FRESH, NO_ENTRY );
855+
CheckWriteCoins(VALUE1, PRUNED, FAIL , DIRTY|FRESH, DIRTY|FRESH, NO_ENTRY );
850856
CheckWriteCoins(VALUE1, VALUE2, VALUE2, 0 , DIRTY , DIRTY );
851-
CheckWriteCoins(VALUE1, VALUE2, VALUE2, 0 , DIRTY|FRESH, DIRTY );
857+
CheckWriteCoins(VALUE1, VALUE2, FAIL , 0 , DIRTY|FRESH, NO_ENTRY );
852858
CheckWriteCoins(VALUE1, VALUE2, VALUE2, FRESH , DIRTY , DIRTY|FRESH);
853-
CheckWriteCoins(VALUE1, VALUE2, VALUE2, FRESH , DIRTY|FRESH, DIRTY|FRESH);
859+
CheckWriteCoins(VALUE1, VALUE2, FAIL , FRESH , DIRTY|FRESH, NO_ENTRY );
854860
CheckWriteCoins(VALUE1, VALUE2, VALUE2, DIRTY , DIRTY , DIRTY );
855-
CheckWriteCoins(VALUE1, VALUE2, VALUE2, DIRTY , DIRTY|FRESH, DIRTY );
861+
CheckWriteCoins(VALUE1, VALUE2, FAIL , DIRTY , DIRTY|FRESH, NO_ENTRY );
856862
CheckWriteCoins(VALUE1, VALUE2, VALUE2, DIRTY|FRESH, DIRTY , DIRTY|FRESH);
857-
CheckWriteCoins(VALUE1, VALUE2, VALUE2, DIRTY|FRESH, DIRTY|FRESH, DIRTY|FRESH);
863+
CheckWriteCoins(VALUE1, VALUE2, FAIL , DIRTY|FRESH, DIRTY|FRESH, NO_ENTRY );
858864

859865
// The checks above omit cases where the child flags are not DIRTY, since
860866
// they would be too repetitive (the parent cache is never updated in these

0 commit comments

Comments
 (0)