-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathTypes.hpp
More file actions
231 lines (201 loc) · 6.3 KB
/
Types.hpp
File metadata and controls
231 lines (201 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2023, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#pragma once
#include "rpc/Errors.hpp"
#include "web/SubscriptionContextInterface.hpp"
#include <boost/asio/spawn.hpp>
#include <boost/json/array.hpp>
#include <boost/json/conversion.hpp>
#include <boost/json/object.hpp>
#include <boost/json/value.hpp>
#include <boost/json/value_from.hpp>
#include <xrpl/basics/base_uint.h>
#include <xrpl/basics/strHex.h>
#include <cstdint>
#include <expected>
#include <optional>
#include <string>
#include <utility>
namespace etl {
class LoadBalancer;
} // namespace etl
namespace web {
struct ConnectionBase;
} // namespace web
namespace rpc {
class Counters;
/**
* @brief Return type used for Validators that can return error but don't have
* specific value to return
*/
using MaybeError = std::expected<void, Status>;
/**
* @brief Check if two MaybeError objects are equal
*
* @param lhs The first MaybeError object
* @param rhs The second MaybeError object
* @return true if the two MaybeError objects are equal, false otherwise
*/
inline bool
operator==(MaybeError const& lhs, MaybeError const& rhs)
{
bool const lhsHasError = !static_cast<bool>(lhs);
bool const rhsHasError = !static_cast<bool>(rhs);
return lhsHasError == rhsHasError && (!lhsHasError || lhs.error() == rhs.error());
}
/**
* @brief The type that represents just the error part of @ref MaybeError
*/
using Error = std::unexpected<Status>;
/**
* @brief Return type for each individual handler
*/
template <typename OutputType>
using HandlerReturnType = std::expected<OutputType, Status>;
/**
* @brief The final return type out of RPC engine
*/
struct ReturnType {
/**
* @brief Construct a new Return Type object
*
* @param result The result of the RPC call
* @param warnings The warnings generated by the RPC call
*/
ReturnType(std::expected<boost::json::value, Status> result, boost::json::array warnings = {})
: result{std::move(result)}, warnings(std::move(warnings))
{
}
/**
* @brief operator bool to check whether the ReturnType contains a response
*/
operator bool() const
{
return result.has_value();
}
std::expected<boost::json::value, Status> result;
boost::json::array warnings;
};
/**
* @brief An empty type used as Output for handlers than don't actually produce output.
*/
struct VoidOutput {};
/**
* @brief Context of an RPC call.
*/
struct Context {
boost::asio::yield_context yield;
web::SubscriptionContextPtr session = {}; // NOLINT(readability-redundant-member-init)
bool isAdmin = false;
std::string clientIp = {}; // NOLINT(readability-redundant-member-init)
uint32_t apiVersion = 0u; // invalid by default
};
/**
* @brief Result type used to return responses or error statuses to the Webserver subsystem.
*/
struct Result {
/**
* @brief Construct a new Result object from ReturnType
*
* @param returnType The ReturnType to construct the result from
*/
explicit Result(ReturnType returnType)
{
if (returnType) {
response = std::move(returnType.result).value().as_object();
} else {
response = std::unexpected{std::move(returnType.result).error()};
}
warnings = std::move(returnType.warnings);
}
/**
* @brief Construct a new Result object from Status
*
* @param status The status to construct the result from
*/
explicit Result(Status status) : response{std::unexpected{std::move(status)}}
{
}
/**
* @brief Construct a new Result object from a response object
*
* @param response The response to construct the result from
*/
explicit Result(boost::json::object response) : response{std::move(response)}
{
}
std::expected<boost::json::object, Status> response;
boost::json::array warnings;
};
/**
* @brief A cursor object used to traverse nodes owned by an account.
*/
struct AccountCursor {
ripple::uint256 index;
std::uint32_t hint{};
/**
* @brief Convert the cursor to a string
*
* @return The string representation of the cursor
*/
std::string
toString() const
{
return ripple::strHex(index) + "," + std::to_string(hint);
}
/**
* @brief Check if the cursor is non-zero
*
* @return true if the cursor is non-zero, false otherwise
*/
bool
isNonZero() const
{
return index.isNonZero() || hint != 0;
}
};
/**
* @brief A delegate object used filter account_tx by specific delegate accounts
*/
struct DelegateFilter {
/**
* @brief A delegate type used in delegate filter
*/
enum class Role {
// This account is the *active* sender, acting on behalf of another party.
// e.g., Account A in "A sends payment to B on behalf of C."
Delegatee,
// This account is the *passive* party whose funds are being moved from.
// e.g., Account C in "A sends payment to B on behalf of C."
Delegator
};
Role delegateType;
std::optional<std::string> counterParty;
};
/**
* @brief Convert an empty output to a JSON object
*
* @note Always generates empty JSON object
*
* @param [out] jv The JSON object to convert to
*/
inline void
tag_invoke(boost::json::value_from_tag, boost::json::value& jv, VoidOutput const&)
{
jv = boost::json::object{};
}
} // namespace rpc