Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F13115482
D8328.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Subscribers
None
D8328.diff
View Options
diff --git a/src/avalanche/validation.h b/src/avalanche/validation.h
--- a/src/avalanche/validation.h
+++ b/src/avalanche/validation.h
@@ -10,27 +10,14 @@
namespace avalanche {
enum class ProofValidationResult {
- NONE,
+ NONE = 0,
NO_STAKE,
DUST_THRESOLD,
DUPLICATE_STAKE,
INVALID_SIGNATURE,
};
-class ProofValidationState : public ValidationState {
-private:
- ProofValidationResult m_result = ProofValidationResult::NONE;
-
-public:
- bool Invalid(ProofValidationResult result,
- const std::string &reject_reason = "",
- const std::string &debug_message = "") {
- m_result = result;
- ValidationState::Invalid(reject_reason, debug_message);
- return false;
- }
- ProofValidationResult GetResult() const { return m_result; }
-};
+class ProofValidationState : public ValidationState<ProofValidationResult> {};
} // namespace avalanche
diff --git a/src/consensus/validation.h b/src/consensus/validation.h
--- a/src/consensus/validation.h
+++ b/src/consensus/validation.h
@@ -15,7 +15,7 @@
*/
enum class TxValidationResult {
//! initial value. Tx has not yet been rejected
- TX_RESULT_UNSET,
+ TX_RESULT_UNSET = 0,
//! invalid by consensus rules
TX_CONSENSUS,
/**
@@ -48,7 +48,7 @@
*/
enum class BlockValidationResult {
//! initial value. Block has not yet been rejected
- BLOCK_RESULT_UNSET,
+ BLOCK_RESULT_UNSET = 0,
//! invalid by consensus rules (excluding any below reasons)
BLOCK_CONSENSUS,
/**
@@ -79,34 +79,33 @@
};
/**
- * Base class for capturing information about block/transaction validation.
- * This is subclassed by TxValidationState and BlockValidationState for
+ * Template for capturing information about block/transaction validation.
+ * This is instantiated by TxValidationState and BlockValidationState for
* validation information on transactions and blocks respectively.
*/
-class ValidationState {
+template <typename Result> class ValidationState {
private:
enum mode_state {
MODE_VALID, //!< everything ok
MODE_INVALID, //!< network rule violation (DoS value may be set)
MODE_ERROR, //!< run-time error
} m_mode{MODE_VALID};
+ Result m_result{};
std::string m_reject_reason;
std::string m_debug_message;
-protected:
- void Invalid(const std::string &reject_reason = "",
+public:
+ bool Invalid(Result result, const std::string &reject_reason = "",
const std::string &debug_message = "") {
+ m_result = result;
m_reject_reason = reject_reason;
m_debug_message = debug_message;
if (m_mode != MODE_ERROR) {
m_mode = MODE_INVALID;
}
+ return false;
}
-public:
- // ValidationState is abstract. Have a pure virtual destructor.
- virtual ~ValidationState() = 0;
-
bool Error(const std::string &reject_reason) {
if (m_mode == MODE_VALID) {
m_reject_reason = reject_reason;
@@ -117,6 +116,7 @@
bool IsValid() const { return m_mode == MODE_VALID; }
bool IsInvalid() const { return m_mode == MODE_INVALID; }
bool IsError() const { return m_mode == MODE_ERROR; }
+ Result GetResult() const { return m_result; }
std::string GetRejectReason() const { return m_reject_reason; }
std::string GetDebugMessage() const { return m_debug_message; }
std::string ToString() const {
@@ -132,36 +132,7 @@
}
};
-inline ValidationState::~ValidationState(){};
-
-class TxValidationState : public ValidationState {
-private:
- TxValidationResult m_result = TxValidationResult::TX_RESULT_UNSET;
-
-public:
- bool Invalid(TxValidationResult result,
- const std::string &reject_reason = "",
- const std::string &debug_message = "") {
- m_result = result;
- ValidationState::Invalid(reject_reason, debug_message);
- return false;
- }
- TxValidationResult GetResult() const { return m_result; }
-};
-
-class BlockValidationState : public ValidationState {
-private:
- BlockValidationResult m_result = BlockValidationResult::BLOCK_RESULT_UNSET;
-
-public:
- bool Invalid(BlockValidationResult result,
- const std::string &reject_reason = "",
- const std::string &debug_message = "") {
- m_result = result;
- ValidationState::Invalid(reject_reason, debug_message);
- return false;
- }
- BlockValidationResult GetResult() const { return m_result; }
-};
+class TxValidationState : public ValidationState<TxValidationResult> {};
+class BlockValidationState : public ValidationState<BlockValidationResult> {};
#endif // BITCOIN_CONSENSUS_VALIDATION_H
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Mar 1, 11:11 (18 h, 59 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5187533
Default Alt Text
D8328.diff (4 KB)
Attached To
D8328: Templatize ValidationState instead of subclassing
Event Timeline
Log In to Comment