xrpld
Loading...
Searching...
No Matches
BookChanges_test.cpp
1#include <test/jtx/Env.h>
2#include <test/jtx/WSClient.h>
3#include <test/jtx/amount.h>
4#include <test/jtx/domain.h>
5#include <test/jtx/offer.h>
6#include <test/jtx/paths.h>
7#include <test/jtx/pay.h>
8#include <test/jtx/permissioned_dex.h>
9#include <test/jtx/sendmax.h>
10
11#include <xrpl/basics/base_uint.h>
12#include <xrpl/beast/unit_test/suite.h>
13#include <xrpl/json/json_value.h>
14#include <xrpl/json/to_string.h>
15#include <xrpl/protocol/Feature.h>
16#include <xrpl/protocol/jss.h>
17
18namespace xrpl::test {
19
21{
22public:
23 void
25 {
26 testcase("Specify well-known strings as ledger input");
27 jtx::Env env(*this);
28 json::Value params, resp;
29
30 // As per convention in XRPL, ledgers can be specified with strings
31 // "closed", "validated" or "current"
32 params["ledger"] = "validated";
33 resp = env.rpc("json", "book_changes", to_string(params));
34 BEAST_EXPECT(!resp[jss::result].isMember(jss::error));
35 BEAST_EXPECT(resp[jss::result][jss::status] == "success");
36 BEAST_EXPECT(resp[jss::result][jss::validated] == true);
37
38 params["ledger"] = "current";
39 resp = env.rpc("json", "book_changes", to_string(params));
40 BEAST_EXPECT(!resp[jss::result].isMember(jss::error));
41 BEAST_EXPECT(resp[jss::result][jss::status] == "success");
42 BEAST_EXPECT(resp[jss::result][jss::validated] == false);
43
44 params["ledger"] = "closed";
45 resp = env.rpc("json", "book_changes", to_string(params));
46 BEAST_EXPECT(!resp[jss::result].isMember(jss::error));
47 BEAST_EXPECT(resp[jss::result][jss::status] == "success");
48
49 // In the unit-test framework, requesting for "closed" ledgers appears
50 // to yield "validated" ledgers. This is not new behavior. It is also
51 // observed in the unit tests for the LedgerHeader class.
52 BEAST_EXPECT(resp[jss::result][jss::validated] == true);
53
54 // non-conventional ledger input should throw an error
55 params["ledger"] = "non_conventional_ledger_input";
56 resp = env.rpc("json", "book_changes", to_string(params));
57 BEAST_EXPECT(resp[jss::result].isMember(jss::error));
58 BEAST_EXPECT(resp[jss::result][jss::status] != "success");
59 }
60
61 void
63 {
65 "If ledger_hash or ledger_index is not specified, the behavior "
66 "must default to the `current` ledger");
67 jtx::Env env(*this);
68
69 // As per convention in XRPL, ledgers can be specified with strings
70 // "closed", "validated" or "current"
71 json::Value const resp = env.rpc("json", "book_changes", to_string(json::Value{}));
72 BEAST_EXPECT(!resp[jss::result].isMember(jss::error));
73 BEAST_EXPECT(resp[jss::result][jss::status] == "success");
74
75 // I dislike asserting the below statement, because its dependent on the
76 // unit-test framework BEAST_EXPECT(resp[jss::result][jss::ledger_index]
77 // == 3);
78 }
79
80 void
82 {
83 testcase("Domain Offer");
84 using namespace jtx;
85
86 FeatureBitset const all{
87 jtx::testableAmendments() | featurePermissionedDomains | featureCredentials |
88 featurePermissionedDEX};
89
90 Env env(*this, all);
91 PermissionedDEX const permDex(env);
92 auto const& [gw, domainOwner, alice, bob, carol, USD, domainID, credType] = permDex;
93
94 auto wsc = makeWSClient(env.app().config());
95
96 env(offer(alice, XRP(10), USD(10)), Domain(domainID));
97 env.close();
98
99 env(pay(bob, carol, USD(10)), Path(~USD), Sendmax(XRP(10)), Domain(domainID));
100 env.close();
101
102 std::string const txHash{
103 env.tx()->getJson(JsonOptions::Values::None)[jss::hash].asString()};
104
105 json::Value const txResult = env.rpc("tx", txHash)[jss::result];
106 auto const ledgerIndex = txResult[jss::ledger_index].asInt();
107
108 json::Value jvParams;
109 jvParams[jss::ledger_index] = ledgerIndex;
110
111 auto jv = wsc->invoke("book_changes", jvParams);
112 auto jrr = jv[jss::result];
113
114 BEAST_EXPECT(jrr[jss::changes].size() == 1);
115 BEAST_EXPECT(jrr[jss::changes][0u][jss::domain].asString() == to_string(domainID));
116 }
117
118 void
119 run() override
120 {
123
125 // Note: Other aspects of the book_changes rpc are fertile grounds
126 // for unit-testing purposes. It can be included in future work
127 }
128};
129
130BEAST_DEFINE_TESTSUITE(BookChanges, rpc, xrpl);
131
132} // namespace xrpl::test
A testsuite class.
Definition suite.h:50
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:149
Represents a JSON value.
Definition json_value.h:130
Int asInt() const
virtual Config & config()=0
void run() override
Runs the suite.
A transaction testing environment.
Definition Env.h:143
Application & app()
Definition Env.h:280
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
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:864
std::shared_ptr< STTx const > tx() const
Return the tx data for the last JTx.
Definition Env.cpp:533
Add a path.
Definition paths.h:39
Sets the SendMax on a JTx.
Definition sendmax.h:13
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
FeatureBitset testableAmendments()
Definition Env.h:76
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:14
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
std::unique_ptr< WSClient > makeWSClient(Config const &cfg, bool v2, unsigned rpcVersion, std::unordered_map< std::string, std::string > const &headers)
Returns a client operating through WebSockets/S.
Definition WSClient.cpp:329
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BaseUInt< 256 > Domain
Domain is a 256-bit hash representing a specific domain.
Definition UintTypes.h:47
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633