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 xrpl {
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 = env.rpc("json", "book_changes", to_string(Json::Value{}));
62 BEAST_EXPECT(!resp[jss::result].isMember(jss::error));
63 BEAST_EXPECT(resp[jss::result][jss::status] == "success");
64
65 // I dislike asserting the below statement, because its dependent on the
66 // unit-test framework BEAST_EXPECT(resp[jss::result][jss::ledger_index]
67 // == 3);
68 }
69
70 void
72 {
73 testcase("Domain Offer");
74 using namespace jtx;
75
76 FeatureBitset const all{
77 jtx::testable_amendments() | featurePermissionedDomains | featureCredentials | featurePermissionedDEX};
78
79 Env env(*this, all);
80 PermissionedDEX permDex(env);
81 auto const& [gw, domainOwner, alice, bob, carol, USD, domainID, credType] = permDex;
82
83 auto wsc = makeWSClient(env.app().config());
84
85 env(offer(alice, XRP(10), USD(10)), domain(domainID));
86 env.close();
87
88 env(pay(bob, carol, USD(10)), path(~USD), sendmax(XRP(10)), domain(domainID));
89 env.close();
90
91 std::string const txHash{env.tx()->getJson(JsonOptions::none)[jss::hash].asString()};
92
93 Json::Value const txResult = env.rpc("tx", txHash)[jss::result];
94 auto const ledgerIndex = txResult[jss::ledger_index].asInt();
95
96 Json::Value jvParams;
97 jvParams[jss::ledger_index] = ledgerIndex;
98
99 auto jv = wsc->invoke("book_changes", jvParams);
100 auto jrr = jv[jss::result];
101
102 BEAST_EXPECT(jrr[jss::changes].size() == 1);
103 BEAST_EXPECT(jrr[jss::changes][0u][jss::domain].asString() == to_string(domainID));
104 }
105
106 void
107 run() override
108 {
111
113 // Note: Other aspects of the book_changes rpc are fertile grounds
114 // for unit-testing purposes. It can be included in future work
115 }
116};
117
118BEAST_DEFINE_TESTSUITE(BookChanges, rpc, xrpl);
119
120} // namespace test
121} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
Int asInt() const
A testsuite class.
Definition suite.h:51
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:147
virtual Config & config()=0
void run() override
Runs the suite.
A transaction testing environment.
Definition Env.h:97
Application & app()
Definition Env.h:229
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:97
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:748
std::shared_ptr< STTx const > tx() const
Return the tx data for the last JTx.
Definition Env.cpp:472
Set the domain on a JTx.
Definition domain.h:11
Add a path.
Definition paths.h:37
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition rpc.h:15
Sets the SendMax on a JTx.
Definition sendmax.h:13
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:90
Json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:11
FeatureBitset testable_amendments()
Definition Env.h:54
Json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:10
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:285
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:597