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