forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 0
Merge bitcoin/bitcoin#17860: fuzz: BIP 30, CVE-2018-17144 #387
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
DashCoreAutoGuix
wants to merge
4
commits into
backport-0.26-batch-415
Choose a base branch
from
backport-0.26-batch-415-pr-17860
base: backport-0.26-batch-415
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1994e38
Merge bitcoin/bitcoin#17860: fuzz: BIP 30, CVE-2018-17144
fanquake 36bc557
fix: add missing LoadVerifyActivateChainstate implementation
claude 1bd9fdf
Update src/test/fuzz/tx_pool.cpp
PastaPastaPasta 8b4f853
fix: correct typo 'acess' -> 'access' in tx_pool.cpp comment
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
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,167 @@ | ||
| // Copyright (c) 2020 The Bitcoin Core developers | ||
| // Distributed under the MIT software license, see the accompanying | ||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
|
||
| #include <chainparams.h> | ||
| #include <consensus/consensus.h> | ||
| #include <consensus/merkle.h> | ||
| #include <node/coinstats.h> | ||
| #include <node/miner.h> | ||
| #include <script/interpreter.h> | ||
| #include <streams.h> | ||
| #include <test/fuzz/FuzzedDataProvider.h> | ||
| #include <test/fuzz/fuzz.h> | ||
| #include <test/fuzz/util.h> | ||
| #include <test/util/mining.h> | ||
| #include <test/util/setup_common.h> | ||
| #include <validation.h> | ||
| #include <version.h> | ||
|
|
||
| FUZZ_TARGET(utxo_total_supply) | ||
| { | ||
| /** The testing setup that creates a chainman only (no chainstate) */ | ||
| ChainTestingSetup test_setup{ | ||
| CBaseChainParams::REGTEST, | ||
| { | ||
| "-testactivationheight=bip34@2", | ||
| }, | ||
| }; | ||
| // Create chainstate | ||
| // In Dash, chainstate is loaded in TestingSetup constructor | ||
| auto& node{test_setup.m_node}; | ||
| auto& chainman{*Assert(test_setup.m_node.chainman)}; | ||
| FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); | ||
|
|
||
| const auto ActiveHeight = [&]() { | ||
| LOCK(cs_main); | ||
| return chainman.ActiveHeight(); | ||
| }; | ||
| const auto PrepareNextBlock = [&]() { | ||
| // Use OP_FALSE to avoid BIP30 check from hitting early | ||
| auto block = PrepareBlock(node, CScript{} << OP_FALSE); | ||
| // Replace OP_FALSE with OP_TRUE | ||
| { | ||
| CMutableTransaction tx{*block->vtx.back()}; | ||
| tx.vout.at(0).scriptPubKey = CScript{} << OP_TRUE; | ||
| block->vtx.back() = MakeTransactionRef(tx); | ||
| } | ||
| return block; | ||
| }; | ||
|
|
||
| /** The block template this fuzzer is working on */ | ||
| auto current_block = PrepareNextBlock(); | ||
| /** Append-only set of tx outpoints, entries are not removed when spent */ | ||
| std::vector<std::pair<COutPoint, CTxOut>> txos; | ||
| /** The utxo stats at the chain tip */ | ||
| std::unique_ptr<node::CCoinsStats> utxo_stats = std::make_unique<node::CCoinsStats>(node::CoinStatsHashType::NONE); | ||
| /** The total amount of coins in the utxo set */ | ||
| CAmount circulation{0}; | ||
|
|
||
|
|
||
| // Store the tx out in the txo map | ||
| const auto StoreLastTxo = [&]() { | ||
| // get last tx | ||
| const CTransaction& tx = *current_block->vtx.back(); | ||
| // get last out | ||
| const uint32_t i = tx.vout.size() - 1; | ||
| // store it | ||
| txos.emplace_back(COutPoint{tx.GetHash(), i}, tx.vout.at(i)); | ||
| if (current_block->vtx.size() == 1 && tx.vout.at(i).scriptPubKey[0] == OP_RETURN) { | ||
| // also store coinbase | ||
| const uint32_t i = tx.vout.size() - 2; | ||
| txos.emplace_back(COutPoint{tx.GetHash(), i}, tx.vout.at(i)); | ||
| } | ||
| }; | ||
| const auto AppendRandomTxo = [&](CMutableTransaction& tx) { | ||
| const auto& txo = txos.at(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, txos.size() - 1)); | ||
| tx.vin.emplace_back(txo.first); | ||
| tx.vout.emplace_back(txo.second.nValue, txo.second.scriptPubKey); // "Forward" coin with no fee | ||
| }; | ||
| const auto UpdateUtxoStats = [&]() { | ||
| LOCK(cs_main); | ||
| chainman.ActiveChainstate().ForceFlushStateToDisk(); | ||
| node::CCoinsStats new_stats{node::CoinStatsHashType::NONE}; | ||
| [[maybe_unused]] bool success = node::GetUTXOStats(&chainman.ActiveChainstate().CoinsDB(), chainman.m_blockman, new_stats, {}); | ||
| assert(success); | ||
| utxo_stats = std::make_unique<node::CCoinsStats>(std::move(new_stats)); | ||
| // Check that miner can't print more money than they are allowed to | ||
| assert(circulation == utxo_stats->total_amount); | ||
| }; | ||
|
|
||
|
|
||
| // Update internal state to chain tip | ||
| StoreLastTxo(); | ||
| UpdateUtxoStats(); | ||
| assert(ActiveHeight() == 0); | ||
| // Get at which height we duplicate the coinbase | ||
| // Assuming that the fuzzer will mine relatively short chains (less than 200 blocks), we want the duplicate coinbase to be not too high. | ||
| // Up to 2000 seems reasonable. | ||
| int64_t duplicate_coinbase_height = fuzzed_data_provider.ConsumeIntegralInRange(0, 20 * COINBASE_MATURITY); | ||
| // Always pad with OP_0 at the end to avoid bad-cb-length error | ||
| const CScript duplicate_coinbase_script = CScript() << duplicate_coinbase_height << OP_0; | ||
| // Mine the first block with this duplicate | ||
| current_block = PrepareNextBlock(); | ||
| StoreLastTxo(); | ||
|
|
||
| { | ||
| // Create duplicate (CScript should match exact format as in CreateNewBlock) | ||
| CMutableTransaction tx{*current_block->vtx.front()}; | ||
| tx.vin.at(0).scriptSig = duplicate_coinbase_script; | ||
|
|
||
| // Mine block and create next block template | ||
| current_block->vtx.front() = MakeTransactionRef(tx); | ||
| } | ||
| current_block->hashMerkleRoot = BlockMerkleRoot(*current_block); | ||
| assert(!MineBlock(node, current_block).IsNull()); | ||
| circulation += GetBlockSubsidyInner(current_block->nBits, ActiveHeight(), Params().GetConsensus(), false); | ||
|
|
||
| assert(ActiveHeight() == 1); | ||
| UpdateUtxoStats(); | ||
| current_block = PrepareNextBlock(); | ||
| StoreLastTxo(); | ||
|
|
||
| LIMITED_WHILE(fuzzed_data_provider.remaining_bytes(), 100'000) | ||
| { | ||
| CallOneOf( | ||
| fuzzed_data_provider, | ||
| [&] { | ||
| // Append an input-output pair to the last tx in the current block | ||
| CMutableTransaction tx{*current_block->vtx.back()}; | ||
| AppendRandomTxo(tx); | ||
| current_block->vtx.back() = MakeTransactionRef(tx); | ||
| StoreLastTxo(); | ||
| }, | ||
| [&] { | ||
| // Append a tx to the list of txs in the current block | ||
| CMutableTransaction tx{}; | ||
| AppendRandomTxo(tx); | ||
| current_block->vtx.push_back(MakeTransactionRef(tx)); | ||
| StoreLastTxo(); | ||
| }, | ||
| [&] { | ||
| // Append the current block to the active chain | ||
| current_block->hashMerkleRoot = BlockMerkleRoot(*current_block); | ||
| const bool was_valid = !MineBlock(node, current_block).IsNull(); | ||
|
|
||
| const auto prev_hash = utxo_stats->hashSerialized; | ||
| if (was_valid) { | ||
| circulation += GetBlockSubsidyInner(current_block->nBits, ActiveHeight(), Params().GetConsensus(), false); | ||
|
|
||
| if (duplicate_coinbase_height == ActiveHeight()) { | ||
| // we mined the duplicate coinbase | ||
| assert(current_block->vtx.at(0)->vin.at(0).scriptSig == duplicate_coinbase_script); | ||
| } | ||
| } | ||
|
|
||
| UpdateUtxoStats(); | ||
|
|
||
| if (!was_valid) { | ||
| // utxo stats must not change | ||
| assert(prev_hash == utxo_stats->hashSerialized); | ||
| } | ||
|
|
||
| current_block = PrepareNextBlock(); | ||
| StoreLastTxo(); | ||
| }); | ||
| } | ||
| } |
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
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.