20#include <test/jtx/Env.h>
21#include <test/jtx/JSONRPCClient.h>
22#include <test/jtx/balance.h>
23#include <test/jtx/fee.h>
24#include <test/jtx/flags.h>
25#include <test/jtx/pay.h>
26#include <test/jtx/require.h>
27#include <test/jtx/seq.h>
28#include <test/jtx/sig.h>
29#include <test/jtx/trust.h>
30#include <test/jtx/utility.h>
31#include <xrpld/app/ledger/LedgerMaster.h>
32#include <xrpld/app/misc/NetworkOPs.h>
33#include <xrpld/app/misc/TxQ.h>
34#include <xrpld/consensus/LedgerTiming.h>
35#include <xrpld/net/HTTPClient.h>
36#include <xrpld/net/RPCCall.h>
37#include <xrpl/basics/Slice.h>
38#include <xrpl/basics/contract.h>
39#include <xrpl/json/to_string.h>
40#include <xrpl/protocol/ErrorCodes.h>
41#include <xrpl/protocol/Feature.h>
42#include <xrpl/protocol/HashPrefix.h>
43#include <xrpl/protocol/Indexes.h>
44#include <xrpl/protocol/LedgerFormats.h>
45#include <xrpl/protocol/Serializer.h>
46#include <xrpl/protocol/SystemParameters.h>
47#include <xrpl/protocol/TER.h>
48#include <xrpl/protocol/TxFlags.h>
49#include <xrpl/protocol/UintTypes.h>
50#include <xrpl/protocol/jss.h>
73 logs = std::make_unique<SuiteLogs>(suite);
76 std::make_unique<SuiteJournalSink>(
"Debug",
kFatal, suite));
78 auto timeKeeper_ = std::make_unique<ManualTimeKeeper>();
83 std::move(config), std::move(logs), std::move(timeKeeper_));
87 Throw<std::runtime_error>(
"Env::AppBundle: setup failed");
105 if (thread.joinable())
126 using namespace std::chrono_literals;
128 closeTime +=
closed()->info().closeTimeResolution - 1s;
136 auto resp =
rpc(
"ledger_accept");
137 if (resp[
"result"][
"status"] !=
std::string(
"success"))
140 if (resp.isMember(
"error_what"))
141 reason = resp[
"error_what"].asString();
142 else if (resp.isMember(
"error_message"))
143 reason = resp[
"error_message"].asString();
144 else if (resp.isMember(
"error"))
145 reason = resp[
"error"].asString();
147 JLOG(
journal.
error()) <<
"Env::close() failed: " << reason;
158 map_.emplace(account.id(), account);
164 auto const iter =
map_.find(
id);
165 if (iter ==
map_.end())
167 std::cout <<
"Unknown account: " <<
id <<
"\n";
168 Throw<std::runtime_error>(
"Env::lookup:: unknown account ID");
176 auto const account = parseBase58<AccountID>(base58ID);
178 Throw<std::runtime_error>(
"Env::lookup: invalid account ID");
185 auto const sle =
le(account);
188 return {sle->getFieldAmount(sfBalance),
""};
198 return {
STAmount(issue, 0), account.name()};
199 auto amount = sle->getFieldAmount(sfBalance);
200 amount.setIssuer(issue.
account);
201 if (account.id() > issue.
account)
209 auto const sle =
le(account);
211 Throw<std::runtime_error>(
"missing account root");
212 return sle->getFieldU32(sfOwnerCount);
218 auto const sle =
le(account);
220 Throw<std::runtime_error>(
"missing account root");
221 return sle->getFieldU32(sfSequence);
240 if (setDefaultRipple)
270 auto const start =
balance(account);
292 if (!
object.isObject())
294 if (
object.isMember(jss::error_code))
296 safe_cast<error_code_i>(
object[jss::error_code].asInt());
297 if (
object.isMember(jss::error_message))
298 parsed.
rpcMessage =
object[jss::error_message].asString();
299 if (
object.isMember(jss::error))
300 parsed.
rpcError =
object[jss::error].asString();
301 if (
object.isMember(jss::error_exception))
302 parsed.
rpcException =
object[jss::error_exception].asString();
307 auto const& result = jr[jss::result];
308 if (result.isMember(jss::engine_result_code))
314 error(parsed, result);
326 auto const jr = [&]() {
356 auto const& passphrase = account.name();
362 auto const jv = boost::lexical_cast<std::string>(
jt.
jv);
363 jr =
rpc(
"submit", passphrase, jv);
374 params[jss::secret] = passphrase;
376 params[jss::tx_json] =
jt.
jv;
396 bool bad = !
test.
expect(parsed.
ter,
"apply: No ter result!");
404 using namespace std::string_literals;
409 "apply: Got RPC result "s +
427 "apply: Got RPC result "s + parsed.
rpcError +
" (" +
472 auto const account =
lookup(jv[jss::Account].asString());
473 if (!
app().checkSigs())
475 jv[jss::SigningPubKey] =
strHex(account.pk().slice());
477 jv[jss::TxnSignature] =
"00";
480 auto const ar =
le(account);
481 if (ar && ar->isFieldPresent(sfRegularKey))
497 if (!jv.isMember(jss::NetworkID) && networkID > 1024)
557 return std::make_shared<STTx const>(std::move(*obj));
578 <<
"Env::do_rpc error, retrying, attempt #" << ctr + 1 <<
" ...";
585 return response.second;
constexpr const char * 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(const char *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 Config & config()=0
virtual void start(bool withTimers)=0
virtual bool setup(boost::program_options::variables_map const &options)=0
virtual JobQueue & getJobQueue()=0
virtual NetworkOPs & getOPs()=0
virtual void signalStop(std::string msg="")=0
virtual LedgerMaster & getLedgerMaster()=0
std::unordered_set< uint256, beast::uhash<> > features
static void initializeSSLContext(Config const &config, 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
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.
std::shared_ptr< ReadView const > closed()
Returns the last closed ledger.
void disableFeature(uint256 const feature)
bool parseFailureExpected_
static ParsedResult parseResult(Json::Value const &jr)
Gets the TER result and didApply flag from a RPC Json result object.
std::uint32_t ownerCount(Account const &account) const
Return the number of objects owned by an account.
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
std::unordered_map< AccountID, Account > map_
beast::unit_test::suite & test
std::shared_ptr< STTx const > tx() const
Return the tx data for the last JTx.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
void postconditions(JTx const &jt, ParsedResult const &parsed, Json::Value const &jr=Json::Value())
Check expected postconditions of JTx submission.
void sign_and_submit(JTx const &jt, Json::Value params=Json::nullValue)
Use the submit RPC command with a provided JTx object.
virtual void autofill(JTx &jt)
AbstractClient & client()
Returns the connected client.
void autofill_sig(JTx &jt)
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Json::Value do_rpc(unsigned apiVersion, std::vector< std::string > const &args, std::unordered_map< std::string, std::string > const &headers={})
std::shared_ptr< STTx const > st(JTx const &jt)
Create a STTx from a JTx The framework requires that JSON is valid.
void enableFeature(uint256 const feature)
Account const & lookup(AccountID const &id) const
Returns the Account given the AccountID.
JTx jt(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
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...
beast::Journal const journal
ManualTimeKeeper & timeKeeper()
virtual void submit(JTx const &jt)
Submit an existing JTx.
Env & apply(JsonValue &&jv, FN const &... fN)
Apply funclets and submit.
bool close()
Close and advance the ledger.
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
std::shared_ptr< STObject const > meta()
Return metadata for the last JTx.
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
void memoize(Account const &account)
Associate AccountID with account.
std::shared_ptr< SLE const > le(Account const &account) const
Return an account root.
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 line(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
Keylet account(AccountID const &id) noexcept
AccountID root.
void fill_seq(Json::Value &jv, ReadView const &view)
Set the sequence number automatically.
static autofill_t const autofill
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
Json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
Json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
Json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
void sign(Json::Value &jv, Account const &account)
Sign automatically.
STObject parse(Json::Value const &jv)
Convert JSON to STObject.
XRP_t const XRP
Converts to XRP Issue or STAmount.
void fill_fee(Json::Value &jv, ReadView const &view)
Set the fee automatically.
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.
std::string transHuman(TER code)
std::shared_ptr< STTx const > sterilize(STTx const &stx)
Sterilize a transaction.
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.
bool isXRP(AccountID const &c)
std::unique_ptr< Application > make_Application(std::unique_ptr< Config > config, std::unique_ptr< Logs > logs, std::unique_ptr< TimeKeeper > timeKeeper)
std::string strHex(FwdIt begin, FwdIt end)
std::string transToken(TER code)
constexpr std::uint32_t asfDefaultRipple
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.
void Rethrow()
Rethrow the exception currently being handled.
A pair of SHAMap key and LedgerEntryType.
std::unique_ptr< Application > owned
ManualTimeKeeper * timeKeeper
std::unique_ptr< AbstractClient > client
Used by parseResult() and postConditions()
std::optional< error_code_i > rpcCode
Execution context for applying a JSON transaction.
std::optional< std::pair< error_code_i, std::string > > rpcCode
std::shared_ptr< STTx const > stx
std::optional< std::pair< std::string, std::optional< std::string > > > rpcException
std::function< void(Env &, JTx &)> signer
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...
Set the sequence number on a JTx.