merge bitcoin#21553: Misc refactor

This commit is contained in:
Kittywhiskers Van Gogh 2024-02-05 22:05:02 +00:00 committed by pasta
parent 51128482a2
commit 689b5134ae
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
7 changed files with 36 additions and 22 deletions

View File

@ -13,6 +13,7 @@ TEST_UTIL_H = \
test/util/logging.h \
test/util/mining.h \
test/util/net.h \
test/util/script.h \
test/util/setup_common.h \
test/util/str.h \
test/util/transaction_utils.h \
@ -28,6 +29,7 @@ libtest_util_a_SOURCES = \
test/util/logging.cpp \
test/util/mining.cpp \
test/util/net.cpp \
test/util/script.cpp \
test/util/setup_common.cpp \
test/util/str.cpp \
test/util/transaction_utils.cpp \

View File

@ -229,11 +229,8 @@ FUZZ_TARGET_INIT(coins_view, initialize_coins_view)
// consensus/tx_verify.cpp:171: bool Consensus::CheckTxInputs(const CTransaction &, TxValidationState&, const CCoinsViewCache &, int, CAmount &): Assertion `!coin.IsSpent()' failed.
return;
}
try {
(void)Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max()), tx_fee_out);
assert(MoneyRange(tx_fee_out));
} catch (const std::runtime_error&) {
}
(void)Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max()), tx_fee_out);
assert(MoneyRange(tx_fee_out));
},
[&] {
const CTransaction transaction{random_mutable_transaction};

View File

@ -5,13 +5,11 @@
#include <pubkey.h>
#include <script/interpreter.h>
#include <streams.h>
#include <test/util/script.h>
#include <version.h>
#include <test/fuzz/fuzz.h>
/** Flags that are not forbidden by an assert */
static bool IsValidFlagCombination(unsigned flags);
FUZZ_TARGET(script_flags)
{
CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION);
@ -67,9 +65,3 @@ FUZZ_TARGET(script_flags)
return;
}
}
static bool IsValidFlagCombination(unsigned flags)
{
if (flags & SCRIPT_VERIFY_CLEANSTACK && ~flags & SCRIPT_VERIFY_P2SH) return false;
return true;
}

View File

@ -6,6 +6,8 @@
#include <script/interpreter.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
#include <test/util/script.h>
#include <cstdint>
#include <limits>
@ -45,14 +47,12 @@ FUZZ_TARGET(signature_checker)
{
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
const unsigned int flags = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
const std::string script_string_1 = fuzzed_data_provider.ConsumeRandomLengthString(65536);
const std::vector<uint8_t> script_bytes_1{script_string_1.begin(), script_string_1.end()};
const std::string script_string_2 = fuzzed_data_provider.ConsumeRandomLengthString(65536);
const std::vector<uint8_t> script_bytes_2{script_string_2.begin(), script_string_2.end()};
const auto script_1 = ConsumeScript(fuzzed_data_provider, 65536);
const auto script_2 = ConsumeScript(fuzzed_data_provider, 65536);
std::vector<std::vector<unsigned char>> stack;
(void)EvalScript(stack, {script_bytes_1.begin(), script_bytes_1.end()}, flags, FuzzedSignatureChecker(fuzzed_data_provider), SigVersion::BASE, nullptr);
if ((flags & SCRIPT_VERIFY_CLEANSTACK) != 0 && ((flags & SCRIPT_VERIFY_P2SH) == 0)) {
(void)EvalScript(stack, script_1, flags, FuzzedSignatureChecker(fuzzed_data_provider), SigVersion::BASE, nullptr);
if (!IsValidFlagCombination(flags)) {
return;
}
(void)VerifyScript({script_bytes_1.begin(), script_bytes_1.end()}, {script_bytes_2.begin(), script_bytes_2.end()}, flags, FuzzedSignatureChecker(fuzzed_data_provider), nullptr);
(void)VerifyScript(script_1, script_2, flags, FuzzedSignatureChecker(fuzzed_data_provider), nullptr);
}

View File

@ -250,7 +250,7 @@ CMutableTransaction ConsumeTransaction(FuzzedDataProvider& fuzzed_data_provider,
CScript ConsumeScript(FuzzedDataProvider& fuzzed_data_provider, const size_t max_length) noexcept
{
const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
return {b.begin(), b.end()};
}

12
src/test/util/script.cpp Normal file
View File

@ -0,0 +1,12 @@
// Copyright (c) 2021 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 <script/interpreter.h>
#include <test/util/script.h>
bool IsValidFlagCombination(unsigned flags)
{
if (flags & SCRIPT_VERIFY_CLEANSTACK && ~flags & SCRIPT_VERIFY_P2SH) return false;
return true;
}

11
src/test/util/script.h Normal file
View File

@ -0,0 +1,11 @@
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_TEST_UTIL_SCRIPT_H
#define BITCOIN_TEST_UTIL_SCRIPT_H
/** Flags that are not forbidden by an assert in script validation */
bool IsValidFlagCombination(unsigned flags);
#endif // BITCOIN_TEST_UTIL_SCRIPT_H