Skip to content

Commit 10efc04

Browse files
committed
Templatize ValidationState instead of subclassing
This removes boilerplate code in the subclasses which otherwise only differ by the result type.
1 parent 10e85d4 commit 10efc04

File tree

1 file changed

+13
-39
lines changed

1 file changed

+13
-39
lines changed

src/consensus/validation.h

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* provider of the transaction should be banned/ignored/disconnected/etc.
1717
*/
1818
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
2020
TX_CONSENSUS, //!< invalid by consensus rules
2121
/**
2222
* Invalid by a change to consensus rules more recent than SegWit.
@@ -50,7 +50,7 @@ enum class TxValidationResult {
5050
* useful for some other use-cases.
5151
*/
5252
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
5454
BLOCK_CONSENSUS, //!< invalid by consensus rules (excluding any below reasons)
5555
/**
5656
* Invalid by a change to consensus rules more recent than SegWit.
@@ -71,30 +71,31 @@ enum class BlockValidationResult {
7171

7272

7373

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
7575
* by TxValidationState and BlockValidationState for validation information on transactions
7676
* and blocks respectively. */
77+
template <typename Result>
7778
class ValidationState {
7879
private:
7980
enum mode_state {
8081
MODE_VALID, //!< everything ok
8182
MODE_INVALID, //!< network rule violation (DoS value may be set)
8283
MODE_ERROR, //!< run-time error
8384
} m_mode{MODE_VALID};
85+
Result m_result{};
8486
std::string m_reject_reason;
8587
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="",
8891
const std::string &debug_message="")
8992
{
93+
m_result = result;
9094
m_reject_reason = reject_reason;
9195
m_debug_message = debug_message;
9296
if (m_mode != MODE_ERROR) m_mode = MODE_INVALID;
97+
return false;
9398
}
94-
public:
95-
// ValidationState is abstract. Have a pure virtual destructor.
96-
virtual ~ValidationState() = 0;
97-
9899
bool Error(const std::string& reject_reason)
99100
{
100101
if (m_mode == MODE_VALID)
@@ -105,6 +106,7 @@ class ValidationState {
105106
bool IsValid() const { return m_mode == MODE_VALID; }
106107
bool IsInvalid() const { return m_mode == MODE_INVALID; }
107108
bool IsError() const { return m_mode == MODE_ERROR; }
109+
Result GetResult() const { return m_result; }
108110
std::string GetRejectReason() const { return m_reject_reason; }
109111
std::string GetDebugMessage() const { return m_debug_message; }
110112
std::string ToString() const
@@ -121,36 +123,8 @@ class ValidationState {
121123
}
122124
};
123125

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> {};
154128

155129
// These implement the weight = (stripped_size * 4) + witness_size formula,
156130
// using only serialization with and without witness data. As witness_size

0 commit comments

Comments
 (0)