Branch data Line data Source code
1 : : // Copyright (c) 2019-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <chainparams.h>
6 : : #include <coins.h>
7 : : #include <consensus/tx_check.h>
8 : : #include <consensus/tx_verify.h>
9 : : #include <consensus/validation.h>
10 : : #include <core_io.h>
11 : : #include <core_memusage.h>
12 : : #include <policy/policy.h>
13 : : #include <policy/settings.h>
14 : : #include <primitives/transaction.h>
15 : : #include <streams.h>
16 : : #include <test/fuzz/fuzz.h>
17 : : #include <test/util/random.h>
18 : : #include <univalue.h>
19 : : #include <util/chaintype.h>
20 : : #include <util/rbf.h>
21 : : #include <validation.h>
22 : :
23 : : #include <cassert>
24 : :
25 : 1 : void initialize_transaction()
26 : : {
27 : 1 : SelectParams(ChainType::REGTEST);
28 : 1 : }
29 : :
30 [ + - ]: 2109 : FUZZ_TARGET(transaction, .init = initialize_transaction)
31 : : {
32 : 1655 : SeedRandomStateForTest(SeedRand::ZEROS);
33 : 1655 : SpanReader ds{buffer};
34 : 1655 : bool valid_tx = true;
35 : 3310 : const CTransaction tx = [&] {
36 : 1655 : try {
37 [ + + ]: 1655 : return CTransaction(deserialize, TX_WITH_WITNESS, ds);
38 [ - + ]: 84 : } catch (const std::ios_base::failure&) {
39 : 84 : valid_tx = false;
40 [ + - + - ]: 84 : return CTransaction{CMutableTransaction{}};
41 : 84 : }
42 : 1655 : }();
43 : 1655 : bool valid_mutable_tx = true;
44 [ + - ]: 1655 : CMutableTransaction mutable_tx;
45 : 1655 : try {
46 [ + + ]: 1655 : SpanReader{buffer} >> TX_WITH_WITNESS(mutable_tx);
47 [ - + ]: 84 : } catch (const std::ios_base::failure&) {
48 : 84 : valid_mutable_tx = false;
49 : 84 : }
50 [ - + ]: 1655 : assert(valid_tx == valid_mutable_tx);
51 [ + + ]: 1655 : if (!valid_tx) {
52 : 84 : return;
53 : : }
54 : :
55 : 1571 : {
56 [ + - ]: 1571 : TxValidationState state_with_dupe_check;
57 [ + - ]: 1571 : const bool res{CheckTransaction(tx, state_with_dupe_check)};
58 [ - + ]: 1571 : Assert(res == state_with_dupe_check.IsValid());
59 : 0 : }
60 : :
61 [ + - ]: 1571 : const CFeeRate dust_relay_fee{DUST_RELAY_TX_FEE};
62 [ + - ]: 1571 : std::string reason;
63 [ + - ]: 1571 : const bool is_standard_with_permit_bare_multisig = IsStandardTx(tx, std::nullopt, /* permit_bare_multisig= */ true, dust_relay_fee, reason);
64 [ + - ]: 1571 : const bool is_standard_without_permit_bare_multisig = IsStandardTx(tx, std::nullopt, /* permit_bare_multisig= */ false, dust_relay_fee, reason);
65 [ + + ]: 1571 : if (is_standard_without_permit_bare_multisig) {
66 [ - + ]: 58 : assert(is_standard_with_permit_bare_multisig);
67 : : }
68 : :
69 : 1571 : (void)tx.GetHash();
70 [ + - ]: 1571 : (void)tx.ComputeTotalSize();
71 : 1571 : try {
72 [ + + ]: 1571 : (void)tx.GetValueOut();
73 [ - + ]: 1161 : } catch (const std::runtime_error&) {
74 : 1161 : }
75 : 1571 : (void)tx.GetWitnessHash();
76 : 1571 : (void)tx.HasWitness();
77 : 1571 : (void)tx.IsCoinBase();
78 : 1571 : (void)tx.IsNull();
79 [ + - ]: 1571 : (void)tx.ToString();
80 : :
81 [ + - ]: 1571 : (void)EncodeHexTx(tx);
82 [ + - ]: 1571 : (void)GetLegacySigOpCount(tx);
83 : 1571 : (void)GetTransactionWeight(tx);
84 [ + - ]: 1571 : (void)GetVirtualTransactionSize(tx);
85 [ + - ]: 1571 : (void)IsFinalTx(tx, /* nBlockHeight= */ 1024, /* nBlockTime= */ 1024);
86 : 1571 : (void)RecursiveDynamicUsage(tx);
87 [ + - ]: 1571 : (void)SignalsOptInRBF(tx);
88 : :
89 : 1571 : CCoinsView coins_view;
90 [ + - ]: 1571 : const CCoinsViewCache coins_view_cache(&coins_view);
91 [ + - ]: 1571 : (void)AreInputsStandard(tx, coins_view_cache);
92 [ + - ]: 1571 : (void)IsWitnessStandard(tx, coins_view_cache);
93 : :
94 [ + - + + ]: 1571 : if (tx.ComputeTotalSize() < 250'000) { // Avoid high memory usage (with msan) due to json encoding
95 : 1478 : {
96 : 1478 : UniValue u{UniValue::VOBJ};
97 [ + - ]: 1478 : TxToUniv(tx, /*block_hash=*/uint256::ZERO, /*entry=*/u);
98 : 1478 : }
99 : 1478 : {
100 : 1478 : UniValue u{UniValue::VOBJ};
101 [ + - ]: 1478 : TxToUniv(tx, /*block_hash=*/uint256::ONE, /*entry=*/u);
102 : 1478 : }
103 : : }
104 : 4881 : }
|