16
16
* provider of the transaction should be banned/ignored/disconnected/etc.
17
17
*/
18
18
enum class TxValidationResult {
19
- TX_RESULT_UNSET, // !< initial value. Tx has not yet been rejected
19
+ TX_RESULT_UNSET = 0 , // !< initial value. Tx has not yet been rejected
20
20
TX_CONSENSUS, // !< invalid by consensus rules
21
21
/* *
22
22
* Invalid by a change to consensus rules more recent than SegWit.
@@ -50,7 +50,7 @@ enum class TxValidationResult {
50
50
* useful for some other use-cases.
51
51
*/
52
52
enum class BlockValidationResult {
53
- BLOCK_RESULT_UNSET, // !< initial value. Block has not yet been rejected
53
+ BLOCK_RESULT_UNSET = 0 , // !< initial value. Block has not yet been rejected
54
54
BLOCK_CONSENSUS, // !< invalid by consensus rules (excluding any below reasons)
55
55
/* *
56
56
* Invalid by a change to consensus rules more recent than SegWit.
@@ -71,30 +71,31 @@ enum class BlockValidationResult {
71
71
72
72
73
73
74
- /* * Base class for capturing information about block/transaction validation. This is subclassed
74
+ /* * Template for capturing information about block/transaction validation. This is instantiated
75
75
* by TxValidationState and BlockValidationState for validation information on transactions
76
76
* and blocks respectively. */
77
+ template <typename Result>
77
78
class ValidationState {
78
79
private:
79
80
enum mode_state {
80
81
MODE_VALID, // !< everything ok
81
82
MODE_INVALID, // !< network rule violation (DoS value may be set)
82
83
MODE_ERROR, // !< run-time error
83
84
} m_mode{MODE_VALID};
85
+ Result m_result{};
84
86
std::string m_reject_reason;
85
87
std::string m_debug_message;
86
- protected:
87
- void Invalid (const std::string &reject_reason=" " ,
88
+ public:
89
+ bool Invalid (Result result,
90
+ const std::string &reject_reason=" " ,
88
91
const std::string &debug_message=" " )
89
92
{
93
+ m_result = result;
90
94
m_reject_reason = reject_reason;
91
95
m_debug_message = debug_message;
92
96
if (m_mode != MODE_ERROR) m_mode = MODE_INVALID;
97
+ return false ;
93
98
}
94
- public:
95
- // ValidationState is abstract. Have a pure virtual destructor.
96
- virtual ~ValidationState () = 0 ;
97
-
98
99
bool Error (const std::string& reject_reason)
99
100
{
100
101
if (m_mode == MODE_VALID)
@@ -105,6 +106,7 @@ class ValidationState {
105
106
bool IsValid () const { return m_mode == MODE_VALID; }
106
107
bool IsInvalid () const { return m_mode == MODE_INVALID; }
107
108
bool IsError () const { return m_mode == MODE_ERROR; }
109
+ Result GetResult () const { return m_result; }
108
110
std::string GetRejectReason () const { return m_reject_reason; }
109
111
std::string GetDebugMessage () const { return m_debug_message; }
110
112
std::string ToString () const
@@ -121,36 +123,8 @@ class ValidationState {
121
123
}
122
124
};
123
125
124
- inline ValidationState::~ValidationState () {};
125
-
126
- class TxValidationState : public ValidationState {
127
- private:
128
- TxValidationResult m_result = TxValidationResult::TX_RESULT_UNSET;
129
- public:
130
- bool Invalid (TxValidationResult result,
131
- const std::string &reject_reason=" " ,
132
- const std::string &debug_message=" " )
133
- {
134
- m_result = result;
135
- ValidationState::Invalid (reject_reason, debug_message);
136
- return false ;
137
- }
138
- TxValidationResult GetResult () const { return m_result; }
139
- };
140
-
141
- class BlockValidationState : public ValidationState {
142
- private:
143
- BlockValidationResult m_result = BlockValidationResult::BLOCK_RESULT_UNSET;
144
- public:
145
- bool Invalid (BlockValidationResult result,
146
- const std::string &reject_reason=" " ,
147
- const std::string &debug_message=" " ) {
148
- m_result = result;
149
- ValidationState::Invalid (reject_reason, debug_message);
150
- return false ;
151
- }
152
- BlockValidationResult GetResult () const { return m_result; }
153
- };
126
+ class TxValidationState : public ValidationState <TxValidationResult> {};
127
+ class BlockValidationState : public ValidationState <BlockValidationResult> {};
154
128
155
129
// These implement the weight = (stripped_size * 4) + witness_size formula,
156
130
// using only serialization with and without witness data. As witness_size
0 commit comments