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/36066. 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. |
|
What about the mutability and mutations of the Please have a look at #35904, which proposes extracting the block validation parts from |
|
Thanks for the feedback, @purpleKarrot ! I will check out #35904 (I've seen 35906, but not 35904).
|
l0rinc
left a comment
There was a problem hiding this comment.
Concept ACK
Separating the check-only entry point makes the CBlockIndex mutability clearer and removes fJustCheck from the public ConnectBlock() interface.
I've split this locally (consider splitting the change similarly, see master...l0rinc:bitcoin:l0rinc/validation-test-connect-block):
- add and route through
TestConnectBlock() - expose the connection outputs
- handle failed checks and genesis in
ConnectBlock() - move the ordered undo/index/tracepoint updates
- then make the check-only index const.
My pending comments cover reusing the stored index hash, passing the index by const reference, and updating the related comments. The broader Chainstate side effects are already being discussed above.
proposes extracting the block validation parts from ConnectBlock into a function that has no side effects
+1 for these functional-style refactors (though, as mentioned, exceptions are the opposite of that), but given how consensus-critical this code is, I’d prefer moving toward side-effect-free validation through tiny, trivial-to-review changes, with this PR limited to making the CBlockIndex mutability explicit (and split into smaller changes that are all obviously correct).
|
|
||
| TRACEPOINT(validation, block_connected, | ||
| block_hash.data(), | ||
| block.GetHash().data(), |
There was a problem hiding this comment.
ConnectBlockChecks() already hashes the block and verifies that it matches pindex.
Could we use the stored index hash here instead?
| block.GetHash().data(), | |
| pindex->GetBlockHash().data(), |
There was a problem hiding this comment.
Good point, that's more optimal. I will store the block hash in a local variable, right after ConnectBlockChecks call, and use it from there. This way it's more similar how the ConnectBlockChecks does it internally.
| // Set fJustCheck to true in order to update, and not clear, validation caches. | ||
| if(!chainstate.ConnectBlock(block, state, &index_dummy, view_dummy, /*fJustCheck=*/true)) { | ||
| // Call TestConnectBlock(), it updates, and does not clear, validation caches. | ||
| if (!chainstate.TestConnectBlock(block, state, &index_dummy, view_dummy)) { |
There was a problem hiding this comment.
TestConnectBlock is never called with a possibly-nullptr block index, consider a const reference instead:
pass block index by reference
diff --git a/src/validation.cpp b/src/validation.cpp
index a4b86f18a9..83bd02944c 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -2700,7 +2700,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
return true;
}
-bool Chainstate::TestConnectBlock(const CBlock& block, BlockValidationState& state, const CBlockIndex* pindex,
+bool Chainstate::TestConnectBlock(const CBlock& block, BlockValidationState& state, const CBlockIndex& index,
CCoinsViewCache& view)
{
AssertLockHeld(cs_main);
@@ -2708,7 +2708,7 @@ bool Chainstate::TestConnectBlock(const CBlock& block, BlockValidationState& sta
CBlockUndo blockundo;
int nInputs = 0;
int64_t nSigOpsCost = 0;
- return ConnectBlockChecks(block, state, pindex, view, /*fJustCheck=*/true, blockundo, nInputs, nSigOpsCost);
+ return ConnectBlockChecks(block, state, &index, view, /*fJustCheck=*/true, blockundo, nInputs, nSigOpsCost);
}
CoinsCacheSizeState Chainstate::GetCoinsCacheSizeState()
@@ -4578,7 +4578,7 @@ BlockValidationState TestBlockValidity(
CCoinsViewCache view_dummy(&chainstate.CoinsTip());
// Call TestConnectBlock(), it updates, and does not clear, validation caches.
- if (!chainstate.TestConnectBlock(block, state, &index_dummy, view_dummy)) {
+ if (!chainstate.TestConnectBlock(block, state, index_dummy, view_dummy)) {
if (state.IsValid()) NONFATAL_UNREACHABLE();
return state;
}
diff --git a/src/validation.h b/src/validation.h
index dfc87331e8..1d2c773296 100644
--- a/src/validation.h
+++ b/src/validation.h
@@ -784,8 +784,8 @@ public:
//! Connect a block to the chain, updating pindex and the block undo/index files on disk.
bool ConnectBlock(const CBlock& block, BlockValidationState& state, CBlockIndex* pindex,
CCoinsViewCache& view) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
- //! Run the same validity checks as ConnectBlock() without mutating pindex or writing anything to disk.
- bool TestConnectBlock(const CBlock& block, BlockValidationState& state, const CBlockIndex* pindex,
+ //! Run the same validity checks as ConnectBlock() without mutating the block index or writing anything to disk.
+ bool TestConnectBlock(const CBlock& block, BlockValidationState& state, const CBlockIndex& index,
CCoinsViewCache& view) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
// Apply the effects of a block disconnection on the UTXO set.There was a problem hiding this comment.
Good point! Applied. (I've overlooked this, even though I have a separate open PR with exactly such changes...)
| bool ConnectBlock(const CBlock& block, BlockValidationState& state, CBlockIndex* pindex, | ||
| CCoinsViewCache& view, bool fJustCheck = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main); | ||
| CCoinsViewCache& view) EXCLUSIVE_LOCKS_REQUIRED(cs_main); | ||
| //! Run the same validity checks as ConnectBlock() without mutating pindex or writing anything to disk. |
There was a problem hiding this comment.
Some of the related comments could be updated now.
update check-only block comments
diff --git a/src/validation.cpp b/src/validation.cpp
index 611fc16805..3c0dd35729 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -4568,7 +4568,7 @@ BlockValidationState TestBlockValidity(
return state;
}
- // We don't want ConnectBlock to update the actual chainstate, so create
+ // We don't want TestConnectBlock to update the actual chainstate, so create
// a cache on top of it, along with a dummy block index.
CBlockIndex index_dummy{block};
uint256 block_hash(block.GetHash());
diff --git a/test/functional/interface_usdt_utxocache.py b/test/functional/interface_usdt_utxocache.py
index da90790d70..37cc205541 100755
--- a/test/functional/interface_usdt_utxocache.py
+++ b/test/functional/interface_usdt_utxocache.py
@@ -241,7 +241,7 @@ class UTXOCacheTracepointTest(BitcoinTestFramework):
# of the UTXO set (see CoinsTip() of CCoinsViewCache). However, in some cases
# temporary clones of the active cache are made. For example, during mining with
# the generate RPC call, the block is first tested in TestBlockValidity(). There,
- # a clone of the active cache is modified during a test ConnectBlock() call.
+ # a clone of the active cache is modified during a TestConnectBlock() call.
# These are implementation details we don't want to test here. Thus, after
# mining, we invalidate the block, start the tracing, and then trace the cache
# changes to the active utxo cache.|
@purpleKarrot, to revisit your comment: I think that the small change from this PR is compatible with the approach laid out in #35904. It is a small step, but it makes the input and update data slightly more explicit. I've also made an attempt to make For reference, I detail here the updates that happen in
|
0e832db to
36739b5
Compare
optout21
left a comment
There was a problem hiding this comment.
Applied review suggestions, minor changes. Thanks for the review, and sorry for the delay.
git diff 0e832dbd0488bb358b7505de5326e20e068eca9c..36739b5f0caeaeb2ac7e77eae75ff0954b8fcd0b
|
|
||
| TRACEPOINT(validation, block_connected, | ||
| block_hash.data(), | ||
| block.GetHash().data(), |
There was a problem hiding this comment.
Good point, that's more optimal. I will store the block hash in a local variable, right after ConnectBlockChecks call, and use it from there. This way it's more similar how the ConnectBlockChecks does it internally.
| // Set fJustCheck to true in order to update, and not clear, validation caches. | ||
| if(!chainstate.ConnectBlock(block, state, &index_dummy, view_dummy, /*fJustCheck=*/true)) { | ||
| // Call TestConnectBlock(), it updates, and does not clear, validation caches. | ||
| if (!chainstate.TestConnectBlock(block, state, &index_dummy, view_dummy)) { |
There was a problem hiding this comment.
Good point! Applied. (I've overlooked this, even though I have a separate open PR with exactly such changes...)
| bool ConnectBlock(const CBlock& block, BlockValidationState& state, CBlockIndex* pindex, | ||
| CCoinsViewCache& view, bool fJustCheck = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main); | ||
| CCoinsViewCache& view) EXCLUSIVE_LOCKS_REQUIRED(cs_main); | ||
| //! Run the same validity checks as ConnectBlock() without mutating pindex or writing anything to disk. |
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
|
UPDATE: Was due to silent merge conflict, fixed. I'm struggling with the CI build errors. Fuzz compile fails for |
36739b5 to
ddc6be9
Compare
|
I've noticed an accidental mistake in the newly-touched
|
|
CI failure is due to silent merge conflict with #35850, fixing. |
|
Rebased and fixed silent merge error, fuzz compile error due to newly introduced
|
ddc6be9 to
fe970b7
Compare
|
|
||
|
|
||
| FUZZ_TARGET(connect_block, .init = initialize_connect_block) | ||
| FUZZ_TARGET(test_connect_block, .init = initialize_connect_block) |
There was a problem hiding this comment.
Seems unnecessary to change the target name, no? I would just keep it the same. Our fuzzing infra corpora are keyed by target name, so we'd have to manually deal with renaming.
There was a problem hiding this comment.
Thanks for the feedback. I've just changed it as it was in sync with the name of the method being tested, and that has changed. But it can stay, no problem. I will revert this rename.
| bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state, CBlockIndex* pindex, | ||
| CCoinsViewCache& view, bool fJustCheck) | ||
| bool Chainstate::ConnectBlockChecks(const CBlock& block, BlockValidationState& state, const CBlockIndex* pindex, | ||
| CCoinsViewCache& view, bool fJustCheck, CBlockUndo& blockundo, |
There was a problem hiding this comment.
The PR description / commit message makes it sound like fJustCheck is dropped, but it is clearly still there, just moved to ConnectBlockChecks?!
There was a problem hiding this comment.
The changes:
- The public
ConnectBlockwith flag changed to protectedConnectBlockChecks, still with flag, but reduced usage. - New public "facade" methods
ConnectBlockandTestConnectBlock, with no flag.
So the flag is no longer present in the public methods, but it is still present in the implementation, but with reduced usage. It was not completely removed from there, that's true. It is passed down to CheckBlock and CheckInputScripts. Two usages have been removed (a conditional SetBestBlock and an early return).
I agree that the "flag dropped" formulation is not accurate, I will update the commit message and the description, thanks.
New `TestConnectBlock` method for the check-only variant of `ConnectBlock`, with const `CBlockIndex` parameter. The `fJustCheck` flag is no longer present in the public method versions. It is still present in the non-pubic common implementation method, but with reduced usage.
fe970b7 to
f478dc0
Compare
| bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state, CBlockIndex* pindex, | ||
| CCoinsViewCache& view, bool fJustCheck) | ||
| bool Chainstate::ConnectBlockChecks(const CBlock& block, BlockValidationState& state, const CBlockIndex* pindex, | ||
| CCoinsViewCache& view, bool fJustCheck, CBlockUndo& blockundo, |
There was a problem hiding this comment.
The changes:
- The public
ConnectBlockwith flag changed to protectedConnectBlockChecks, still with flag, but reduced usage. - New public "facade" methods
ConnectBlockandTestConnectBlock, with no flag.
So the flag is no longer present in the public methods, but it is still present in the implementation, but with reduced usage. It was not completely removed from there, that's true. It is passed down to CheckBlock and CheckInputScripts. Two usages have been removed (a conditional SetBestBlock and an early return).
I agree that the "flag dropped" formulation is not accurate, I will update the commit message and the description, thanks.
This is a small code-hygiene refactor to
ConnectBlockmethod.Problem: In the "check-only" mode of
ConnectBlockthe mutability of theCBlockIndexparameter is inconsistent with the semantics. In the default operation,ConnectBlockchanges the providedCBlockIndex. However, it has a special "check-only" mode, where the provided chain is not changed. This mode is used from only one call site (TestBlockValidity), and is signaled by thefJustCheck = trueflag.Solution: Provide two versions of
ConnectBlock: one with mutable chain and one "check-only" with const chain (TestConnectBlock). There is no need for thefJustCheckflag. Both reuse the same internal implementation. The non-public implementation method still uses the flag, but its usage has been reduced.Benefits:
pindexparameter is in sync with the semanticsfJustCheck) on the public methodsCBlockIndexis now unnecessaryAdditional details:
ConnectBlock, with dummyCBlockIndex.blockundo,nInputs,nSigOpsCost) are now outputs of the internal method, and used in the default outer method (ConnectBlock).SetBestBlockhas been moved to the outerConnectBlock. It was also present in the special genesis block early return, this is preserved.