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
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
{
29 MODE_VALID
, //!< everything ok
30 MODE_INVALID
, //!< network rule violation (DoS value may be set)
31 MODE_ERROR
, //!< run-time error
34 std::string strRejectReason
;
35 unsigned int chRejectCode
;
36 bool corruptionPossible
;
37 std::string strDebugMessage
;
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
)
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
;
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 {
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