rippled
Loading...
Searching...
No Matches
STInteger.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/Log.h>
21#include <xrpl/basics/safe_cast.h>
22#include <xrpl/beast/utility/instrumentation.h>
23#include <xrpl/json/json_value.h>
24#include <xrpl/protocol/LedgerFormats.h>
25#include <xrpl/protocol/Permissions.h>
26#include <xrpl/protocol/SField.h>
27#include <xrpl/protocol/STBase.h>
28#include <xrpl/protocol/STInteger.h>
29#include <xrpl/protocol/Serializer.h>
30#include <xrpl/protocol/TER.h>
31#include <xrpl/protocol/TxFormats.h>
32
33#include <charconv>
34#include <cstdint>
35#include <iterator>
36#include <string>
37#include <system_error>
38
39namespace ripple {
40
41template <>
43 : STInteger(name, sit.get8())
44{
45}
46
47template <>
50{
51 return STI_UINT8;
52}
53
54template <>
57{
58 if (getFName() == sfTransactionResult)
59 {
60 std::string token, human;
61
62 if (transResultInfo(TER::fromInt(value_), token, human))
63 return human;
64
65 JLOG(debugLog().error())
66 << "Unknown result code in metadata: " << value_;
67 }
68
69 return std::to_string(value_);
70}
71
72template <>
75{
76 if (getFName() == sfTransactionResult)
77 {
78 std::string token, human;
79
80 if (transResultInfo(TER::fromInt(value_), token, human))
81 return token;
82
83 JLOG(debugLog().error())
84 << "Unknown result code in metadata: " << value_;
85 }
86
87 return value_;
88}
89
90//------------------------------------------------------------------------------
91
92template <>
94 : STInteger(name, sit.get16())
95{
96}
97
98template <>
100STUInt16::getSType() const
101{
102 return STI_UINT16;
103}
104
105template <>
107STUInt16::getText() const
108{
109 if (getFName() == sfLedgerEntryType)
110 {
112 safe_cast<LedgerEntryType>(value_));
113
114 if (item != nullptr)
115 return item->getName();
116 }
117
118 if (getFName() == sfTransactionType)
119 {
120 auto item =
121 TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
122
123 if (item != nullptr)
124 return item->getName();
125 }
126
127 return std::to_string(value_);
128}
129
130template <>
132STUInt16::getJson(JsonOptions) const
133{
134 if (getFName() == sfLedgerEntryType)
135 {
137 safe_cast<LedgerEntryType>(value_));
138
139 if (item != nullptr)
140 return item->getName();
141 }
142
143 if (getFName() == sfTransactionType)
144 {
145 auto item =
146 TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
147
148 if (item != nullptr)
149 return item->getName();
150 }
151
152 return value_;
153}
154
155//------------------------------------------------------------------------------
156
157template <>
159 : STInteger(name, sit.get32())
160{
161}
162
163template <>
165STUInt32::getSType() const
166{
167 return STI_UINT32;
168}
169
170template <>
172STUInt32::getText() const
173{
174 return std::to_string(value_);
175}
176
177template <>
179STUInt32::getJson(JsonOptions) const
180{
181 if (getFName() == sfPermissionValue)
182 {
183 auto const permissionValue =
184 static_cast<GranularPermissionType>(value_);
185 auto const granular =
186 Permission::getInstance().getGranularName(permissionValue);
187
188 if (granular)
189 {
190 return *granular;
191 }
192 else
193 {
194 auto const txType =
196 auto item = TxFormats::getInstance().findByType(txType);
197 if (item != nullptr)
198 return item->getName();
199 }
200 }
201
202 return value_;
203}
204
205//------------------------------------------------------------------------------
206
207template <>
209 : STInteger(name, sit.get64())
210{
211}
212
213template <>
215STUInt64::getSType() const
216{
217 return STI_UINT64;
218}
219
220template <>
222STUInt64::getText() const
223{
224 return std::to_string(value_);
225}
226
227template <>
229STUInt64::getJson(JsonOptions) const
230{
231 auto convertToString = [](uint64_t const value, int const base) {
232 XRPL_ASSERT(
233 base == 10 || base == 16,
234 "ripple::STUInt64::getJson : base 10 or 16");
235 std::string str(
236 base == 10 ? 20 : 16, 0); // Allocate space depending on base
237 auto ret =
238 std::to_chars(str.data(), str.data() + str.size(), value, base);
239 XRPL_ASSERT(
240 ret.ec == std::errc(),
241 "ripple::STUInt64::getJson : to_chars succeeded");
242 str.resize(std::distance(str.data(), ret.ptr));
243 return str;
244 };
245
246 if (auto const& fName = getFName(); fName.shouldMeta(SField::sMD_BaseTen))
247 {
248 return convertToString(value_, 10); // Convert to base 10
249 }
250
251 return convertToString(value_, 16); // Convert to base 16
252}
253
254} // namespace ripple
Represents a JSON value.
Definition: json_value.h:150
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
Definition: KnownFormats.h:129
static LedgerFormats const & getInstance()
TxType permissionToTxType(uint32_t const &value) const
std::optional< std::string > getGranularName(GranularPermissionType const &value) const
static Permission const & getInstance()
Definition: Permissions.cpp:84
Identifies fields.
Definition: SField.h:143
bool shouldMeta(int c) const
Definition: SField.h:273
SField const & getFName() const
Definition: STBase.cpp:141
SField const * fName
Definition: STBase.h:137
STInteger(Integer v)
Definition: STInteger.h:85
Json::Value getJson(JsonOptions) const override
Definition: STInteger.cpp:74
std::string getText() const override
Definition: STInteger.cpp:56
value_type value() const noexcept
Definition: STInteger.h:146
Integer value_
Definition: STInteger.h:35
SerializedTypeID getSType() const override
Definition: STInteger.cpp:49
static constexpr TERSubset fromInt(int from)
Definition: TER.h:427
static TxFormats const & getInstance()
Definition: TxFormats.cpp:70
T distance(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
SerializedTypeID
Definition: SField.h:107
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:468
GranularPermissionType
We have both transaction type permissions and granular type permissions.
Definition: Permissions.h:38
bool transResultInfo(TER code, std::string &token, std::string &text)
Definition: TER.cpp:247
Note, should be treated as flags that can be | and &.
Definition: STBase.h:38
T to_chars(T... args)
T to_string(T... args)