xrpld
Loading...
Searching...
No Matches
Tx.cpp
1#include <xrpld/app/ledger/LedgerMaster.h>
2#include <xrpld/app/ledger/TransactionMaster.h>
3#include <xrpld/app/misc/DeliverMax.h>
4#include <xrpld/app/misc/Transaction.h>
5#include <xrpld/rpc/CTID.h>
6#include <xrpld/rpc/Context.h>
7#include <xrpld/rpc/DeliveredAmount.h>
8#include <xrpld/rpc/detail/RPCHelpers.h>
9#include <xrpld/rpc/detail/SpecBridge.hpp>
10#include <xrpld/rpc/detail/SyntheticFields.h>
11
12#include <xrpl/basics/Blob.h>
13#include <xrpl/basics/RangeSet.h>
14#include <xrpl/basics/Slice.h>
15#include <xrpl/basics/base_uint.h>
16#include <xrpl/basics/chrono.h>
17#include <xrpl/basics/strHex.h>
18#include <xrpl/beast/utility/instrumentation.h>
19#include <xrpl/core/NetworkIDService.h>
20#include <xrpl/json/json_value.h>
21#include <xrpl/protocol/ErrorCodes.h>
22#include <xrpl/protocol/RPCErr.h>
23#include <xrpl/protocol/SField.h>
24#include <xrpl/protocol/STBase.h>
25#include <xrpl/protocol/TxSearched.h>
26#include <xrpl/protocol/jss.h>
27#include <xrpl/rdb/RelationalDatabase.h>
28#include <xrpl/server/NetworkOPs.h>
29
30#include <rpcspec/Errors.hpp>
31
32#include <cstdint>
33#include <memory>
34#include <optional>
35#include <sstream>
36#include <string>
37#include <utility>
38#include <variant>
39
40namespace xrpl {
41
42static bool
43isValidated(LedgerMaster& ledgerMaster, std::uint32_t seq, UInt256 const& hash)
44{
45 if (!ledgerMaster.haveLedger(seq))
46 return false;
47
48 if (seq > ledgerMaster.getValidatedLedger()->header().seq)
49 return false;
50
51 return ledgerMaster.getHashBySeq(seq) == hash;
52}
53
64
72
75{
76 TxResult result;
77
79
80 if (args.ledgerRange)
81 {
82 static constexpr uint16_t kMaxRange = 1000;
83
84 if (args.ledgerRange->second < args.ledgerRange->first)
85 return {result, RpcInvalidLgrRange};
86
87 if (args.ledgerRange->second - args.ledgerRange->first > kMaxRange)
88 return {result, RpcExcessiveLgrRange};
89
90 range = ClosedInterval<uint32_t>(args.ledgerRange->first, args.ledgerRange->second);
91 }
92
93 auto ec{RpcSuccess};
94
96
99
100 if (args.ctid)
101 {
102 args.hash =
103 context.app.getLedgerMaster().txnIdFromIndex(args.ctid->first, args.ctid->second);
104
105 if (args.hash)
106 range = ClosedInterval<uint32_t>(args.ctid->first, args.ctid->second);
107 }
108
109 if (!args.hash)
110 return {result, RpcTxnNotFound};
111
112 if (args.ledgerRange)
113 {
114 v = context.app.getMasterTransaction().fetch(*args.hash, range, ec);
115 }
116 else
117 {
118 v = context.app.getMasterTransaction().fetch(*args.hash, ec);
119 }
120
121 if (auto e = std::get_if<TxSearched>(&v))
122 {
123 result.searchedAll = *e;
124 return {result, RpcTxnNotFound};
125 }
126
127 auto [txn, meta] = std::get<TxPair>(v);
128
129 if (ec == RpcDbDeserialization)
130 {
131 return {result, ec};
132 }
133 if (!txn)
134 {
135 return {result, RpcTxnNotFound};
136 }
137
138 // populate transaction data
139 result.txn = txn;
140 if (txn->getLedger() == 0)
141 {
142 return {result, RpcSuccess};
143 }
144
145 std::shared_ptr<Ledger const> const ledger =
146 context.ledgerMaster.getLedgerBySeq(txn->getLedger());
147
148 if (ledger && !ledger->open())
149 result.ledgerHash = ledger->header().hash;
150
151 if (ledger && meta)
152 {
153 if (args.binary)
154 {
155 result.meta = meta->getAsObject().getSerializer().getData();
156 }
157 else
158 {
159 result.meta = meta;
160 }
161 result.validated =
162 isValidated(context.ledgerMaster, ledger->header().seq, ledger->header().hash);
163 if (result.validated)
164 result.closeTime = context.ledgerMaster.getCloseTimeBySeq(txn->getLedger());
165
166 // compute outgoing CTID
167 if (meta->getAsObject().isFieldPresent(sfTransactionIndex))
168 {
169 uint32_t const lgrSeq = ledger->header().seq;
170 uint32_t const txnIdx = meta->getAsObject().getFieldU32(sfTransactionIndex);
171 uint32_t const netID = context.app.getNetworkIDService().getNetworkID();
172
173 if (txnIdx <= 0xFFFFU && netID < 0xFFFFU && lgrSeq < 0x0FFF'FFFFUL)
174 result.ctid = rpc::encodeCTID(lgrSeq, txnIdx, netID);
175 }
176 }
177
178 return {result, RpcSuccess};
179}
180
184 TxArgs const& args,
185 rpc::JsonContext const& context)
186{
187 json::Value response;
188 ::rpc::Status const& error = res.second;
189 TxResult const& result = res.first;
190 // handle errors
191 if (error != RpcSuccess)
192 {
193 if (error == RpcTxnNotFound && result.searchedAll != TxSearched::Unknown)
194 {
196 response[jss::searched_all] = (result.searchedAll == TxSearched::All);
197 rpc::injectSpecError(response, error);
198 }
199 else
200 {
201 rpc::injectSpecError(response, error);
202 }
203 }
204 // no errors
205 else if (result.txn)
206 {
207 auto const& sttx = result.txn->getSTransaction();
208 if (context.apiVersion > 1)
209 {
210 static constexpr auto kOptionsJson =
213 if (args.binary)
214 {
215 response[jss::tx_blob] = result.txn->getJson(kOptionsJson, true);
216 }
217 else
218 {
219 response[jss::tx_json] = result.txn->getJson(kOptionsJson);
221 response[jss::tx_json], sttx->getTxnType(), context.apiVersion);
222 }
223
224 // Note, result.ledgerHash is only set in a closed or validated
225 // ledger - as seen in `doTxHelp`
226 if (result.ledgerHash)
227 response[jss::ledger_hash] = to_string(*result.ledgerHash);
228
229 response[jss::hash] = to_string(result.txn->getID());
230 if (result.validated)
231 {
232 response[jss::ledger_index] = result.txn->getLedger();
233 if (result.closeTime)
234 response[jss::close_time_iso] = toStringIso(*result.closeTime);
235 }
236 }
237 else
238 {
239 response = result.txn->getJson(JsonOptions::Values::IncludeDate, args.binary);
240 if (!args.binary)
241 rpc::insertDeliverMax(response, sttx->getTxnType(), context.apiVersion);
242 }
243
244 // populate binary metadata
245 if (auto blob = std::get_if<Blob>(&result.meta))
246 {
247 XRPL_ASSERT(args.binary, "xrpl::populateJsonResponse : binary is set");
248 auto const jsonMeta = (context.apiVersion > 1 ? jss::meta_blob : jss::meta);
249 response[jsonMeta] = strHex(makeSlice(*blob));
250 }
251 // populate meta data
252 else if (auto m = std::get_if<std::shared_ptr<TxMeta>>(&result.meta))
253 {
254 auto& meta = *m;
255 if (meta)
256 {
257 response[jss::meta] = meta->getJson(JsonOptions::Values::None);
258 rpc::insertAllSyntheticInJson(response[jss::meta], context, sttx, *meta);
259 }
260 }
261 response[jss::validated] = result.validated;
262
263 if (result.ctid)
264 response[jss::ctid] = *result.ctid;
265 }
266 return response;
267}
268
271{
272 if (!context.app.config().useTxTables())
273 return rpcError(RpcNotEnabled);
274
275 // Deserialize and validate JSON arguments
276
277 TxArgs args;
278
279 if (context.params.isMember(jss::transaction) && context.params.isMember(jss::ctid))
280 {
281 // specifying both is ambiguous
283 }
284
285 if (context.params.isMember(jss::transaction))
286 {
287 UInt256 hash;
288 if (!hash.parseHex(context.params[jss::transaction].asString()))
289 return rpcError(RpcNotImpl);
290 args.hash = hash;
291 }
292 else if (context.params.isMember(jss::ctid))
293 {
294 auto ctid = rpc::decodeCTID(context.params[jss::ctid].asString());
295 if (!ctid)
297
298 auto const [lgrSeq, txnIdx, netId] = *ctid;
299 if (netId != context.app.getNetworkIDService().getNetworkID())
300 {
302 out << "Wrong network. You should submit this request to a node "
303 "running on NetworkID: "
304 << netId;
305 return rpc::makeError(RpcWrongNetwork, out.str());
306 }
307 args.ctid = {lgrSeq, txnIdx};
308 }
309 else
310 {
312 }
313
314 args.binary = context.params.isMember(jss::binary) && context.params[jss::binary].asBool();
315
316 if (context.params.isMember(jss::min_ledger) && context.params.isMember(jss::max_ledger))
317 {
318 try
319 {
321 context.params[jss::min_ledger].asUInt(), context.params[jss::max_ledger].asUInt());
322 }
323 catch (...)
324 {
325 // One of the calls to `asUInt ()` failed.
327 }
328 }
329
330 std::pair<TxResult, ::rpc::Status> const res = doTxHelp(context, args);
331 return populateJsonResponse(res, args, context);
332}
333
334} // namespace xrpl
Represents a JSON value.
Definition json_value.h:121
bool asBool() const
UInt asUInt() const
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
virtual Config & config()=0
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:526
bool useTxTables() const
Tracks the current ledger and any ledgers in the process of closing.
std::shared_ptr< Ledger const > getLedgerBySeq(std::uint32_t index)
Finds a ledger by sequence, preferring the validated chain.
UInt256 getHashBySeq(std::uint32_t index)
Get a ledger's hash by sequence number using the cache.
std::optional< UInt256 > txnIdFromIndex(uint32_t ledgerSeq, uint32_t txnIndex)
Iff a txn exists at the specified ledger and offset then return its txnid.
std::optional< NetClock::time_point > getCloseTimeBySeq(LedgerIndex ledgerIndex)
std::shared_ptr< Ledger const > getValidatedLedger()
The validated ledger is the last fully validated ledger.
bool haveLedger(std::uint32_t seq) const
virtual std::uint32_t getNetworkID() const noexcept=0
Get the configured network ID.
virtual TransactionMaster & getMasterTransaction()=0
virtual NetworkIDService & getNetworkIDService()=0
virtual LedgerMaster & getLedgerMaster()=0
std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > fetch(UInt256 const &, ErrorCodeI &ec)
json::Value getJson(JsonOptions options, bool binary=false) const
LedgerIndex getLedger() const
Definition Transaction.h:89
UInt256 const & getID() const
Definition Transaction.h:83
std::shared_ptr< Transaction > pointer
Definition Transaction.h:57
std::shared_ptr< STTx const > const & getSTransaction()
Definition Transaction.h:77
T get_if(T... args)
T make_pair(T... args)
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
std::optional< std::string > encodeCTID(uint32_t ledgerSeq, uint32_t txnIndex, uint32_t networkID) noexcept
Encodes ledger sequence, transaction index, and network ID into a CTID string.
Definition CTID.h:36
void insertDeliverMax(json::Value &txJson, TxType txnType, unsigned int apiVersion)
Copy Amount field to DeliverMax field in transaction output JSON.
Definition DeliverMax.cpp:9
void insertAllSyntheticInJson(json::Value &metadata, ReadView const &ledger, std::shared_ptr< STTx const > const &transaction, TxMeta const &transactionMeta)
Adds all synthetic fields to transaction metadata JSON.
json::Value makeError(ErrorCodeI code)
Returns a new json object that reflects the error code.
std::optional< std::tuple< uint32_t, uint16_t, uint16_t > > decodeCTID(T const ctid) noexcept
Decodes a CTID string or integer into its component parts.
Definition CTID.h:63
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcNotImpl
Definition ErrorCodes.h:114
@ RpcSuccess
Definition ErrorCodes.h:27
@ RpcExcessiveLgrRange
Definition ErrorCodes.h:118
@ RpcTxnNotFound
Definition ErrorCodes.h:63
@ RpcNotEnabled
Definition ErrorCodes.h:42
@ RpcInvalidParams
Definition ErrorCodes.h:67
@ RpcWrongNetwork
Definition ErrorCodes.h:33
@ RpcDbDeserialization
Definition ErrorCodes.h:117
@ RpcInvalidLgrRange
Definition ErrorCodes.h:119
json::Value populateJsonResponse(std::pair< AccountTxResult, ::rpc::Status > const &res, AccountTxArgs const &args, rpc::JsonContext const &context)
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition RangeSet.h:37
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
static bool isValidated(LedgerMaster &ledgerMaster, std::uint32_t seq, UInt256 const &hash)
Definition Tx.cpp:43
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
std::string toStringIso(date::sys_time< Duration > tp)
Definition chrono.h:70
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
std::pair< TxResult, ::rpc::Status > doTxHelp(rpc::Context &context, TxArgs args)
Definition Tx.cpp:74
json::Value rpcError(ErrorCodeI iError)
Definition RPCErr.cpp:13
BaseUInt< 256 > UInt256
Definition base_uint.h:581
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition RangeSet.h:27
TxSearched
Definition TxSearched.h:5
json::Value doTxJson(rpc::JsonContext &)
Definition Tx.cpp:270
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:11
T str(T... args)
unsigned int UnderlyingT
Definition STBase.h:23
std::optional< std::pair< uint32_t, uint32_t > > ledgerRange
Definition Tx.cpp:70
std::optional< std::pair< uint32_t, uint16_t > > ctid
Definition Tx.cpp:68
bool binary
Definition Tx.cpp:69
std::optional< UInt256 > hash
Definition Tx.cpp:67
Transaction::pointer txn
Definition Tx.cpp:56
TxSearched searchedAll
Definition Tx.cpp:62
std::optional< NetClock::time_point > closeTime
Definition Tx.cpp:60
bool validated
Definition Tx.cpp:58
std::optional< std::string > ctid
Definition Tx.cpp:59
std::optional< UInt256 > ledgerHash
Definition Tx.cpp:61
std::variant< std::shared_ptr< TxMeta >, Blob > meta
Definition Tx.cpp:57
The context of information needed to call an RPC.
Definition Context.h:27
unsigned int apiVersion
Definition Context.h:37
Application & app
Definition Context.h:29
LedgerMaster & ledgerMaster
Definition Context.h:32
json::Value params
Definition Context.h:51