rippled
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/NetworkOPs.h>
5#include <xrpld/app/misc/Transaction.h>
6#include <xrpld/app/rdb/RelationalDatabase.h>
7#include <xrpld/rpc/CTID.h>
8#include <xrpld/rpc/Context.h>
9#include <xrpld/rpc/DeliveredAmount.h>
10#include <xrpld/rpc/GRPCHandlers.h>
11#include <xrpld/rpc/MPTokenIssuanceID.h>
12
13#include <xrpl/basics/ToString.h>
14#include <xrpl/protocol/ErrorCodes.h>
15#include <xrpl/protocol/NFTSyntheticSerializer.h>
16#include <xrpl/protocol/RPCErr.h>
17#include <xrpl/protocol/jss.h>
18
19#include <regex>
20
21namespace xrpl {
22
23static bool
25{
26 if (!ledgerMaster.haveLedger(seq))
27 return false;
28
29 if (seq > ledgerMaster.getValidatedLedger()->header().seq)
30 return false;
31
32 return ledgerMaster.getHashBySeq(seq) == hash;
33}
34
45
53
56{
57 TxResult result;
58
60
61 if (args.ledgerRange)
62 {
63 constexpr uint16_t MAX_RANGE = 1000;
64
65 if (args.ledgerRange->second < args.ledgerRange->first)
66 return {result, rpcINVALID_LGR_RANGE};
67
68 if (args.ledgerRange->second - args.ledgerRange->first > MAX_RANGE)
69 return {result, rpcEXCESSIVE_LGR_RANGE};
70
71 range = ClosedInterval<uint32_t>(args.ledgerRange->first, args.ledgerRange->second);
72 }
73
74 auto ec{rpcSUCCESS};
75
77
80
81 if (args.ctid)
82 {
83 args.hash = context.app.getLedgerMaster().txnIdFromIndex(args.ctid->first, args.ctid->second);
84
85 if (args.hash)
86 range = ClosedInterval<uint32_t>(args.ctid->first, args.ctid->second);
87 }
88
89 if (!args.hash)
90 return {result, rpcTXN_NOT_FOUND};
91
92 if (args.ledgerRange)
93 {
94 v = context.app.getMasterTransaction().fetch(*(args.hash), range, ec);
95 }
96 else
97 {
98 v = context.app.getMasterTransaction().fetch(*(args.hash), ec);
99 }
100
101 if (auto e = std::get_if<TxSearched>(&v))
102 {
103 result.searchedAll = *e;
104 return {result, rpcTXN_NOT_FOUND};
105 }
106
107 auto [txn, meta] = std::get<TxPair>(v);
108
109 if (ec == rpcDB_DESERIALIZATION)
110 {
111 return {result, ec};
112 }
113 if (!txn)
114 {
115 return {result, rpcTXN_NOT_FOUND};
116 }
117
118 // populate transaction data
119 result.txn = txn;
120 if (txn->getLedger() == 0)
121 {
122 return {result, rpcSUCCESS};
123 }
124
125 std::shared_ptr<Ledger const> ledger = context.ledgerMaster.getLedgerBySeq(txn->getLedger());
126
127 if (ledger && !ledger->open())
128 result.ledgerHash = ledger->header().hash;
129
130 if (ledger && meta)
131 {
132 if (args.binary)
133 {
134 result.meta = meta->getAsObject().getSerializer().getData();
135 }
136 else
137 {
138 result.meta = meta;
139 }
140 result.validated = isValidated(context.ledgerMaster, ledger->header().seq, ledger->header().hash);
141 if (result.validated)
142 result.closeTime = context.ledgerMaster.getCloseTimeBySeq(txn->getLedger());
143
144 // compute outgoing CTID
145 if (meta->getAsObject().isFieldPresent(sfTransactionIndex))
146 {
147 uint32_t lgrSeq = ledger->header().seq;
148 uint32_t txnIdx = meta->getAsObject().getFieldU32(sfTransactionIndex);
149 uint32_t netID = context.app.config().NETWORK_ID;
150
151 if (txnIdx <= 0xFFFFU && netID < 0xFFFFU && lgrSeq < 0x0FFF'FFFFUL)
152 result.ctid = RPC::encodeCTID(lgrSeq, (uint32_t)txnIdx, (uint32_t)netID);
153 }
154 }
155
156 return {result, rpcSUCCESS};
157}
158
161{
162 Json::Value response;
163 RPC::Status const& error = res.second;
164 TxResult const& result = res.first;
165 // handle errors
166 if (error.toErrorCode() != rpcSUCCESS)
167 {
168 if (error.toErrorCode() == rpcTXN_NOT_FOUND && result.searchedAll != TxSearched::unknown)
169 {
170 response = Json::Value(Json::objectValue);
171 response[jss::searched_all] = (result.searchedAll == TxSearched::all);
172 error.inject(response);
173 }
174 else
175 {
176 error.inject(response);
177 }
178 }
179 // no errors
180 else if (result.txn)
181 {
182 auto const& sttx = result.txn->getSTransaction();
183 if (context.apiVersion > 1)
184 {
186 if (args.binary)
187 response[jss::tx_blob] = result.txn->getJson(optionsJson, true);
188 else
189 {
190 response[jss::tx_json] = result.txn->getJson(optionsJson);
191 RPC::insertDeliverMax(response[jss::tx_json], sttx->getTxnType(), context.apiVersion);
192 }
193
194 // Note, result.ledgerHash is only set in a closed or validated
195 // ledger - as seen in `doTxHelp`
196 if (result.ledgerHash)
197 response[jss::ledger_hash] = to_string(*result.ledgerHash);
198
199 response[jss::hash] = to_string(result.txn->getID());
200 if (result.validated)
201 {
202 response[jss::ledger_index] = result.txn->getLedger();
203 if (result.closeTime)
204 response[jss::close_time_iso] = to_string_iso(*result.closeTime);
205 }
206 }
207 else
208 {
209 response = result.txn->getJson(JsonOptions::include_date, args.binary);
210 if (!args.binary)
211 RPC::insertDeliverMax(response, sttx->getTxnType(), context.apiVersion);
212 }
213
214 // populate binary metadata
215 if (auto blob = std::get_if<Blob>(&result.meta))
216 {
217 XRPL_ASSERT(args.binary, "xrpl::populateJsonResponse : binary is set");
218 auto json_meta = (context.apiVersion > 1 ? jss::meta_blob : jss::meta);
219 response[json_meta] = strHex(makeSlice(*blob));
220 }
221 // populate meta data
222 else if (auto m = std::get_if<std::shared_ptr<TxMeta>>(&result.meta))
223 {
224 auto& meta = *m;
225 if (meta)
226 {
227 response[jss::meta] = meta->getJson(JsonOptions::none);
228 insertDeliveredAmount(response[jss::meta], context, result.txn, *meta);
229 RPC::insertNFTSyntheticInJson(response, sttx, *meta);
230 RPC::insertMPTokenIssuanceID(response[jss::meta], sttx, *meta);
231 }
232 }
233 response[jss::validated] = result.validated;
234
235 if (result.ctid)
236 response[jss::ctid] = *(result.ctid);
237 }
238 return response;
239}
240
243{
244 if (!context.app.config().useTxTables())
245 return rpcError(rpcNOT_ENABLED);
246
247 // Deserialize and validate JSON arguments
248
249 TxArgs args;
250
251 if (context.params.isMember(jss::transaction) && context.params.isMember(jss::ctid))
252 // specifying both is ambiguous
254
255 if (context.params.isMember(jss::transaction))
256 {
257 uint256 hash;
258 if (!hash.parseHex(context.params[jss::transaction].asString()))
259 return rpcError(rpcNOT_IMPL);
260 args.hash = hash;
261 }
262 else if (context.params.isMember(jss::ctid))
263 {
264 auto ctid = RPC::decodeCTID(context.params[jss::ctid].asString());
265 if (!ctid)
267
268 auto const [lgr_seq, txn_idx, net_id] = *ctid;
269 if (net_id != context.app.config().NETWORK_ID)
270 {
272 out << "Wrong network. You should submit this request to a node "
273 "running on NetworkID: "
274 << net_id;
275 return RPC::make_error(rpcWRONG_NETWORK, out.str());
276 }
277 args.ctid = {lgr_seq, txn_idx};
278 }
279 else
281
282 args.binary = context.params.isMember(jss::binary) && context.params[jss::binary].asBool();
283
284 if (context.params.isMember(jss::min_ledger) && context.params.isMember(jss::max_ledger))
285 {
286 try
287 {
288 args.ledgerRange =
289 std::make_pair(context.params[jss::min_ledger].asUInt(), context.params[jss::max_ledger].asUInt());
290 }
291 catch (...)
292 {
293 // One of the calls to `asUInt ()` failed.
295 }
296 }
297
298 std::pair<TxResult, RPC::Status> res = doTxHelp(context, args);
299 return populateJsonResponse(res, args, context);
300}
301
302} // namespace xrpl
Represents a JSON value.
Definition json_value.h:131
UInt asUInt() const
std::string asString() const
Returns the unquoted string value.
bool asBool() const
bool isMember(char const *key) const
Return true if the object has a member named key.
virtual Config & config()=0
virtual LedgerMaster & getLedgerMaster()=0
virtual TransactionMaster & getMasterTransaction()=0
uint32_t NETWORK_ID
Definition Config.h:138
bool useTxTables() const
Definition Config.h:318
std::shared_ptr< Ledger const > getLedgerBySeq(std::uint32_t index)
std::optional< NetClock::time_point > getCloseTimeBySeq(LedgerIndex ledgerIndex)
std::optional< uint256 > txnIdFromIndex(uint32_t ledgerSeq, uint32_t txnIndex)
std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > fetch(uint256 const &, error_code_i &ec)
LedgerIndex getLedger() const
Definition Transaction.h:78
uint256 const & getID() const
Definition Transaction.h:72
Json::Value getJson(JsonOptions options, bool binary=false) const
std::shared_ptr< STTx const > const & getSTransaction()
Definition Transaction.h:66
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:472
T get_if(T... args)
T is_same_v
T make_pair(T... args)
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:27
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:34
void insertDeliverMax(Json::Value &tx_json, TxType txnType, unsigned int apiVersion)
Copy Amount field to DeliverMax field in transaction output JSON.
Definition DeliverMax.cpp:9
void insertMPTokenIssuanceID(Json::Value &response, std::shared_ptr< STTx const > const &transaction, TxMeta const &transactionMeta)
void insertNFTSyntheticInJson(Json::Value &, std::shared_ptr< STTx const > const &, TxMeta const &)
Adds common synthetic fields to transaction-related JSON responses.
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:61
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
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:35
Json::Value doTxJson(RPC::JsonContext &)
Definition Tx.cpp:242
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:598
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:11
static bool isValidated(LedgerMaster &ledgerMaster, std::uint32_t seq, uint256 const &hash)
Definition Tx.cpp:24
std::pair< TxResult, RPC::Status > doTxHelp(RPC::Context &context, TxArgs args)
Definition Tx.cpp:55
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition RangeSet.h:26
Json::Value rpcError(error_code_i iError)
Definition RPCErr.cpp:12
std::string to_string_iso(date::sys_time< Duration > tp)
Definition chrono.h:68
@ ledgerMaster
ledger master data for signing
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition Slice.h:214
@ rpcTXN_NOT_FOUND
Definition ErrorCodes.h:61
@ rpcNOT_IMPL
Definition ErrorCodes.h:112
@ rpcEXCESSIVE_LGR_RANGE
Definition ErrorCodes.h:116
@ rpcNOT_ENABLED
Definition ErrorCodes.h:40
@ rpcDB_DESERIALIZATION
Definition ErrorCodes.h:115
@ rpcWRONG_NETWORK
Definition ErrorCodes.h:31
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:65
@ rpcINVALID_LGR_RANGE
Definition ErrorCodes.h:117
@ rpcSUCCESS
Definition ErrorCodes.h:25
The context of information needed to call an RPC.
Definition Context.h:20
Application & app
Definition Context.h:22
unsigned int apiVersion
Definition Context.h:30
LedgerMaster & ledgerMaster
Definition Context.h:25
Json::Value params
Definition Context.h:44
Status represents the results of an operation that might fail.
Definition Status.h:21
std::optional< std::pair< uint32_t, uint32_t > > ledgerRange
Definition Tx.cpp:51
std::optional< std::pair< uint32_t, uint16_t > > ctid
Definition Tx.cpp:49
bool binary
Definition Tx.cpp:50
std::optional< uint256 > hash
Definition Tx.cpp:48
std::optional< uint256 > ledgerHash
Definition Tx.cpp:42
Transaction::pointer txn
Definition Tx.cpp:37
TxSearched searchedAll
Definition Tx.cpp:43
std::optional< NetClock::time_point > closeTime
Definition Tx.cpp:41
bool validated
Definition Tx.cpp:39
std::optional< std::string > ctid
Definition Tx.cpp:40
std::variant< std::shared_ptr< TxMeta >, Blob > meta
Definition Tx.cpp:38