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 <primitives/transaction.h>
6 : :
7 : : #include <chainparams.h>
8 : : #include <coins.h>
9 : : #include <consensus/tx_check.h>
10 : : #include <consensus/tx_verify.h>
11 : : #include <consensus/validation.h>
12 : : #include <core_io.h>
13 : : #include <core_memusage.h>
14 : : #include <policy/policy.h>
15 : : #include <policy/settings.h>
16 : : #include <streams.h>
17 : : #include <test/fuzz/fuzz.h>
18 : : #include <test/util/random.h>
19 : : #include <univalue.h>
20 : : #include <util/chaintype.h>
21 : : #include <util/check.h>
22 : : #include <util/rbf.h>
23 : : #include <validation.h>
24 : :
25 : :
26 : 1 : void initialize_transaction()
27 : : {
28 : 1 : SelectParams(ChainType::REGTEST);
29 : 1 : }
30 : :
31 [ + - ]: 1989 : FUZZ_TARGET(transaction, .init = initialize_transaction)
32 : : {
33 : 1511 : SeedRandomStateForTest(SeedRand::ZEROS);
34 : 1511 : SpanReader ds{buffer};
35 : 1511 : bool valid_tx = true;
36 : 3022 : const CTransaction tx = [&] {
37 : 1511 : try {
38 [ + + ]: 1511 : return CTransaction(deserialize, TX_WITH_WITNESS, ds);
39 [ - + ]: 74 : } catch (const std::ios_base::failure&) {
40 : 74 : valid_tx = false;
41 [ + - + - ]: 74 : return CTransaction{CMutableTransaction{}};
42 : 74 : }
43 : 1511 : }();
44 : 1511 : bool valid_mutable_tx = true;
45 [ + - ]: 1511 : CMutableTransaction mutable_tx;
46 : 1511 : try {
47 [ + + ]: 1511 : SpanReader{buffer} >> TX_WITH_WITNESS(mutable_tx);
48 [ - + ]: 74 : } catch (const std::ios_base::failure&) {
49 : 74 : valid_mutable_tx = false;
50 : 74 : }
51 [ - + ]: 1511 : assert(valid_tx == valid_mutable_tx);
52 [ + + ]: 1511 : if (!valid_tx) {
53 : 74 : return;
54 : : }
55 : :
56 : 1437 : {
57 [ + - ]: 1437 : TxValidationState state_with_dupe_check;
58 [ + - ]: 1437 : const bool res{CheckTransaction(tx, state_with_dupe_check)};
59 [ + - ]: 1437 : Assert(res == state_with_dupe_check.IsValid());
60 : 0 : }
61 : :
62 : 1437 : const CFeeRate dust_relay_fee{DUST_RELAY_TX_FEE};
63 [ + - ]: 1437 : std::string reason;
64 [ + - ]: 1437 : const bool is_standard_with_permit_bare_multisig = IsStandardTx(tx, std::nullopt, /* permit_bare_multisig= */ true, dust_relay_fee, reason);
65 [ + - ]: 1437 : const bool is_standard_without_permit_bare_multisig = IsStandardTx(tx, std::nullopt, /* permit_bare_multisig= */ false, dust_relay_fee, reason);
66 [ + + ]: 1437 : if (is_standard_without_permit_bare_multisig) {
67 [ - + ]: 47 : assert(is_standard_with_permit_bare_multisig);
68 : : }
69 : :
70 : 1437 : (void)tx.GetHash();
71 [ + - ]: 1437 : (void)tx.ComputeTotalSize();
72 : 1437 : try {
73 [ + + ]: 1437 : (void)tx.GetValueOut();
74 [ - + ]: 1077 : } catch (const std::runtime_error&) {
75 : 1077 : }
76 : 1437 : (void)tx.GetWitnessHash();
77 : 1437 : (void)tx.HasWitness();
78 : 1437 : (void)tx.IsCoinBase();
79 : 1437 : (void)tx.IsNull();
80 [ + - ]: 1437 : (void)tx.ToString();
81 : :
82 [ + - ]: 1437 : (void)EncodeHexTx(tx);
83 [ + - ]: 1437 : (void)GetLegacySigOpCount(tx);
84 : 1437 : (void)GetTransactionWeight(tx);
85 [ + - ]: 1437 : (void)GetVirtualTransactionSize(tx);
86 [ + - ]: 1437 : (void)IsFinalTx(tx, /* nBlockHeight= */ 1024, /* nBlockTime= */ 1024);
87 : 1437 : (void)RecursiveDynamicUsage(tx);
88 [ + - ]: 1437 : (void)SignalsOptInRBF(tx);
89 : :
90 [ + - + - ]: 1437 : const CCoinsViewCache coins_view_cache{&CoinsViewEmpty::Get()};
91 [ + - ]: 1437 : (void)ValidateInputsStandardness(tx, coins_view_cache);
92 [ + - ]: 1437 : (void)IsWitnessStandard(tx, coins_view_cache);
93 : :
94 [ + - + + ]: 1437 : if (tx.ComputeTotalSize() < 250'000) { // Avoid high memory usage (with msan) due to json encoding
95 : 1351 : {
96 : 1351 : UniValue u{UniValue::VOBJ};
97 [ + - ]: 1351 : TxToUniv(tx, /*block_hash=*/uint256::ZERO, /*entry=*/u);
98 : 1351 : }
99 : 1351 : {
100 : 1351 : UniValue u{UniValue::VOBJ};
101 [ + - ]: 1351 : TxToUniv(tx, /*block_hash=*/uint256::ONE, /*entry=*/u);
102 : 1351 : }
103 : : }
104 : 4459 : }
|