/root/bitcoin/src/test/fuzz/transaction.cpp
Line | Count | Source |
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 | | void initialize_transaction() |
26 | 0 | { |
27 | 0 | SelectParams(ChainType::REGTEST); |
28 | 0 | } |
29 | | |
30 | | FUZZ_TARGET(transaction, .init = initialize_transaction) |
31 | 0 | { |
32 | 0 | SeedRandomStateForTest(SeedRand::ZEROS); |
33 | 0 | SpanReader ds{buffer}; |
34 | 0 | bool valid_tx = true; |
35 | 0 | const CTransaction tx = [&] { |
36 | 0 | try { |
37 | 0 | return CTransaction(deserialize, TX_WITH_WITNESS, ds); |
38 | 0 | } catch (const std::ios_base::failure&) { |
39 | 0 | valid_tx = false; |
40 | 0 | return CTransaction{CMutableTransaction{}}; |
41 | 0 | } |
42 | 0 | }(); |
43 | 0 | bool valid_mutable_tx = true; |
44 | 0 | CMutableTransaction mutable_tx; |
45 | 0 | try { |
46 | 0 | SpanReader{buffer} >> TX_WITH_WITNESS(mutable_tx); |
47 | 0 | } catch (const std::ios_base::failure&) { |
48 | 0 | valid_mutable_tx = false; |
49 | 0 | } |
50 | 0 | assert(valid_tx == valid_mutable_tx); Branch (50:5): [True: 0, False: 0]
|
51 | 0 | if (!valid_tx) { Branch (51:9): [True: 0, False: 0]
|
52 | 0 | return; |
53 | 0 | } |
54 | | |
55 | 0 | { |
56 | 0 | TxValidationState state_with_dupe_check; |
57 | 0 | const bool res{CheckTransaction(tx, state_with_dupe_check)}; |
58 | 0 | Assert(res == state_with_dupe_check.IsValid()); |
59 | 0 | } |
60 | |
|
61 | 0 | const CFeeRate dust_relay_fee{DUST_RELAY_TX_FEE}; |
62 | 0 | std::string reason; |
63 | 0 | const bool is_standard_with_permit_bare_multisig = IsStandardTx(tx, std::nullopt, /* permit_bare_multisig= */ true, dust_relay_fee, reason); |
64 | 0 | const bool is_standard_without_permit_bare_multisig = IsStandardTx(tx, std::nullopt, /* permit_bare_multisig= */ false, dust_relay_fee, reason); |
65 | 0 | if (is_standard_without_permit_bare_multisig) { Branch (65:9): [True: 0, False: 0]
|
66 | 0 | assert(is_standard_with_permit_bare_multisig); Branch (66:9): [True: 0, False: 0]
|
67 | 0 | } |
68 | | |
69 | 0 | (void)tx.GetHash(); |
70 | 0 | (void)tx.ComputeTotalSize(); |
71 | 0 | try { |
72 | 0 | (void)tx.GetValueOut(); |
73 | 0 | } catch (const std::runtime_error&) { |
74 | 0 | } |
75 | 0 | (void)tx.GetWitnessHash(); |
76 | 0 | (void)tx.HasWitness(); |
77 | 0 | (void)tx.IsCoinBase(); |
78 | 0 | (void)tx.IsNull(); |
79 | 0 | (void)tx.ToString(); |
80 | |
|
81 | 0 | (void)EncodeHexTx(tx); |
82 | 0 | (void)GetLegacySigOpCount(tx); |
83 | 0 | (void)GetTransactionWeight(tx); |
84 | 0 | (void)GetVirtualTransactionSize(tx); |
85 | 0 | (void)IsFinalTx(tx, /* nBlockHeight= */ 1024, /* nBlockTime= */ 1024); |
86 | 0 | (void)RecursiveDynamicUsage(tx); |
87 | 0 | (void)SignalsOptInRBF(tx); |
88 | |
|
89 | 0 | const CCoinsViewCache coins_view_cache{&CoinsViewEmpty::Get()}; |
90 | 0 | (void)ValidateInputsStandardness(tx, coins_view_cache); |
91 | 0 | (void)IsWitnessStandard(tx, coins_view_cache); |
92 | |
|
93 | 0 | if (tx.ComputeTotalSize() < 250'000) { // Avoid high memory usage (with msan) due to json encoding Branch (93:9): [True: 0, False: 0]
|
94 | 0 | { |
95 | 0 | UniValue u{UniValue::VOBJ}; |
96 | 0 | TxToUniv(tx, /*block_hash=*/uint256::ZERO, /*entry=*/u); |
97 | 0 | } |
98 | 0 | { |
99 | 0 | UniValue u{UniValue::VOBJ}; |
100 | 0 | TxToUniv(tx, /*block_hash=*/uint256::ONE, /*entry=*/u); |
101 | 0 | } |
102 | 0 | } |
103 | 0 | } |