Merge #11676: contrib/init: Update openrc-run filename
[bitcoinplatinum.git] / src / consensus / validation.h
blobb6740c9d9fdd1bc266832d933eec8428fb7bff30
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_CONSENSUS_VALIDATION_H
7 #define BITCOIN_CONSENSUS_VALIDATION_H
9 #include <string>
10 #include "version.h"
11 #include "consensus/consensus.h"
12 #include "primitives/transaction.h"
13 #include "primitives/block.h"
15 /** "reject" message codes */
16 static const unsigned char REJECT_MALFORMED = 0x01;
17 static const unsigned char REJECT_INVALID = 0x10;
18 static const unsigned char REJECT_OBSOLETE = 0x11;
19 static const unsigned char REJECT_DUPLICATE = 0x12;
20 static const unsigned char REJECT_NONSTANDARD = 0x40;
21 // static const unsigned char REJECT_DUST = 0x41; // part of BIP 61
22 static const unsigned char REJECT_INSUFFICIENTFEE = 0x42;
23 static const unsigned char REJECT_CHECKPOINT = 0x43;
25 /** Capture information about block/transaction validation */
26 class CValidationState {
27 private:
28 enum mode_state {
29 MODE_VALID, //!< everything ok
30 MODE_INVALID, //!< network rule violation (DoS value may be set)
31 MODE_ERROR, //!< run-time error
32 } mode;
33 int nDoS;
34 std::string strRejectReason;
35 unsigned int chRejectCode;
36 bool corruptionPossible;
37 std::string strDebugMessage;
38 public:
39 CValidationState() : mode(MODE_VALID), nDoS(0), chRejectCode(0), corruptionPossible(false) {}
40 bool DoS(int level, bool ret = false,
41 unsigned int chRejectCodeIn=0, const std::string &strRejectReasonIn="",
42 bool corruptionIn=false,
43 const std::string &strDebugMessageIn="") {
44 chRejectCode = chRejectCodeIn;
45 strRejectReason = strRejectReasonIn;
46 corruptionPossible = corruptionIn;
47 strDebugMessage = strDebugMessageIn;
48 if (mode == MODE_ERROR)
49 return ret;
50 nDoS += level;
51 mode = MODE_INVALID;
52 return ret;
54 bool Invalid(bool ret = false,
55 unsigned int _chRejectCode=0, const std::string &_strRejectReason="",
56 const std::string &_strDebugMessage="") {
57 return DoS(0, ret, _chRejectCode, _strRejectReason, false, _strDebugMessage);
59 bool Error(const std::string& strRejectReasonIn) {
60 if (mode == MODE_VALID)
61 strRejectReason = strRejectReasonIn;
62 mode = MODE_ERROR;
63 return false;
65 bool IsValid() const {
66 return mode == MODE_VALID;
68 bool IsInvalid() const {
69 return mode == MODE_INVALID;
71 bool IsError() const {
72 return mode == MODE_ERROR;
74 bool IsInvalid(int &nDoSOut) const {
75 if (IsInvalid()) {
76 nDoSOut = nDoS;
77 return true;
79 return false;
81 bool CorruptionPossible() const {
82 return corruptionPossible;
84 void SetCorruptionPossible() {
85 corruptionPossible = true;
87 unsigned int GetRejectCode() const { return chRejectCode; }
88 std::string GetRejectReason() const { return strRejectReason; }
89 std::string GetDebugMessage() const { return strDebugMessage; }
92 // These implement the weight = (stripped_size * 4) + witness_size formula,
93 // using only serialization with and without witness data. As witness_size
94 // is equal to total_size - stripped_size, this formula is identical to:
95 // weight = (stripped_size * 3) + total_size.
96 static inline int64_t GetTransactionWeight(const CTransaction& tx)
98 return ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
100 static inline int64_t GetBlockWeight(const CBlock& block)
102 return ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION);
105 #endif // BITCOIN_CONSENSUS_VALIDATION_H