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