forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: validate CbTx chainlock height diff #7363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thepastaclaw
wants to merge
2
commits into
dashpay:develop
Choose a base branch
from
thepastaclaw:fix-cbtx-chainlock-height-bound
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+91
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Copyright (c) 2026 The Dash Core developers | ||
| // Distributed under the MIT software license, see the accompanying | ||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
|
||
| #include <test/util/setup_common.h> | ||
|
|
||
| #include <bls/bls.h> | ||
| #include <chain.h> | ||
| #include <chainlock/chainlock.h> | ||
| #include <chainparams.h> | ||
| #include <consensus/validation.h> | ||
| #include <evo/cbtx.h> | ||
| #include <evo/specialtxman.h> | ||
| #include <llmq/context.h> | ||
| #include <llmq/quorumsman.h> | ||
| #include <uint256.h> | ||
| #include <validation.h> | ||
|
|
||
| #include <cstdint> | ||
| #include <limits> | ||
|
|
||
| #include <boost/test/unit_test.hpp> | ||
|
|
||
| BOOST_AUTO_TEST_SUITE(evo_cbtx_tests) | ||
|
|
||
| // Out-of-range bestCLHeightDiff (>= pindex->nHeight) must be rejected with | ||
| // "bad-cbtx-cldiff" so that the subsequent GetAncestor() call sees a valid height. | ||
| // | ||
| // The defensive nullptr branch after GetAncestor() returns "bad-cbtx-cldiff-ancestor". | ||
| // That branch is unreachable in practice (the range check guarantees the requested | ||
| // ancestor height is in [0, pindex->nHeight - 1], for which GetAncestor() never returns | ||
| // nullptr) and cannot be exercised from a unit test: a fake CBlockIndex with no pprev | ||
| // would trip GetAncestor()'s `assert(pprev)` while walking, not return nullptr. | ||
| BOOST_FIXTURE_TEST_CASE(check_cbtx_best_chainlock_rejects_excessive_height_diff, RegTestingSetup) | ||
| { | ||
| const auto& consensus_params = Params().GetConsensus(); | ||
| const auto& chain = m_node.chainman->ActiveChain(); | ||
| auto& qman = *Assert(m_node.llmq_ctx)->qman; | ||
| auto& chainlocks = *Assert(m_node.chainlocks); | ||
|
|
||
| // Standalone fake block index with no predecessor, so the prevBlockCoinbaseChainlock | ||
| // branch is skipped and the validation path under test is reached directly. | ||
| CBlockIndex pindex; | ||
| pindex.nHeight = 5; | ||
|
|
||
| // A structurally-valid BLS signature is required for the IsValid() guard. | ||
| CBLSSecretKey sk; | ||
| sk.MakeNewKey(); | ||
| const bool legacy_scheme = bls::bls_legacy_scheme.load(); | ||
| CBLSSignature valid_sig = sk.Sign(uint256::ONE, legacy_scheme); | ||
| BOOST_REQUIRE(valid_sig.IsValid()); | ||
|
|
||
| CCbTx cbTx; | ||
| cbTx.nVersion = CCbTx::Version::CLSIG_AND_BALANCE; | ||
| cbTx.bestCLSignature = valid_sig; | ||
|
|
||
| // bestCLHeightDiff == nHeight: lower boundary of the rejected range. | ||
| cbTx.bestCLHeightDiff = static_cast<uint32_t>(pindex.nHeight); | ||
| BlockValidationState state; | ||
| BOOST_CHECK(!CheckCbTxBestChainlock(cbTx, &pindex, consensus_params, chain, qman, chainlocks, state)); | ||
| BOOST_CHECK_EQUAL(state.GetRejectReason(), "bad-cbtx-cldiff"); | ||
|
|
||
| // Upper boundary: uint32_t max. | ||
| cbTx.bestCLHeightDiff = std::numeric_limits<uint32_t>::max(); | ||
| BlockValidationState state_big; | ||
| BOOST_CHECK(!CheckCbTxBestChainlock(cbTx, &pindex, consensus_params, chain, qman, chainlocks, state_big)); | ||
| BOOST_CHECK_EQUAL(state_big.GetRejectReason(), "bad-cbtx-cldiff"); | ||
| } | ||
|
thepastaclaw marked this conversation as resolved.
|
||
|
|
||
| BOOST_AUTO_TEST_SUITE_END() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.