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