veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
validation_state.hpp
1// Copyright (c) 2019-2022 Xenios SEZC
2// https://www.veriblock.org
3// Distributed under the MIT software license, see the accompanying
4// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef ALT_INTEGRATION_VERIBLOCK_VALIDATION_STATE_HPP
7#define ALT_INTEGRATION_VERIBLOCK_VALIDATION_STATE_HPP
8
9#include <algorithm>
10#include <sstream>
11#include <string>
12#include <vector>
13
14#include "json.hpp"
15
16namespace altintegration {
17
18// clang-format off
27// clang-format on
29 public:
30 ValidationState() : m_mode(MODE_VALID) {}
31
32 std::string toString() const;
33
34 void reset() {
35 stack_trace.clear();
36 m_debug_message.clear();
37 m_mode = MODE_VALID;
38 }
39
40 bool Invalid(const std::string &reject_reason,
41 const std::string &debug_message = "");
42
52 bool Invalid(const std::string &reject_reason,
53 const std::string &debug_message,
54 size_t index) {
55 stack_trace.push_back(std::to_string(index));
56 return Invalid(reject_reason, debug_message);
57 }
58
59 bool Invalid(const std::string &reject_reason, size_t index) {
60 return Invalid(reject_reason, "", index);
61 }
62
63 bool IsValid() const { return m_mode == MODE_VALID; }
64 bool IsInvalid() const { return m_mode == MODE_INVALID; }
65 std::string GetDebugMessage() const { return m_debug_message; }
66
67 std::vector<std::string> GetPathParts() const;
68
69 std::string GetPath() const;
70
71 private:
72 enum mode_state {
73 MODE_VALID,
74 MODE_INVALID,
75 } m_mode;
76 std::string m_debug_message;
77 std::vector<std::string> stack_trace;
78};
79
81template <typename JsonValue>
82JsonValue ToJSON(const ValidationState &s, bool *acceptedToMempool = nullptr) {
83 auto obj = json::makeEmptyObject<JsonValue>();
84
85 if (acceptedToMempool != nullptr) {
86 json::putBoolKV(obj, "accepted", *acceptedToMempool);
87 }
88
89 if (s.IsValid()) {
90 json::putStringKV(obj, "state", "valid");
91 return obj;
92 } else if (s.IsInvalid()) {
93 json::putStringKV(obj, "state", "invalid");
94 }
95
96 json::putStringKV(obj, "code", s.GetPath());
97 json::putStringKV(obj, "message", s.GetDebugMessage());
98
99 return obj;
100}
101
102} // namespace altintegration
103
104#endif // ALT_INTEGRATION_INCLUDE_VERIBLOCK_VALIDATION_STATE_HPP_
Class that is used for storing validation state.
bool Invalid(const std::string &reject_reason, const std::string &debug_message, size_t index)
Changes this ValidationState into "INVALID" mode.
Defines logging helpers.
Definition: block.hpp:14