PRCYCoin  2.0.0.7rc1
P2P Digital Currency
tx_verify.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2017 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 "tx_verify.h"
6 
7 #include "consensus/consensus.h"
8 #include "main.h"
9 #include "script/interpreter.h"
10 #include "timedata.h"
11 
12 bool IsFinalTx(const CTransaction& tx, int nBlockHeight, int64_t nBlockTime)
13 {
15  // Time based nLockTime implemented in 0.1.6
16  if (tx.nLockTime == 0)
17  return true;
18  if (nBlockHeight == 0)
19  nBlockHeight = chainActive.Height();
20  if (nBlockTime == 0)
21  nBlockTime = GetAdjustedTime();
22  if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
23  return true;
24  for (const CTxIn& txin : tx.vin)
25  if (!txin.IsFinal())
26  return false;
27  return true;
28 }
29 
30 unsigned int GetLegacySigOpCount(const CTransaction& tx)
31 {
32  unsigned int nSigOps = 0;
33  for (const CTxIn& txin : tx.vin) {
34  nSigOps += txin.scriptSig.GetSigOpCount(false);
35  }
36  for (const CTxOut& txout : tx.vout) {
37  nSigOps += txout.scriptPubKey.GetSigOpCount(false);
38  }
39  return nSigOps;
40 }
41 
42 bool CheckTransaction(const CTransaction& tx, bool fRejectBadUTXO, CValidationState& state)
43 {
44  // Basic checks that don't depend on any context
45  if (tx.vin.empty())
46  return state.DoS(10, error("CheckTransaction() : vin empty"),
47  REJECT_INVALID, "bad-txns-vin-empty");
48  if (tx.vout.empty())
49  return state.DoS(10, error("CheckTransaction() : vout empty"),
50  REJECT_INVALID, "bad-txns-vout-empty");
51 
52  // Size limits
53  unsigned int nMaxSize = MAX_STANDARD_TX_SIZE;
54 
55  if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) > nMaxSize)
56  return state.DoS(100, error("CheckTransaction() : size limits failed"),
57  REJECT_INVALID, "bad-txns-oversize");
58 
59  // Check for negative or overflow output values
60  for (const CTxOut& txout : tx.vout) {
61  if (txout.IsEmpty() && !tx.IsCoinBase() && !tx.IsCoinStake())
62  return state.DoS(100, error("CheckTransaction(): txout empty for user transaction"));
63 
64  if (txout.nValue < 0)
65  return state.DoS(100, error("CheckTransaction() : txout.nValue negative"),
66  REJECT_INVALID, "bad-txns-vout-negative");
67  if (txout.nValue > MAX_MONEY_OUT) {
68  return state.DoS(100, error("CheckTransaction() : txout.nValue too high"),
69  REJECT_INVALID, "bad-txns-vout-toolarge");
70  }
71  }
72 
73  if (tx.IsCoinBase()) {
74  if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 150)
75  return state.DoS(100, error("CheckTransaction() : coinbase script size=%d", tx.vin[0].scriptSig.size()),
76  REJECT_INVALID, "bad-cb-length");
77  }
78 
79  return true;
80 }
CTxIn
An input of a transaction.
Definition: transaction.h:83
chainActive
CChain chainActive
The currently-connected chain of blocks.
Definition: main.cpp:70
timedata.h
CTransaction::nLockTime
const uint32_t nLockTime
Definition: transaction.h:287
CTxOut::IsEmpty
bool IsEmpty() const
Definition: transaction.h:220
interpreter.h
GetSerializeSize
unsigned int GetSerializeSize(char a, int, int=0)
Definition: serialize.h:194
GetLegacySigOpCount
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Count ECDSA signature operations the old-fashioned (pre-0.6) way.
Definition: tx_verify.cpp:30
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:159
IsFinalTx
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
Check if transaction is final and can be included in a block with the specified height and time.
Definition: tx_verify.cpp:12
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:269
CTxOut::nValue
CAmount nValue
Definition: transaction.h:167
cs_main
RecursiveMutex cs_main
Global state.
Definition: main.cpp:65
CTransaction::IsCoinBase
bool IsCoinBase() const
Definition: transaction.h:359
CheckTransaction
bool CheckTransaction(const CTransaction &tx, bool fRejectBadUTXO, CValidationState &state)
Transaction validation functions.
Definition: tx_verify.cpp:42
CTxOut
An output of a transaction.
Definition: transaction.h:164
tx_verify.h
CTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:286
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:168
consensus.h
CTxIn::IsFinal
bool IsFinal() const
Definition: transaction.h:124
AssertLockHeld
#define AssertLockHeld(cs)
Definition: sync.h:71
CChain::Height
int Height() const
Return the maximal height in the chain.
Definition: chain.h:641
CValidationState::DoS
bool DoS(int level, bool ret=false, unsigned char chRejectCodeIn=0, std::string strRejectReasonIn="", bool corruptionIn=false)
Definition: validation.h:38
GetAdjustedTime
int64_t GetAdjustedTime()
Definition: timedata.cpp:30
main.h
CTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:285
CScript::GetSigOpCount
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs as 20 sigops.
Definition: script.cpp:163
CTxIn::scriptSig
CScript scriptSig
Definition: transaction.h:87
CValidationState
Capture information about block/transaction validation.
Definition: validation.h:23
error
bool error(const char *fmt, const Args &... args)
Definition: util.h:61
CTransaction::IsCoinStake
bool IsCoinStake() const
Definition: transaction.cpp:143