/root/bitcoin/src/consensus/tx_check.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 9.56k | { |
13 | | // Basic checks that don't depend on any context |
14 | 9.56k | if (tx.vin.empty()) |
15 | 0 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vin-empty"); |
16 | 9.56k | if (tx.vout.empty()) |
17 | 0 | 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 | 9.56k | if (::GetSerializeSize(TX_NO_WITNESS(tx)) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT) { |
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 | 9.56k | CAmount nValueOut = 0; |
25 | 9.56k | for (const auto& txout : tx.vout) |
26 | 9.56k | { |
27 | 9.56k | if (txout.nValue < 0) |
28 | 0 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-negative"); |
29 | 9.56k | if (txout.nValue > MAX_MONEY) |
30 | 0 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-toolarge"); |
31 | 9.56k | nValueOut += txout.nValue; |
32 | 9.56k | if (!MoneyRange(nValueOut)) |
33 | 0 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-txouttotal-toolarge"); |
34 | 9.56k | } |
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 | 9.56k | std::set<COutPoint> vInOutPoints; |
42 | 9.56k | for (const auto& txin : tx.vin) { |
43 | 9.56k | if (!vInOutPoints.insert(txin.prevout).second) |
44 | 0 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate"); |
45 | 9.56k | } |
46 | | |
47 | 9.56k | if (tx.IsCoinBase()) |
48 | 9.56k | { |
49 | 9.56k | if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100) |
50 | 0 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length"); |
51 | 9.56k | } |
52 | 0 | else |
53 | 0 | { |
54 | 0 | for (const auto& txin : tx.vin) |
55 | 0 | if (txin.prevout.IsNull()) |
56 | 0 | return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null"); |
57 | 0 | } |
58 | | |
59 | 9.56k | return true; |
60 | 9.56k | } |