rippled
Loading...
Searching...
No Matches
AccountInfo.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2014 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 <xrpld/app/main/Application.h>
21#include <xrpld/app/misc/TxQ.h>
22#include <xrpld/rpc/Context.h>
23#include <xrpld/rpc/GRPCHandlers.h>
24#include <xrpld/rpc/detail/RPCHelpers.h>
25
26#include <xrpl/json/json_value.h>
27#include <xrpl/ledger/ReadView.h>
28#include <xrpl/protocol/ErrorCodes.h>
29#include <xrpl/protocol/Indexes.h>
30#include <xrpl/protocol/UintTypes.h>
31#include <xrpl/protocol/jss.h>
32
33namespace ripple {
34
35// {
36// account: <ident>,
37// ledger_hash : <ledger>
38// ledger_index : <ledger_index>
39// signer_lists : <bool> // optional (default false)
40// // if true return SignerList(s).
41// queue : <bool> // optional (default false)
42// // if true return information about transactions
43// // in the current TxQ, only if the requested
44// // ledger is open. Otherwise if true, returns an
45// // error.
46// }
47
48// TODO(tom): what is that "default"?
51{
52 auto& params = context.params;
53
54 std::string strIdent;
55 if (params.isMember(jss::account))
56 {
57 if (!params[jss::account].isString())
58 return RPC::invalid_field_error(jss::account);
59 strIdent = params[jss::account].asString();
60 }
61 else if (params.isMember(jss::ident))
62 {
63 if (!params[jss::ident].isString())
64 return RPC::invalid_field_error(jss::ident);
65 strIdent = params[jss::ident].asString();
66 }
67 else
68 return RPC::missing_field_error(jss::account);
69
71 auto result = RPC::lookupLedger(ledger, context);
72
73 if (!ledger)
74 return result;
75
76 // Get info on account.
77 auto id = parseBase58<AccountID>(strIdent);
78 if (!id)
79 {
81 return result;
82 }
83 auto const accountID{std::move(id.value())};
84
85 static constexpr std::
87 lsFlags{
88 {{"defaultRipple", lsfDefaultRipple},
89 {"depositAuth", lsfDepositAuth},
90 {"disableMasterKey", lsfDisableMaster},
91 {"disallowIncomingXRP", lsfDisallowXRP},
92 {"globalFreeze", lsfGlobalFreeze},
93 {"noFreeze", lsfNoFreeze},
94 {"passwordSpent", lsfPasswordSpent},
95 {"requireAuthorization", lsfRequireAuth},
96 {"requireDestinationTag", lsfRequireDestTag}}};
97
98 static constexpr std::
100 disallowIncomingFlags{
101 {{"disallowIncomingNFTokenOffer",
103 {"disallowIncomingCheck", lsfDisallowIncomingCheck},
104 {"disallowIncomingPayChan", lsfDisallowIncomingPayChan},
105 {"disallowIncomingTrustline", lsfDisallowIncomingTrustline}}};
106
108 allowTrustLineClawbackFlag{
109 "allowTrustLineClawback", lsfAllowTrustLineClawback};
110
112 allowTrustLineLockingFlag{
113 "allowTrustLineLocking", lsfAllowTrustLineLocking};
114
115 auto const sleAccepted = ledger->read(keylet::account(accountID));
116 if (sleAccepted)
117 {
118 auto const queue =
119 params.isMember(jss::queue) && params[jss::queue].asBool();
120
121 if (queue && !ledger->open())
122 {
123 // It doesn't make sense to request the queue
124 // with any closed or validated ledger.
126 return result;
127 }
128
129 Json::Value jvAccepted(Json::objectValue);
130 RPC::injectSLE(jvAccepted, *sleAccepted);
131 result[jss::account_data] = jvAccepted;
132
134 for (auto const& lsf : lsFlags)
135 acctFlags[lsf.first.data()] = sleAccepted->isFlag(lsf.second);
136
137 if (ledger->rules().enabled(featureDisallowIncoming))
138 {
139 for (auto const& lsf : disallowIncomingFlags)
140 acctFlags[lsf.first.data()] = sleAccepted->isFlag(lsf.second);
141 }
142
143 if (ledger->rules().enabled(featureClawback))
144 acctFlags[allowTrustLineClawbackFlag.first.data()] =
145 sleAccepted->isFlag(allowTrustLineClawbackFlag.second);
146
147 if (ledger->rules().enabled(featureTokenEscrow))
148 acctFlags[allowTrustLineLockingFlag.first.data()] =
149 sleAccepted->isFlag(allowTrustLineLockingFlag.second);
150
151 result[jss::account_flags] = std::move(acctFlags);
152
153 // The document[https://xrpl.org/account_info.html#account_info] states
154 // that signer_lists is a bool, however assigning any string value
155 // works. Do not allow this. This check is for api Version 2 onwards
156 // only
157 if (context.apiVersion > 1u && params.isMember(jss::signer_lists) &&
158 !params[jss::signer_lists].isBool())
159 {
161 return result;
162 }
163
164 // Return SignerList(s) if that is requested.
165 if (params.isMember(jss::signer_lists) &&
166 params[jss::signer_lists].asBool())
167 {
168 // We put the SignerList in an array because of an anticipated
169 // future when we support multiple signer lists on one account.
170 Json::Value jvSignerList = Json::arrayValue;
171
172 // This code will need to be revisited if in the future we support
173 // multiple SignerLists on one account.
174 auto const sleSigners = ledger->read(keylet::signers(accountID));
175 if (sleSigners)
176 jvSignerList.append(sleSigners->getJson(JsonOptions::none));
177
178 // Documentation states this is returned as part of the account_info
179 // response, but previously the code put it under account_data. We
180 // can move this to the documentated location from apiVersion 2
181 // onwards.
182 if (context.apiVersion == 1)
183 {
184 result[jss::account_data][jss::signer_lists] =
185 std::move(jvSignerList);
186 }
187 else
188 {
189 result[jss::signer_lists] = std::move(jvSignerList);
190 }
191 }
192 // Return queue info if that is requested
193 if (queue)
194 {
195 Json::Value jvQueueData = Json::objectValue;
196
197 auto const txs = context.app.getTxQ().getAccountTxs(accountID);
198 if (!txs.empty())
199 {
200 jvQueueData[jss::txn_count] =
201 static_cast<Json::UInt>(txs.size());
202
203 auto& jvQueueTx = jvQueueData[jss::transactions];
204 jvQueueTx = Json::arrayValue;
205
206 std::uint32_t seqCount = 0;
207 std::uint32_t ticketCount = 0;
210 std::optional<std::uint32_t> lowestTicket;
211 std::optional<std::uint32_t> highestTicket;
212 bool anyAuthChanged = false;
213 XRPAmount totalSpend(0);
214
215 // We expect txs to be returned sorted by SeqProxy. Verify
216 // that with a couple of asserts.
217 SeqProxy prevSeqProxy = SeqProxy::sequence(0);
218 for (auto const& tx : txs)
219 {
221
222 if (tx.seqProxy.isSeq())
223 {
224 XRPL_ASSERT(
225 prevSeqProxy < tx.seqProxy,
226 "rpple::doAccountInfo : first sorted proxy");
227 prevSeqProxy = tx.seqProxy;
228 jvTx[jss::seq] = tx.seqProxy.value();
229 ++seqCount;
230 if (!lowestSeq)
231 lowestSeq = tx.seqProxy.value();
232 highestSeq = tx.seqProxy.value();
233 }
234 else
235 {
236 XRPL_ASSERT(
237 prevSeqProxy < tx.seqProxy,
238 "rpple::doAccountInfo : second sorted proxy");
239 prevSeqProxy = tx.seqProxy;
240 jvTx[jss::ticket] = tx.seqProxy.value();
241 ++ticketCount;
242 if (!lowestTicket)
243 lowestTicket = tx.seqProxy.value();
244 highestTicket = tx.seqProxy.value();
245 }
246
247 jvTx[jss::fee_level] = to_string(tx.feeLevel);
248 if (tx.lastValid)
249 jvTx[jss::LastLedgerSequence] = *tx.lastValid;
250
251 jvTx[jss::fee] = to_string(tx.consequences.fee());
252 auto const spend = tx.consequences.potentialSpend() +
253 tx.consequences.fee();
254 jvTx[jss::max_spend_drops] = to_string(spend);
255 totalSpend += spend;
256 bool const authChanged = tx.consequences.isBlocker();
257 if (authChanged)
258 anyAuthChanged = authChanged;
259 jvTx[jss::auth_change] = authChanged;
260
261 jvQueueTx.append(std::move(jvTx));
262 }
263
264 if (seqCount)
265 jvQueueData[jss::sequence_count] = seqCount;
266 if (ticketCount)
267 jvQueueData[jss::ticket_count] = ticketCount;
268 if (lowestSeq)
269 jvQueueData[jss::lowest_sequence] = *lowestSeq;
270 if (highestSeq)
271 jvQueueData[jss::highest_sequence] = *highestSeq;
272 if (lowestTicket)
273 jvQueueData[jss::lowest_ticket] = *lowestTicket;
274 if (highestTicket)
275 jvQueueData[jss::highest_ticket] = *highestTicket;
276
277 jvQueueData[jss::auth_change_queued] = anyAuthChanged;
278 jvQueueData[jss::max_spend_drops_total] = to_string(totalSpend);
279 }
280 else
281 jvQueueData[jss::txn_count] = 0u;
282
283 result[jss::queue_data] = std::move(jvQueueData);
284 }
285 }
286 else
287 {
288 result[jss::account] = toBase58(accountID);
290 }
291
292 return result;
293}
294
295} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
Value & append(Value const &value)
Append value to array at the end.
virtual TxQ & getTxQ()=0
A type that represents either a sequence value or a ticket value.
Definition SeqProxy.h:56
static constexpr SeqProxy sequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:76
std::vector< TxDetails > getAccountTxs(AccountID const &account) const
Returns information about the transactions currently in the queue for the account.
Definition TxQ.cpp:1822
T is_same_v
@ arrayValue
array value (ordered list)
Definition json_value.h:44
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:45
unsigned int UInt
Json::Value invalid_field_error(std::string const &name)
Definition ErrorCodes.h:325
void injectSLE(Json::Value &jv, SLE const &sle)
Inject JSON describing ledger entry.
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition ErrorCodes.h:233
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext &context, Json::Value &result)
Look up a ledger from a request and fill a Json::Result with the data representing a ledger.
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:283
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:184
Keylet signers(AccountID const &account) noexcept
A SignerList.
Definition Indexes.cpp:330
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
@ rpcACT_NOT_FOUND
Definition ErrorCodes.h:70
@ rpcACT_MALFORMED
Definition ErrorCodes.h:90
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:84
@ lsfRequireDestTag
@ lsfPasswordSpent
@ lsfDefaultRipple
@ lsfDisallowIncomingCheck
@ lsfAllowTrustLineClawback
@ lsfDisableMaster
@ lsfDisallowIncomingPayChan
@ lsfDisallowIncomingTrustline
@ lsfAllowTrustLineLocking
@ lsfDisallowIncomingNFTokenOffer
@ lsfGlobalFreeze
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
Json::Value doAccountInfo(RPC::JsonContext &context)
unsigned int apiVersion
Definition Context.h:49
Application & app
Definition Context.h:41
T value(T... args)