rippled
Loading...
Searching...
No Matches
BookChanges_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2024 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/jtx.h>
21#include <test/jtx/WSClient.h>
22
23#include "xrpl/beast/unit_test/suite.h"
24#include "xrpl/protocol/jss.h"
25
26namespace ripple {
27namespace test {
28
30{
31public:
32 void
34 {
35 testcase("Specify well-known strings as ledger input");
36 jtx::Env env(*this);
37 Json::Value params, resp;
38
39 // As per convention in XRPL, ledgers can be specified with strings
40 // "closed", "validated" or "current"
41 params["ledger"] = "validated";
42 resp = env.rpc("json", "book_changes", to_string(params));
43 BEAST_EXPECT(!resp[jss::result].isMember(jss::error));
44 BEAST_EXPECT(resp[jss::result][jss::status] == "success");
45 BEAST_EXPECT(resp[jss::result][jss::validated] == true);
46
47 params["ledger"] = "current";
48 resp = env.rpc("json", "book_changes", to_string(params));
49 BEAST_EXPECT(!resp[jss::result].isMember(jss::error));
50 BEAST_EXPECT(resp[jss::result][jss::status] == "success");
51 BEAST_EXPECT(resp[jss::result][jss::validated] == false);
52
53 params["ledger"] = "closed";
54 resp = env.rpc("json", "book_changes", to_string(params));
55 BEAST_EXPECT(!resp[jss::result].isMember(jss::error));
56 BEAST_EXPECT(resp[jss::result][jss::status] == "success");
57
58 // In the unit-test framework, requesting for "closed" ledgers appears
59 // to yield "validated" ledgers. This is not new behavior. It is also
60 // observed in the unit tests for the LedgerHeader class.
61 BEAST_EXPECT(resp[jss::result][jss::validated] == true);
62
63 // non-conventional ledger input should throw an error
64 params["ledger"] = "non_conventional_ledger_input";
65 resp = env.rpc("json", "book_changes", to_string(params));
66 BEAST_EXPECT(resp[jss::result].isMember(jss::error));
67 BEAST_EXPECT(resp[jss::result][jss::status] != "success");
68 }
69
70 void
72 {
74 "If ledger_hash or ledger_index is not specified, the behavior "
75 "must default to the `current` ledger");
76 jtx::Env env(*this);
77
78 // As per convention in XRPL, ledgers can be specified with strings
79 // "closed", "validated" or "current"
80 Json::Value const resp =
81 env.rpc("json", "book_changes", to_string(Json::Value{}));
82 BEAST_EXPECT(!resp[jss::result].isMember(jss::error));
83 BEAST_EXPECT(resp[jss::result][jss::status] == "success");
84
85 // I dislike asserting the below statement, because its dependent on the
86 // unit-test framework BEAST_EXPECT(resp[jss::result][jss::ledger_index]
87 // == 3);
88 }
89
90 void
92 {
93 testcase("Domain Offer");
94 using namespace jtx;
95
96 FeatureBitset const all{
97 jtx::testable_amendments() | featurePermissionedDomains |
98 featureCredentials | featurePermissionedDEX};
99
100 Env env(*this, all);
101 PermissionedDEX permDex(env);
102 auto const& [gw, domainOwner, alice, bob, carol, USD, domainID, credType] =
103 permDex;
104
105 auto wsc = makeWSClient(env.app().config());
106
107 env(offer(alice, XRP(10), USD(10)), domain(domainID));
108 env.close();
109
110 env(pay(bob, carol, USD(10)),
111 path(~USD),
112 sendmax(XRP(10)),
113 domain(domainID));
114 env.close();
115
116 std::string const txHash{
117 env.tx()->getJson(JsonOptions::none)[jss::hash].asString()};
118
119 Json::Value const txResult = env.rpc("tx", txHash)[jss::result];
120 auto const ledgerIndex = txResult[jss::ledger_index].asInt();
121
122 Json::Value jvParams;
123 jvParams[jss::ledger_index] = ledgerIndex;
124
125 auto jv = wsc->invoke("book_changes", jvParams);
126 auto jrr = jv[jss::result];
127
128 BEAST_EXPECT(jrr[jss::changes].size() == 1);
129 BEAST_EXPECT(
130 jrr[jss::changes][0u][jss::domain].asString() ==
131 to_string(domainID));
132 }
133
134 void
135 run() override
136 {
139
141 // Note: Other aspects of the book_changes rpc are fertile grounds
142 // for unit-testing purposes. It can be included in future work
143 }
144};
145
146BEAST_DEFINE_TESTSUITE(BookChanges, rpc, ripple);
147
148} // namespace test
149} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
Int asInt() const
A testsuite class.
Definition suite.h:55
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:155
virtual Config & config()=0
void run() override
Runs the suite.
A transaction testing environment.
Definition Env.h:121
std::shared_ptr< STTx const > tx() const
Return the tx data for the last JTx.
Definition Env.cpp:525
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:121
Application & app()
Definition Env.h:261
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:791
Set the domain on a JTx.
Definition domain.h:30
Add a path.
Definition paths.h:58
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition rpc.h:35
Sets the SendMax on a JTx.
Definition sendmax.h:33
Json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:30
FeatureBitset testable_amendments()
Definition Env.h:74
Json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:29
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:111
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:323
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630