Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
consensus
tx_check.cpp
Go to the documentation of this file.
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
{
21
// Basic checks that don't depend on any context
22
if
(tx.
vin
.empty())
23
return
state.
Invalid
(
TxValidationResult::TX_CONSENSUS
,
"bad-txns-vin-empty"
);
24
if
(tx.
vout
.empty())
25
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
if
(
::GetSerializeSize
(
TX_NO_WITNESS
(tx)) *
WITNESS_SCALE_FACTOR
>
MAX_BLOCK_WEIGHT
) {
28
return
state.
Invalid
(
TxValidationResult::TX_CONSENSUS
,
"bad-txns-oversize"
);
29
}
30
31
// Check for negative or overflow output values (see CVE-2010-5139)
32
CAmount
nValueOut = 0;
33
for
(
const
auto
& txout : tx.
vout
)
34
{
35
if
(txout.nValue < 0)
36
return
state.
Invalid
(
TxValidationResult::TX_CONSENSUS
,
"bad-txns-vout-negative"
);
37
if
(txout.nValue >
MAX_MONEY
)
38
return
state.
Invalid
(
TxValidationResult::TX_CONSENSUS
,
"bad-txns-vout-toolarge"
);
39
nValueOut += txout.nValue;
40
if
(!
MoneyRange
(nValueOut))
41
return
state.
Invalid
(
TxValidationResult::TX_CONSENSUS
,
"bad-txns-txouttotal-toolarge"
);
42
}
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
std::set<COutPoint> vInOutPoints;
50
for
(
const
auto
& txin : tx.
vin
) {
51
if
(!vInOutPoints.insert(txin.prevout).second)
52
return
state.
Invalid
(
TxValidationResult::TX_CONSENSUS
,
"bad-txns-inputs-duplicate"
);
53
}
54
55
if
(tx.
IsCoinBase
())
56
{
57
if
(tx.
vin
[0].scriptSig.size() < 2 || tx.
vin
[0].scriptSig.size() > 100)
58
return
state.
Invalid
(
TxValidationResult::TX_CONSENSUS
,
"bad-cb-length"
);
59
}
60
else
61
{
62
for
(
const
auto
& txin : tx.
vin
)
63
if
(txin.prevout.IsNull())
64
return
state.
Invalid
(
TxValidationResult::TX_CONSENSUS
,
"bad-txns-prevout-null"
);
65
}
66
67
return
true
;
68
}
amount.h
MAX_MONEY
constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition
amount.h:26
MoneyRange
bool MoneyRange(const CAmount &nValue)
Definition
amount.h:27
CAmount
int64_t CAmount
Amount in satoshis (Can be negative).
Definition
amount.h:12
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition
transaction.h:287
CTransaction::vout
const std::vector< CTxOut > vout
Definition
transaction.h:298
CTransaction::IsCoinBase
bool IsCoinBase() const
Definition
transaction.h:347
CTransaction::vin
const std::vector< CTxIn > vin
Definition
transaction.h:297
TxValidationState
Definition
validation.h:132
ValidationState::Invalid
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition
validation.h:95
validation.h
TxValidationResult::TX_CONSENSUS
@ TX_CONSENSUS
invalid by consensus rules
Definition
validation.h:32
consensus.h
MAX_BLOCK_WEIGHT
constexpr unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule).
Definition
consensus.h:15
WITNESS_SCALE_FACTOR
constexpr int WITNESS_SCALE_FACTOR
Definition
consensus.h:21
transaction.h
TX_NO_WITNESS
constexpr TransactionSerParams TX_NO_WITNESS
Definition
transaction.h:182
script.h
serialize.h
GetSerializeSize
uint64_t GetSerializeSize(const T &t)
Definition
serialize.h:1157
CheckTransaction
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
Definition
tx_check.cpp:19
tx_check.h
Generated on
for Bitcoin Core by
1.18.0