rippled
Loading...
Searching...
No Matches
Env.h
1#pragma once
2
3#include <test/jtx/AbstractClient.h>
4#include <test/jtx/Account.h>
5#include <test/jtx/JTx.h>
6#include <test/jtx/ManualTimeKeeper.h>
7#include <test/jtx/amount.h>
8#include <test/jtx/envconfig.h>
9#include <test/jtx/require.h>
10#include <test/jtx/tags.h>
11#include <test/jtx/vault.h>
12#include <test/unit_test/SuiteJournal.h>
13
14#include <xrpld/app/ledger/OpenLedger.h>
15#include <xrpld/app/main/Application.h>
16#include <xrpld/core/Config.h>
17#include <xrpld/rpc/detail/Pathfinder.h>
18
19#include <xrpl/basics/Log.h>
20#include <xrpl/basics/chrono.h>
21#include <xrpl/beast/utility/Journal.h>
22#include <xrpl/json/json_value.h>
23#include <xrpl/json/to_string.h>
24#include <xrpl/ledger/Ledger.h>
25#include <xrpl/protocol/ApiVersion.h>
26#include <xrpl/protocol/Feature.h>
27#include <xrpl/protocol/Indexes.h>
28#include <xrpl/protocol/Issue.h>
29#include <xrpl/protocol/STAmount.h>
30#include <xrpl/protocol/STObject.h>
31#include <xrpl/protocol/STTx.h>
32
33#include <functional>
34#include <future>
35#include <source_location>
36#include <string>
37#include <tuple>
38#include <type_traits>
39#include <unordered_map>
40#include <utility>
41#include <vector>
42
43namespace xrpl {
44namespace test {
45namespace jtx {
46
55template <class T>
57{
60
61 // Non-explicit constructor allows implicit conversion.
62 // The default argument for loc is evaluated at the call site.
67};
68
70template <class... Args>
71std::array<Account, 1 + sizeof...(Args)>
72noripple(Account const& account, Args const&... args)
73{
74 return {{account, args...}};
75}
76
77inline FeatureBitset
79{
80 static FeatureBitset const ids = [] {
81 auto const& sa = allAmendments();
83 feats.reserve(sa.size());
84 for (auto const& [s, vote] : sa)
85 {
86 (void)vote;
87 if (auto const f = getRegisteredFeature(s))
88 feats.push_back(*f);
89 else
90 Throw<std::runtime_error>("Unknown feature: " + s + " in allAmendments.");
91 }
92 return FeatureBitset(feats);
93 }();
94 return ids;
95}
96
97//------------------------------------------------------------------------------
98
99class SuiteLogs : public Logs
100{
102
103public:
105 : Logs(beast::severities::kError), suite_(suite)
106 {
107 }
108
109 ~SuiteLogs() override = default;
110
113 {
115 }
116};
117
118//------------------------------------------------------------------------------
119
121class Env
122{
123public:
125
127
130 {
132 // RPC errors tend to return either a "code" and a "message" (sometimes
133 // with an "error" that corresponds to the "code"), or with an "error"
134 // and an "exception". However, this structure allows all possible
135 // combinations.
140 };
141
142private:
159
161
162public:
164
165 Env() = delete;
166 Env&
167 operator=(Env const&) = delete;
168 Env(Env const&) = delete;
169
184 // VFALCO Could wrap the suite::log in a Journal here
187 FeatureBitset features,
188 std::unique_ptr<Logs> logs = nullptr,
190 : test(suite_)
191 , bundle_(suite_, std::move(config), std::move(logs), thresh)
192 , journal{bundle_.app->getJournal("Env")}
193 {
196 foreachFeature(features, [&appFeats = app().config().features](uint256 const& f) {
197 appFeats.insert(f);
198 });
199 }
200
215 FeatureBitset features,
216 std::unique_ptr<Logs> logs = nullptr)
217 : Env(suite_, envconfig(), features, std::move(logs))
218 {
219 }
220
235 std::unique_ptr<Logs> logs = nullptr,
237 : Env(suite_, std::move(config), testable_amendments(), std::move(logs), thresh)
238 {
239 }
240
252 : Env(suite_, envconfig(), nullptr, thresh)
253 {
254 }
255
256 virtual ~Env() = default;
257
260 {
261 return *bundle_.app;
262 }
263
264 Application const&
265 app() const
266 {
267 return *bundle_.app;
268 }
269
272 {
273 return *bundle_.timeKeeper;
274 }
275
283 {
284 return timeKeeper().now();
285 }
286
290 {
291 return *bundle_.client;
292 }
293
299 template <class... Args>
301 rpc(unsigned apiVersion,
303 std::string const& cmd,
304 Args&&... args);
305
306 template <class... Args>
308 rpc(unsigned apiVersion, std::string const& cmd, Args&&... args);
309
310 template <class... Args>
313 std::string const& cmd,
314 Args&&... args);
315
316 template <class... Args>
318 rpc(std::string const& cmd, Args&&... args);
319
329 current() const
330 {
331 return app().getOpenLedger().current();
332 }
333
342 closed();
343
363 bool
364 close(
365 NetClock::time_point closeTime,
367
375 template <class Rep, class Period>
376 bool
378 {
379 // VFALCO Is this the correct time?
380 return close(now() + elapsed);
381 }
382
390 bool
392 {
393 // VFALCO Is this the correct time?
394 return close(std::chrono::seconds(5));
395 }
396
425 [[nodiscard]] bool
426 syncClose(std::chrono::steady_clock::duration timeout = std::chrono::seconds{1})
427 {
428 XRPL_ASSERT(
429 app().getNumberOfThreads() == 1,
430 "syncClose() is only useful on an application with a single thread");
431 auto const result = close();
432 auto serverBarrier = std::make_shared<std::promise<void>>();
433 auto future = serverBarrier->get_future();
434 boost::asio::post(app().getIOContext(), [serverBarrier]() { serverBarrier->set_value(); });
435 auto const status = future.wait_for(timeout);
436 return result && status == std::future_status::ready;
437 }
438
442 void
443 trace(int howMany = -1)
444 {
445 trace_ = howMany;
446 }
447
449 void
451 {
452 trace_ = 0;
453 }
454
455 void
460
462 void
464 {
465 app().checkSigs(false);
466 }
467
468 // set rpc retries
469 void
470 set_retries(unsigned r = 5)
471 {
472 retries_ = r;
473 }
474
475 // get rpc retries
476 unsigned
477 retries() const
478 {
479 return retries_;
480 }
481
483 void
484 memoize(Account const& account);
485
488 Account const&
489 lookup(AccountID const& id) const;
490
491 Account const&
492 lookup(std::string const& base58ID) const;
499 balance(Account const& account) const;
500
506 seq(Account const& account) const;
507
511 // VFALCO NOTE This should return a unit-less amount
513 // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
514 balance(Account const& account, Asset const& asset) const;
515
517 balance(Account const& account, Issue const& issue) const;
518
520 balance(Account const& account, MPTIssue const& mptIssue) const;
521
526 limit(Account const& account, Issue const& issue) const;
527
532 ownerCount(Account const& account) const;
533
538 le(Account const& account) const;
539
544 le(Keylet const& k) const;
545
547 template <class JsonValue, class... FN>
548 JTx
549 jt(JsonValue&& jv, FN const&... fN)
550 {
552 invoke(jt, fN...);
553 autofill(jt);
554 jt.stx = st(jt);
555 return jt;
556 }
557
559 template <class JsonValue, class... FN>
560 JTx
561 jtnofill(JsonValue&& jv, FN const&... fN)
562 {
564 invoke(jt, fN...);
566 jt.stx = st(jt);
567 return jt;
568 }
569
573 template <class JsonValue, class... FN>
575 json(JsonValue&& jv, FN const&... fN)
576 {
577 auto tj = jt(std::forward<JsonValue>(jv), fN...);
578 return std::move(tj.jv);
579 }
580
586 template <class... Args>
587 void
588 require(Args const&... args)
589 {
590 jtx::required(args...)(*this);
591 }
592
595 static ParsedResult
596 parseResult(Json::Value const& jr);
597
601 virtual void
603
607 void
609 JTx const& jt,
612
616 void
618 JTx const& jt,
619 ParsedResult const& parsed,
620 Json::Value const& jr = Json::Value(),
622
625 template <class... FN>
626 Env&
628 {
629 submit(jt(std::move(jv.value), fN...), jv.loc);
630 return *this;
631 }
632
633 template <class... FN>
634 Env&
635 apply(WithSourceLocation<JTx> jv, FN const&... fN)
636 {
637 submit(jt(std::move(jv.value), fN...), jv.loc);
638 return *this;
639 }
640
641 template <class... FN>
642 Env&
644 {
645 return apply(std::move(jv), fN...);
646 }
647
648 template <class... FN>
649 Env&
651 {
652 return apply(std::move(jv), fN...);
653 }
657 TER
658 ter() const
659 {
660 return ter_;
661 }
662
675 meta();
676
689 tx() const;
690
691 void
692 enableFeature(uint256 const feature);
693
694 void
695 disableFeature(uint256 const feature);
696
697 bool
698 enabled(uint256 feature) const
699 {
700 return current()->rules().enabled(feature);
701 }
702
703private:
704 void
705 fund(bool setDefaultRipple, STAmount const& amount, Account const& account);
706
707 void
708 fund_arg(STAmount const& amount, Account const& account)
709 {
710 fund(true, amount, account);
711 }
712
713 template <std::size_t N>
714 void
716 {
717 for (auto const& account : list)
718 fund(false, amount, account);
719 }
720
721public:
748 template <class Arg, class... Args>
749 void
750 fund(STAmount const& amount, Arg const& arg, Args const&... args)
751 {
752 fund_arg(amount, arg);
753 if constexpr (sizeof...(args) > 0)
754 fund(amount, args...);
755 }
756
775 void
776 trust(STAmount const& amount, Account const& account);
777
778 template <class... Accounts>
779 void
780 trust(STAmount const& amount, Account const& to0, Account const& to1, Accounts const&... toN)
781 {
782 trust(amount, to0);
783 trust(amount, to1, toN...);
784 }
792 ust(JTx const& jt);
793
794protected:
795 int trace_ = 0;
800 unsigned retries_ = 5;
801
803 do_rpc(
804 unsigned apiVersion,
805 std::vector<std::string> const& args,
807
808 void
810
811 virtual void
812 autofill(JTx& jt);
813
822 st(JTx const& jt);
823
824 // Invoke funclets on stx
825 // Note: The STTx may not be modified
826 template <class... FN>
827 void
828 invoke(STTx& stx, FN const&... fN)
829 {
830 (fN(*this, stx), ...);
831 }
832
833 // Invoke funclets on jt
834 template <class... FN>
835 void
836 invoke(JTx& jt, FN const&... fN)
837 {
838 (fN(*this, jt), ...);
839 }
840
841 // Map of account IDs to Account
843};
844
845template <class... Args>
848 unsigned apiVersion,
850 std::string const& cmd,
851 Args&&... args)
852{
853 return do_rpc(apiVersion, std::vector<std::string>{cmd, std::forward<Args>(args)...}, headers);
854}
855
856template <class... Args>
858Env::rpc(unsigned apiVersion, std::string const& cmd, Args&&... args)
859{
860 return rpc(
861 apiVersion,
863 cmd,
864 std::forward<Args>(args)...);
865}
866
867template <class... Args>
871 std::string const& cmd,
872 Args&&... args)
873{
874 return do_rpc(
877 headers);
878}
879
880template <class... Args>
882Env::rpc(std::string const& cmd, Args&&... args)
883{
885}
886
887} // namespace jtx
888} // namespace test
889} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
A generic endpoint for log messages.
Definition Journal.h:40
A testsuite class.
Definition suite.h:51
virtual bool checkSigs() const =0
A currency issued by an account.
Definition Issue.h:13
Manages partitions for logging.
Definition Log.h:32
beast::severities::Severity threshold() const
Definition Log.cpp:140
std::shared_ptr< OpenView const > current() const
Returns a view to the current open ledger.
static void initPathTable()
virtual OpenLedger & getOpenLedger()=0
time_point now() const override
Returns the current time.
Immutable cryptographic account descriptor.
Definition Account.h:19
static Account const master
The master account.
Definition Account.h:28
A transaction testing environment.
Definition Env.h:122
Application & app()
Definition Env.h:259
bool close(std::chrono::duration< Rep, Period > const &elapsed)
Close and advance the ledger.
Definition Env.h:377
void notrace()
Turn off JSON tracing.
Definition Env.h:450
Env & operator()(WithSourceLocation< Json::Value > jv, FN const &... fN)
Definition Env.h:643
static ParsedResult parseResult(Json::Value const &jr)
Gets the TER result and didApply flag from a RPC Json result object.
Definition Env.cpp:318
bool syncClose(std::chrono::steady_clock::duration timeout=std::chrono::seconds{1})
Close and advance the ledger, then synchronize with the server's io_context to ensure all async opera...
Definition Env.h:426
std::shared_ptr< STTx const > st(JTx const &jt)
Create a STTx from a JTx The framework requires that JSON is valid.
Definition Env.cpp:583
TER ter() const
Return the TER for the last JTx.
Definition Env.h:658
void invoke(JTx &jt, FN const &... fN)
Definition Env.h:836
Env(beast::unit_test::suite &suite_, std::unique_ptr< Config > config, FeatureBitset features, std::unique_ptr< Logs > logs=nullptr, beast::severities::Severity thresh=beast::severities::kError)
Create Env using suite, Config pointer, and explicit features.
Definition Env.h:185
Env(beast::unit_test::suite &suite_, beast::severities::Severity thresh=beast::severities::kError)
Create Env with only the current test suite.
Definition Env.h:250
Env(Env const &)=delete
void fund(STAmount const &amount, Arg const &arg, Args const &... args)
Create a new account with some XRP.
Definition Env.h:750
void postconditions(JTx const &jt, ParsedResult const &parsed, Json::Value const &jr=Json::Value(), std::source_location const &loc=std::source_location::current())
Check expected postconditions of JTx submission.
Definition Env.cpp:425
void fund_arg(STAmount const &amount, std::array< Account, N > const &list)
Definition Env.h:715
Env(beast::unit_test::suite &suite_, std::unique_ptr< Config > config, std::unique_ptr< Logs > logs=nullptr, beast::severities::Severity thresh=beast::severities::kError)
Create Env using suite and Config pointer.
Definition Env.h:233
Env & operator()(WithSourceLocation< JTx > jv, FN const &... fN)
Definition Env.h:650
std::uint32_t ownerCount(Account const &account) const
Return the number of objects owned by an account.
Definition Env.cpp:240
void set_parse_failure_expected(bool b)
Definition Env.h:456
void autofill_sig(JTx &jt)
Definition Env.cpp:511
AppBundle bundle_
Definition Env.h:160
std::shared_ptr< ReadView const > closed()
Returns the last closed ledger.
Definition Env.cpp:94
std::shared_ptr< SLE const > le(Account const &account) const
Return an account root.
Definition Env.cpp:258
Account const & lookup(AccountID const &id) const
Returns the Account given the AccountID.
Definition Env.cpp:147
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:270
virtual void submit(JTx const &jt, std::source_location const &loc=std::source_location::current())
Submit an existing JTx.
Definition Env.cpp:359
void disable_sigs()
Turn off signature checks.
Definition Env.h:463
void enableFeature(uint256 const feature)
Definition Env.cpp:654
Env & apply(WithSourceLocation< JTx > jv, FN const &... fN)
Definition Env.h:635
PrettyAmount limit(Account const &account, Issue const &issue) const
Returns the IOU limit on an account.
Definition Env.cpp:228
void disableFeature(uint256 const feature)
Definition Env.cpp:662
void set_retries(unsigned r=5)
Definition Env.h:470
void sign_and_submit(JTx const &jt, Json::Value params=Json::nullValue, std::source_location const &loc=std::source_location::current())
Use the submit RPC command with a provided JTx object.
Definition Env.cpp:387
bool close()
Close and advance the ledger.
Definition Env.h:391
Account const & master
Definition Env.h:126
Application const & app() const
Definition Env.h:265
Json::Value do_rpc(unsigned apiVersion, std::vector< std::string > const &args, std::unordered_map< std::string, std::string > const &headers={})
Definition Env.cpp:635
JTx jt(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition Env.h:549
virtual ~Env()=default
void fund_arg(STAmount const &amount, Account const &account)
Definition Env.h:708
unsigned retries_
Definition Env.h:800
void trust(STAmount const &amount, Account const &to0, Account const &to1, Accounts const &... toN)
Definition Env.h:780
uint256 txid_
Definition Env.h:797
unsigned retries() const
Definition Env.h:477
Json::Value json(JsonValue &&jv, FN const &... fN)
Create JSON from parameters.
Definition Env.h:575
beast::unit_test::suite & test
Definition Env.h:124
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:301
Json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition Env.h:847
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...
Definition Env.cpp:609
std::shared_ptr< STObject const > meta()
Return metadata for the last JTx.
Definition Env.cpp:483
JTx jtnofill(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition Env.h:561
void invoke(STTx &stx, FN const &... fN)
Definition Env.h:828
std::unordered_map< AccountID, Account > map_
Definition Env.h:842
Env(beast::unit_test::suite &suite_, FeatureBitset features, std::unique_ptr< Logs > logs=nullptr)
Create Env with default config and specified features.
Definition Env.h:214
Env & apply(WithSourceLocation< Json::Value > jv, FN const &... fN)
Apply funclets and submit.
Definition Env.h:627
TestStopwatch stopwatch_
Definition Env.h:796
ManualTimeKeeper & timeKeeper()
Definition Env.h:271
bool parseFailureExpected_
Definition Env.h:799
std::shared_ptr< STTx const > tx() const
Return the tx data for the last JTx.
Definition Env.cpp:505
bool enabled(uint256 feature) const
Definition Env.h:698
Env & operator=(Env const &)=delete
void memoize(Account const &account)
Associate AccountID with account.
Definition Env.cpp:141
beast::Journal const journal
Definition Env.h:163
AbstractClient & client()
Returns the connected client.
Definition Env.h:289
void require(Args const &... args)
Check a set of requirements.
Definition Env.h:588
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:329
NetClock::time_point now()
Returns the current network time.
Definition Env.h:282
void trace(int howMany=-1)
Turn on JSON tracing.
Definition Env.h:443
~SuiteLogs() override=default
SuiteLogs(beast::unit_test::suite &suite)
Definition Env.h:104
beast::unit_test::suite & suite_
Definition Env.h:101
std::unique_ptr< beast::Journal::Sink > makeSink(std::string const &partition, beast::severities::Severity threshold) override
Definition Env.h:112
A balance matches.
Definition balance.h:19
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition rpc.h:15
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:15
T current(T... args)
T is_same_v
@ nullValue
'null' value
Definition json_value.h:19
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:12
STL namespace.
static constexpr auto apiCommandLineVersion
Definition ApiVersion.h:44
require_t required(Args const &... args)
Compose many condition functors into one.
Definition require.h:29
static autofill_t const autofill
Definition tags.h:22
FeatureBitset testable_amendments()
Definition Env.h:78
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:34
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void foreachFeature(FeatureBitset bs, F &&f)
Definition Feature.h:373
std::map< std::string, AmendmentSupport > const & allAmendments()
All amendments libxrpl knows about.
std::optional< uint256 > getRegisteredFeature(std::string const &name)
@ tesSUCCESS
Definition TER.h:225
T push_back(T... args)
T reserve(T... args)
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19
ManualTimeKeeper * timeKeeper
Definition Env.h:147
std::unique_ptr< AbstractClient > client
Definition Env.h:149
std::unique_ptr< Application > owned
Definition Env.h:146
Used by parseResult() and postConditions()
Definition Env.h:130
std::optional< error_code_i > rpcCode
Definition Env.h:136
Execution context for applying a JSON transaction.
Definition JTx.h:25
std::shared_ptr< STTx const > stx
Definition JTx.h:35
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...
Wrapper that captures std::source_location when implicitly constructed.
Definition Env.h:57
WithSourceLocation(T v, std::source_location l=std::source_location::current())
Definition Env.h:63
std::source_location loc
Definition Env.h:59
Set the sequence number on a JTx.
Definition seq.h:14