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/Ledger.h>
15#include <xrpld/app/ledger/OpenLedger.h>
16#include <xrpld/app/main/Application.h>
17#include <xrpld/app/paths/Pathfinder.h>
18#include <xrpld/core/Config.h>
19
20#include <xrpl/basics/Log.h>
21#include <xrpl/basics/chrono.h>
22#include <xrpl/beast/utility/Journal.h>
23#include <xrpl/json/json_value.h>
24#include <xrpl/json/to_string.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 <source_location>
35#include <string>
36#include <tuple>
37#include <type_traits>
38#include <unordered_map>
39#include <utility>
40#include <vector>
41
42namespace xrpl {
43namespace test {
44namespace jtx {
45
54template <class T>
56{
59
60 // Non-explicit constructor allows implicit conversion.
61 // The default argument for loc is evaluated at the call site.
65};
66
68template <class... Args>
69std::array<Account, 1 + sizeof...(Args)>
70noripple(Account const& account, Args const&... args)
71{
72 return {{account, args...}};
73}
74
75inline FeatureBitset
77{
78 static FeatureBitset const ids = [] {
79 auto const& sa = allAmendments();
81 feats.reserve(sa.size());
82 for (auto const& [s, vote] : sa)
83 {
84 (void)vote;
85 if (auto const f = getRegisteredFeature(s))
86 feats.push_back(*f);
87 else
88 Throw<std::runtime_error>("Unknown feature: " + s + " in allAmendments.");
89 }
90 return FeatureBitset(feats);
91 }();
92 return ids;
93}
94
95//------------------------------------------------------------------------------
96
97class SuiteLogs : public Logs
98{
100
101public:
102 explicit SuiteLogs(beast::unit_test::suite& suite) : Logs(beast::severities::kError), suite_(suite)
103 {
104 }
105
106 ~SuiteLogs() override = default;
107
110 {
112 }
113};
114
115//------------------------------------------------------------------------------
116
118class Env
119{
120public:
122
124
127 {
129 // RPC errors tend to return either a "code" and a "message" (sometimes
130 // with an "error" that corresponds to the "code"), or with an "error"
131 // and an "exception". However, this structure allows all possible
132 // combinations.
137 };
138
139private:
156
158
159public:
161
162 Env() = delete;
163 Env&
164 operator=(Env const&) = delete;
165 Env(Env const&) = delete;
166
181 // VFALCO Could wrap the suite::log in a Journal here
184 FeatureBitset features,
185 std::unique_ptr<Logs> logs = nullptr,
187 : test(suite_)
188 , bundle_(suite_, std::move(config), std::move(logs), thresh)
189 , journal{bundle_.app->journal("Env")}
190 {
193 foreachFeature(features, [&appFeats = app().config().features](uint256 const& f) { appFeats.insert(f); });
194 }
195
210 : Env(suite_, envconfig(), features, std::move(logs))
211 {
212 }
213
228 std::unique_ptr<Logs> logs = nullptr,
230 : Env(suite_, std::move(config), testable_amendments(), std::move(logs), thresh)
231 {
232 }
233
244 : Env(suite_, envconfig(), nullptr, thresh)
245 {
246 }
247
248 virtual ~Env() = default;
249
252 {
253 return *bundle_.app;
254 }
255
256 Application const&
257 app() const
258 {
259 return *bundle_.app;
260 }
261
264 {
265 return *bundle_.timeKeeper;
266 }
267
275 {
276 return timeKeeper().now();
277 }
278
282 {
283 return *bundle_.client;
284 }
285
291 template <class... Args>
293 rpc(unsigned apiVersion,
295 std::string const& cmd,
296 Args&&... args);
297
298 template <class... Args>
300 rpc(unsigned apiVersion, std::string const& cmd, Args&&... args);
301
302 template <class... Args>
304 rpc(std::unordered_map<std::string, std::string> const& headers, std::string const& cmd, Args&&... args);
305
306 template <class... Args>
308 rpc(std::string const& cmd, Args&&... args);
309
319 current() const
320 {
321 return app().openLedger().current();
322 }
323
332 closed();
333
353 bool
355
363 template <class Rep, class Period>
364 bool
366 {
367 // VFALCO Is this the correct time?
368 return close(now() + elapsed);
369 }
370
378 bool
380 {
381 // VFALCO Is this the correct time?
382 return close(std::chrono::seconds(5));
383 }
384
388 void
389 trace(int howMany = -1)
390 {
391 trace_ = howMany;
392 }
393
395 void
397 {
398 trace_ = 0;
399 }
400
401 void
406
408 void
410 {
411 app().checkSigs(false);
412 }
413
414 // set rpc retries
415 void
416 set_retries(unsigned r = 5)
417 {
418 retries_ = r;
419 }
420
421 // get rpc retries
422 unsigned
423 retries() const
424 {
425 return retries_;
426 }
427
429 void
430 memoize(Account const& account);
431
434 Account const&
435 lookup(AccountID const& id) const;
436
437 Account const&
438 lookup(std::string const& base58ID) const;
445 balance(Account const& account) const;
446
452 seq(Account const& account) const;
453
457 // VFALCO NOTE This should return a unit-less amount
459 balance(Account const& account, Asset const& asset) const;
460
462 balance(Account const& account, Issue const& issue) const;
463
465 balance(Account const& account, MPTIssue const& mptIssue) const;
466
471 limit(Account const& account, Issue const& issue) const;
472
477 ownerCount(Account const& account) const;
478
483 le(Account const& account) const;
484
489 le(Keylet const& k) const;
490
492 template <class JsonValue, class... FN>
493 JTx
494 jt(JsonValue&& jv, FN const&... fN)
495 {
497 invoke(jt, fN...);
498 autofill(jt);
499 jt.stx = st(jt);
500 return jt;
501 }
502
504 template <class JsonValue, class... FN>
505 JTx
506 jtnofill(JsonValue&& jv, FN const&... fN)
507 {
509 invoke(jt, fN...);
511 jt.stx = st(jt);
512 return jt;
513 }
514
518 template <class JsonValue, class... FN>
520 json(JsonValue&& jv, FN const&... fN)
521 {
522 auto tj = jt(std::forward<JsonValue>(jv), fN...);
523 return std::move(tj.jv);
524 }
525
531 template <class... Args>
532 void
533 require(Args const&... args)
534 {
535 jtx::required(args...)(*this);
536 }
537
540 static ParsedResult
541 parseResult(Json::Value const& jr);
542
546 virtual void
548
552 void
554 JTx const& jt,
557
561 void
563 JTx const& jt,
564 ParsedResult const& parsed,
565 Json::Value const& jr = Json::Value(),
567
570 template <class... FN>
571 Env&
573 {
574 submit(jt(std::move(jv.value), fN...), jv.loc);
575 return *this;
576 }
577
578 template <class... FN>
579 Env&
580 apply(WithSourceLocation<JTx> jv, FN const&... fN)
581 {
582 submit(jt(std::move(jv.value), fN...), jv.loc);
583 return *this;
584 }
585
586 template <class... FN>
587 Env&
589 {
590 return apply(std::move(jv), fN...);
591 }
592
593 template <class... FN>
594 Env&
596 {
597 return apply(std::move(jv), fN...);
598 }
602 TER
603 ter() const
604 {
605 return ter_;
606 }
607
620 meta();
621
634 tx() const;
635
636 void
637 enableFeature(uint256 const feature);
638
639 void
640 disableFeature(uint256 const feature);
641
642 bool
643 enabled(uint256 feature) const
644 {
645 return current()->rules().enabled(feature);
646 }
647
648private:
649 void
650 fund(bool setDefaultRipple, STAmount const& amount, Account const& account);
651
652 void
653 fund_arg(STAmount const& amount, Account const& account)
654 {
655 fund(true, amount, account);
656 }
657
658 template <std::size_t N>
659 void
661 {
662 for (auto const& account : list)
663 fund(false, amount, account);
664 }
665
666public:
693 template <class Arg, class... Args>
694 void
695 fund(STAmount const& amount, Arg const& arg, Args const&... args)
696 {
697 fund_arg(amount, arg);
698 if constexpr (sizeof...(args) > 0)
699 fund(amount, args...);
700 }
701
720 void
721 trust(STAmount const& amount, Account const& account);
722
723 template <class... Accounts>
724 void
725 trust(STAmount const& amount, Account const& to0, Account const& to1, Accounts const&... toN)
726 {
727 trust(amount, to0);
728 trust(amount, to1, toN...);
729 }
737 ust(JTx const& jt);
738
739protected:
740 int trace_ = 0;
745 unsigned retries_ = 5;
746
748 do_rpc(
749 unsigned apiVersion,
750 std::vector<std::string> const& args,
752
753 void
755
756 virtual void
757 autofill(JTx& jt);
758
767 st(JTx const& jt);
768
769 // Invoke funclets on stx
770 // Note: The STTx may not be modified
771 template <class... FN>
772 void
773 invoke(STTx& stx, FN const&... fN)
774 {
775 (fN(*this, stx), ...);
776 }
777
778 // Invoke funclets on jt
779 template <class... FN>
780 void
781 invoke(JTx& jt, FN const&... fN)
782 {
783 (fN(*this, jt), ...);
784 }
785
786 // Map of account IDs to Account
788};
789
790template <class... Args>
793 unsigned apiVersion,
795 std::string const& cmd,
796 Args&&... args)
797{
798 return do_rpc(apiVersion, std::vector<std::string>{cmd, std::forward<Args>(args)...}, headers);
799}
800
801template <class... Args>
803Env::rpc(unsigned apiVersion, std::string const& cmd, Args&&... args)
804{
805 return rpc(apiVersion, std::unordered_map<std::string, std::string>(), cmd, std::forward<Args>(args)...);
806}
807
808template <class... Args>
810Env::rpc(std::unordered_map<std::string, std::string> const& headers, std::string const& cmd, Args&&... args)
811{
813}
814
815template <class... Args>
817Env::rpc(std::string const& cmd, Args&&... args)
818{
820}
821
822} // namespace jtx
823} // namespace test
824} // 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 & openLedger()=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:119
Application & app()
Definition Env.h:251
bool close(std::chrono::duration< Rep, Period > const &elapsed)
Close and advance the ledger.
Definition Env.h:365
void notrace()
Turn off JSON tracing.
Definition Env.h:396
Env & operator()(WithSourceLocation< Json::Value > jv, FN const &... fN)
Definition Env.h:588
static ParsedResult parseResult(Json::Value const &jr)
Gets the TER result and didApply flag from a RPC Json result object.
Definition Env.cpp:297
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:548
TER ter() const
Return the TER for the last JTx.
Definition Env.h:603
void invoke(JTx &jt, FN const &... fN)
Definition Env.h:781
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:182
Env(beast::unit_test::suite &suite_, beast::severities::Severity thresh=beast::severities::kError)
Create Env with only the current test suite.
Definition Env.h:243
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:695
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:401
void fund_arg(STAmount const &amount, std::array< Account, N > const &list)
Definition Env.h:660
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:226
Env & operator()(WithSourceLocation< JTx > jv, FN const &... fN)
Definition Env.h:595
std::uint32_t ownerCount(Account const &account) const
Return the number of objects owned by an account.
Definition Env.cpp:231
void set_parse_failure_expected(bool b)
Definition Env.h:402
void autofill_sig(JTx &jt)
Definition Env.cpp:481
AppBundle bundle_
Definition Env.h:157
std::shared_ptr< ReadView const > closed()
Returns the last closed ledger.
Definition Env.cpp:92
std::shared_ptr< SLE const > le(Account const &account) const
Return an account root.
Definition Env.cpp:249
Account const & lookup(AccountID const &id) const
Returns the Account given the AccountID.
Definition Env.cpp:137
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:261
virtual void submit(JTx const &jt, std::source_location const &loc=std::source_location::current())
Submit an existing JTx.
Definition Env.cpp:334
void disable_sigs()
Turn off signature checks.
Definition Env.h:409
void enableFeature(uint256 const feature)
Definition Env.cpp:619
Env & apply(WithSourceLocation< JTx > jv, FN const &... fN)
Definition Env.h:580
PrettyAmount limit(Account const &account, Issue const &issue) const
Returns the IOU limit on an account.
Definition Env.cpp:219
void disableFeature(uint256 const feature)
Definition Env.cpp:627
void set_retries(unsigned r=5)
Definition Env.h:416
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:364
bool close()
Close and advance the ledger.
Definition Env.h:379
Account const & master
Definition Env.h:123
Application const & app() const
Definition Env.h:257
Json::Value do_rpc(unsigned apiVersion, std::vector< std::string > const &args, std::unordered_map< std::string, std::string > const &headers={})
Definition Env.cpp:600
JTx jt(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition Env.h:494
virtual ~Env()=default
void fund_arg(STAmount const &amount, Account const &account)
Definition Env.h:653
unsigned retries_
Definition Env.h:745
void trust(STAmount const &amount, Account const &to0, Account const &to1, Accounts const &... toN)
Definition Env.h:725
uint256 txid_
Definition Env.h:742
unsigned retries() const
Definition Env.h:423
Json::Value json(JsonValue &&jv, FN const &... fN)
Create JSON from parameters.
Definition Env.h:520
beast::unit_test::suite & test
Definition Env.h:121
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:284
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:792
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:574
std::shared_ptr< STObject const > meta()
Return metadata for the last JTx.
Definition Env.cpp:453
JTx jtnofill(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition Env.h:506
void invoke(STTx &stx, FN const &... fN)
Definition Env.h:773
std::unordered_map< AccountID, Account > map_
Definition Env.h:787
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:209
Env & apply(WithSourceLocation< Json::Value > jv, FN const &... fN)
Apply funclets and submit.
Definition Env.h:572
TestStopwatch stopwatch_
Definition Env.h:741
ManualTimeKeeper & timeKeeper()
Definition Env.h:263
bool parseFailureExpected_
Definition Env.h:744
std::shared_ptr< STTx const > tx() const
Return the tx data for the last JTx.
Definition Env.cpp:475
bool enabled(uint256 feature) const
Definition Env.h:643
Env & operator=(Env const &)=delete
void memoize(Account const &account)
Associate AccountID with account.
Definition Env.cpp:131
beast::Journal const journal
Definition Env.h:160
AbstractClient & client()
Returns the connected client.
Definition Env.h:281
void require(Args const &... args)
Check a set of requirements.
Definition Env.h:533
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:319
NetClock::time_point now()
Returns the current network time.
Definition Env.h:274
void trace(int howMany=-1)
Turn on JSON tracing.
Definition Env.h:389
~SuiteLogs() override=default
SuiteLogs(beast::unit_test::suite &suite)
Definition Env.h:102
beast::unit_test::suite & suite_
Definition Env.h:99
std::unique_ptr< beast::Journal::Sink > makeSink(std::string const &partition, beast::severities::Severity threshold) override
Definition Env.h:109
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:76
auto const amount
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:330
std::map< std::string, AmendmentSupport > const & allAmendments()
All amendments libxrpl knows about.
Definition Feature.cpp:305
std::optional< uint256 > getRegisteredFeature(std::string const &name)
Definition Feature.cpp:336
@ 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:144
std::unique_ptr< AbstractClient > client
Definition Env.h:146
std::unique_ptr< Application > owned
Definition Env.h:143
Used by parseResult() and postConditions()
Definition Env.h:127
std::optional< error_code_i > rpcCode
Definition Env.h:133
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:56
WithSourceLocation(T v, std::source_location l=std::source_location::current())
Definition Env.h:62
std::source_location loc
Definition Env.h:58
Set the sequence number on a JTx.
Definition seq.h:14