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/detail/SyntheticFields.h>
8#include <xrpld/rpc/detail/TransactionSign.h>
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/RPCErr.h>
24#include <xrpl/protocol/SField.h>
25#include <xrpl/protocol/STParsedJSON.h>
26#include <xrpl/protocol/Serializer.h>
27#include <xrpl/protocol/TER.h>
28#include <xrpl/protocol/TxFlags.h>
29#include <xrpl/protocol/TxFormats.h>
30#include <xrpl/protocol/jss.h>
31#include <xrpl/resource/Fees.h>
45static std::expected<std::uint32_t, json::Value>
49 bool const hasTicketSeq = txJson.
isMember(sfTicketSequence.jsonName);
50 auto const& accountStr = txJson[jss::Account];
51 if (!accountStr.isString())
59 if (!srcAddressID.has_value())
66 if (!hasTicketSeq && !sle)
69 <<
"Failed to find source account "
70 <<
"in current ledger: " <<
toBase58(*srcAddressID);
81 if (!sigObject.
isMember(jss::SigningPubKey))
84 sigObject[jss::SigningPubKey] =
"";
87 if (sigObject.
isMember(jss::Signers))
89 if (!sigObject[jss::Signers].isArray())
92 for (
unsigned index = 0; index < sigObject[jss::Signers].
size(); index++)
94 auto& signer = sigObject[jss::Signers][index];
95 if (!signer.isObject() || !signer.isMember(jss::Signer) ||
96 !signer[jss::Signer].isObject())
102 if (!signer[jss::Signer].isMember(jss::SigningPubKey))
105 signer[jss::Signer][jss::SigningPubKey] =
"";
108 if (!signer[jss::Signer].isMember(jss::TxnSignature))
111 signer[jss::Signer][jss::TxnSignature] =
"";
113 else if (signer[jss::Signer][jss::TxnSignature] !=
"")
121 if (!sigObject.
isMember(jss::TxnSignature))
124 sigObject[jss::TxnSignature] =
"";
126 else if (sigObject[jss::TxnSignature] !=
"")
140 if (txJson.
isMember(sfSponsorSignature.jsonName))
142 auto& sponsorSignature = txJson[sfSponsorSignature.jsonName];
143 if (!sponsorSignature.isObject())
146 if (
auto const error =
autofillSignature(sponsorSignature,
"tx.SponsorSignature"))
150 if (!txJson.
isMember(jss::Sequence))
155 txJson[sfSequence.jsonName] = *seq;
158 if (!txJson.
isMember(jss::NetworkID))
161 if (networkId > 1024)
162 txJson[jss::NetworkID] =
to_string(networkId);
176 if (feeOrError.isMember(jss::error))
178 txJson[jss::Fee] = feeOrError;
196 auto const txBlob = params[jss::tx_blob];
197 if (!txBlob.isString())
202 auto unHexed =
strUnHex(txBlob.asString());
203 if (!unHexed || unHexed->empty())
216 else if (params.
isMember(jss::tx_json))
218 txJson = params[jss::tx_json];
230 if (!txJson.
isMember(jss::TransactionType))
250 context.
app, view, transaction->getSTransaction(),
TapDryRun, context.
j);
252 jvResult[jss::applied] = result.applied;
253 jvResult[jss::ledger_index] = view.
seq();
255 bool const isBinaryOutput = context.
params.
get(jss::binary,
false).
asBool();
263 jvResult[jss::engine_result] = token;
264 jvResult[jss::engine_result_code] = result.ter;
265 jvResult[jss::engine_result_message] = message;
271 jvResult[jss::engine_result] =
"unknown";
272 jvResult[jss::engine_result_code] = result.ter;
273 jvResult[jss::engine_result_message] =
"unknown";
277 if (token ==
"tesSUCCESS")
279 jvResult[jss::engine_result_message] =
"The simulated transaction would have been applied.";
286 auto const metaBlob = result.metadata->getAsObject().getSerializer().getData();
293 jvResult[jss::meta], view, transaction->getSTransaction(), *result.metadata);
299 auto const txBlob = transaction->getSTransaction()->getSerializer().getData();
327 for (
auto const field : {jss::secret, jss::seed, jss::seed_hex, jss::passphrase})
345 if (!parsed.
object.has_value())
356 jvResult[jss::error] =
"invalidTransaction";
357 jvResult[jss::error_exception] = e.
what();
361 if (stTx->getTxnType() == ttBATCH)
370 RpcInvalidParams,
"tfInnerBatchTxn flag is not allowed on top-level transactions.");
384 jvResult[jss::error] =
"internalSimulate";
385 jvResult[jss::error_exception] = e.
what();
Value get(UInt index, Value const &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
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.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
std::shared_ptr< STLedgerEntry const > const_pointer
json::Value getJson(JsonOptions=JsonOptions::Values::None) const override
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
virtual beast::Journal getJournal(std::string const &name)=0
virtual OpenLedger & getOpenLedger()=0
virtual NetworkIDService & getNetworkIDService()=0
virtual LoadFeeTrack & getFeeTrack()=0
SeqProxy nextQueuableSeq(SLE::ConstRef sleAccount) const
Return the next sequence that would go in the TxQ for an account.
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.
@ Object
object value (collection of name/value pairs).
Keylet account(AccountID const &id) noexcept
AccountID root.
Charge const kFeeMediumBurdenRpc
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)
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.
std::string invalidFieldMessage(std::string const &name)
json::Value makeParamError(std::string const &message)
Returns a new json object that indicates invalid parameters.
json::Value invalidFieldError(std::string const &name)
json::Value missingFieldError(std::string const &name)
json::Value makeError(ErrorCodeI code)
Returns a new json object that reflects the error code.
json::Value objectFieldError(std::string const &name)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr FlagValue tfInnerBatchTxn
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string strHex(FwdIt begin, FwdIt end)
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
static json::Value simulateTxn(rpc::JsonContext &context, std::shared_ptr< Transaction > transaction)
bool transResultInfo(TER code, std::string &token, std::string &text)
json::Value doSimulate(rpc::JsonContext &)
static json::Value getTxJsonFromParams(json::Value const ¶ms)
std::string to_string(BaseUInt< Bits, Tag > const &a)
Slice makeSlice(std::array< T, N > const &a)
json::Value rpcError(ErrorCodeI iError)
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
static std::optional< json::Value > autofillTx(json::Value &txJson, rpc::JsonContext &context)
static std::optional< json::Value > autofillSignature(json::Value &sigObject, std::string const &fieldPrefix="tx")
static std::expected< std::uint32_t, json::Value > getAutofillSequence(json::Value const &txJson, rpc::JsonContext &context)
resource::Charge & loadType