/bitcoin/src/consensus/tx_check.cpp
Line | Count | Source |
1 | | // Copyright (c) 2017-2021 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 <consensus/tx_check.h> |
6 | | |
7 | | #include <consensus/amount.h> |
8 | | #include <primitives/transaction.h> |
9 | | #include <consensus/validation.h> |
10 | | |
11 | | bool CheckTransaction(const CTransaction& tx, TxValidationState& state) |
12 | 2.89M | { |
13 | | // Basic checks that don't depend on any context |
14 | 2.89M | if (tx.vin.empty()) Branch (14:9): [True: 57, False: 2.89M]
|
15 | 57 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vin-empty"); |
16 | 2.89M | if (tx.vout.empty()) Branch (16:9): [True: 135, False: 2.89M]
|
17 | 135 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-empty"); |
18 | | // Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability) |
19 | 2.89M | if (::GetSerializeSize(TX_NO_WITNESS(tx)) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT) { Branch (19:9): [True: 0, False: 2.89M]
|
20 | 0 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-oversize"); |
21 | 0 | } |
22 | | |
23 | | // Check for negative or overflow output values (see CVE-2010-5139) |
24 | 2.89M | CAmount nValueOut = 0; |
25 | 2.89M | for (const auto& txout : tx.vout) Branch (25:28): [True: 5.23M, False: 2.89M]
|
26 | 5.23M | { |
27 | 5.23M | if (txout.nValue < 0) Branch (27:13): [True: 21, False: 5.23M]
|
28 | 21 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-negative"); |
29 | 5.23M | if (txout.nValue > MAX_MONEY) Branch (29:13): [True: 37, False: 5.23M]
|
30 | 37 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-toolarge"); |
31 | 5.23M | nValueOut += txout.nValue; |
32 | 5.23M | if (!MoneyRange(nValueOut)) Branch (32:13): [True: 2, False: 5.23M]
|
33 | 2 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-txouttotal-toolarge"); |
34 | 5.23M | } |
35 | | |
36 | | // Check for duplicate inputs (see CVE-2018-17144) |
37 | | // While Consensus::CheckTxInputs does check if all inputs of a tx are available, and UpdateCoins marks all inputs |
38 | | // of a tx as spent, it does not check if the tx has duplicate inputs. |
39 | | // Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of |
40 | | // the underlying coins database. |
41 | 2.89M | std::set<COutPoint> vInOutPoints; |
42 | 3.00M | for (const auto& txin : tx.vin) { Branch (42:27): [True: 3.00M, False: 2.89M]
|
43 | 3.00M | if (!vInOutPoints.insert(txin.prevout).second) Branch (43:13): [True: 121, False: 3.00M]
|
44 | 121 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate"); |
45 | 3.00M | } |
46 | | |
47 | 2.89M | if (tx.IsCoinBase()) Branch (47:9): [True: 2.26M, False: 628k]
|
48 | 2.26M | { |
49 | 2.26M | if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100) Branch (49:13): [True: 0, False: 2.26M]
Branch (49:47): [True: 0, False: 2.26M]
|
50 | 0 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length"); |
51 | 2.26M | } |
52 | 628k | else |
53 | 628k | { |
54 | 628k | for (const auto& txin : tx.vin) Branch (54:31): [True: 739k, False: 628k]
|
55 | 739k | if (txin.prevout.IsNull()) Branch (55:17): [True: 0, False: 739k]
|
56 | 0 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null"); |
57 | 628k | } |
58 | | |
59 | 2.89M | return true; |
60 | 2.89M | } |