xrpld
Loading...
Searching...
No Matches
Simulate.cpp
1#include <xrpld/app/ledger/LedgerMaster.h>
2#include <xrpld/app/ledger/OpenLedger.h>
3#include <xrpld/app/misc/Transaction.h>
4#include <xrpld/app/misc/TxQ.h>
5#include <xrpld/rpc/Context.h>
6#include <xrpld/rpc/DeliveredAmount.h>
7#include <xrpld/rpc/MPTokenIssuanceID.h>
8#include <xrpld/rpc/detail/TransactionSign.h>
9
10#include <xrpl/basics/Log.h>
11#include <xrpl/basics/Number.h>
12#include <xrpl/basics/Slice.h>
13#include <xrpl/basics/StringUtilities.h>
14#include <xrpl/basics/strHex.h>
15#include <xrpl/core/NetworkIDService.h>
16#include <xrpl/core/ServiceRegistry.h>
17#include <xrpl/json/json_value.h>
18#include <xrpl/ledger/ApplyView.h>
19#include <xrpl/ledger/OpenView.h>
20#include <xrpl/protocol/AccountID.h>
21#include <xrpl/protocol/ErrorCodes.h>
22#include <xrpl/protocol/Indexes.h>
23#include <xrpl/protocol/NFTSyntheticSerializer.h>
24#include <xrpl/protocol/RPCErr.h>
25#include <xrpl/protocol/SField.h>
26#include <xrpl/protocol/STParsedJSON.h>
27#include <xrpl/protocol/Serializer.h>
28#include <xrpl/protocol/TER.h>
29#include <xrpl/protocol/TxFormats.h>
30#include <xrpl/protocol/jss.h>
31#include <xrpl/resource/Fees.h>
32
33#include <cstdint>
34#include <exception>
35#include <expected>
36#include <functional>
37#include <memory>
38#include <optional>
39#include <stdexcept>
40#include <string>
41#include <utility>
42
43namespace xrpl {
44
45static std::expected<std::uint32_t, json::Value>
47{
48 // autofill Sequence
49 bool const hasTicketSeq = txJson.isMember(sfTicketSequence.jsonName);
50 auto const& accountStr = txJson[jss::Account];
51 if (!accountStr.isString())
52 {
53 // sanity check, should fail earlier
54 // LCOV_EXCL_START
55 return std::unexpected(RPC::invalidFieldError("tx.Account"));
56 // LCOV_EXCL_STOP
57 }
58 auto const srcAddressID = parseBase58<AccountID>(accountStr.asString());
59 if (!srcAddressID.has_value())
60 {
61 return std::unexpected(
63 }
64 SLE::const_pointer const sle =
65 context.app.getOpenLedger().current()->read(keylet::account(*srcAddressID));
66 if (!hasTicketSeq && !sle)
67 {
68 JLOG(context.app.getJournal("Simulate").debug())
69 << "Failed to find source account "
70 << "in current ledger: " << toBase58(*srcAddressID);
71
73 }
74
75 return hasTicketSeq ? 0 : context.app.getTxQ().nextQueuableSeq(sle).value();
76}
77
80{
81 if (!sigObject.isMember(jss::SigningPubKey))
82 {
83 // autofill SigningPubKey
84 sigObject[jss::SigningPubKey] = "";
85 }
86
87 if (sigObject.isMember(jss::Signers))
88 {
89 if (!sigObject[jss::Signers].isArray())
90 return RPC::invalidFieldError("tx.Signers");
91 // check multisigned signers
92 for (unsigned index = 0; index < sigObject[jss::Signers].size(); index++)
93 {
94 auto& signer = sigObject[jss::Signers][index];
95 if (!signer.isObject() || !signer.isMember(jss::Signer) ||
96 !signer[jss::Signer].isObject())
97 return RPC::invalidFieldError("tx.Signers[" + std::to_string(index) + "]");
98
99 if (!signer[jss::Signer].isMember(jss::SigningPubKey))
100 {
101 // autofill SigningPubKey
102 signer[jss::Signer][jss::SigningPubKey] = "";
103 }
104
105 if (!signer[jss::Signer].isMember(jss::TxnSignature))
106 {
107 // autofill TxnSignature
108 signer[jss::Signer][jss::TxnSignature] = "";
109 }
110 else if (signer[jss::Signer][jss::TxnSignature] != "")
111 {
112 // Transaction must not be signed
113 return rpcError(RpcTxSigned);
114 }
115 }
116 }
117
118 if (!sigObject.isMember(jss::TxnSignature))
119 {
120 // autofill TxnSignature
121 sigObject[jss::TxnSignature] = "";
122 }
123 else if (sigObject[jss::TxnSignature] != "")
124 {
125 // Transaction must not be signed
126 return rpcError(RpcTxSigned);
127 }
128 return std::nullopt;
129}
130
133{
134 if (!txJson.isMember(jss::Fee))
135 {
136 // autofill Fee
137 // Must happen after all the other autofills happen
138 // Error handling/messaging works better that way
139 auto feeOrError = RPC::getCurrentNetworkFee(
140 context.role,
141 context.app.config(),
142 context.app.getFeeTrack(),
143 context.app.getTxQ(),
144 context.app,
145 txJson);
146 if (feeOrError.isMember(jss::error))
147 return feeOrError;
148 txJson[jss::Fee] = feeOrError;
149 }
150
151 if (auto error = autofillSignature(txJson))
152 return error;
153
154 if (!txJson.isMember(jss::Sequence))
155 {
156 auto const seq = getAutofillSequence(txJson, context);
157 if (!seq)
158 return seq.error();
159 txJson[sfSequence.jsonName] = *seq;
160 }
161
162 if (!txJson.isMember(jss::NetworkID))
163 {
164 auto const networkId = context.app.getNetworkIDService().getNetworkID();
165 if (networkId > 1024)
166 txJson[jss::NetworkID] = to_string(networkId);
167 }
168
169 return std::nullopt;
170}
171
172static json::Value
174{
175 json::Value txJson;
176
177 if (params.isMember(jss::tx_blob))
178 {
179 if (params.isMember(jss::tx_json))
180 {
181 return RPC::makeParamError("Can only include one of `tx_blob` and `tx_json`.");
182 }
183
184 auto const txBlob = params[jss::tx_blob];
185 if (!txBlob.isString())
186 {
187 return RPC::invalidFieldError(jss::tx_blob);
188 }
189
190 auto unHexed = strUnHex(txBlob.asString());
191 if (!unHexed || unHexed->empty())
192 return RPC::invalidFieldError(jss::tx_blob);
193
194 try
195 {
196 SerialIter sitTrans(makeSlice(*unHexed));
198 }
199 catch (std::runtime_error const&)
200 {
201 return RPC::invalidFieldError(jss::tx_blob);
202 }
203 }
204 else if (params.isMember(jss::tx_json))
205 {
206 txJson = params[jss::tx_json];
207 if (!txJson.isObject())
208 {
209 return RPC::objectFieldError(jss::tx_json);
210 }
211 }
212 else
213 {
214 return RPC::makeParamError("Neither `tx_blob` nor `tx_json` included.");
215 }
216
217 // basic sanity checks for transaction shape
218 if (!txJson.isMember(jss::TransactionType))
219 {
220 return RPC::missingFieldError("tx.TransactionType");
221 }
222
223 if (!txJson.isMember(jss::Account))
224 {
225 return RPC::missingFieldError("tx.Account");
226 }
227
228 return txJson;
229}
230
231static json::Value
233{
234 json::Value jvResult;
235 // Process the transaction
236 OpenView view = *context.app.getOpenLedger().current();
237 auto const result = context.app.getTxQ().apply(
238 context.app, view, transaction->getSTransaction(), TapDryRun, context.j);
239
240 jvResult[jss::applied] = result.applied;
241 jvResult[jss::ledger_index] = view.seq();
242
243 bool const isBinaryOutput = context.params.get(jss::binary, false).asBool();
244
245 // Convert the TER to human-readable values
246 std::string token;
247 std::string message;
248 if (transResultInfo(result.ter, token, message))
249 {
250 // Engine result
251 jvResult[jss::engine_result] = token;
252 jvResult[jss::engine_result_code] = result.ter;
253 jvResult[jss::engine_result_message] = message;
254 }
255 else
256 {
257 // shouldn't be hit
258 // LCOV_EXCL_START
259 jvResult[jss::engine_result] = "unknown";
260 jvResult[jss::engine_result_code] = result.ter;
261 jvResult[jss::engine_result_message] = "unknown";
262 // LCOV_EXCL_STOP
263 }
264
265 if (token == "tesSUCCESS")
266 {
267 jvResult[jss::engine_result_message] = "The simulated transaction would have been applied.";
268 }
269
270 if (result.metadata)
271 {
272 if (isBinaryOutput)
273 {
274 auto const metaBlob = result.metadata->getAsObject().getSerializer().getData();
275 jvResult[jss::meta_blob] = strHex(makeSlice(metaBlob));
276 }
277 else
278 {
279 jvResult[jss::meta] = result.metadata->getJson(JsonOptions::Values::None);
281 jvResult[jss::meta], view, transaction->getSTransaction(), *result.metadata);
283 jvResult, transaction->getSTransaction(), *result.metadata);
285 jvResult[jss::meta], transaction->getSTransaction(), *result.metadata);
286 }
287 }
288
289 if (isBinaryOutput)
290 {
291 auto const txBlob = transaction->getSTransaction()->getSerializer().getData();
292 jvResult[jss::tx_blob] = strHex(makeSlice(txBlob));
293 }
294 else
295 {
296 jvResult[jss::tx_json] = transaction->getJson(JsonOptions::Values::None);
297 }
298
299 return jvResult;
300}
301
302// {
303// tx_blob: <string> XOR tx_json: <object>,
304// binary: <bool>
305// }
308{
310
311 json::Value txJson; // the tx as a JSON
312
313 // check validity of `binary` param
314 if (context.params.isMember(jss::binary) && !context.params[jss::binary].isBool())
315 {
316 return RPC::invalidFieldError(jss::binary);
317 }
318
319 for (auto const field : {jss::secret, jss::seed, jss::seed_hex, jss::passphrase})
320 {
321 if (context.params.isMember(field))
322 {
323 return RPC::invalidFieldError(field);
324 }
325 }
326
327 // get JSON equivalent of transaction
328 txJson = getTxJsonFromParams(context.params);
329 if (txJson.isMember(jss::error))
330 return txJson;
331
332 // autofill fields if they're not included (e.g. `Fee`, `Sequence`)
333 if (auto error = autofillTx(txJson, context))
334 return *error;
335
336 STParsedJSONObject parsed(std::string(jss::tx_json), txJson);
337 if (!parsed.object.has_value())
338 return parsed.error;
339
341 try
342 {
343 stTx = std::make_shared<STTx>(std::move(parsed.object.value()));
344 }
345 catch (std::exception& e)
346 {
348 jvResult[jss::error] = "invalidTransaction";
349 jvResult[jss::error_exception] = e.what();
350 return jvResult;
351 }
352
353 if (stTx->getTxnType() == ttBATCH)
354 {
356 }
357
358 std::string reason;
359 auto transaction = std::make_shared<Transaction>(stTx, reason, context.app);
360 // Actually run the transaction through the transaction processor
361 try
362 {
363 return simulateTxn(context, transaction);
364 }
365 // LCOV_EXCL_START this is just in case, so xrpld doesn't crash
366 catch (std::exception const& e)
367 {
369 jvResult[jss::error] = "internalSimulate";
370 jvResult[jss::error_exception] = e.what();
371 return jvResult;
372 }
373 // LCOV_EXCL_STOP
374}
375
376} // namespace xrpl
Stream debug() const
Definition Journal.h:297
Represents a JSON value.
Definition json_value.h:130
bool isObject() const
bool asBool() const
Value get(UInt index, Value const &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
bool isBool() const
UInt size() const
Number of values in array or object.
bool isMember(char const *key) const
Return true if the object has a member named key.
virtual Config & config()=0
virtual std::uint32_t getNetworkID() const noexcept=0
Get the configured network ID.
std::shared_ptr< OpenView const > current() const
Returns a view to the current open ledger.
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:45
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition ReadView.h:97
std::shared_ptr< STLedgerEntry const > const_pointer
json::Value getJson(JsonOptions=JsonOptions::Values::None) const override
Definition STObject.cpp:835
Holds the serialized result of parsing an input JSON object.
std::optional< STObject > object
The STObject if the parse was successful.
json::Value error
On failure, an appropriate set of error values.
constexpr std::uint32_t value() const
Definition SeqProxy.h:62
virtual TxQ & getTxQ()=0
virtual beast::Journal getJournal(std::string const &name)=0
virtual OpenLedger & getOpenLedger()=0
virtual NetworkIDService & getNetworkIDService()=0
virtual LoadFeeTrack & getFeeTrack()=0
SeqProxy nextQueuableSeq(SLE::const_ref sleAccount) const
Return the next sequence that would go in the TxQ for an account.
Definition TxQ.cpp:1579
ApplyResult apply(Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, ApplyFlags flags, beast::Journal j)
Add a new transaction to the open ledger, hold it in the queue, or reject it.
Definition TxQ.cpp:728
T make_shared(T... args)
@ Object
object value (collection of name/value pairs).
Definition json_value.h:26
void insertMPTokenIssuanceID(json::Value &response, std::shared_ptr< STTx const > const &transaction, TxMeta const &transactionMeta)
json::Value objectFieldError(std::string const &name)
Definition ErrorCodes.h:249
std::string invalidFieldMessage(std::string const &name)
Definition ErrorCodes.h:261
json::Value makeError(ErrorCodeI code)
Returns a new json object that reflects the error code.
void insertNFTSyntheticInJson(json::Value &, std::shared_ptr< STTx const > const &, TxMeta const &)
Adds common synthetic fields to transaction-related JSON responses.
json::Value getCurrentNetworkFee(Role const role, Config const &config, LoadFeeTrack const &feeTrack, TxQ const &txQ, Application const &app, json::Value const &tx, int mult, int div)
json::Value invalidFieldError(std::string const &name)
Definition ErrorCodes.h:273
json::Value makeParamError(std::string const &message)
Returns a new json object that indicates invalid parameters.
Definition ErrorCodes.h:219
json::Value missingFieldError(std::string const &name)
Definition ErrorCodes.h:231
void insertDeliveredAmount(json::Value &meta, ReadView const &, std::shared_ptr< STTx const > const &serializedTx, TxMeta const &)
Add a delivered_amount field to the meta input/output parameter.
Charge const kFeeMediumBurdenRpc
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcSrcActMalformed
Definition ErrorCodes.h:102
@ RpcNotImpl
Definition ErrorCodes.h:113
@ RpcTxSigned
Definition ErrorCodes.h:137
@ RpcSrcActNotFound
Definition ErrorCodes.h:104
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:10
SField const sfGeneric
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:93
bool transResultInfo(TER code, std::string &token, std::string &text)
Definition TER.cpp:232
static std::expected< std::uint32_t, json::Value > getAutofillSequence(json::Value const &txJson, RPC::JsonContext &context)
Definition Simulate.cpp:46
static json::Value getTxJsonFromParams(json::Value const &params)
Definition Simulate.cpp:173
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
json::Value rpcError(ErrorCodeI iError)
Definition RPCErr.cpp:13
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
static std::optional< json::Value > autofillSignature(json::Value &sigObject)
Definition Simulate.cpp:79
@ TapDryRun
Definition ApplyView.h:31
json::Value doSimulate(RPC::JsonContext &)
Definition Simulate.cpp:307
static std::optional< json::Value > autofillTx(json::Value &txJson, RPC::JsonContext &context)
Definition Simulate.cpp:132
static json::Value simulateTxn(RPC::JsonContext &context, std::shared_ptr< Transaction > transaction)
Definition Simulate.cpp:232
std::enable_if_t< std::is_same_v< T, char >||std::is_same_v< T, unsigned char >, Slice > makeSlice(std::array< T, N > const &a)
Definition Slice.h:215
T ref(T... args)
beast::Journal const j
Definition Context.h:20
Application & app
Definition Context.h:21
Resource::Charge & loadType
Definition Context.h:22
json::Value params
Definition Context.h:43
T to_string(T... args)
T unexpected(T... args)
T what(T... args)