Skip to content

Commit 42b2520

Browse files
committed
chain: add BLOCK_ASSUMED_VALID for use with assumeutxo
Instead of (ab)using the existing BLOCK_VALID_* flags to mark CBlockIndex entries which we haven't yet fully validated (but assume validity for use with UTXO snapshot loading), introduce a status flag that specifically marks an assumed-valid state. This state is then removed in RaiseValidity() when the block has actually been validated. This distinction will allow us to make the necessary changes to various parts of the system to facilitate assumeutxo/background chainstate validation but without leaking details like snapshot height, as we had done previously. Changes that actually make use of this flag follow in future commits.
1 parent b217020 commit 42b2520

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/chain.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,15 @@ enum BlockStatus: uint32_t {
126126
BLOCK_FAILED_CHILD = 64, //!< descends from failed block
127127
BLOCK_FAILED_MASK = BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD,
128128

129-
BLOCK_OPT_WITNESS = 128, //!< block data in blk*.data was received with a witness-enforcing client
129+
BLOCK_OPT_WITNESS = 128, //!< block data in blk*.dat was received with a witness-enforcing client
130+
131+
/**
132+
* If set, this indicates that the block index entry is assumed-valid.
133+
* Certain diagnostics will be skipped in e.g. CheckBlockIndex().
134+
* It almost certainly means that the block's full validation is pending
135+
* on a background chainstate. See `doc/assumeutxo.md`.
136+
*/
137+
BLOCK_ASSUMED_VALID = 256,
130138
};
131139

132140
/** The block chain is a tree shaped structure starting with the
@@ -300,14 +308,24 @@ class CBlockIndex
300308
return ((nStatus & BLOCK_VALID_MASK) >= nUpTo);
301309
}
302310

311+
//! @returns true if the block is assumed-valid; this means it is queued to be
312+
//! validated by a background chainstate.
313+
bool IsAssumedValid() const { return nStatus & BLOCK_ASSUMED_VALID; }
314+
303315
//! Raise the validity level of this block index entry.
304316
//! Returns true if the validity was changed.
305317
bool RaiseValidity(enum BlockStatus nUpTo)
306318
{
307319
assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
308-
if (nStatus & BLOCK_FAILED_MASK)
309-
return false;
320+
if (nStatus & BLOCK_FAILED_MASK) return false;
321+
310322
if ((nStatus & BLOCK_VALID_MASK) < nUpTo) {
323+
// If this block had been marked assumed-valid and we're raising
324+
// its validity to a certain point, there is no longer an assumption.
325+
if (nStatus & BLOCK_ASSUMED_VALID && nUpTo >= BLOCK_VALID_SCRIPTS) {
326+
nStatus &= ~BLOCK_ASSUMED_VALID;
327+
}
328+
311329
nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo;
312330
return true;
313331
}

0 commit comments

Comments
 (0)