Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34864. ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
andrewtoth
left a comment
There was a problem hiding this comment.
Concept ACK
Thanks for picking this up.
| cachedCoinsUsage += entry.coin.DynamicMemoryUsage(); | ||
| // We can mark it FRESH in the parent if it was FRESH in the child | ||
| // Otherwise it might have just been flushed from the parent's cache | ||
| // and already exist in the grandparent |
There was a problem hiding this comment.
Why remove this comment? It's still relevant and provides valuable context.
There was a problem hiding this comment.
Restored above the new SetDirty call
| LogDebug(BCLog::COINDB, "Writing final batch of %.2f MiB\n", batch.ApproximateSize() * (1.0 / 1048576.0)); | ||
| m_db->WriteBatch(batch); | ||
| LogDebug(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...", (unsigned int)dirty_count, (unsigned int)count); | ||
| LogDebug(BCLog::COINDB, "Committed %u transaction outputs to coin database...", dirty_count); |
There was a problem hiding this comment.
The changed adjective could be left here. Existing transaction outputs that are deleted are changed. The log now can be conflated with committing only new transactions outputs.
|
|
||
| void SetClean() noexcept | ||
| { | ||
| if (!m_flags) return; |
There was a problem hiding this comment.
Why is this touched here?
There was a problem hiding this comment.
Just for symmetry with SetDirty
| Assume(pair.second.m_prev && pair.second.m_next); | ||
| pair.second.m_flags |= DIRTY | (fresh ? FRESH : 0); | ||
| Assume(pair.second.IsDirty()); | ||
| pair.second.m_fresh = fresh; |
There was a problem hiding this comment.
This is a behavior change, and the commit message says:
Make `SetDirty()` take the resulting freshness explicitly so callers cannot accidentally preserve an old fresh state by passing `false`.
Why do we want to set a fresh entry to not-fresh? If an entry is fresh, it should stay fresh until cleaned or erased as before.
There was a problem hiding this comment.
This is a behavior change
Why do we want to set a fresh entry to not-fresh?
Added Assume(!pair.second.m_fresh || fresh) so the API can't silently clear FRESH, thanks
5c30d62 to
fa9683a
Compare
| Assume(pair.second.m_prev && pair.second.m_next); | ||
| pair.second.m_flags |= DIRTY | (fresh ? FRESH : 0); | ||
| Assume(pair.second.IsDirty()); | ||
| pair.second.m_fresh = fresh; |
There was a problem hiding this comment.
This is a behavior change
Why do we want to set a fresh entry to not-fresh?
Added Assume(!pair.second.m_fresh || fresh) so the API can't silently clear FRESH, thanks
|
|
||
| void SetClean() noexcept | ||
| { | ||
| if (!m_flags) return; |
There was a problem hiding this comment.
Just for symmetry with SetDirty
| LogDebug(BCLog::COINDB, "Writing final batch of %.2f MiB\n", batch.ApproximateSize() * (1.0 / 1048576.0)); | ||
| m_db->WriteBatch(batch); | ||
| LogDebug(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...", (unsigned int)dirty_count, (unsigned int)count); | ||
| LogDebug(BCLog::COINDB, "Committed %u transaction outputs to coin database...", dirty_count); |
| cachedCoinsUsage += entry.coin.DynamicMemoryUsage(); | ||
| // We can mark it FRESH in the parent if it was FRESH in the child | ||
| // Otherwise it might have just been flushed from the parent's cache | ||
| // and already exist in the grandparent |
There was a problem hiding this comment.
Restored above the new SetDirty call
optout21
left a comment
There was a problem hiding this comment.
ACK fa9683af5f74d81c4907ad81d888a94ef2e880be
My perspective on the benefit of this change is as follows: The CCoinsCacheEntry class is capable of expressing several combinations of properties, some of which are invalid and never used. They are invalid logically, and enforced by convention only. Post-change the invalid states cannot be represented. This change is a code maintainability benefit, because any code change could potentially break the conventions. It also results in slightly simpler code.
The relevant state space dimensions are:
- The "dirty" and "fresh" properties were represented as independent bools (stored in a bitfield), however, of the 2x2 combinations the fresh-but-not-dirty is invalid (because new entries in the cache that are not present in an underlying level must always be saved). This combination is not used.
- A dirty (or fresh) entry must be placed in the linked list of dirty elements. The dirty flag and the existence of the linked list pointers may contradict.
Post change:
- Dirtyness is implicitly represented by being included in the linked list, not by an explicit flag.
- Freshness is represented as a single bool flag, and the setter methods have been changed to prevent the invalid state. Asserts have been also added.
The essence of the change is concentrated to the CCoinsCacheEntry class, and is relatively simple. However, auxiliary changes are needed to ensure/assert the call sites and the tests. Moreover, the change is broken up into smaller consistent steps.
An overview of the commits (in chronological order):
SetFreshis being folded intoSetDirtywith afreshflag. At the relevant call site the "freshness" information is already available.- Tests are adjusted to not use the invalid fresh-but-not-dirty state.
- In
BatchWritedrop the conditional for invalid non-dirty cases, replace them withassert. - Also in
BatchWrite, for the invalid fresh-but-spent case instead of being ignored it is handled as an exception. - Drop
SetFresh. - Small refactor to fold the now-single-use
AddFlagsintoSetDirty - Perform the internal representation change (Dirtiness is derived from the linked list pointers, and freshness is stored as a bool. Extra asserts are added to checks for preconditions. Also, the default value for the
freshoption onSetDirtyis removed (so its usage is more explicit)).
I have reimplemented part of the changes independently. I've reviewed the code, and found no issues. (Note: as the PR had only light review so far, further changes are likely.)
|
Tested the coins fuzz targets on this branch (commit fa9683af5f), since the PR turns the previously-handled FRESH+spent cases in Setup: arm64 macOS (Apple M-series), Homebrew clang 22,
No crashes, OOMs, sanitizer errors, or hits on the new Caveat: this was an unseeded 2h run, so it's shallower than a seeded run against the qa-assets corpus would be; happy to do a longer seeded run if useful. I also read through the |
fa9683a to
4812de5
Compare
|
Rebased and added two commits tightening |
4812de5 to
f27eba8
Compare
|
reACK f27eba8 No issues. A net reduction in complexity around coins view flags (my previous reivew had more details). |
Limit fixtures to entries that are dirty whenever they are fresh, and prepare the fuzz harness to avoid spent fresh entries and duplicate insertion accounting. Record that repeated spends report success and overwrite caller-owned output, while retaining the existing state and accounting checks. Co-authored-by: Lőrinc <[email protected]>
Fresh entries must also be dirty. Require the resulting freshness at every `SetDirty()` call and remove the bare `SetFresh()` helper, so callers cannot create a fresh entry without linking it into the dirty list. Co-authored-by: Andrew Toth <[email protected]>
`CoinsViewCacheCursor` iterates the dirty-entry list. Treat cursor entries as dirty instead of checking them again, while retaining assertions of the cursor contract. Co-authored-by: Andrew Toth <[email protected]>
Spending a fresh entry erases it locally because the parent never knew it existed. Reject a spent fresh entry before merging it into the parent cache instead of treating it as a valid state.
`SetDirty()` is the only remaining caller of `AddFlags()`. Inline the helper so the representation change is confined to the entry's state management.
List membership already identifies dirty entries, so the separate `DIRTY` bit duplicates state. Store only freshness and require it to be preserved while an entry remains dirty. Co-authored-by: Andrew Toth <[email protected]>
Return false without overwriting caller-owned output when an entry is already cached as spent. Cache accounting was already unchanged for reachable spent entries. Production callers retain their behavior: checked inputs remain spendable, disconnects report unclean state, and replay ignores repeated deletes. The rejected spend no longer emits the `utxocache:spent` tracepoint.
Mark `SpendCoin()` as `[[nodiscard]]` so accidental ignored results are rejected. The mempool consistency check requires successful spends, while replay explicitly ignores repeated deletes after an interrupted coins DB flush.
f27eba8 to
6fd59bf
Compare
|
Rebased to make sure related changes are included and applied some AI review cleanup suggestions. |
Problem: The coins cache still exposed and tested states that production code should not create: standalone
FRESHentries, non-dirtyBatchWrite()cursor entries, and spentFRESHentries.That makes the representation harder to reason about, because
CCoinsCacheEntrystores a separateDIRTYbit even though dirty entries are already tracked in the linked list.Fix: This revives and splits the relevant parts of #30673 and #33018.
The series passes freshness through
SetDirty(), removes fresh-only test states, treatsBatchWrite()cursor entries as dirty by construction, rejects spentFRESHentries, removes the bareSetFresh()helper, and derives dirtiness from linked-list membership.The final commits also tighten
SpendCoin()around the same cache-state contract: repeated spends of an entry already cached as spent now return false without overwriting caller-owned output, while preserving the already-unchanged cache accounting. Callers must either check the result or explicitly discard it.coinscache_simnow checks the realSpendCoin()result against its simulation model, and the no-op repeated-spend path no longer emits theutxocache:spenttracepoint because no spend occurs.Production callers retain their behavior: checked inputs remain spendable, disconnects report unclean state, and replay ignores repeated deletes.