xrpld
Loading...
Searching...
No Matches
ServerDefinitions.cpp
1#include <xrpld/rpc/handlers/server_info/ServerDefinitions.h>
2
3#include <xrpld/rpc/Context.h>
4
5#include <xrpl/basics/StringUtilities.h>
6#include <xrpl/basics/base_uint.h>
7#include <xrpl/json/json_value.h>
8#include <xrpl/json/json_writer.h>
9#include <xrpl/protocol/ErrorCodes.h>
10#include <xrpl/protocol/LedgerFormats.h>
11#include <xrpl/protocol/SField.h>
12#include <xrpl/protocol/TER.h>
13#include <xrpl/protocol/TxFlags.h>
14#include <xrpl/protocol/TxFormats.h>
15#include <xrpl/protocol/digest.h>
16#include <xrpl/protocol/jss.h>
17
18#include <boost/algorithm/string/replace.hpp>
19
20#include <cstddef>
21#include <cstdint>
22#include <map>
23#include <set>
24#include <string>
25#include <string_view>
26#include <unordered_map>
27
28namespace xrpl {
29
30namespace detail {
31
33{
34private:
35 static std::string
36 // translate e.g. STI_LEDGERENTRY to LedgerEntry
37 translate(std::string const& inp);
38
41
42public:
44
45 [[nodiscard]] bool
46 hashMatches(UInt256 hash) const
47 {
48 return defsHash_ == hash;
49 }
50
51 [[nodiscard]] json::Value const&
52 get() const
53 {
54 return defs_;
55 }
56};
57
60{
61 auto const replace = [&](std::string_view oldStr, std::string_view newStr) -> std::string {
62 std::string out = inp;
63 boost::replace_all(out, oldStr, newStr);
64 return out;
65 };
66
67 auto const contains = [&](std::string_view s) -> bool { return inp.contains(s); };
68
69 if (contains("UINT"))
70 {
71 if (contains("512") || contains("384") || contains("256") || contains("192") ||
72 contains("160") || contains("128"))
73 {
74 return replace("UINT", "Hash");
75 }
76
77 return replace("UINT", "UInt");
78 }
79
81 {"OBJECT", "STObject"},
82 {"ARRAY", "STArray"},
83 {"ACCOUNT", "AccountID"},
84 {"LEDGERENTRY", "LedgerEntry"},
85 {"NOTPRESENT", "NotPresent"},
86 {"PATHSET", "PathSet"},
87 {"VL", "Blob"},
88 };
89
90 if (auto const& it = kReplacements.find(inp); it != kReplacements.end())
91 {
92 return std::string(it->second);
93 }
94
95 std::string out;
96 size_t pos = 0;
97 std::string inpToProcess = inp;
98
99 // convert snake_case to CamelCase
100 for (;;)
101 {
102 pos = inpToProcess.find('_');
103 if (pos == std::string::npos)
104 pos = inpToProcess.size();
105 std::string token = inpToProcess.substr(0, pos);
106 if (token.size() > 1)
107 {
108 token = toLower(token);
109 token[0] -= ('a' - 'A');
110 out += token;
111 }
112 else
113 {
114 out += token;
115 }
116 if (pos == inpToProcess.size())
117 break;
118 inpToProcess = inpToProcess.substr(pos + 1);
119 }
120 return out;
121};
122
124{
125 // populate SerializedTypeID names and values
126 defs_[jss::TYPES] = json::ValueType::Object;
127
128 defs_[jss::TYPES]["Done"] = -1;
129 std::map<int32_t, std::string> typeMap{{-1, "Done"}};
130 for (auto const& [rawName, typeValue] : kSTypeMap)
131 {
132 std::string const typeName = translate(std::string(rawName).substr(4) /* remove STI_ */);
133 defs_[jss::TYPES][typeName] = typeValue;
134 typeMap[typeValue] = typeName;
135 }
136
137 // populate LedgerEntryType names and values
138 defs_[jss::LEDGER_ENTRY_TYPES] = json::ValueType::Object;
139 defs_[jss::LEDGER_ENTRY_TYPES][jss::Invalid] = -1;
140
141 for (auto const& f : LedgerFormats::getInstance())
142 {
143 defs_[jss::LEDGER_ENTRY_TYPES][f.getName()] = f.getType();
144 }
145
146 // populate SField serialization data
147 defs_[jss::FIELDS] = json::ValueType::Array;
148
149 uint32_t i = 0;
150
151 {
153 a[0U] = "Invalid";
155 v[jss::nth] = -1;
156 v[jss::isVLEncoded] = false;
157 v[jss::isSerialized] = false;
158 v[jss::isSigningField] = false;
159 v[jss::type] = "Unknown";
160 a[1U] = v;
161 defs_[jss::FIELDS][i++] = a;
162 }
163
164 {
166 a[0U] = "ObjectEndMarker";
168 v[jss::nth] = 1;
169 v[jss::isVLEncoded] = false;
170 v[jss::isSerialized] = true;
171 v[jss::isSigningField] = true;
172 v[jss::type] = "STObject";
173 a[1U] = v;
174 defs_[jss::FIELDS][i++] = a;
175 }
176
177 {
179 a[0U] = "ArrayEndMarker";
181 v[jss::nth] = 1;
182 v[jss::isVLEncoded] = false;
183 v[jss::isSerialized] = true;
184 v[jss::isSigningField] = true;
185 v[jss::type] = "STArray";
186 a[1U] = v;
187 defs_[jss::FIELDS][i++] = a;
188 }
189
190 {
192 a[0U] = "taker_gets_funded";
194 v[jss::nth] = 258;
195 v[jss::isVLEncoded] = false;
196 v[jss::isSerialized] = false;
197 v[jss::isSigningField] = false;
198 v[jss::type] = "Amount";
199 a[1U] = v;
200 defs_[jss::FIELDS][i++] = a;
201 }
202
203 {
205 a[0U] = "taker_pays_funded";
207 v[jss::nth] = 259;
208 v[jss::isVLEncoded] = false;
209 v[jss::isSerialized] = false;
210 v[jss::isSigningField] = false;
211 v[jss::type] = "Amount";
212 a[1U] = v;
213 defs_[jss::FIELDS][i++] = a;
214 }
215
216 // copy into a sorted map to ensure deterministic output order (sorted by fieldCode)
217 static std::map<int, SField const*> const kSortedFields(
219
220 for (auto const& [code, field] : kSortedFields)
221 {
222 if (field->fieldName.empty())
223 continue;
224
226
227 int32_t const type = field->fieldType;
228
229 innerObj[jss::nth] = field->fieldValue;
230
231 // whether the field is variable-length encoded this means that the length is included
232 // before the content
233 innerObj[jss::isVLEncoded] =
234 (type == STI_VL || type == STI_ACCOUNT || type == STI_VECTOR256);
235 static_assert(
236 STI_VL == 7U && STI_ACCOUNT == 8U && STI_VECTOR256 == 19U,
237 "STI_VL, STI_ACCOUNT, STI_VECTOR256 must be 7, 8, 19 respectively");
238
239 // whether the field is included in serialization
240 innerObj[jss::isSerialized] =
241 (type < 10000 && field->fieldName != "hash" &&
242 field->fieldName !=
243 "index"); // hash, index, TRANSACTION, LEDGER_ENTRY, VALIDATION, METADATA
244
245 // whether the field is included in serialization when signing
246 innerObj[jss::isSigningField] = field->shouldInclude(false);
247
248 innerObj[jss::type] = typeMap[type];
249
251 innerArray[0U] = field->fieldName;
252 innerArray[1U] = innerObj;
253
254 defs_[jss::FIELDS][i++] = innerArray;
255 }
256
257 // populate TER code names and values
258 defs_[jss::TRANSACTION_RESULTS] = json::ValueType::Object;
259
260 for (auto const& [code, terInfo] : transResults())
261 {
262 defs_[jss::TRANSACTION_RESULTS][terInfo.first] = code;
263 }
264
265 // populate TxType names and values
266 defs_[jss::TRANSACTION_TYPES] = json::ValueType::Object;
267 defs_[jss::TRANSACTION_TYPES][jss::Invalid] = -1;
268 for (auto const& f : TxFormats::getInstance())
269 {
270 defs_[jss::TRANSACTION_TYPES][f.getName()] = f.getType();
271 }
272
273 // populate TxFormats
274 defs_[jss::TRANSACTION_FORMATS] = json::ValueType::Object;
275
276 defs_[jss::TRANSACTION_FORMATS][jss::common] = json::ValueType::Array;
277 auto txCommonFields = std::set<std::string>();
278 for (auto const& element : TxFormats::getCommonFields())
279 {
281 elementObj[jss::name] = element.sField().getName();
282 elementObj[jss::optionality] = element.style();
283 defs_[jss::TRANSACTION_FORMATS][jss::common].append(elementObj);
284 txCommonFields.insert(element.sField().getName());
285 }
286
287 for (auto const& format : TxFormats::getInstance())
288 {
289 auto const& soTemplate = format.getSOTemplate();
290 json::Value templateArray = json::ValueType::Array;
291 for (auto const& element : soTemplate)
292 {
293 if (txCommonFields.contains(element.sField().getName()))
294 continue; // skip common fields, already added
296 elementObj[jss::name] = element.sField().getName();
297 elementObj[jss::optionality] = element.style();
298 templateArray.append(elementObj);
299 }
300 defs_[jss::TRANSACTION_FORMATS][format.getName()] = templateArray;
301 }
302
303 // populate LedgerFormats
304 defs_[jss::LEDGER_ENTRY_FORMATS] = json::ValueType::Object;
305 defs_[jss::LEDGER_ENTRY_FORMATS][jss::common] = json::ValueType::Array;
306 auto ledgerCommonFields = std::set<std::string>();
307 for (auto const& element : LedgerFormats::getCommonFields())
308 {
310 elementObj[jss::name] = element.sField().getName();
311 elementObj[jss::optionality] = element.style();
312 defs_[jss::LEDGER_ENTRY_FORMATS][jss::common].append(elementObj);
313 ledgerCommonFields.insert(element.sField().getName());
314 }
315 for (auto const& format : LedgerFormats::getInstance())
316 {
317 auto const& soTemplate = format.getSOTemplate();
318 json::Value templateArray = json::ValueType::Array;
319 for (auto const& element : soTemplate)
320 {
321 if (ledgerCommonFields.contains(element.sField().getName()))
322 continue; // skip common fields, already added
324 elementObj[jss::name] = element.sField().getName();
325 elementObj[jss::optionality] = element.style();
326 templateArray.append(elementObj);
327 }
328 defs_[jss::LEDGER_ENTRY_FORMATS][format.getName()] = templateArray;
329 }
330
331 defs_[jss::TRANSACTION_FLAGS] = json::ValueType::Object;
332 for (auto const& [name, value] : getAllTxFlags())
333 {
335 for (auto const& [flagName, flagValue] : value)
336 {
337 txObj[flagName] = flagValue;
338 }
339 defs_[jss::TRANSACTION_FLAGS][name] = txObj;
340 }
341
342 defs_[jss::LEDGER_ENTRY_FLAGS] = json::ValueType::Object;
343 for (auto const& [name, value] : getAllLedgerFlags())
344 {
346 for (auto const& [flagName, flagValue] : value)
347 {
348 ledgerObj[flagName] = flagValue;
349 }
350 defs_[jss::LEDGER_ENTRY_FLAGS][name] = ledgerObj;
351 }
352
353 defs_[jss::ACCOUNT_SET_FLAGS] = json::ValueType::Object;
354 for (auto const& [name, value] : getAsfFlagMap())
355 {
356 defs_[jss::ACCOUNT_SET_FLAGS][name] = value;
357 }
358
359 // generate hash
360 {
363 defs_[jss::hash] = to_string(defsHash_);
364 }
365}
366
369{
370 static ServerDefinitions const kDefs{};
371 return kDefs;
372}
373
374} // namespace detail
375
376json::Value const&
381
384{
385 auto& params = context.params;
386
387 UInt256 hash;
388 if (params.isMember(jss::hash))
389 {
390 if (!params[jss::hash].isString() || !hash.parseHex(params[jss::hash].asString()))
391 return rpc::invalidFieldError(jss::hash);
392 }
393
394 auto const& defs = detail::getDefinitions();
395 if (defs.hashMatches(hash))
396 {
398 jv[jss::hash] = to_string(hash);
399 return jv;
400 }
401 return defs.get();
402}
403
404} // namespace xrpl
Outputs a Value in JSON format without formatting (not human friendly).
Definition json_writer.h:37
std::string write(Value const &root) override
Represents a JSON value.
Definition json_value.h:121
Value & append(Value const &value)
Append value to array at the end.
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:526
static std::vector< SOElement > const & getCommonFields()
static LedgerFormats const & getInstance()
static std::unordered_map< int, SField const * > const & getKnownCodeToField()
Definition SField.h:291
An immutable linear range of bytes.
Definition Slice.h:28
static std::vector< SOElement > const & getCommonFields()
Definition TxFormats.cpp:13
static TxFormats const & getInstance()
Definition TxFormats.cpp:60
json::Value const & get() const
bool hashMatches(UInt256 hash) const
static std::string translate(std::string const &inp)
T data(T... args)
T end(T... args)
T find(T... args)
JSON (JavaScript Object Notation).
Definition json_errors.h:5
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
ServerDefinitions const & getDefinitions()
json::Value invalidFieldError(std::string const &name)
Definition ErrorCodes.h:285
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string toLower(std::string str)
Fold ASCII upper case letters to lower case.
json::Value const & getServerDefinitionsJson()
static std::map< std::string, int > const kSTypeMap
Definition SField.h:95
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
BaseUInt< 256 > UInt256
Definition base_uint.h:581
json::Value doServerDefinitions(rpc::JsonContext &)
FlagMapPairList const & getAllTxFlags()
Definition TxFlags.h:337
std::unordered_map< TERUnderlyingType, std::pair< char const *const, char const *const > > const & transResults()
Definition TER.cpp:14
std::vector< std::pair< std::string, LedgerFlagMap > > const & getAllLedgerFlags()
std::map< std::string, FlagValue > const & getAsfFlagMap()
Definition TxFlags.h:450
Sha512HalfHasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:215
T size(T... args)
json::Value params
Definition Context.h:51
T substr(T... args)