1#include <test/jtx/Env.h>
2#include <test/jtx/JSONRPCClient.h>
3#include <test/jtx/balance.h>
4#include <test/jtx/fee.h>
5#include <test/jtx/flags.h>
6#include <test/jtx/pay.h>
7#include <test/jtx/seq.h>
8#include <test/jtx/sig.h>
9#include <test/jtx/trust.h>
10#include <test/jtx/utility.h>
12#include <xrpld/app/ledger/LedgerMaster.h>
13#include <xrpld/app/misc/NetworkOPs.h>
14#include <xrpld/rpc/RPCCall.h>
16#include <xrpl/basics/Slice.h>
17#include <xrpl/basics/contract.h>
18#include <xrpl/basics/scope.h>
19#include <xrpl/json/to_string.h>
20#include <xrpl/net/HTTPClient.h>
21#include <xrpl/protocol/ErrorCodes.h>
22#include <xrpl/protocol/Indexes.h>
23#include <xrpl/protocol/Serializer.h>
24#include <xrpl/protocol/TER.h>
25#include <xrpl/protocol/TxFlags.h>
26#include <xrpl/protocol/UintTypes.h>
27#include <xrpl/protocol/jss.h>
63 Throw<std::runtime_error>(
"Env::AppBundle: setup failed");
81 if (thread.joinable())
100 using namespace std::chrono_literals;
102 closeTime +=
closed()->header().closeTimeResolution - 1s;
110 auto resp =
rpc(
"ledger_accept");
111 if (resp[
"result"][
"status"] !=
std::string(
"success"))
114 if (resp.isMember(
"error_what"))
115 reason = resp[
"error_what"].asString();
116 else if (resp.isMember(
"error_message"))
117 reason = resp[
"error_message"].asString();
118 else if (resp.isMember(
"error"))
119 reason = resp[
"error"].asString();
121 JLOG(
journal.
error()) <<
"Env::close() failed: " << reason;
132 map_.emplace(account.id(), account);
138 auto const iter =
map_.find(
id);
139 if (iter ==
map_.end())
141 std::cout <<
"Unknown account: " <<
id <<
"\n";
142 Throw<std::runtime_error>(
"Env::lookup:: unknown account ID");
150 auto const account = parseBase58<AccountID>(base58ID);
152 Throw<std::runtime_error>(
"Env::lookup: invalid account ID");
159 auto const sle =
le(account);
162 return {sle->getFieldAmount(sfBalance),
""};
172 return {
STAmount(issue, 0), account.name()};
173 auto amount = sle->getFieldAmount(sfBalance);
175 if (account.id() > issue.
account)
185 return {
STAmount(mptIssue, 0), account.name()};
188 if (account.id() == issuer)
193 return {
STAmount(mptIssue, 0), account.name()};
196 STAmount const amount{mptIssue, sle->getFieldU64(sfOutstandingAmount), 0,
true};
204 return {
STAmount(mptIssue, 0), account.name()};
222 return {
STAmount(issue, 0), account.name()};
223 auto const aHigh = account.id() > issue.
account;
224 if (sle && sle->isFieldPresent(aHigh ? sfLowLimit : sfHighLimit))
225 return {(*sle)[aHigh ? sfLowLimit : sfHighLimit], account.name()};
226 return {
STAmount(issue, 0), account.name()};
232 auto const sle =
le(account);
234 Throw<std::runtime_error>(
"missing account root");
235 return sle->getFieldU32(sfOwnerCount);
241 auto const sle =
le(account);
243 Throw<std::runtime_error>(
"missing account root");
244 return sle->getFieldU32(sfSequence);
263 if (setDefaultRipple)
285 auto const start =
balance(account);
303 if (!
object.isObject())
305 if (
object.isMember(jss::error_code))
306 parsed.
rpcCode = safe_cast<error_code_i>(
object[jss::error_code].asInt());
307 if (
object.isMember(jss::error_message))
308 parsed.
rpcMessage =
object[jss::error_message].asString();
309 if (
object.isMember(jss::error))
310 parsed.
rpcError =
object[jss::error].asString();
311 if (
object.isMember(jss::error_exception))
312 parsed.
rpcException =
object[jss::error_exception].asString();
317 auto const& result = jr[jss::result];
318 if (result.isMember(jss::engine_result_code))
324 error(parsed, result);
336 auto const jr = [&]() {
366 auto const& passphrase = account.name();
373 jr =
rpc(
"submit", passphrase, jv);
383 params[jss::secret] = passphrase;
385 params[jss::tx_json] =
jt.
jv;
403 bool bad = !
test.
expect(parsed.
ter,
"apply: No ter result!" + line);
410 using namespace std::string_literals;
414 "apply: Got RPC result "s +
459 auto const result = item.second;
460 if (result ==
nullptr)
463 test.
log <<
"This is probably because the transaction failed with a "
466 Throw<std::runtime_error>(
"Env::meta: no metadata for txid");
499 auto const account = jv.isMember(sfDelegate.jsonName) ?
lookup(jv[sfDelegate.jsonName].asString())
500 :
lookup(jv[jss::Account].asString());
501 if (!
app().checkSigs())
503 jv[jss::SigningPubKey] =
strHex(account.pk().slice());
505 jv[jss::TxnSignature] =
"00";
508 auto const ar =
le(account);
509 if (ar && ar->isFieldPresent(sfRegularKey))
527 if (!jv.isMember(jss::NetworkID) && networkID > 1024)
602 auto response =
rpcClient(args,
app().config(),
app().logs(), apiVersion, headers);
606 JLOG(
journal.
error()) <<
"Env::do_rpc error, retrying, attempt #" << ctr + 1 <<
" ...";
609 response =
rpcClient(args,
app().config(),
app().logs(), apiVersion, headers);
612 return response.second;
constexpr char const * c_str() const
std::string asString() const
Returns the unquoted string value.
bool isNull() const
isNull() tests to see if this field is null.
bool isMember(char const *key) const
Return true if the object has a member named key.
log_os< char > log
Logging output stream.
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
virtual bool setup(boost::program_options::variables_map const &options)=0
virtual Config & config()=0
virtual void signalStop(std::string msg)=0
virtual LedgerMaster & getLedgerMaster()=0
virtual JobQueue & getJobQueue()=0
virtual void start(bool withTimers)=0
virtual NetworkOPs & getOPs()=0
constexpr value_type const & value() const
std::unordered_set< uint256, beast::uhash<> > features
static void initializeSSLContext(std::string const &sslVerifyDir, std::string const &sslVerifyFile, bool sslVerify, beast::Journal j)
A currency issued by an account.
void rendezvous()
Block until no jobs running.
std::shared_ptr< Ledger const > getClosedLedger()
beast::severities::Severity threshold() const
constexpr MPTID const & getMptID() const
AccountID const & getIssuer() const
virtual std::uint32_t acceptLedger(std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)=0
Accepts the current transaction tree, return the new ledger's sequence.
Slice slice() const noexcept
static constexpr TERSubset fromInt(int from)
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
virtual Json::Value invoke(std::string const &cmd, Json::Value const ¶ms={})=0
Submit a command synchronously.
Immutable cryptographic account descriptor.
std::string const & name() const
Return the name.
static ParsedResult parseResult(Json::Value const &jr)
Gets the TER result and didApply flag from a RPC Json result object.
std::shared_ptr< STTx const > st(JTx const &jt)
Create a STTx from a JTx The framework requires that JSON is valid.
std::uint32_t ownerCount(Account const &account) const
Return the number of objects owned by an account.
void autofill_sig(JTx &jt)
void sign_and_submit(JTx const &jt, Json::Value params=Json::nullValue)
Use the submit RPC command with a provided JTx object.
std::shared_ptr< ReadView const > closed()
Returns the last closed ledger.
std::shared_ptr< SLE const > le(Account const &account) const
Return an account root.
Account const & lookup(AccountID const &id) const
Returns the Account given the AccountID.
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
void enableFeature(uint256 const feature)
virtual void submit(JTx const &jt)
Submit an existing JTx.
PrettyAmount limit(Account const &account, Issue const &issue) const
Returns the IOU limit on an account.
void disableFeature(uint256 const feature)
void postconditions(JTx const &jt, ParsedResult const &parsed, Json::Value const &jr=Json::Value())
Check expected postconditions of JTx submission.
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
virtual void autofill(JTx &jt)
bool close()
Close and advance the ledger.
Json::Value do_rpc(unsigned apiVersion, std::vector< std::string > const &args, std::unordered_map< std::string, std::string > const &headers={})
JTx jt(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
Env & apply(JsonValue &&jv, FN const &... fN)
Apply funclets and submit.
beast::unit_test::suite & test
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
std::shared_ptr< STTx const > ust(JTx const &jt)
Create a STTx from a JTx without sanitizing Use to inject bogus values into test transactions by firs...
std::shared_ptr< STObject const > meta()
Return metadata for the last JTx.
std::unordered_map< AccountID, Account > map_
ManualTimeKeeper & timeKeeper()
bool parseFailureExpected_
std::shared_ptr< STTx const > tx() const
Return the tx data for the last JTx.
void memoize(Account const &account)
Associate AccountID with account.
beast::Journal const journal
AbstractClient & client()
Returns the connected client.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Match clear account flags.
Check a set of conditions.
Set the expected result code for a JTx The test will fail if the code doesn't match.
Set the regular signature on a JTx.
A namespace for easy access to logging severity values.
Severity
Severity level / threshold of a Journal message.
ErrorInfo const & get_error_info(error_code_i code)
Returns an ErrorInfo that reflects the error code.
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Keylet line(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Keylet account(AccountID const &id) noexcept
AccountID root.
void fill_seq(Json::Value &jv, ReadView const &view)
Set the sequence number automatically.
Json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
XRP_t const XRP
Converts to XRP Issue or STAmount.
Json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
static autofill_t const autofill
void sign(Json::Value &jv, Account const &account, Json::Value &sigObject)
Sign automatically into a specific Json field of the jv object.
STObject parse(Json::Value const &jv)
Convert JSON to STObject.
void fill_fee(Json::Value &jv, ReadView const &view)
Set the fee automatically.
Json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
std::unique_ptr< AbstractClient > makeJSONRPCClient(Config const &cfg, unsigned rpc_version)
Returns a client using JSON-RPC over HTTP/S.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool isXRP(AccountID const &c)
beast::Journal debugLog()
Returns a debug journal.
std::unique_ptr< beast::Journal::Sink > setDebugLogSink(std::unique_ptr< beast::Journal::Sink > sink)
Set the sink for the debug journal.
std::string to_string(base_uint< Bits, Tag > const &a)
std::string strHex(FwdIt begin, FwdIt end)
std::string transHuman(TER code)
std::pair< int, Json::Value > rpcClient(std::vector< std::string > const &args, Config const &config, Logs &logs, unsigned int apiVersion, std::unordered_map< std::string, std::string > const &headers)
Internal invocation of RPC client.
std::string transToken(TER code)
std::unique_ptr< Application > make_Application(std::unique_ptr< Config > config, std::unique_ptr< Logs > logs, std::unique_ptr< TimeKeeper > timeKeeper)
constexpr std::uint32_t asfDefaultRipple
void Rethrow()
Rethrow the exception currently being handled.
std::shared_ptr< STTx const > sterilize(STTx const &stx)
Sterilize a transaction.
A pair of SHAMap key and LedgerEntryType.
ManualTimeKeeper * timeKeeper
std::unique_ptr< AbstractClient > client
std::unique_ptr< Application > owned
Used by parseResult() and postConditions()
std::optional< error_code_i > rpcCode
Execution context for applying a JSON transaction.
std::vector< std::function< void(Env &, JTx &)> > postSigners
std::vector< std::function< void(Env &, JTx &)> > mainSigners
std::shared_ptr< STTx const > stx
std::optional< std::pair< error_code_i, std::string > > rpcCode
std::optional< std::pair< std::string, std::optional< std::string > > > rpcException
std::optional< int > testLine
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...
Set the sequence number on a JTx.
A signer in a SignerList.